-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_manager.py
117 lines (91 loc) · 3.53 KB
/
alert_manager.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
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
#!/usr/bin/env python
#Alerting Manager handles contact with Buzzer and Camera, eventually LEDS
#Author: Kevin Murphy
#Date : 4 - Jan - 15
import json
import time
from configurable import Configurable
import constants as CONSTS
from py_sensors.buzzer import Buzzer
from camera_manager import CameraManager
from pn_manager import PNManager
class AlertManager(Configurable):
DEBUG = True
LOGTAG = "AlertManager"
__buzzerOn = True
__pushOn = True
__lockdownOn = False
__cameraOn = True
__videoMode = False
__lastNotificationSent = None
def __init__(self):
super(AlertManager, self).__init__(CONSTS.JSON_KEY_ALERT_MANAGER_CONFIG)
if self.DEBUG:
print self.LOGTAG, " :: Created"
def configure(self, config):
if self.DEBUG:
print self.LOGTAG, ":: Configuring"
if config is not None:
try:
alertConfig = config[self.getJsonConfigKey()]
self.setBuzzerStatus(alertConfig[CONSTS.JSON_KEY_ALERT_BUZZER_ON])
self.setCameraStatus(alertConfig[CONSTS.JSON_KEY_ALERT_CAMERA_ON])
self.setVideoMode(alertConfig[CONSTS.JSON_KEY_ALERT_VIDEO_MODE])
self.setPushStatus(alertConfig[CONSTS.JSON_KEY_ALERT_PUSH_ON])
self.setLockdownStatus(alertConfig[CONSTS.JSON_KEY_ALERT_LOCKDOWN_ON])
except KeyError:
if self.DEBUG:
print self.LOGTAG, " :: Config key not present"
def ringBuzzer(self):
if self.getBuzzerStatus():
Buzzer.buzz()
@staticmethod
def forceBuzzerRing():
Buzzer.buzz()
def activateCamera(self):
if self.getCameraStatus():
if self.getVideoMode():
CameraManager.recordVideo()
else:
CameraManager.takeStill()
def sendPush(self, sensor, value):
if self.getPushStatus():
#Only send a push every 120 seconds
if self.__lastNotificationSent is None or int(round(time.time() * 1000)) - self.__lastNotificationSent > 120000:
self.__lastNotificationSent = int(round(time.time() * 1000))
if sensor == CONSTS.SENSOR_MOTION and not self.getLockdownStatus():
#Do nothing when motion is detected
pass
else:
data = {"sensor" : sensor, "value" : value}
pnManager = PNManager()
pnManager.sendJsonPush(data)
def setBuzzerStatus(self, isOn):
self.__buzzerOn = isOn
def getBuzzerStatus(self):
return self.__buzzerOn
def setPushStatus(self, isOn):
self.__pushOn = isOn
def getPushStatus(self):
return self.__pushOn
def setLockdownStatus(self, isOn):
self.__lockdownOn = isOn
def getLockdownStatus(self):
return self.__lockdownOn
def setCameraStatus(self, isOn):
self.__cameraOn = isOn
def getCameraStatus(self):
return self.__cameraOn
def setVideoMode(self, isOn):
self.__videoMode = isOn
def getVideoMode(self):
return self.__videoMode
def toString(self):
data = { CONSTS.JSON_KEY_ALERT_BUZZER_ON : self.getBuzzerStatus(),
CONSTS.JSON_KEY_ALERT_CAMERA_ON : self.getCameraStatus(),
CONSTS.JSON_KEY_ALERT_PUSH_ON : self.getPushStatus(),
CONSTS.JSON_KEY_ALERT_LOCKDOWN_ON : self.getLockdownStatus(),
CONSTS.JSON_KEY_ALERT_VIDEO_MODE : self.getVideoMode()}
if self.DEBUG:
print self.LOGTAG , json.dumps(data)
return data