Skip to content

Commit

Permalink
Added support for sending notifications after a specified time. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianonline committed Feb 18, 2016
1 parent b5c9bca commit 39ad905
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
75 changes: 50 additions & 25 deletions octoprint_telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self):
else:
self.main.send_msg("Currently I'm not printing, so there is nothing to stop.")
elif command=="Yes, abort the print!":
self.main.send_msg("Aborting the print...")
self.main.send_msg("Aborting the print.")
self.main._printer.cancel_print()
elif command=="No, don't abort the print.":
self.main.send_msg("Okay, nevermind.")
Expand Down Expand Up @@ -141,6 +141,7 @@ class TelegramPlugin(octoprint.plugin.EventHandlerPlugin,
def __init__(self):
self.thread = None
self.last_z = 0.0
self.last_notification_time = 0
self.bot_url = None
self.first_contact = True
self.known_chats = {}
Expand Down Expand Up @@ -188,7 +189,8 @@ def get_settings_defaults(self):
return dict(
token = "",
chat = "",
height = "5.0",
notification_height = "5.0",
notification_time = "15",
message_at_print_started = True,
message_at_print_done = True,
message_at_print_failed = True,
Expand All @@ -205,6 +207,23 @@ def get_template_configs(self):
dict(type="settings", name="Telegram", custom_bindings=True)
]

def is_notification_necessary(self, new_z, old_z):
timediff = self._settings.get_int(['notification_time'])
if timediff and timediff > 0:
# check the timediff
if self.last_notification_time + timediff*60 <= time.time():
return True
zdiff = self._settings.get_float(['notification_height'])
if zdiff and zdiff > 0.0:
# check the zdiff
if abs(new_z - (old_z or 0.0)) >= 2.0:
# big changes in height are not interesting for notifications - we ignore them
self.last_z = new_z
return False
if new_z >= self.last_z + zdiff or new_z < self.last_z:
return True
return False

def on_event(self, event, payload, *args, **kwargs):
try:
if event != "PrintDone" and event != "PrintStarted" and event != "ZChange" and event!="PrintFailed":
Expand All @@ -215,40 +234,46 @@ def on_event(self, event, payload, *args, **kwargs):
# PrintFailed Payload: {'origin': 'local', 'file': u'cube.gcode'}
# MovieDone Payload: {'gcode': u'cube.gcode', 'movie_basename': 'cube_20160216125143.mpg', 'movie': '/home/pi/.octoprint/timelapse/cube_20160216125143.mpg'}

if event=="PrintDone":
self.shut_up = False
if not self._settings.get_boolean(["message_at_print_done"]):
return
elif event=="PrintStarted" and not self._settings.get_boolean(["message_at_print_started"]):
return
elif event=="PrintFailed":
self.shut_up = False
if not self._settings.get_boolean(["message_at_print_failed"]):
return

z = ""
file = ""

if event=="PrintStarted":
self.last_z = 0.0


if event=="ZChange":
z = payload['new']
self._logger.debug("Z-Change. z=" + str(z) + " last_z=" + str(self.last_z) + ", settings_height=" + str(self._settings.get_float(['height'])))
if abs(z - (payload['old'] or 0.0)) >= 2.0:
# a big jump in height is usually due to lifting at the beginning or end of a print
# we just ignore this.
self.last_z = z
self._logger.debug("Z-Change. new_z=%.2f old_z=%.2f last_z=%.2f notification_height=%.2f notification_time=%d",
z,
payload['old'],
self.last_z,
self._settings.get_float(['notification_height']),
self._settings.get_int(['notification_time']))

if not self.is_notification_necessary(payload['new'], payload['old']):
return
elif event=="PrintStarted":
self.last_z = 0.0
self.last_notification_time = time.time()
if not self._settings.get_boolean(["message_at_print_started"]):
return
elif event=="PrintDone":
if self.shut_up:
self.shut_up = False
return
if z >= self.last_z + self._settings.get_float(["height"]) or z < self.last_z:
self.last_z = z
else:
if not self._settings.get_boolean(["message_at_print_done"]):
return
elif event=="PrintFailed":
if self.shut_up:
self.shut_up = False
return
if not self._settings.get_boolean(["message_at_print_failed"]):
return

self.last_notification_time = time.time()
self.last_z = z

if self.shut_up:
return

status = self._printer.get_current_data()
self._logger.debug(str(status))
temps = self._printer.get_current_temperatures()
bed_temp = temps['bed']['actual']
bed_target = temps['bed']['target']
Expand Down
10 changes: 7 additions & 3 deletions octoprint_telegram/templates/telegram_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@

<h4>Notification settings</h4>
<div class="control-group">
<label class="control-label">{{ _('Z distance') }}</label>
<label class="control-label">{{ _('Send notification every') }}</label>
<div class="controls">
<div class="input-append">
<input type="number" step="any" min="0.1" class="input-mini text-right" data-bind="value: settings.settings.plugins.telegram.height" />
<input type="number" step="any" min="0.0" class="input-mini text-right" data-bind="value: settings.settings.plugins.telegram.notification_height" />
<span class="add-on">mm</span>
</div>
<span class="help-block">Sends a notification with image after this many mm have been printed.</span>
<div class="input-append">
<input type="number" step="1" min="0" class="input-mini text-right" data-bind="value: settings.settings.plugins.telegram.notification_time" />
<span class="add-on">min</span>
</div>
<span class="help-block">Sends a message after layer height changed by this height or this many minutes passed since the last notification. Set to 0 to disable.</span>
</div>
</div>
<div class="control-group">
Expand Down

0 comments on commit 39ad905

Please sign in to comment.