Skip to content

Commit

Permalink
Send snapshots (#14)
Browse files Browse the repository at this point in the history
The plugin can now take a snapshot when messaging.

Based on the work by @beeepus.
  • Loading branch information
aerickson authored Jan 21, 2019
1 parent 1727a83 commit 708da34
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
45 changes: 40 additions & 5 deletions octoprint_signalnotifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import shlex
import socket
import subprocess
import time

from octoprint.timelapse import Timelapse as timelapse
from octoprint.settings import settings

class SignalNotifierPlugin(octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.SettingsPlugin,
Expand Down Expand Up @@ -36,12 +40,13 @@ def get_settings_defaults(self):
recipient="",
message_format=dict(
body="OctoPrint@{host}: Job complete: {filename} done printing after {elapsed_time}."
)
),
send_snapshot=False
)

def get_settings_restricted_paths(self):
return dict(admin=[["path"], ["sender"], ["recipient"]],
user=[["message_format", "body"]],
user=[["message_format", "body"], ["send_snapshot"]],
never=[])

def get_settings_version(self):
Expand Down Expand Up @@ -71,6 +76,7 @@ def on_event(self, event, payload):
sender = self._settings.get(["sender"])
recipient = self._settings.get(["recipient"])
message = self._settings.get(["message_format", "body"]).format(**tags)
send_snapshot = self._settings.get(["send_snapshot"])

# check that path is a valid executable
if not self.is_exe(path):
Expand All @@ -96,13 +102,42 @@ def on_event(self, event, payload):
self._logger.error("Command output: '%s'" % osstdout)
return

# TODO: pull command generation into a function so it's more easily tested

# ./signal-cli -u +4915151111111 send -m "My first message from the CLI" +4915152222222
# ./signal-cli -u +4915151111111 send -g <group_id> -m "My first message from the CLI to a group"

group_string = ""
recipient_string = ""
# if a single recipient
if recipient[:1] == "+":
the_command = "%s -u %s send -m \"%s\" %s" % (path, sender, message, recipient)
recipient_string = recipient
# the_command = "%s -u %s send -m \"%s\" %s" % (path, sender, message, recipient)
# if a group
else:
the_command = "%s -u %s send -g %s -m \"%s\"" % (path, sender, recipient, message)
self._logger.debug("Command plugin will run is: '%s'" % the_command)
group_string = "-g %s" % (recipient)
# the_command = "%s -u %s send -g %s -m \"%s\"" % (path, sender, recipient, message)

attachment_argument = ""
if send_snapshot:
# only try to grab a snapshot if the url has been configured
if settings().get(["webcam", "snapshot"]):
tl=timelapse()
tl._image_number = 0
tl._capture_errors = 0
tl._capture_success = 0
tl._in_timelapse = True
tl._file_prefix = time.strftime("%Y%m%d%H%M%S")
self._logger.info("AJE: snapshot url: %s" % settings().get(["webcam", "snapshot"]))
snapshot_file=tl.capture_image()
attachment_argument = "-a \"%s\"" % (snapshot_file)
# the_command = "%s -u %s send -m \"%s\" %s -a \"%s\"" % (path, sender, message, recipient, snapshot_file)
else:
self._logger.error("Please configure the webcam before enabling snapshots!")

the_command = "%s -u %s send %s -m \"%s\" %s %s" % (path, sender, group_string, message, attachment_argument, recipient_string)
self._logger.info("Command plugin will run is: '%s'" % the_command)

try:
rc, osstdout = self.run_command(the_command)
# TODO: catch subprocess.CalledProcessError vs generic error?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
</div>
</div>

<p>{{ _('To send to an individual, the recipient should be the phone number beginning with a "+". To send to a group, enter the group ID') }}</p>
<p>{{ _('To send to an individual, the recipient should be the phone number beginning with a "+".') }}</p>
<p>{{ _('To send to a group, enter the group ID.') }}</p>

<h4>{{ _('Message Content') }}</h4>

Expand All @@ -44,4 +45,17 @@
<input type="text" class="input-block-level" data-bind="value: settings.plugins.signalnotifier.message_format.body">
</div>
</div>

<h4>{{ _('Snapshot Attachments') }}</h4>

<p>{{ _('Only enable if a camera is configured and working. Will break the plugin if not working.') }}</p>

<div class="control-group">
<div class="controls" title="{{ _('send snapshot') }}">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.signalnotifier.send_snapshot" /> {{ _('Send Snapshot') }}
</label>
</div>
</div>

</form>

0 comments on commit 708da34

Please sign in to comment.