2019-11-19 21:29:32 +01:00
|
|
|
# coding=utf-8
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import ssl
|
|
|
|
|
|
|
|
import octoprint.plugin
|
2019-11-20 20:08:10 +01:00
|
|
|
import requests
|
2019-11-19 21:29:32 +01:00
|
|
|
from octoprint.util import RepeatedTimer
|
|
|
|
|
|
|
|
|
2019-11-19 21:46:26 +01:00
|
|
|
class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
|
2019-11-19 22:19:56 +01:00
|
|
|
octoprint.plugin.AssetPlugin,
|
|
|
|
octoprint.plugin.TemplatePlugin,
|
2019-11-20 20:08:10 +01:00
|
|
|
octoprint.plugin.StartupPlugin,
|
|
|
|
octoprint.plugin.ShutdownPlugin):
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2019-11-19 22:19:56 +01:00
|
|
|
self.ip = None
|
|
|
|
self.intervall = 1
|
2019-11-20 20:08:10 +01:00
|
|
|
self._timer = None
|
2019-11-19 22:19:56 +01:00
|
|
|
|
2019-11-19 21:29:32 +01:00
|
|
|
self.ctx = ssl.create_default_context()
|
|
|
|
self.ctx.check_hostname = False
|
|
|
|
self.ctx.verify_mode = ssl.CERT_NONE
|
|
|
|
|
|
|
|
def initialize(self):
|
2019-11-19 22:19:56 +01:00
|
|
|
self.ip = self._settings.get(["ip"])
|
|
|
|
self._logger.debug("ip: %s" % self.ip)
|
2019-11-19 21:29:32 +01:00
|
|
|
|
2019-11-19 22:19:56 +01:00
|
|
|
self.intervall = self._settings.get_int(["intervall"])
|
|
|
|
self._logger.debug("intervall: %s" % self.intervall)
|
2019-11-20 20:08:10 +01:00
|
|
|
self._timer_start()
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def get_assets(self):
|
2019-11-19 21:55:31 +01:00
|
|
|
return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"])
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def get_template_configs(self):
|
|
|
|
return [dict(type="sidebar",
|
2019-11-19 21:46:26 +01:00
|
|
|
name="MyStrom Switch",
|
2019-11-19 21:29:32 +01:00
|
|
|
custom_bindings=False,
|
|
|
|
icon="power-off"),
|
|
|
|
dict(type="settings", custom_bindings=False)]
|
|
|
|
|
|
|
|
def _timer_start(self):
|
2019-11-20 20:08:10 +01:00
|
|
|
if self._timer is not None:
|
|
|
|
self._timer.cancel()
|
|
|
|
self._logger.info("Canceling Timer")
|
2019-11-19 21:29:32 +01:00
|
|
|
|
2019-11-20 21:41:44 +01:00
|
|
|
if self.intervall >= 1 and self.ip is not None:
|
|
|
|
self._logger.info("Starting timer")
|
|
|
|
self._timer = RepeatedTimer(self.intervall, self._timer_task)
|
|
|
|
self._timer.start()
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def _timer_task(self):
|
2019-11-20 20:08:10 +01:00
|
|
|
if self.ip is not None:
|
|
|
|
try:
|
|
|
|
request = requests.get(
|
|
|
|
'http://{}/report'.format(self.ip), timeout=1)
|
2019-11-20 21:47:14 +01:00
|
|
|
self._logger.info(request.json())
|
2019-11-20 21:50:02 +01:00
|
|
|
self._plugin_manager.send_plugin_message(self._identifier,
|
|
|
|
request.json())
|
2019-11-20 20:08:10 +01:00
|
|
|
except (requests.exceptions.ConnectionError, ValueError):
|
|
|
|
self._logger.info('Connection Error Host: {}'.format(self.ip))
|
2019-11-20 21:12:33 +01:00
|
|
|
else:
|
|
|
|
self._logger.info("Ip is None")
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def on_after_startup(self):
|
|
|
|
self._logger.info("Hello World!")
|
|
|
|
|
2019-11-20 20:08:10 +01:00
|
|
|
def on_shutdown(self):
|
|
|
|
self._logger.info("Hello World!")
|
|
|
|
|
2019-11-20 21:20:55 +01:00
|
|
|
def on_settings_migrate(self, target, current):
|
|
|
|
pass
|
|
|
|
|
2019-11-20 20:18:16 +01:00
|
|
|
def get_settings_version(self):
|
|
|
|
return 1
|
|
|
|
|
2019-11-19 21:29:32 +01:00
|
|
|
def get_settings_defaults(self):
|
|
|
|
return dict(
|
2019-11-20 20:08:10 +01:00
|
|
|
ip=None,
|
2019-11-19 22:19:56 +01:00
|
|
|
intervall=1
|
2019-11-19 21:29:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def on_settings_save(self, data):
|
2019-11-20 21:36:23 +01:00
|
|
|
self._logger.info("on_settings_save")
|
2019-11-19 21:29:32 +01:00
|
|
|
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
|
2019-11-20 20:08:10 +01:00
|
|
|
self.initialize()
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
def get_update_information(self):
|
|
|
|
return dict(
|
2019-11-19 21:55:31 +01:00
|
|
|
mystromswitch=dict(
|
2019-11-19 21:35:43 +01:00
|
|
|
displayName="OctoPrint-MyStromSwitch",
|
2019-11-19 21:29:32 +01:00
|
|
|
displayVersion=self._plugin_version,
|
|
|
|
|
|
|
|
# version check: github repository
|
|
|
|
type="github_release",
|
2019-11-19 21:35:43 +01:00
|
|
|
user="da4id",
|
|
|
|
repo="OctoPrint-MyStromSwitch",
|
2019-11-19 21:29:32 +01:00
|
|
|
current=self._plugin_version,
|
|
|
|
|
|
|
|
# update method: pip w/ dependency links
|
2019-11-19 21:46:26 +01:00
|
|
|
pip="https://github.com/da4id/OctoPrint-MyStromSwitch/archive/{target_version}.zip"
|
2019-11-19 21:29:32 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-11-20 20:08:10 +01:00
|
|
|
__plugin_name__ = "MyStrom Switch"
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __plugin_load__():
|
|
|
|
global __plugin_implementation__
|
2019-11-19 21:46:26 +01:00
|
|
|
__plugin_implementation__ = MyStromSwitchPlugin()
|
2019-11-19 21:29:32 +01:00
|
|
|
|
|
|
|
global __plugin_hooks__
|
|
|
|
__plugin_hooks__ = {
|
|
|
|
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
|
|
|
|
}
|