-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainstem.py
206 lines (181 loc) · 8.05 KB
/
brainstem.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
"""
brainstem.py
Reactions happen here.
When Lutron values change, perform an action.
- Any sleep button turns off Laundry Room lights.
- Master Bathroom and Guest Bathroom occupancy sensors turn on Rinnai
recirculation for 5 min.
"""
import asyncio, heapq, logging, prometheus_client, requests
from datetime import time, datetime, timedelta, timezone
LOGGER = logging.getLogger('porter.brainstem')
class BrainstemError(Exception):
pass
class Timers:
def __init__(self, timers, runner):
self.runner = runner
zero = datetime.strptime('000000', '%H%M%S')
def cnv(hhmmss):
try:
return datetime.strptime('%06d' % hhmmss, '%H%M%S').time().replace(tzinfo=timezone.utc)
except ValueError:
LOGGER.error(f'could not parse brainstem timer {hhmmss} in {timers}')
raise
self.timers = [(cnv(hhmmss), action) for (hhmmss, action) in timers]
now = datetime.now(timezone.utc)
self.today = now.date()
self.heap_of_timers = [(datetime.combine(self.today, t), action)
for (t, action) in self.timers
if datetime.combine(self.today, t) >= now]
heapq.heapify(self.heap_of_timers)
async def run(self, action):
return await self.runner(action)
async def process_timers(self):
"""Waits for the next timer and runs it. Returns a coroutine to run next."""
now = datetime.now(timezone.utc)
if self.today < now.date():
self.today = now.date()
while self.heap_of_timers:
(t, action) = heapq.heappop(self.heap_of_timers)
await self.run(action)
self.heap_of_timers = [(datetime.combine(self.today, t), action)
for (t, action) in self.timers]
heapq.heapify(self.heap_of_timers)
if self.heap_of_timers:
# then run the next timer if it's runnable; otherwise wait until runnable
future = (self.heap_of_timers[0][0] - now).total_seconds()
if future <= 0:
(t, action) = heapq.heappop(self.heap_of_timers)
LOGGER.info(f'timer running {action} scheduled {-future}s ago')
try:
await self.run(action)
except Exception as ex:
LOGGER.error(f'timer running {action}', exc_info=ex)
else:
return asyncio.sleep(future, self.process_timers())
else:
midnight = datetime.combine(self.today + timedelta(days=1), time(), tzinfo=timezone.utc)
return asyncio.sleep((midnight - now).total_seconds(), self.process_timers())
return self.process_timers()
class EventPropagator:
def __init__(self, bclient, modulename, target=None):
self.bclient = bclient
self.modulename = modulename
self.targ = target
self.awaitables = set()
self.children = []
def target(self, targ):
assert self.targ is None
ep = EventPropagator(self.bclient, self.modulename, targ)
self.children.append(ep)
return ep
def propagate(self, selector):
assert self.targ
try:
coro = self.bclient.observe_event(self.modulename, self.targ, selector)
if coro:
self.awaitables.add(coro)
except Exception as ex:
LOGGER.error(f'exception in propagate() {self.modulename} {self.targ} {selector}', exc_info=ex)
def add_awaitables_to(self, otherset):
for child in self.children:
child.add_awaitables_to(otherset)
otherset |= self.awaitables
self.awaitables = set()
class CircularBuffer:
# TODO: could be more efficient
def __init__(self, size):
self.size = size
self.items = []
def add(self, item):
self.items.append(item)
if len(self.items) <= self.size:
return False
del self.items[0]
return True
class Brainstem:
def __init__(self, config):
self.config = config
self.module_to_client = {}
myconfig = self.config.get('brainstem', {})
self.timers = Timers(myconfig.get('timers', []), self.run)
self.reactions = {}
self.ratelimits = {}
self.eventbuffer = CircularBuffer(10)
for (mod, target, selector, cmd) in myconfig.get('reactions', []):
m = self.reactions.get(mod)
if not m:
m = {}
self.reactions[mod] = m
t = m.get(target)
if not t:
t = {}
m[target] = t
tup = tuple(selector)
assert tup not in t
t[tup] = cmd
def register_modules(self, module_to_client):
self.module_to_client = module_to_client
def module(self, modulename):
# self.module('foo').target('bar').propagate(selector) will call observe_event()
return EventPropagator(self, modulename)
async def run(self, action, next_coro=None):
"""
Coroutine to execute "action," which must be the name of a sequence
in brainstem.actions. The actions within that sequence are executed
in order. After the last step has been executed and our task is complete,
we return next_coro so it is scheduled to replace our task.
"""
self.eventbuffer.add((datetime.now(timezone.utc), 'run', action))
seq = self.config['brainstem'].get('actions', {}).get(action, [])
LOGGER.debug(f'running {action}')
assert seq, action # FIXME: verify this at load time
for a in seq:
if isinstance(a, str):
await self.run(a)
elif len(a) < 4:
(funcname, *args) = a
if funcname == 'ratelimit' and len(args) == 1:
last = self.ratelimits.get(action, 0)
now = datetime.now(timezone.utc).timestamp()
if now - last < float(args[0]):
LOGGER.debug(f'action {action} inhibited by ratelimit')
return next_coro
self.ratelimits[action] = now
LOGGER.info(f'action {action} allowed by ratelimit')
else:
LOGGER.warning(f'unknown function {funcname} {args}')
else:
(module, target, selector, command, *args) = a
client = self.module_to_client[module] # FIXME: verify this at load time
await client.run(target, selector, command, *args)
return next_coro
def observe_event(self, module, target, selector):
"""Module knows that the event described by selector has occurred on target.
We are ultimately called. If we return None, there is no reaction to the event.
Otherwise we return a coroutine that, when scheduled, triggers the reaction.
"""
LOGGER.debug(f'observe_event {module} {target} {selector}')
self.eventbuffer.add((datetime.now(timezone.utc), 'observed', module, target, selector))
for (sel, cmd) in self.reactions.get(module, {}).get(target, {}).items():
if (sel[0] == selector[0] or sel[1] in selector[1]) and sel[2:] == selector[2:]:
return self.run(cmd)
return None
def collect(self, target):
"""Don't actually probe this from Prometheus; all the data is in the descriptions."""
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily
gmfs = []
nnn = 0
for (dt, direction, name, *args) in self.eventbuffer.items:
desc = f'{dt.ctime()}: {direction} {name} {args}'
gmf = GaugeMetricFamily(f'event{nnn}', desc, labels=[])
nnn += 1
gmf.add_metric([], 1 if direction == 'run' else 0)
gmfs.append(gmf)
return gmfs
async def poll(self):
await asyncio.sleep(60)
return self.poll()
def get_awaitables(self):
# self.poll() doesn't do anything yet so we don't return it
return set([self.timers.process_timers()])