Skip to content

Commit

Permalink
track timers in order to cancel delayed off triggered by gcode when a…
Browse files Browse the repository at this point in the history
…n on trigger is sent within the delay period, #335
  • Loading branch information
jneilliii committed Apr 16, 2023
1 parent 63ff8b7 commit 501267a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions octoprint_tplinksmartplug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(self):
self.poll_status = None
self.power_off_queue = []
self._gcode_queued = False
self.active_timers = {"on": {}, "off": {}}

##~~ StartupPlugin mixin

Expand Down Expand Up @@ -1060,6 +1061,10 @@ def sendCommand(self, cmd, plugip, plug_num=0):
##~~ Gcode processing hook

def gcode_turn_off(self, plug):
if plug["ip"] in self.active_timers["off"]:
self.active_timers["off"][plug["ip"]].cancel()
del self.active_timers["off"][plug["ip"]]

if self._printer.is_printing() and plug["warnPrinting"] is True:
self._tplinksmartplug_logger.debug(
"Not powering off %s immediately because printer is printing." % plug["label"])
Expand All @@ -1068,7 +1073,12 @@ def gcode_turn_off(self, plug):
chk = self.turn_off(plug["ip"])
self._plugin_manager.send_plugin_message(self._identifier, chk)


def gcode_turn_on(self, plug):
if plug["ip"] in self.active_timers["on"]:
self.active_timers["on"][plug["ip"]].cancel()
del self.active_timers["on"][plug["ip"]]

chk = self.turn_on(plug["ip"])
self._plugin_manager.send_plugin_message(self._identifier, chk)

Expand Down Expand Up @@ -1108,19 +1118,25 @@ def processAtCommand(self, comm_instance, phase, command, parameters, tags=None,
plug = self.plug_search(self._settings.get(["arrSmartplugs"]), "ip", plugip)
self._tplinksmartplug_logger.debug(plug)
if plug and plug["gcodeEnabled"]:
t = threading.Timer(int(plug["gcodeOnDelay"]), self.gcode_turn_on, [plug])
t.daemon = True
t.start()
if plugip in self.active_timers["off"]:
self.active_timers["off"][plugip].cancel()
del self.active_timers["off"][plugip]
self.active_timers["on"][plugip] = threading.Timer(int(plug["gcodeOnDelay"]), self.gcode_turn_on, [plug])
self.active_timers["on"][plugip].daemon = True
self.active_timers["on"][plugip].start()
return None
if command == "TPLINKOFF":
plugip = parameters
self._tplinksmartplug_logger.debug("Received TPLINKOFF command, attempting power off of %s." % plugip)
plug = self.plug_search(self._settings.get(["arrSmartplugs"]), "ip", plugip)
self._tplinksmartplug_logger.debug(plug)
if plug and plug["gcodeEnabled"]:
t = threading.Timer(int(plug["gcodeOffDelay"]), self.gcode_turn_off, [plug])
t.daemon = True
t.start()
if plugip in self.active_timers["on"]:
self.active_timers["on"][plugip].cancel()
del self.active_timers["on"][plugip]
self.active_timers["off"][plugip] = threading.Timer(int(plug["gcodeOffDelay"]), self.gcode_turn_off, [plug])
self.active_timers["off"][plugip].daemon = True
self.active_timers["off"][plugip].start()
return None
if command == 'TPLINKIDLEON':
self.powerOffWhenIdle = True
Expand Down
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-TPLinkSmartplug"

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

# 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 501267a

Please sign in to comment.