82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
|
|
||
|
import datetime
|
||
|
import logging
|
||
|
from threading import Thread
|
||
|
import time
|
||
|
|
||
|
from w1thermsensor import W1ThermSensor
|
||
|
|
||
|
import heat
|
||
|
|
||
|
class Control(Thread):
|
||
|
def __init__(self, heat_pin: int):
|
||
|
super(Control, self).__init__()
|
||
|
self.__run_condition = True
|
||
|
self.__runtime = datetime.timedelta(hours=2)
|
||
|
self.__on_time = None
|
||
|
self.__off_time = datetime.datetime.now() + self.__runtime
|
||
|
self.__heat = heat.Heat(heat_pin)
|
||
|
self.__state = False
|
||
|
self.__target_temperature = 80.0
|
||
|
self.__span_temperature = 1.5
|
||
|
self.__sensor = W1ThermSensor()
|
||
|
self.__log = logging.getLogger()
|
||
|
|
||
|
def run(self):
|
||
|
while self.__run_condition:
|
||
|
if self.__state:
|
||
|
# check for elapsed runtime
|
||
|
now = datetime.datetime.now()
|
||
|
if now > self.__off_time:
|
||
|
self.__state = False
|
||
|
self.__log.info("switch off after %s", self.__runtime)
|
||
|
continue
|
||
|
# check for regulating
|
||
|
temperature = float(self.__sensor.get_temperature())
|
||
|
if self.__heat.state() and temperature > (self.__target_temperature + self.__span_temperature):
|
||
|
self.__heat.off()
|
||
|
self.__log.info("switch heat off for regulating")
|
||
|
self.__log.info("current temperature: %s", temperature)
|
||
|
else:
|
||
|
if not self.__heat.state() and temperature < (self.__target_temperature - self.__span_temperature):
|
||
|
self.__heat.on()
|
||
|
self.__log.info("switch heat on for regulating")
|
||
|
self.__log.info("current temperature: %s", temperature)
|
||
|
else:
|
||
|
if self.__heat.state():
|
||
|
self.__heat.off()
|
||
|
time.sleep(0.8)
|
||
|
|
||
|
def stop(self):
|
||
|
self.__run_condition = False
|
||
|
self.join()
|
||
|
|
||
|
def set_runtime(self, runtime):
|
||
|
dtime = datetime.datetime.strptime(runtime, "%H:%M")
|
||
|
self.__runtime = datetime.timedelta(hours=dtime.hour, minutes=dtime.minute)
|
||
|
|
||
|
def get_runtime(self):
|
||
|
return self.__runtime
|
||
|
|
||
|
def set_target_temperature(self, target_temperature):
|
||
|
self.__target_temperature = float(target_temperature)
|
||
|
|
||
|
def get_target_temperature(self):
|
||
|
return self.__target_temperature
|
||
|
|
||
|
def switch_on(self):
|
||
|
self.__on_time = datetime.datetime.now()
|
||
|
self.__off_time = self.__on_time + self.__runtime
|
||
|
self.__state = True
|
||
|
|
||
|
def switch_off(self):
|
||
|
self.__state = False
|
||
|
|
||
|
def state(self):
|
||
|
return self.__state
|
||
|
|
||
|
def time_to_switch_off(self):
|
||
|
if self.__state:
|
||
|
return self.__off_time - datetime.datetime.now()
|
||
|
return None
|