-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary.py
58 lines (47 loc) · 1.69 KB
/
summary.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
from aiovelib.service import IntegerItem
class Summary(object):
def __init__(self, path, item=None):
self.make_item = IntegerItem if item is None else item
self.path = path
def summarise(self, leader):
raise NotImplementedError("summarise")
def initial(self, v):
""" Initial value for the summary. Uses `v` as a hunt, but can
replace it with something else. This is so we can override
the behaviour for some summaries and delay the decision
instead of starting with the first value from the first
RS device."""
return v
class SummaryAll(Summary):
def summarise(self, leader):
return int(all(x.get_value(self.path) for x in leader.subservices))
class SummaryAny(Summary):
def summarise(self, leader):
return int(any(x.get_value(self.path) for x in leader.subservices))
class SummaryMax(Summary):
def summarise(self, leader):
try:
return max(y for y in (x.get_value(self.path) for x in leader.subservices) if y is not None)
except ValueError:
return None
class SummaryFirst(Summary):
def summarise(self, leader):
for x in leader.subservices:
return x.get_value(self.path)
return None
class SettingMixin(object):
""" Enherit from this, and one of the other Summary methods to make
one dependent on a setting.
Eg: class SummarySomething(SettingMixin, SummaryMax): pass """
_default = None
def __init__(self, setting, path, item=None):
self.setting = setting
super().__init__(path, item)
def summarise(self, leader):
if leader.settings.get_value(self.setting) == 1:
return super().summarise(leader)
return self._default
def initial(self, v):
return self._default
class SummaryOptionalAlarm(SettingMixin, SummaryMax):
_default = 0