-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_system.py
98 lines (81 loc) · 2.48 KB
/
security_system.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
import time
import threading
import RPi.GPIO as GPIO
from pir import MyPiPIR
from led import MyPiLed
from buzzer import MyPiBuzzer
from my_logger import MyLogger
from lock import Lock
class SecuritySystem:
CHECK_INTERVAL = 0.01
THREADS = []
CODE = 1234
def __init__(self):
self.pir = MyPiPIR(MyPiPIR.DEFAULT)
self.led = MyPiLed(MyPiLed.RED)
self.buzzer = MyPiBuzzer(MyPiBuzzer.DEFAULT)
self.locks = []
self.tries = 0
self.max_tries = 3
self.locks.append(Lock('Vault'))
self.logger = MyLogger("SecuritySystem")
self.check_interval = self.__class__.CHECK_INTERVAL
self.enabled = False
def __check_code(self):
while self.tries <= self.max_tries:
code = input("Enter security system code (Tries: " + str(self.max_tries - self.tries) + "): ")
if str(code) == str(self.__class__.CODE):
return True
else:
self.tries+=1
self.logger.warn("Code entered incorrectly " + str(self.max_tries) + " times")
self.buzzer.on()
return False
# Public implementation (non-blocking)
def enable(self):
if self.__check_code():
self.lockdown()
if len(self.__class__.THREADS) == 0:
self.enabled = True
t = threading.Thread(target=self.__enable)
t.start()
self.__class__.THREADS.append(t)
return t
else:
self.logger.warn("Security already active")
return self.__class__THREADS[0]
# Private implementation (blocking)
def __enable(self):
while self.enabled == True:
state = self.pir.state()
if state == MyPiPIR.ACTIVATED:
self.logger.warn("Motion detected!")
self.buzzer.on()
self.led.blink(5,0.2)
time.sleep(1)
elif state == MyPiPIR.DEACTIVATED:
self.logger.info("Waiting for motion...")
self.led.off()
self.buzzer.off()
elif state == MyPiPIR.ACTIVE:
self.logger.warn("Motion still being detected!")
self.led.blink(5,0.2)
self.buzzer.on()
time.sleep(1)
elif state == MyPiPIR.INACTIVE:
self.led.off()
self.buzzer.off()
time.sleep(self.check_interval)
# Disable the security system, wait for threads to finish
def disable(self):
if self.__check_code():
self.enabled = False
self.end_lockdown()
for t in self.__class__.THREADS:
t.join()
def lockdown(self):
for lock in self.locks:
lock.lock()
def end_lockdown(self):
for lock in self.locks:
lock.unlock()