-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsensor.py
50 lines (41 loc) · 1.64 KB
/
sensor.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
import logging
import json
import random
import datetime
logger = logging.getLogger("restsense")
class SensorDataCollector(object):
"""
This is a dummy data collector interface that will currently generate
random sensor data.
"""
__singleton = None # the one, true Singleton
def __new__(cls, *args, **kwargs):
# Check to see if a __singleton exists already for this class
# Compare class types instead of just looking for None so
# that subclasses will create their own __singleton objects
if cls != type(cls.__singleton):
#if not cls.__singleton:
cls.__singleton = super(SensorDataCollector, cls).__new__(cls, *args, **kwargs)
return cls.__singleton
def __init__(self, cfg):
self.cfg = cfg
self.poll_frequency = self.cfg.get("sensors", "poll_frequency")
self.alarms = list()
def get_snapshot(self):
d = dict()
d["sensors"] = {}
d["sensors"]["living_room_temperature"] = {"status" : "OK", "value": str(random.randrange(20, 30)), "unit" : "degree c"}
d["sensors"]["perimeter_security"] = {"status" : "OK", "value": "armed", "unit" : "boolean"}
return d
def get_alarms(self):
if random.random() > 0.5:
self.alarms.append("Perimeter breach detected at %s" % datetime.datetime.strftime(
datetime.datetime.now(),
"%Y-%m-%d %H:%M:%S"))
if len(self.alarms) > 0:
return {"alarms": self.alarms}
else:
return {"alarms": 0}
def reset_alarms(self):
del self.alarms[:]
return {"msg": "All alarms are reset"}