-
Notifications
You must be signed in to change notification settings - Fork 6
/
poupool.py
443 lines (345 loc) · 13.6 KB
/
poupool.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Poupool - swimming pool control software
# Copyright (C) 2019 Cyril Jaquier
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import itertools
import logging.config
import os
import signal
import sys
import time
import pykka
from controller.arduino import Arduino
from controller.config import as_list, config
from controller.device import DeviceRegistry
from controller.disinfection import Disinfection
from controller.dispatcher import Dispatcher
from controller.encoder import Encoder
from controller.filtration import Filtration
from controller.heating import Heater, Heating
from controller.lcd import Lcd
from controller.light import Light
from controller.mqtt import Mqtt
from controller.sensor import DisinfectionReader, DisinfectionWriter, TemperatureReader, TemperatureWriter
from controller.swim import Swim
from controller.tank import Tank
def setup_gpio(registry, gpio):
from controller.device import PumpDevice, SwitchDevice
def create(device, name):
pins = as_list(config["pins", name])
return device(name, gpio, pins if len(pins) > 1 else pins[0])
gpio.setmode(gpio.BCM)
registry.add_pump(create(PumpDevice, "variable"))
registry.add_pump(create(SwitchDevice, "boost"))
registry.add_pump(create(SwitchDevice, "ph"))
registry.add_pump(create(SwitchDevice, "cl"))
registry.add_valve(create(SwitchDevice, "gravity"))
registry.add_valve(create(SwitchDevice, "backwash"))
registry.add_valve(create(SwitchDevice, "tank"))
registry.add_valve(create(SwitchDevice, "drain"))
registry.add_valve(create(SwitchDevice, "main"))
registry.add_valve(create(SwitchDevice, "heating"))
registry.add_valve(create(SwitchDevice, "light"))
def setup_rpi(registry):
# Relay
import RPi.GPIO as GPIO
from controller.device import (
ArduinoDevice,
EZOSensorDevice,
LcdDevice,
SwimPumpDevice,
TankSensorDevice,
TempSensorDevice,
)
setup_gpio(registry, GPIO)
# Initialize I2C bus.
import board
import busio
i2c = busio.I2C(board.SCL, board.SDA)
# ADC
import adafruit_ads1x15.ads1015 as ADS
# Create the ADC object using the I2C bus
adc = ADS.ADS1015(i2c)
# With a gain of 2/3 and a sensor output of 0.25V-5V, the values should be around 83 and 1665
params = ((int, "channel"), (float, "gain"), (int, "low"), (int, "high"))
registry.add_sensor(TankSensorDevice("tank", adc, *[t(config["adc", n]) for t, n in params]))
# DAC
import adafruit_mcp4725
dac = adafruit_mcp4725.MCP4725(i2c)
registry.add_pump(SwimPumpDevice("swim", GPIO, int(config["pins", "swim"]), dac))
# pH, ORP
registry.add_sensor(EZOSensorDevice("ph", config["serial", "ph"]))
registry.add_sensor(EZOSensorDevice("orp", config["serial", "orp"]))
# Arduino (cover, water)
registry.add_device(ArduinoDevice("arduino", config["serial", "arduino"]))
# LCD
registry.add_device(LcdDevice("lcd", config["serial", "lcd"]))
# 1-wire
# 28-031634d04aff
# 28-0416350909ff
# 28-031634d54bff
# 28-041635088bff
registry.add_sensor(TempSensorDevice("temperature_pool", config["1-wire", "pool"]))
registry.add_sensor(TempSensorDevice("temperature_air", config["1-wire", "air"]))
registry.add_sensor(TempSensorDevice("temperature_local", config["1-wire", "local"]))
registry.add_sensor(TempSensorDevice("temperature_ncc", config["1-wire", "ncc"]))
def setup_fake(registry):
from controller.device import SensorDevice, StoppableDevice, SwimPumpDevice
class FakeGpio:
OUT = "OUT"
BCM = "BCM"
def setmode(self, mode):
print("Set mode to %s" % mode)
def setup(self, pins, pins_type):
print(f"Setup pin(s) {pins!s} to {pins_type}")
def output(self, pins, values):
print(f"Set pin(s) {pins!s} to {values!s}")
class FakeSensor(SensorDevice):
def __init__(self, name, value):
super().__init__(name)
self.__value = value
@property
def value(self):
return self.__value
class FakeRandomSensor(SensorDevice):
def __init__(self, name, min_value, max_value):
super().__init__(name)
self.__min = min_value
self.__max = max_value
@property
def value(self):
import random
return random.uniform(self.__min, self.__max)
class FakeArduino(StoppableDevice):
def __init__(self, name):
super().__init__(name)
self.__cover_position = 0
self.__cover_direction = 0
self.__water_counter = 0
@property
def cover_position(self):
if self.__cover_direction == 1:
self.__cover_position += 40
elif self.__cover_direction == -1:
self.__cover_position -= 40
self.__cover_position = min(max(self.__cover_position, 0), 100)
return self.__cover_position
def cover_open(self):
self.__cover_direction = 1
def cover_close(self):
self.__cover_direction = -1
def cover_stop(self):
self.__cover_direction = 0
@property
def water_counter(self):
self.__water_counter += 1
return self.__water_counter
def stop(self):
self.cover_stop()
class FakeDAC:
def __init__(self):
self.__value = 0
@property
def normalized_value(self):
return self.__value
@normalized_value.setter
def normalized_value(self, value):
self.__value = value
@property
def value(self):
return self.__value
@value.setter
def value(self, value):
self.__value = value
class FakeLcd(StoppableDevice):
def __init__(self, name):
super().__init__(name)
self.__counter = 0
def __getattr__(self, attr):
# Just do nothing. We already implemented the minimum attributes for the fake
return lambda *x: None
def stop(self):
pass
def write(self, value):
if self.__counter % 5 == 0:
# The display is a 4x20 LCD.
print("\n" + "\n".join(value[20 * i : 20 * i + 20] for i in range(4)) + "\n")
self.__counter += 1
# Relay
GPIO = FakeGpio()
setup_gpio(registry, GPIO)
# ADC
registry.add_sensor(FakeSensor("tank", 51.234))
# DAC
registry.add_pump(SwimPumpDevice("swim", GPIO, int(config["pins", "swim"]), FakeDAC()))
# pH, ORP
registry.add_sensor(FakeRandomSensor("ph", 6.5, 8))
registry.add_sensor(FakeRandomSensor("orp", 640, 800))
# 1-wire
registry.add_sensor(FakeSensor("temperature_pool", 24.5))
registry.add_sensor(FakeSensor("temperature_local", 20.6))
registry.add_sensor(FakeSensor("temperature_air", 19.4))
registry.add_sensor(FakeSensor("temperature_ncc", 21.3))
# Arduino
registry.add_device(FakeArduino("arduino"))
# Lcd
registry.add_device(FakeLcd("lcd"))
def toggle_test(device):
print("Toggling %s " % device.name, end="")
result = input("[y/N]: ")
if result == "y":
device.on()
time.sleep(2)
device.off()
def read_test(device):
print("Read %s " % device.name, end="")
try:
result = int(input("[0-10000]: "))
if 0 < result <= 10000:
for _ in range(result):
print(device.value)
time.sleep(1)
except ValueError:
pass
def test(args, devices):
pump = devices.get_pump("variable")
print("Toggling %s " % pump.name, end="")
result = input("[y/N]: ")
if result == "y":
for speed in reversed(range(4)):
print("%s: speed %d" % (pump.name, speed))
pump.speed(speed)
time.sleep(2)
toggle_test(devices.get_pump("boost"))
toggle_test(devices.get_pump("swim"))
toggle_test(devices.get_pump("ph"))
toggle_test(devices.get_pump("cl"))
toggle_test(devices.get_valve("gravity"))
toggle_test(devices.get_valve("backwash"))
toggle_test(devices.get_valve("tank"))
toggle_test(devices.get_valve("drain"))
toggle_test(devices.get_valve("main"))
toggle_test(devices.get_valve("light"))
# toggle_test(devices.get_valve("heater"))
toggle_test(devices.get_valve("heating"))
read_test(devices.get_sensor("temperature_pool"))
read_test(devices.get_sensor("temperature_local"))
read_test(devices.get_sensor("temperature_air"))
read_test(devices.get_sensor("temperature_ncc"))
read_test(devices.get_sensor("tank"))
read_test(devices.get_sensor("ph"))
read_test(devices.get_sensor("orp"))
# Main running flag
running = True
def main(args, devices):
dispatcher = Dispatcher()
mqtt = Mqtt.start(dispatcher).proxy()
lcd = Lcd.start(devices.get_device("lcd")).proxy()
encoder = Encoder(mqtt, lcd)
# Temperature
sensors = [
devices.get_sensor("temperature_pool"),
devices.get_sensor("temperature_local"),
devices.get_sensor("temperature_air"),
devices.get_sensor("temperature_ncc"),
]
temperature_reader = TemperatureReader.start(sensors).proxy()
temperature_writer = TemperatureWriter.start(encoder, temperature_reader).proxy()
# Filtration
filtration = Filtration.start(temperature_reader, encoder, devices).proxy()
# Swimming pump
swim = Swim.start(temperature_reader, encoder, devices).proxy()
# Tank
tank = Tank.start(encoder, devices).proxy()
# Disinfection
sensors = [devices.get_sensor("ph"), devices.get_sensor("orp")]
disinfection_reader = DisinfectionReader.start(sensors).proxy()
disinfection_writer = DisinfectionWriter.start(encoder, disinfection_reader).proxy()
disinfection = Disinfection.start(
encoder, devices, disinfection_reader, disinfection_writer, args.no_disinfection
).proxy()
# Heating
switch = devices.get_valve("heater")
heater = Heater.start(temperature_reader, switch).proxy()
heating = Heating.start(temperature_reader, encoder, devices).proxy()
# Light
light = Light.start(encoder, devices).proxy()
# Cover and water meter
arduino = Arduino.start(encoder, devices).proxy()
dispatcher.register(filtration, tank, swim, light, heater, heating, disinfection, arduino)
# Start actors that run all the time
mqtt.do_start().get()
temperature_reader.do_read.defer()
temperature_writer.do_write.defer()
disinfection_reader.do_read.defer()
# disinfection_writer is started/stopped by the disinfection actor
lcd.do_start.defer()
# Monitor the main actors. If one dies, we will exit the main thread.
main_actors = [filtration.actor_ref, tank.actor_ref, disinfection.actor_ref, heating.actor_ref]
# Start test mode
if args.test_start:
global running
running = False
time.sleep(2)
# Wait forever or until SIGTERM is caught
while running and all(actor.is_alive() for actor in main_actors):
time.sleep(0.5)
# If possible try to stop the filtration actor which in turn will stop others.
if filtration.actor_ref.is_alive():
filtration.halt()
return 1 if running else 0
def sigterm_handler(signo, stack_frame):
global running
running = False
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--log-config", action="store", default="logging.conf", help="log configuration file")
parser.add_argument("--no-disinfection", action="store_true", help="disable disinfection support")
parser.add_argument("--test-mode", action="store_true", help="test mode for the hardware")
parser.add_argument("--fake-devices", action="store_true", help="fake the underlying hardware")
parser.add_argument("--test-start", action="store_true", help="test application start")
args = parser.parse_args()
# Setup logging
if os.path.isfile(args.log_config):
logging.config.fileConfig(args.log_config, disable_existing_loggers=False)
else:
logging.error("Log configuration file (%s) cannot be used" % args.log_config)
# Handle SIGTERM nicely. It is used by systemd to stop us.
signal.signal(signal.SIGTERM, sigterm_handler)
# Handle SIGINT. It is ctrl+c
signal.signal(signal.SIGINT, sigterm_handler)
devices = DeviceRegistry()
try:
if args.fake_devices:
setup_fake(devices)
else:
setup_rpi(devices)
if args.test_mode:
test(args, devices)
else:
sys.exit(main(args, devices))
finally:
pykka.ActorRegistry.stop_all()
# Turn off all the devices on exit
for device in itertools.chain(devices.get_pumps(), devices.get_valves()):
device.off()
# Stop stoppable devices
for device in devices.get_devices():
device.stop()
# Ensure all the pins are configured back as inputs
if not args.fake_devices:
import RPi.GPIO as GPIO
GPIO.cleanup()