-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruuvi_fe.py
More file actions
128 lines (109 loc) · 4.82 KB
/
Copy pathruuvi_fe.py
File metadata and controls
128 lines (109 loc) · 4.82 KB
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
"""
Ruuvi midas frontend to readout multiple ruuvi sensors
"""
import midas
import midas.frontend
import midas.event
from ruuvitag_sensor.ruuvi import RuuviTagSensor
class RuuviEquipment(midas.frontend.EquipmentBase):
def __init__(self, client):
# The name of our equipment. This name will be used on the midas status
# page, and our info will appear in /Equipment/RuuviEquipment in
# the ODB.
equip_name = "Ruuviquipment"
# Define the "common" settings of a frontend. These will appear in
# /Equipment/RuuviEquipment/Common. The values you set here are
# only used the very first time this frontend/equipment runs; after
# that the ODB settings are used.
default_common = midas.frontend.InitialEquipmentCommon()
default_common.equip_type = midas.EQ_PERIODIC
default_common.buffer_name = "SYSTEM"
default_common.trigger_mask = 0
default_common.event_id = 1
default_common.period_ms = 100
default_common.read_when = midas.RO_RUNNING | midas.RO_STOPPED
default_common.log_history = 1
# setup the ODB
settings = {"NAME": ["EG", "OG", "Bad"],
"MAC": ["E5:C5:29:91:A2:5E", "ED:91:8B:2F:BF:D6", "EC:DC:87:B8:02:D0"],
"timeout": 4,
"batteryNorm": 3100,
"Names RUBK": [
"TempEG", "HumidityEG", "BatteryEG",
"TempOG", "HumidityOG", "BatteryOG",
"TempBad", "HumidityBad", "BatteryBad",
],
"Enabled": [True, False, False]
}
# You MUST call midas.frontend.EquipmentBase.__init__ in your equipment's __init__ method!
midas.frontend.EquipmentBase.__init__(
self, client, equip_name, default_common, settings
)
# You can set the status of the equipment (appears in the midas status page)
self.set_status("Initialized")
def readout_func(self):
"""
For a periodic equipment, this function will be called periodically
(every 100ms in this case). It should return either a `midas.event.Event`
or None (if we shouldn't write an event).
"""
# In this example, we just make a simple event with one bank.
event = midas.event.Event()
all_macs = False
event_data = []
while not all_macs:
all_macs = True
# get sensors data
data = RuuviTagSensor.get_data_for_sensors(
self.settings["MAC"],
self.settings["timeout"]
)
# create event
event_data = []
for enabled, mac in zip(self.settings["Enabled"], self.settings["MAC"]):
if not enabled:
continue
if mac not in data:
all_macs = False
break
event_data.append(data[mac]["temperature"])
event_data.append(data[mac]["humidity"])
event_data.append(data[mac]["battery"] / self.settings["batteryNorm"])
event.create_bank("RUBK", midas.TID_FLOAT, event_data)
return event
class RuuviFrontend(midas.frontend.FrontendBase):
"""
A frontend contains a collection of equipment.
You can access self.client to access the ODB etc (see `midas.client.MidasClient`).
"""
def __init__(self):
# You must call __init__ from the base class.
midas.frontend.FrontendBase.__init__(self, "ruuvife")
# You can add equipment at any time before you call `run()`, but doing
# it in __init__() seems logical.
self.add_equipment(RuuviEquipment(self.client))
def begin_of_run(self, run_number):
"""
This function will be called at the beginning of the run.
You don't have to define it, but you probably should.
You can access individual equipment classes through the `self.equipment`
dict if needed.
"""
self.set_all_equipment_status("Running", "greenLight")
self.client.msg("Frontend has seen start of run number %d" % run_number)
return midas.status_codes["SUCCESS"]
def end_of_run(self, run_number):
self.set_all_equipment_status("Finished", "greenLight")
self.client.msg("Frontend has seen end of run number %d" % run_number)
return midas.status_codes["SUCCESS"]
def frontend_exit(self):
"""
Most people won't need to define this function, but you can use
it for final cleanup if needed.
"""
print("Goodbye from user code!")
if __name__ == "__main__":
# The main executable is very simple - just create the frontend object,
# and call run() on it.
with RuuviFrontend() as my_fe:
my_fe.run()