@@ -103,6 +103,7 @@ def __init__(self):
103
103
self .poll_status = None
104
104
self .power_off_queue = []
105
105
self ._gcode_queued = False
106
+ self .active_timers = {"on" : {}, "off" : {}}
106
107
107
108
##~~ StartupPlugin mixin
108
109
@@ -1060,6 +1061,10 @@ def sendCommand(self, cmd, plugip, plug_num=0):
1060
1061
##~~ Gcode processing hook
1061
1062
1062
1063
def gcode_turn_off (self , plug ):
1064
+ if plug ["ip" ] in self .active_timers ["off" ]:
1065
+ self .active_timers ["off" ][plug ["ip" ]].cancel ()
1066
+ del self .active_timers ["off" ][plug ["ip" ]]
1067
+
1063
1068
if self ._printer .is_printing () and plug ["warnPrinting" ] is True :
1064
1069
self ._tplinksmartplug_logger .debug (
1065
1070
"Not powering off %s immediately because printer is printing." % plug ["label" ])
@@ -1068,7 +1073,12 @@ def gcode_turn_off(self, plug):
1068
1073
chk = self .turn_off (plug ["ip" ])
1069
1074
self ._plugin_manager .send_plugin_message (self ._identifier , chk )
1070
1075
1076
+
1071
1077
def gcode_turn_on (self , plug ):
1078
+ if plug ["ip" ] in self .active_timers ["on" ]:
1079
+ self .active_timers ["on" ][plug ["ip" ]].cancel ()
1080
+ del self .active_timers ["on" ][plug ["ip" ]]
1081
+
1072
1082
chk = self .turn_on (plug ["ip" ])
1073
1083
self ._plugin_manager .send_plugin_message (self ._identifier , chk )
1074
1084
@@ -1108,19 +1118,25 @@ def processAtCommand(self, comm_instance, phase, command, parameters, tags=None,
1108
1118
plug = self .plug_search (self ._settings .get (["arrSmartplugs" ]), "ip" , plugip )
1109
1119
self ._tplinksmartplug_logger .debug (plug )
1110
1120
if plug and plug ["gcodeEnabled" ]:
1111
- t = threading .Timer (int (plug ["gcodeOnDelay" ]), self .gcode_turn_on , [plug ])
1112
- t .daemon = True
1113
- t .start ()
1121
+ if plugip in self .active_timers ["off" ]:
1122
+ self .active_timers ["off" ][plugip ].cancel ()
1123
+ del self .active_timers ["off" ][plugip ]
1124
+ self .active_timers ["on" ][plugip ] = threading .Timer (int (plug ["gcodeOnDelay" ]), self .gcode_turn_on , [plug ])
1125
+ self .active_timers ["on" ][plugip ].daemon = True
1126
+ self .active_timers ["on" ][plugip ].start ()
1114
1127
return None
1115
1128
if command == "TPLINKOFF" :
1116
1129
plugip = parameters
1117
1130
self ._tplinksmartplug_logger .debug ("Received TPLINKOFF command, attempting power off of %s." % plugip )
1118
1131
plug = self .plug_search (self ._settings .get (["arrSmartplugs" ]), "ip" , plugip )
1119
1132
self ._tplinksmartplug_logger .debug (plug )
1120
1133
if plug and plug ["gcodeEnabled" ]:
1121
- t = threading .Timer (int (plug ["gcodeOffDelay" ]), self .gcode_turn_off , [plug ])
1122
- t .daemon = True
1123
- t .start ()
1134
+ if plugip in self .active_timers ["on" ]:
1135
+ self .active_timers ["on" ][plugip ].cancel ()
1136
+ del self .active_timers ["on" ][plugip ]
1137
+ self .active_timers ["off" ][plugip ] = threading .Timer (int (plug ["gcodeOffDelay" ]), self .gcode_turn_off , [plug ])
1138
+ self .active_timers ["off" ][plugip ].daemon = True
1139
+ self .active_timers ["off" ][plugip ].start ()
1124
1140
return None
1125
1141
if command == 'TPLINKIDLEON' :
1126
1142
self .powerOffWhenIdle = True
0 commit comments