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

@@ -14,7 +14,7 @@ import heat
class Control(threading.Thread):
def __init__(self, configfile):
super(Control, self).__init__()
threading.Thread.__init__(self)
self.__run_condition = True
self.__config_file = configfile
self.__config = None
@@ -26,6 +26,7 @@ class Control(threading.Thread):
self.__temperature = None
def reload_config(self):
self.__log.info("Reloading configuration triggered")
self.__trigger_read_config = True
def load_config(self):
@@ -38,6 +39,8 @@ class Control(threading.Thread):
shutil.copyfile("config/config.json", self.__config_file)
with open(self.__config_file, "r", encoding="UTF-8") as handle:
self.__config = json.load(handle)
if self.__heat is not None:
self.__heat.stop()
self.__heat = heat.Heat(int(self.__config['heat'][0]['pin']))
for _ in range(len(self.__config['water'])):
self.__water_state.append(False)
@@ -50,15 +53,13 @@ class Control(threading.Thread):
GPIO.output(pin, 1)
def run(self):
self.load_config()
self.__heat.off()
self.__heat.start()
while self.__run_condition:
if self.__trigger_read_config:
self.__trigger_read_config = False
self.load_config()
self.__heat.start()
tmp = float(self.__sensor.get_temperature())
tmp = round(float(self.__sensor.get_temperature()), 1)
if tmp != self.__temperature:
self.__temperature = tmp
self.__log.info("Temperature: %.1f °C", self.__temperature)