51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
|
|
||
|
import datetime
|
||
|
import threading
|
||
|
import time
|
||
|
|
||
|
import RPi.GPIO as GPIO
|
||
|
|
||
|
class Heat(threading.Thread):
|
||
|
def __init__(self, pin):
|
||
|
super(Heat, self).__init__()
|
||
|
self.__pin = pin
|
||
|
self.__state = False
|
||
|
|
||
|
GPIO.setwarnings(False)
|
||
|
GPIO.setmode(GPIO.BCM)
|
||
|
GPIO.setup(pin, GPIO.OUT)
|
||
|
|
||
|
if 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)
|
||
|
|
||
|
def off(self):
|
||
|
self.__state = False
|
||
|
GPIO.output(self.__pin, 1)
|
||
|
|
||
|
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):
|
||
|
self.__run_condition = False
|
||
|
self.join()
|
||
|
|
||
|
def state(self):
|
||
|
return self.__state
|