OctoPrint-FanSpeedSlider/octoprint_fanspeedslider/__init__.py

144 lines
4.6 KiB
Python
Raw Normal View History

2017-08-28 23:10:51 +02:00
# coding=utf-8
from __future__ import absolute_import
2017-09-04 19:52:55 +02:00
from decimal import *
import re
2017-08-28 23:10:51 +02:00
import octoprint.plugin
class FanSliderPlugin(octoprint.plugin.StartupPlugin,
2017-08-30 10:28:26 +02:00
octoprint.plugin.TemplatePlugin,
octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin):
2018-11-17 02:41:55 +01:00
def __init__(self):
self.minPWM=0,
self.maxPWM=255,
self.lockfan=False
2017-08-30 10:28:26 +02:00
2017-09-04 19:52:55 +02:00
def on_after_startup(self):
2017-09-05 02:57:55 +02:00
self.get_settings_updates()
2017-09-04 19:52:55 +02:00
2017-08-30 10:28:26 +02:00
def get_settings_defaults(self):
2017-09-04 19:52:55 +02:00
return dict(
defaultFanSpeed=100,
2017-09-04 19:52:55 +02:00
minSpeed=0,
maxSpeed=100,
notifyDelay=4000,
2018-12-30 13:26:22 +01:00
lockfan=False,
lastSentSpeed=0,
defaultLastSpeed=False
2017-09-04 19:52:55 +02:00
)
def on_settings_save(self, data):
s = self._settings
2020-07-17 00:44:02 +02:00
if "defaultFanSpeed" in list(data.keys()):
s.setInt(["defaultFanSpeed"], data["defaultFanSpeed"])
2020-07-17 00:44:02 +02:00
if "minSpeed" in list(data.keys()):
s.setInt(["minSpeed"], data["minSpeed"])
2020-07-17 00:44:02 +02:00
if "maxSpeed" in list(data.keys()):
s.setInt(["maxSpeed"], data["maxSpeed"])
2020-07-17 00:44:02 +02:00
if "notifyDelay" in list(data.keys()):
s.setInt(["notifyDelay"], data["notifyDelay"])
2020-07-17 00:44:02 +02:00
if "lockfan" in list(data.keys()):
s.set(["lockfan"], data["lockfan"])
2020-07-17 00:44:02 +02:00
if "lastSentSpeed" in list(data.keys()):
s.setInt(["lastSentSpeed"], data["lastSentSpeed"])
2020-07-17 00:44:02 +02:00
if "defaultLastSpeed" in list(data.keys()):
s.set(["defaultLastSpeed"], data["defaultLastSpeed"])
2017-09-05 02:57:55 +02:00
self.get_settings_updates()
#clean up settings if everything's default
self.on_settings_cleanup()
s.save()
#function stolen...err borrowed :D from types.py @ 1663
def on_settings_cleanup(self):
import octoprint.util
from octoprint.settings import NoSuchSettingsPath
try:
config = self._settings.get_all_data(merged=False, incl_defaults=False, error_on_path=True)
except NoSuchSettingsPath:
return
if config is None:
self._settings.clean_all_data()
return
if self.config_version_key in config and config[self.config_version_key] is None:
del config[self.config_version_key]
defaults = self.get_settings_defaults()
diff = octoprint.util.dict_minimal_mergediff(defaults, config)
if not diff:
self._settings.clean_all_data()
else:
self._settings.set([], diff)
2017-08-30 10:28:26 +02:00
def get_assets(self):
return dict(
js=["js/fanslider.js"],
css=["css/style.css"]
)
def get_template_configs(self):
return [
dict(type="settings", custom_bindings=False)
2017-09-04 19:52:55 +02:00
]
2017-09-05 02:57:55 +02:00
def get_settings_updates(self):
self.defaultFanSpeed = self._settings.getInt(["defaultFanSpeed"])
self.minSpeed = self._settings.getInt(["minSpeed"])
self.maxSpeed = self._settings.getInt(["maxSpeed"])
self.lockfan = self._settings.get(["lockfan"])
2017-09-05 02:57:55 +02:00
getcontext().prec=5 #sets precision for "Decimal" not sure if this'll cause conflicts, ideas?
2017-09-05 03:13:39 +02:00
self.minPWM = round( Decimal(self.minSpeed) * Decimal(2.55), 2 )
self.maxPWM = round( Decimal(self.maxSpeed) * Decimal(2.55), 2 )
2017-09-05 02:57:55 +02:00
2017-09-04 19:52:55 +02:00
def rewrite_m106(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
if gcode and gcode.startswith('M106') and not self.lockfan:
fanPwm = re.search("S(\d+\.?\d*)", cmd)
2017-09-04 19:52:55 +02:00
if fanPwm and fanPwm.group(1):
fanPwm = fanPwm.group(1)
if Decimal(fanPwm) < self.minPWM and Decimal(fanPwm) != 0:
self._logger.info("fan pwm value " + str(fanPwm) + " is below threshold, increasing to " + str(self.minPWM) + " (" + str(self.minSpeed) + "%)")
cmd = "M106 S" + str(self.minPWM)
2017-09-04 19:52:55 +02:00
return cmd,
elif Decimal(fanPwm) > self.maxPWM:
self._logger.info("fan pwm value " + str(fanPwm) + " is above threshold, decreasing to " + str(self.maxPWM) + " (" + str(self.maxSpeed) + "%)")
2017-09-04 19:52:55 +02:00
cmd = "M106 S" + str(self.maxPWM)
return cmd,
elif gcode and gcode.startswith(('M106', 'M107')) and self.lockfan:
self._logger.info("A cooling fan control command was seen, but fanspeedslider is locked. Control command " + str(cmd) + " removed from queue.")
return None,
2017-08-30 10:28:26 +02:00
def get_update_information(self):
2017-08-28 23:10:51 +02:00
return dict(
fanspeedslider=dict(
2017-09-04 19:52:55 +02:00
displayName="Fan Speed Control",
2017-08-28 23:10:51 +02:00
displayVersion=self._plugin_version,
# version check: github repository
type="github_release",
user="ntoff",
repo="OctoPrint-FanSpeedSlider",
current=self._plugin_version,
# update method: pip
pip="https://github.com/ntoff/OctoPrint-FanSpeedSlider/archive/{target_version}.zip"
)
)
2017-09-04 19:52:55 +02:00
__plugin_name__ = "Fan Speed Control"
2020-07-17 00:44:02 +02:00
__plugin_pythoncompat__ = ">=2.7,<4"
2017-09-04 19:52:55 +02:00
def __plugin_load__():
global __plugin_implementation__
__plugin_implementation__ = __plugin_implementation__ = FanSliderPlugin()
global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.comm.protocol.gcode.queuing": __plugin_implementation__.rewrite_m106,
2017-08-28 23:10:51 +02:00
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}