-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarm.py
84 lines (77 loc) · 2.81 KB
/
alarm.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
import os
import sys
import json
import RPi.GPIO as io
from webServer import *
import time
class alarm:
alarmEndPoint = '/entrance/?format=json'
alarmCount = 0
alarms = []
response = ''
statusValues = {0:True,1:False}
alarmStatus = [2,2,2,2,2]
index = 0
def initPorts(self):
for alarm in self.alarms:
io.setmode(io.BCM)
io.setup(alarm['gpio'], io.IN, pull_up_down=io.PUD_UP)
def registerAlarm(self,GPIO,label,type,webServer,raspberryPi):
data = {'raspberry_pi_id':raspberryPi.getId(),
'status':False,
'gpio':GPIO,
'label':label,
'entrance_type' :type
}
if webServer.postToDatabase(data,self.alarmEndPoint):
return 'Alarm with GPIO: ' + str(GPIO) + ' successfully registered'
else:
return 'Probelm registering Alarm with GPIO: ' + str(GPIO)
def updateAlarmInfo(self,alarm):
self.alarms = alarm
#This function is not needed
#Only need updateAlarmInfo and updateAlarm to detect change in sensor
#using this function along with the other two cuases alarmStatus to be
#overwritten with new value before it is checked in updateAlarm
def updateStatus(self,webServer,raspberryPi):
for alarm in self.alarms:
self.alarmStatus[self.index] = io.input(alarm['gpio'])
data = {'raspberry_pi_id': raspberryPi.getId(),
'status': self.statusValues[self.alarmStatus[self.index]]
}
endPoint = '/entrance/' + str(alarm['id']) + '/?format=json'
self.response = webServer.putToDatabase(data,endPoint)
self.index = self.index + 1
self.index = 0
def updateAlarm(self,webServer,raspberryPi):
for alarm in self.alarms:
if alarm['alarm']:
currentStatus = io.input(alarm['gpio'])
if currentStatus != self.alarmStatus[self.index]:
self.alarmStatus[self.index] = currentStatus
data = {'raspberry_pi_id': raspberryPi.getId(),
'status': self.statusValues[currentStatus],
}
endPoint = '/entrance/' + str(alarm['id']) + '/?format=json'
self.response = webServer.putToDatabase(data,endPoint)
print 'Break in'
self.index= self.index + 1
self.index = 0
'''
web = webServer()
web.setUsername('test7')
web.setPassword('test')
web.setAuth()
pi = raspberryPi(web)
door = alarm()
print door.registerAlarm(17,'Front Door', 'door', web, pi)
print door.registerAlarm(22,'Back Door', 'door', web, pi)
door.updateAlarmInfo(web)
door.initPorts()
door.updateStatus()
while 1:
print 'Start Loop'
door.updateAlarmInfo(web)
door.updateAlarm(web,pi)
time.sleep(1)
'''