~travisshears/sewa-reef-controller

ref: 2f911edf8a2c580b64a6319060d167276baeb9f3 sewa-reef-controller/CIRCUITPY/aquarium_controller/feed_mode.py -rwxr-xr-x 2.5 KiB
2f911edf — Travis Shears add more power heads and tidal sim 9 months ago
                                                                                
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
import time
from .power import feed_mode_prio, all_pumps, power_heads

_food_mixing_pumps = [power_heads[0], power_heads[1]]
_prio = feed_mode_prio

class FeedMode():
    def __init__(self, core):
        self.core = core
        self.start_time = 0
        self.started_part_one = False
        self.started_part_two = False

        core.subscribe('mode change', self.on_mode)
        core.subscribe('mode click', self.on_click)
        core.subscribe('tick 5', self.on_update)

    def start_part_one(self):
        self.started_part_one = True
        self.started_part_two = False
        self.start_time = time.time()
        # turn pumps oof
        for pump in all_pumps:
            self.core.publish('pwr', {
                'code': pump,
                'value': False,
                'prio': _prio
            })
        self.render()

    def start_part_two(self):
        self.started_part_two = True
        # turn flow food mixing pumps on
        for pump in _food_mixing_pumps:
            self.core.publish('pwr', {
                'code': pump,
                'value': True,
                'prio': _prio
            })
        self.render()

    def end(self):
        self.started_part_one = False
        self.started_part_two = False
        # important turn main pump back onn
        self.core.publish('reset', {
            'code': 'main_pump',
            'prio': _prio
        })
        for pump in all_pumps:
            self.core.publish('release', {
                'code': pump,
                'prio': _prio,
            })

        self.render()

    def on_update(self, _):
        if (self.started_part_one):
            time_diff = time.time() - self.start_time
            if(time_diff > 300 and not self.started_part_two): # 300 = 5min
                self.start_part_two()
            if(time_diff > 600): # 600 = 10min
                self.start_time = 0
                self.end()

    def on_click(self, mode):
        if(mode['code'] is not 'feed'):
            return
        self.start_part_one()

    def render(self):
        if (not self.started_part_one):
            msg = "feed mode: off"
        else:
            part_one_txt = "running" if not self.started_part_two else "done"
            part_two_txt = "waiting" if not self.started_part_two else "running"
            msg = f"feed mode: on\npart_one: {part_one_txt}\npart_two: {part_two_txt}"

        self.core.publish('render', {
            'text': msg
        })

    def on_mode(self, mode):
        if(mode['code'] is not 'feed'):
            return
        self.render()