Fix: prevent multiple objects of heat in case of config reload

This commit is contained in:
Thomas Klaehn
2022-03-30 12:01:05 +02:00
parent b5db8b943d
commit 3688c95cbf
2 changed files with 20 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import datetime
import logging
import threading
import time
@@ -7,26 +8,31 @@ import RPi.GPIO as GPIO
class Heat(threading.Thread):
def __init__(self, pin):
super(Heat, self).__init__()
threading.Thread.__init__(self)
self.__pin = pin
self.__state = False
self.__lock = threading.Lock()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
if GPIO.input(pin):
if not GPIO.input(pin):
self.__state = True
self.__run_condition = True
self.__next_update = datetime.datetime.now()
def on(self):
self.__state = True
GPIO.output(self.__pin, 0)
"""Switch the heat on"""
with self.__lock:
self.__state = True
GPIO.output(self.__pin, 0)
def off(self):
self.__state = False
GPIO.output(self.__pin, 1)
"""Switch the heat off"""
with self.__lock:
self.__state = False
GPIO.output(self.__pin, 1)
def run(self):
self.__next_update = datetime.datetime.now()
@@ -43,8 +49,10 @@ class Heat(threading.Thread):
self.off()
def stop(self):
"""Stop the controlling thead"""
self.__run_condition = False
self.join()
def state(self):
"""Return the state of the heat"""
return self.__state