2021-05-05 10:30:56 +00:00
|
|
|
|
|
|
|
import datetime
|
2022-03-30 10:01:05 +00:00
|
|
|
import logging
|
2021-05-05 10:30:56 +00:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
|
|
|
|
class Heat(threading.Thread):
|
|
|
|
def __init__(self, pin):
|
2022-03-30 10:01:05 +00:00
|
|
|
threading.Thread.__init__(self)
|
2021-05-05 10:30:56 +00:00
|
|
|
self.__pin = pin
|
|
|
|
self.__state = False
|
2022-03-30 10:01:05 +00:00
|
|
|
self.__lock = threading.Lock()
|
2021-05-05 10:30:56 +00:00
|
|
|
|
|
|
|
GPIO.setwarnings(False)
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.setup(pin, GPIO.OUT)
|
|
|
|
|
2022-03-30 10:01:05 +00:00
|
|
|
if not GPIO.input(pin):
|
2021-05-05 10:30:56 +00:00
|
|
|
self.__state = True
|
|
|
|
self.__run_condition = True
|
|
|
|
self.__next_update = datetime.datetime.now()
|
|
|
|
|
|
|
|
def on(self):
|
2022-03-30 10:01:05 +00:00
|
|
|
"""Switch the heat on"""
|
|
|
|
with self.__lock:
|
|
|
|
self.__state = True
|
|
|
|
GPIO.output(self.__pin, 0)
|
2021-05-05 10:30:56 +00:00
|
|
|
|
|
|
|
def off(self):
|
2022-03-30 10:01:05 +00:00
|
|
|
"""Switch the heat off"""
|
|
|
|
with self.__lock:
|
|
|
|
self.__state = False
|
|
|
|
GPIO.output(self.__pin, 1)
|
2021-05-05 10:30:56 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.__next_update = datetime.datetime.now()
|
|
|
|
while self.__run_condition:
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
if now >= self.__next_update:
|
|
|
|
if self.__state:
|
|
|
|
# Do a power cycle to prevent auto-poweroff
|
|
|
|
GPIO.output(self.__pin, 1)
|
|
|
|
time.sleep(5)
|
|
|
|
GPIO.output(self.__pin, 0)
|
|
|
|
self.__next_update = now + datetime.timedelta(minutes=5)
|
|
|
|
time.sleep(1)
|
|
|
|
self.off()
|
|
|
|
|
|
|
|
def stop(self):
|
2022-03-30 10:01:05 +00:00
|
|
|
"""Stop the controlling thead"""
|
2021-05-05 10:30:56 +00:00
|
|
|
self.__run_condition = False
|
|
|
|
self.join()
|
|
|
|
|
|
|
|
def state(self):
|
2022-03-30 10:01:05 +00:00
|
|
|
"""Return the state of the heat"""
|
2021-05-05 10:30:56 +00:00
|
|
|
return self.__state
|