-
Notifications
You must be signed in to change notification settings - Fork 3
/
canary.py
85 lines (69 loc) · 1.82 KB
/
canary.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
import sys, signal
def trigger_alarm(signal, frame):
raise SecurityError(408, 'REQUEST_TIMEOUT')
class Error(Exception):
pass
class SecurityError(Error):
def __init__(self, code, message):
Error.__init__(self,message)
self.code = code
class Security():
def __init__(self, passcode=None, status=0, alarm=0):
self.passcode = passcode
self.status = status
self.alarm = alarm
self.sensors = {}
def set_passcode(self, passcode=None):
self.passcode = passcode
def add_sensor(self, sensor=None):
self.sensors.update(sensor)
def set_status(self, status=0):
self.status = status
def get_status(self):
return self.status
def arm_home(self):
try:
self.enter_passcode()
self.set_status(1)
except:
self.alarm = 1
def arm_away(self):
try:
self.enter_passcode()
self.set_status(2)
except:
self.alarm = 1
def disarm(self):
try:
self.enter_passcode()
self.set_status(0)
self.alarm = 0
except:
self.alarm = 1
def open_door(self, key):
if key not in self.sensors:
raise SecurityError(404, 'NOT_FOUND')
if self.sensors[key]['class'] != 'door':
raise SecurityError(404, 'NOT_FOUND')
self.sensors[key]['status'] = 1
if self.get_status() == 1:
self.alarm = 1
if self.get_status() == 2:
self.disarm()
def open_window(self, key):
if key not in self.sensors:
raise SecurityError(404, 'NOT_FOUND')
if self.sensors[key]['class'] != 'window':
raise SecurityError(404, 'NOT_A_FOUND')
self.sensors[key]['status'] = 1
if self.get_status() == 1:
self.alarm = 1
if self.get_status() == 2:
self.disarm()
def enter_passcode(self):
signal.signal(signal.SIGALRM, trigger_alarm)
signal.alarm(10)
passcode = raw_input('enter passcode: ')
while (int(passcode) != self.passcode):
passcode = raw_input('enter passcode: ')
signal.alarm(0)