-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensors-src
executable file
·59 lines (48 loc) · 1.82 KB
/
sensors-src
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
#!/usr/bin/env python
import sys
import signal
import time
import json
from rpjios import Sensors
from rpjios.SubscriberBase import Subscriber
from rpjios.Types import Message
RUNNING = True
def sh(s, f):
global RUNNING
RUNNING = False
if __name__ == '__main__':
cfg = None
cfgFname = sys.argv[1] if len(sys.argv) > 1 else 'rpjios-sensors.config.json'
print "* Loading configuration from {}".format(cfgFname)
with open(cfgFname) as f:
try:
cfg = json.load(f)
except Exception as e:
raise BaseException("Bad configuration file! Details:\n\t{}".format(e))
map(lambda s: signal.signal(s, sh), [signal.SIGINT, signal.SIGTERM])
if 'sensors' not in cfg:
print "No sensors are configured! Exiting."
sys.exit(0)
redis_cfg = cfg['redis_config'] if 'redis_config' in cfg else None
print "* Using Redis configuration: {}".format(redis_cfg)
sens = Sensors.Sensors()
sl = {}
print "* Configured sensors: {}".format(", ".join(map(lambda s: "{}{}".format(s['name'], " (disabled)" if 'disabled' in s and s['disabled'] else ""), cfg['sensors'])))
for s in cfg['sensors']:
if 'disabled' in s and s['disabled']:
continue
if s['name'] in sens.list():
ns = sens.create(s['name'], config=s['config'] if 'config' in s else None, redis_cfg=redis_cfg)
ns.start()
sl[ns.id()] = ns
print "* Loaded {}:\n{}".format(ns, ns.metadata())
else:
raise BaseException("Unknown sensor type '{}' configured!".format(s['name']))
if len(sl.keys()) == 0:
print "* No sensors enabled, ending immediately."
sh(None, None)
while RUNNING:
time.sleep(0.25)
print "* Shutting down all sensors..."
map(lambda s: s.stop(), sl.values())
print "* Done."