#5 Rest Api integration
This commit is contained in:
parent
7d7c218fdb
commit
b1dfbcd51b
@ -4,29 +4,35 @@ from __future__ import absolute_import
|
|||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
import octoprint.plugin
|
import octoprint.plugin
|
||||||
|
import requests
|
||||||
from octoprint.util import RepeatedTimer
|
from octoprint.util import RepeatedTimer
|
||||||
|
|
||||||
|
|
||||||
class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
||||||
octoprint.plugin.AssetPlugin,
|
octoprint.plugin.AssetPlugin,
|
||||||
octoprint.plugin.TemplatePlugin,
|
octoprint.plugin.TemplatePlugin,
|
||||||
octoprint.plugin.StartupPlugin):
|
octoprint.plugin.StartupPlugin,
|
||||||
|
octoprint.plugin.ShutdownPlugin):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._pollTimer = None
|
self._pollTimer = None
|
||||||
self.ip = None
|
self.ip = None
|
||||||
self.intervall = 1
|
self.intervall = 1
|
||||||
|
self._timer = None
|
||||||
|
|
||||||
self.ctx = ssl.create_default_context()
|
self.ctx = ssl.create_default_context()
|
||||||
self.ctx.check_hostname = False
|
self.ctx.check_hostname = False
|
||||||
self.ctx.verify_mode = ssl.CERT_NONE
|
self.ctx.verify_mode = ssl.CERT_NONE
|
||||||
|
|
||||||
|
self.initialize()
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.ip = self._settings.get(["ip"])
|
self.ip = self._settings.get(["ip"])
|
||||||
self._logger.debug("ip: %s" % self.ip)
|
self._logger.debug("ip: %s" % self.ip)
|
||||||
|
|
||||||
self.intervall = self._settings.get_int(["intervall"])
|
self.intervall = self._settings.get_int(["intervall"])
|
||||||
self._logger.debug("intervall: %s" % self.intervall)
|
self._logger.debug("intervall: %s" % self.intervall)
|
||||||
|
self._timer_start()
|
||||||
|
|
||||||
def get_assets(self):
|
def get_assets(self):
|
||||||
return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"])
|
return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"])
|
||||||
@ -38,74 +44,42 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
|||||||
icon="power-off"),
|
icon="power-off"),
|
||||||
dict(type="settings", custom_bindings=False)]
|
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):
|
def _timer_start(self):
|
||||||
if self._abort_timer is not None:
|
if self._timer is not None:
|
||||||
return
|
self._timer.cancel()
|
||||||
|
self._logger.info("Canceling Timer")
|
||||||
|
|
||||||
self._logger.info("Starting abort shutdown printer timer.")
|
self._logger.info("Starting timer.")
|
||||||
|
self._timer = RepeatedTimer(self.intervall, self._timer_task)
|
||||||
self._timeout_value = self.abortTimeout
|
self._timer.start()
|
||||||
self._abort_timer = RepeatedTimer(1, self._timer_task)
|
|
||||||
self._abort_timer.start()
|
|
||||||
|
|
||||||
def _timer_task(self):
|
def _timer_task(self):
|
||||||
if self._timeout_value is None:
|
if self.ip is not None:
|
||||||
return
|
try:
|
||||||
|
request = requests.get(
|
||||||
self._timeout_value -= 1
|
'http://{}/report'.format(self.ip), timeout=1)
|
||||||
|
self.data = request.json()
|
||||||
if self._printer.get_state_id() == "PRINTING" and self._printer.is_printing() == True:
|
self._logger.info(self.data)
|
||||||
self._timeout_value = 0
|
except (requests.exceptions.ConnectionError, ValueError):
|
||||||
|
self._logger.info('Connection Error Host: {}'.format(self.ip))
|
||||||
self._abort_timer.cancel()
|
# Do all Staff here
|
||||||
self._abort_timer = None
|
pass
|
||||||
return
|
|
||||||
if self._timeout_value <= 0:
|
|
||||||
if self._abort_timer is not None:
|
|
||||||
self._abort_timer.cancel()
|
|
||||||
self._abort_timer = None
|
|
||||||
self._shutdown_printer()
|
|
||||||
|
|
||||||
def on_after_startup(self):
|
def on_after_startup(self):
|
||||||
self._logger.info("Hello World!")
|
self._logger.info("Hello World!")
|
||||||
|
|
||||||
|
def on_shutdown(self):
|
||||||
|
self._logger.info("Hello World!")
|
||||||
|
|
||||||
def get_settings_defaults(self):
|
def get_settings_defaults(self):
|
||||||
return dict(
|
return dict(
|
||||||
ip="",
|
ip=None,
|
||||||
intervall=1
|
intervall=1
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_settings_save(self, data):
|
def on_settings_save(self, data):
|
||||||
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
|
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
|
||||||
self.ip = self._settings.get(["ip"])
|
self.initialize()
|
||||||
self.intervall = self._settings.get_int(["intervall"])
|
|
||||||
|
|
||||||
def get_update_information(self):
|
def get_update_information(self):
|
||||||
return dict(
|
return dict(
|
||||||
@ -125,7 +99,7 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
__plugin_name__ = "OctoPrint-MyStromSwitch"
|
__plugin_name__ = "MyStrom Switch"
|
||||||
|
|
||||||
|
|
||||||
def __plugin_load__():
|
def __plugin_load__():
|
||||||
|
@ -5,5 +5,5 @@
|
|||||||
#
|
#
|
||||||
# works as expected. Requirements can be found in setup.py.
|
# works as expected. Requirements can be found in setup.py.
|
||||||
###
|
###
|
||||||
|
requests
|
||||||
.
|
.
|
||||||
|
2
setup.py
2
setup.py
@ -33,7 +33,7 @@ plugin_url = "https://github.com/da4id/OctoPrint-MyStromSwitch"
|
|||||||
plugin_license = "AGPLv3"
|
plugin_license = "AGPLv3"
|
||||||
|
|
||||||
# Any additional requirements besides OctoPrint should be listed here
|
# 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
|
### More advanced options that you usually shouldn't have to touch follow after this point
|
||||||
|
Loading…
Reference in New Issue
Block a user