Skip to content

Commit

Permalink
Feature : select which events you want to trigger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinaute committed Dec 2, 2020
1 parent e7069b8 commit 4968dab
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 55 deletions.
Binary file modified extras/freemobilenotifier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 75 additions & 37 deletions octoprint_freemobilenotifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,95 @@
from urllib import request,parse

class FreemobilenotifierPlugin(octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin):
octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin):

#~~ SettingsPlugin
def get_settings_defaults(self):
return dict(
enabled=False,
login="",
pass_key="",
message_format=dict(
body="Job complete: {filename} done printing after {elapsed_time}"
print_events=dict(
PrintStarted=dict(
Enabled=False,
Message="A new print has started! - Filename: {filename}",
),
PrintFailed=dict(
Enabled=False,
Message="Oh no! The print has failed... - Filename: {filename}",
),
PrintCancelled=dict(
Enabled=False,
Message="Uh oh... someone cancelled the print! - Filename: {filename}",
),
PrintDone=dict(
Enabled=True,
Message="Print finished successfully! - Filename: {filename}, Time: {time}",
),
PrintPaused=dict(
Enabled=False,
Message="Printing has been paused... - Filename: {filename}",
Color="warning",
),
PrintResumed=dict(
Enabled=False,
Message="Phew! Printing has been resumed! Back to work... - Filename: {filename}",
),
),
)
)

def get_settings_version(self):
return 1

#~~ TemplatePlugin
def get_template_configs(self):
return [dict(type="settings", name="FreeMobile Notifier", custom_bindings=False)]

#~~ EventPlugin

def on_event(self, event, payload):
if event != "PrintDone":
return

if not self._settings.get(['enabled']):
return

filename = payload["name"]

import datetime
import octoprint.util
elapsed_time = octoprint.util.get_formatted_timedelta(datetime.timedelta(seconds=payload["time"]))

tags = {'filename': filename, 'elapsed_time': elapsed_time}
message = parse.quote(self._settings.get(["message_format", "body"]).format(**tags))
login = self._settings.get(["login"])
pass_key = self._settings.get(["pass_key"])
url = 'https://smsapi.free-mobile.fr/sendmsg?&user='+login+'&pass='+pass_key+'&msg='+message

try:
request.urlopen(url)
except Exception as e:
# report problem sending sms
self._logger.exception("SMS notification error: %s" % (str(e)))

events = self._settings.get(['print_events'], merged=True)

if event in events and events[event] and events[event]['Enabled']:

login = self._settings.get(['login'])
if not login:
self._logger.exception("Free login is not set!")
return
pass_key = self._settings.get(['pass_key'])
if not pass_key:
self._logger.exception("Free key is not set!")
return

filename = payload["name"]
message = {}

## event settings
event = self._settings.get(['print_events', event], merged=True)

import datetime
import octoprint.util
if "time" in payload and payload["time"]:
elapsed_time = octoprint.util.get_formatted_timedelta(datetime.timedelta(seconds=payload["time"]))
else:
elapsed_time = ""

message = parse.quote(event['Message'].format(**{'filename': filename, 'time':elapsed_time}))
login = self._settings.get(["login"])
pass_key = self._settings.get(["pass_key"])
url = 'https://smsapi.free-mobile.fr/sendmsg?&user='+login+'&pass='+pass_key+'&msg='+message

try:
request.urlopen(url)
except Exception as e:
# report problem sending sms
self._logger.exception("SMS notification error: %s" % (str(e)))
else:
# report notification was sent
self._logger.info("Print notification sent to %s" % (self._settings.get(['login'])))
else:
# report notification was sent
self._logger.info("Print notification sent to %s" % (self._settings.get(['login'])))

self._logger.debug("SMS not configured for event.")
return
##~~ Softwareupdate hook
def get_update_information(self):
return dict(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
<h3>{{ _('SMS Notifier Configuration') }}</h3>

<form class="form-horizontal">
<div class="control-group">
<div class="controls" title="{{ _('You can toggle whether notifications are enabled while a print is in progress.') }}">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.enabled" /> {{ _('Enabled') }}
</label>
</div>
</div>

<h4>{{ _('FreeMobile Settings') }}</h4>

<div class="control-group" title="{{ _('Login') }}">
Expand All @@ -25,14 +17,97 @@
</div>
</div>

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

<p>{{ _('The SMS template may contain the tags <code>{filename}</code> and <code>{elapsed_time}</code>.') }}</p>
<h4>{{ _('Event Notifications') }}</h4>

<div class="control-group" title="{{ _('Content of notification email') }}">
<label class="control-label">{{ _('Body') }}</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.message_format.body">
<p>{{ _('Choose the events that send a message to SMS. You can also customize the message itself (or leave blank to use the default).') }}</p>

<div class="control-group">
<h5>Print Started</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print has started.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintStarted.Message">
</div>
</div>
</div>
</form>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print has started.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintStarted.Enabled" />
</div>
</div>

<div class="control-group">
<h5>Print Done</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print is done.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintDone.Message">
</div>
</div>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print is done.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintDone.Enabled" />
</div>
</div>

<div class="control-group">
<h5>Print Failed</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print has failed.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintFailed.Message">
</div>
</div>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print has failed.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintFailed.Enabled" />
</div>
</div>

<div class="control-group">
<h5>Print Cancelled</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print has been cancelled.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintCancelled.Message">
</div>
</div>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print has been cancelled.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintCancelled.Enabled" />
</div>
</div>

<div class="control-group">
<h5>Print Paused</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print has paused.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintPaused.Message">
</div>
</div>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print has paused.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintPaused.Enabled" />
</div>
</div>

<div class="control-group">
<h5>Print Resumed</h5>
<label class="control-label">Message: </label>
<div class="controls" title="{{ _('A print has resumed.') }}">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.freemobilenotifier.print_events.PrintResumed.Message">
</div>
</div>

<div class="control-group">
<label class="control-label">Enabled: </label>
<div class="controls" title="{{ _('A print has resumed.') }}">
<input type="checkbox" data-bind="checked: settings.plugins.freemobilenotifier.print_events.PrintResumed.Enabled" />
</div>
</div>
</form>
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint_FreeMobile-Notifier"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.1.5"
plugin_version = "0.1.6"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 4968dab

Please sign in to comment.