diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index 11dfbd3..32b87d6 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -4,29 +4,35 @@ from __future__ import absolute_import import ssl import octoprint.plugin +import requests from octoprint.util import RepeatedTimer class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.TemplatePlugin, - octoprint.plugin.StartupPlugin): + octoprint.plugin.StartupPlugin, + octoprint.plugin.ShutdownPlugin): def __init__(self): self._pollTimer = None self.ip = None self.intervall = 1 + self._timer = None self.ctx = ssl.create_default_context() self.ctx.check_hostname = False self.ctx.verify_mode = ssl.CERT_NONE + self.initialize() + def initialize(self): self.ip = self._settings.get(["ip"]) self._logger.debug("ip: %s" % self.ip) self.intervall = self._settings.get_int(["intervall"]) self._logger.debug("intervall: %s" % self.intervall) + self._timer_start() def get_assets(self): return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"]) @@ -38,74 +44,42 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, icon="power-off"), dict(type="settings", custom_bindings=False)] - def _temperature_target(self): - if self._abort_timer_temp is not None: - return - if self.temperatureTarget: - self._abort_timer_temp = RepeatedTimer(2, self._temperature_task) - self._abort_timer_temp.start() - else: - self._timer_start() - - def _temperature_task(self): - if self._printer.get_state_id() == "PRINTING" and self._printer.is_printing() == True: - self._abort_timer_temp.cancel() - self._abort_timer_temp = None - return - self._temp = self._printer.get_current_temperatures() - tester = 0; - number = 0; - for tool in self._temp.keys(): - if not tool == "bed": - if self._temp[tool]["actual"] <= self.temperatureValue: - tester += 1 - number += 1 - if tester == number: - self._abort_timer_temp.cancel() - self._abort_timer_temp = None - self._timer_start() - def _timer_start(self): - if self._abort_timer is not None: - return + if self._timer is not None: + self._timer.cancel() + self._logger.info("Canceling Timer") - self._logger.info("Starting abort shutdown printer timer.") - - self._timeout_value = self.abortTimeout - self._abort_timer = RepeatedTimer(1, self._timer_task) - self._abort_timer.start() + self._logger.info("Starting timer.") + self._timer = RepeatedTimer(self.intervall, self._timer_task) + self._timer.start() def _timer_task(self): - if self._timeout_value is None: - return - - self._timeout_value -= 1 - - if self._printer.get_state_id() == "PRINTING" and self._printer.is_printing() == True: - self._timeout_value = 0 - - self._abort_timer.cancel() - self._abort_timer = None - return - if self._timeout_value <= 0: - if self._abort_timer is not None: - self._abort_timer.cancel() - self._abort_timer = None - self._shutdown_printer() + if self.ip is not None: + try: + request = requests.get( + 'http://{}/report'.format(self.ip), timeout=1) + self.data = request.json() + self._logger.info(self.data) + except (requests.exceptions.ConnectionError, ValueError): + self._logger.info('Connection Error Host: {}'.format(self.ip)) + # Do all Staff here + pass def on_after_startup(self): self._logger.info("Hello World!") + def on_shutdown(self): + self._logger.info("Hello World!") + def get_settings_defaults(self): return dict( - ip="", + ip=None, intervall=1 ) def on_settings_save(self, data): octoprint.plugin.SettingsPlugin.on_settings_save(self, data) - self.ip = self._settings.get(["ip"]) - self.intervall = self._settings.get_int(["intervall"]) + self.initialize() def get_update_information(self): return dict( @@ -125,7 +99,7 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, ) -__plugin_name__ = "OctoPrint-MyStromSwitch" +__plugin_name__ = "MyStrom Switch" def __plugin_load__(): diff --git a/requirements.txt b/requirements.txt index a1dc463..b2807ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,5 @@ # # works as expected. Requirements can be found in setup.py. ### - +requests . diff --git a/setup.py b/setup.py index bf504ce..fb09a5d 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ plugin_url = "https://github.com/da4id/OctoPrint-MyStromSwitch" plugin_license = "AGPLv3" # Any additional requirements besides OctoPrint should be listed here -plugin_requires = [] +plugin_requires = [requests] ### -------------------------------------------------------------------------------------------------------------------- ### More advanced options that you usually shouldn't have to touch follow after this point