-
Notifications
You must be signed in to change notification settings - Fork 7
/
ec3k_recv
executable file
·48 lines (39 loc) · 1.26 KB
/
ec3k_recv
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
#!/usr/bin/python
import ec3k
import signal
import time
import argparse
import json
want_stop = False
def handler(signum, frame):
global want_stop
print "Signal %d caught! Stopping..." % (signum,)
want_stop = True
def main():
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--frequency', type = float, default = 868.402e6)
parser.add_argument('-j', '--json', action = 'store_true', default = False)
parser.add_argument('-q', '--quiet', action = 'store_true', default = False)
args = parser.parse_args()
def callback(state):
if args.json:
print(json.dumps(dict(id=state.id,
device_on_flag=state.device_on_flag,
energy=state.energy,
power_current=state.power_current,
power_max=state.power_max,
time_on=state.time_on,
time_total=state.time_total)))
else:
print(state)
my_ec3k = ec3k.EnergyCount3K(callback=callback, freq=args.frequency)
my_ec3k.start()
while not want_stop:
time.sleep(2)
if not args.quiet:
print "Noise level: %.1f dB" % (my_ec3k.noise_level,)
my_ec3k.stop()
if __name__ == '__main__':
main()