forked from brycesub/silvia-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silvia-pi.py
96 lines (82 loc) · 3.38 KB
/
silvia-pi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import asyncio
import apigpio_fork
import config as conf
from utils import topics, PubSub
import functools
from coroutines import *
from hardware import boiler, pump, solenoid, temperature_sensor, display
import logging
async def printer(hub, ignored_topics=frozenset()):
with PubSub.Listener(hub) as queue:
while True:
key, msg = await queue.get()
if key not in ignored_topics:
print(f'Reader for key {key} got message: {msg}')
async def publish_initial_authoritative_state(coros):
await asyncio.sleep(2)
for coro in coros:
coro.publish_authoritative()
async def safe_actuators(pi, solenoid_pin, pump_pin, he_pin):
print("Safing actuators")
await pi.write(he_pin, 0)
await pi.write(pump_pin, 0)
await pi.write(solenoid_pin, 0)
if __name__ == '__main__':
bleak_logger = logging.getLogger('bleak')
bleak_logger.setLevel(logging.WARN)
loop = asyncio.get_event_loop()
hub = PubSub.Hub()
if conf.test_hardware:
s = temperature_sensor.EmulatedSensor({})
b = boiler.EmulatedBoiler(s)
p = pump.EmulatedPump()
else:
s = temperature_sensor.Max31865Sensor(conf.boiler_temp_sensor_cs_pin, conf.group_temp_sensor_cs_pin, rtd_nominal_boiler=100.65, rtd_nominal_group=101.5)
b = boiler.GpioBoiler(conf.he_pin)
p = pump.GpioPump(conf.pump_pin)
pi = apigpio_fork.Pi(loop)
address = (conf.pigpio_host, conf.pigpio_port)
loop.run_until_complete(pi.connect(address))
loop.run_until_complete(safe_actuators(pi, conf.solenoid_pin, conf.pump_pin, conf.he_pin))
dc = DisplayController(hub, pi, dev_num=1, cs_num=0, dc=12, res=22)
coros = [
PigpioPins(hub, pi),
TemperatureSensor(hub, s),
SteamControlSignal(hub, conf.steam_set_point, conf.steam_delta),
HeatingElementController(hub, b),
ButtonControls(hub),
SimplePidControlSignal(hub, (3.4, 0.3, 40.0), default_setpoint=conf.set_point),
AcaiaScaleSensor(hub, conf.acaia_mac),
MQTTProxy(hub, conf.mqtt_server, prefix=conf.mqtt_prefix, debug_mappings=True),
ActuatorControl(hub, pi, conf.pump_pin, conf.solenoid_pin),
BrewControl(
hub,
default_use_preinfusion=conf.use_preinfusion,
default_preinfusion_time=conf.preinfusion_time,
default_dwell_time=conf.dwell_time
),
BrewTimer(hub),
WeightedShotController(hub, conf.weighted_shot_reaction_time),
dc,
BrewProfiler(hub, conf.brew_profile_directory),
DoseControl(hub, conf.default_target_ratio),
]
futures = functools.reduce(lambda carry, coro: carry + coro.futures(loop), coros, [])
futures.append(printer(hub, frozenset([
topics.TOPIC_CURRENT_BOILER_TEMPERATURE,
topics.TOPIC_AVERAGE_BOILER_TEMPERATURE,
topics.TOPIC_PID_AVERAGE_VALUE,
topics.TOPIC_PID_VALUE,
topics.TOPIC_STEAM_HE_ON,
topics.TOPIC_HE_ON,
topics.TOPIC_PID_TERMS,
topics.TOPIC_CURRENT_GROUP_TEMPERATURE,
topics.TOPIC_ALL_TEMPERATURES,
])))
futures.append(publish_initial_authoritative_state(coros))
try:
loop.run_until_complete(asyncio.gather(*futures))
loop.run_forever()
finally:
loop.run_until_complete(safe_actuators(pi, conf.solenoid_pin, conf.pump_pin, conf.he_pin))
loop.run_until_complete(dc.close_handle())