2022-11-19 18:52:01 +01:00
|
|
|
import atexit
|
2023-02-21 14:00:07 +01:00
|
|
|
from time import perf_counter
|
2022-11-19 18:52:01 +01:00
|
|
|
try:
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
rpi = True
|
|
|
|
except:
|
|
|
|
rpi = False
|
2023-02-21 14:00:07 +01:00
|
|
|
class GPIO():
|
|
|
|
LOW = 0
|
|
|
|
HIGH = 1
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Actuator():
|
2023-02-21 14:00:07 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.wattage = kwargs.pop('wattage')
|
|
|
|
self.total_seconds_on = 0
|
|
|
|
self.logging = False
|
|
|
|
self.started_at = 0
|
|
|
|
|
|
|
|
def log_on(self):
|
|
|
|
if self.logging:
|
|
|
|
return
|
|
|
|
self.started_at = perf_counter()
|
|
|
|
self.logging = True
|
|
|
|
|
|
|
|
def elapsed(self):
|
|
|
|
return perf_counter() - self.started_at
|
|
|
|
|
|
|
|
def log_off(self):
|
|
|
|
if not self.logging:
|
|
|
|
return
|
|
|
|
self.total_seconds_on += self.elapsed()
|
|
|
|
self.logging = False
|
2022-11-19 18:52:01 +01:00
|
|
|
|
2023-02-21 14:00:07 +01:00
|
|
|
def s_to_hr(self, s):
|
|
|
|
return s / 60 / 60
|
|
|
|
|
|
|
|
def consumption(self):
|
|
|
|
total_time = self.total_seconds_on
|
|
|
|
if self.logging:
|
|
|
|
# If we are on, count this time too
|
|
|
|
total_time += self.elapsed()
|
|
|
|
return self.s_to_hr(total_time) * self.wattage
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
class GPIOPin(Actuator):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-21 14:00:07 +01:00
|
|
|
self.pin = kwargs.pop('pin')
|
|
|
|
self.initial = kwargs.pop('initial')
|
|
|
|
self.onstate = kwargs.pop('onstate')
|
|
|
|
self.offstate = kwargs.pop('offstate')
|
2022-11-19 18:52:01 +01:00
|
|
|
GPIO.setup(self.pin, GPIO.OUT, initial=self.initial)
|
|
|
|
atexit.register(self.deinit)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def deinit(self):
|
|
|
|
# Be sure everything is off at exit
|
|
|
|
print('Cleaning before exit')
|
|
|
|
GPIO.output(self.pin, self.initial)
|
|
|
|
GPIO.cleanup()
|
|
|
|
|
|
|
|
def enable(self, enable=True):
|
|
|
|
GPIO.output(self.pin, self.onstate) if enable else self.disable()
|
2023-02-21 14:00:07 +01:00
|
|
|
if enable:
|
|
|
|
self.log_on()
|
|
|
|
else:
|
|
|
|
self.log_off()
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
GPIO.output(self.pin, self.offstate)
|
2023-02-21 14:00:07 +01:00
|
|
|
self.log_off()
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
class MockPIN(Actuator):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-21 14:00:07 +01:00
|
|
|
self.pin = kwargs.pop('pin')
|
|
|
|
self.initial = kwargs.pop('initial')
|
|
|
|
self.onstate = kwargs.pop('onstate')
|
|
|
|
self.offstate = kwargs.pop('offstate')
|
2022-11-19 18:52:01 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def enable(self, enable=True):
|
2023-02-21 14:00:07 +01:00
|
|
|
if enable:
|
|
|
|
self.log_on()
|
|
|
|
else:
|
|
|
|
self.log_off()
|
2022-11-19 21:42:54 +01:00
|
|
|
# print('FAKE ENABLE') if enable else self.disable()
|
2022-11-19 18:52:01 +01:00
|
|
|
|
|
|
|
def disable(self):
|
2023-02-21 14:00:07 +01:00
|
|
|
self.log_off()
|
2022-11-19 21:42:54 +01:00
|
|
|
# print('FAKE DISABLE')
|
2022-11-19 18:52:01 +01:00
|
|
|
|
2023-02-21 14:00:07 +01:00
|
|
|
def PIN(*args, **kwargs):
|
|
|
|
return (GPIOPin if rpi else MockPIN)(*args, **kwargs)
|
|
|
|
|
2022-11-19 18:52:01 +01:00
|
|
|
actuators = {
|
2023-02-21 14:00:07 +01:00
|
|
|
'heater': PIN(pin=22, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH,
|
|
|
|
wattage=100),
|
|
|
|
'fan': PIN(pin=27, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH,
|
|
|
|
wattage=12*0.4),
|
|
|
|
'humidifier': PIN(pin=17, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH,
|
2023-02-21 19:24:34 +01:00
|
|
|
wattage=25),
|
2023-02-25 11:04:44 +01:00
|
|
|
'heater2': PIN(pin=17, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH,
|
2023-03-30 15:45:02 +02:00
|
|
|
wattage=400),
|
|
|
|
'freezer': PIN(pin=14, initial=GPIO.LOW, onstate=GPIO.HIGH, offstate=GPIO.LOW,
|
2023-02-25 11:35:51 +01:00
|
|
|
wattage=165),
|
2022-11-19 18:52:01 +01:00
|
|
|
}
|