-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.py
230 lines (191 loc) · 6.87 KB
/
gpio.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
# Copyright: 2016-2018, Jens Carroll
# These sources are released under the terms of the MIT license: see LICENSE
import time, os, signal, random, math
from threading import Lock, Thread, Event
from logger import Logger
import RPi.GPIO as GPIO
OPEN_FRONT_DOOR_OUTPUT = 4 # Pin 5
OPEN_APARTMENT_DOOR_OUTPUT = 17 # Pin 11
RING_INPUT = 15 # Pin 10
lock = Lock()
class BreakoutException(Exception):
pass
class OpenFrontDoorThread(Thread):
def __init__(self, logger, wait = False, test_mode = False):
self._wait = wait
self._logger = logger
self._test_mode = test_mode
super(OpenFrontDoorThread, self).__init__()
def run(self):
delay = random.randint(3, 6)
if self._wait:
time.sleep(delay) # wait 3-6 sec until we open the door
if self._test_mode:
self._logger.info("** Opendoor in test mode. Door will not be opened. **")
if not self._test_mode:
GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.LOW) # Relais close
self._logger.warn("Front door relais on (4 secs).")
time.sleep(4) # Relais closed for for 4 secs.
if not self._test_mode:
GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.HIGH) # Relais open
self._logger.warn("Front door relais off.")
class OpenApartmentDoorThread(Thread):
def __init__(self, logger, wait = 0, loops = 1, delay = 55, pipe = None, test_mode = False):
super(OpenApartmentDoorThread, self).__init__()
self._logger = logger
self._wait = wait # secs before execution
self._loops = loops # to prolong door opening
self._loop_delay = delay # delay in secs for loops > 1
self._pipe = pipe
self._stop_event = Event()
self._test_mode = test_mode
def _send_to_app(self, msg):
if self._pipe != None:
self._pipe.send_to_app(msg)
def _stopped(self):
return self._stop_event.is_set()
def stop(self):
self._stop_event.set()
def run(self):
if lock.acquire(False):
try:
self._logger.debug("Enter apartment door thread (wait=%d, loops=%d, delay=%d)." %
(self._wait, self._loops, self._loop_delay))
if self._wait > 0:
time.sleep(self._wait) # wait ? secs before we close the relais
self._logger.debug("Continue apartment door thread.")
for i in range(0, self._loops):
if self._test_mode:
self._logger.info("** Opendoor in test mode. Door will not be opened. **")
self._logger.warn("Apartment door relais on (loop: %d of %d)." % (i+1, self._loops))
if not self._test_mode:
GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.LOW) # Relais close
time.sleep(1) # Relais closed for 1 sec.
if not self._test_mode:
GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.HIGH) # Relais open
self._logger.warn("Apartment door relais off.")
if self._loops > 1:
for j in range(0, self._loop_delay):
if self._stopped():
raise BreakoutException
counter = self._loops * self._loop_delay - i * self._loop_delay - j
self._send_to_app("APARTMENT DOOR TIMER %d" % counter)
time.sleep(1)
except BreakoutException:
self._logger.warn("Apartment door timer stopped.")
finally:
self._send_to_app("APARTMENT DOOR TIMER OFF")
lock.release()
class Port(object):
def __init__(self, logger, test_mode=False):
self._logger = logger
self._doorbell_rang = False
self._test_mode = test_mode
self._setup_gpio()
self._add_event_detect()
self._thread = None
signal.signal(signal.SIGALRM, self._timeout_callback)
self._logger.debug("Port initialized!")
def _setup_gpio(self):
"""
Setup GPIO ports
"""
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(RING_INPUT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Relais open (inverse logic)
GPIO.setup(OPEN_FRONT_DOOR_OUTPUT, GPIO.OUT)
GPIO.output(OPEN_FRONT_DOOR_OUTPUT, GPIO.HIGH)
# Relais open (inverse logic)
GPIO.setup(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.OUT)
GPIO.output(OPEN_APARTMENT_DOOR_OUTPUT, GPIO.HIGH)
def _add_event_detect(self):
"""
Enable interrupts on doorbell
"""
GPIO.add_event_detect(RING_INPUT, GPIO.FALLING, callback = self._ringing_callback, bouncetime = 300)
def _remove_event_detect(self):
"""
Disable interrupts on doorbell
"""
GPIO.remove_event_detect(RING_INPUT)
def _ringing_callback(self, channel):
"""
Interrupt triggered (keep this callback as fast as possible)
"""
self._remove_event_detect() # No interrupts after that
signal.setitimer(signal.ITIMER_REAL, 14) # 14 sec timeout
self._doorbell_rang = True
def _timeout_callback(self, a, b):
signal.setitimer(signal.ITIMER_REAL, 0) # Timeout timer off
self._logger.debug("Timeout callback - Doorbell Interrupts enabled again.")
self._add_event_detect()
def open_front_door(self):
"""
Keep the front door open for a few secs.
"""
self._logger.debug("Disable Doorbell Interrupts.")
self._remove_event_detect() # No interrupts after that
signal.setitimer(signal.ITIMER_REAL, 12) # 12 sec timeout
thread = OpenFrontDoorThread(self._logger, False)
thread.start()
def open_apartment_door(self, after=None):
"""
Keep the apartment door open for a minute.
"""
wait = 0
if after == 1:
wait = 60
elif after == 2:
wait = 90
elif after == 3:
wait = 120
thread = OpenApartmentDoorThread(self._logger, wait)
thread.start()
def open_apartment_door_for(self, pipe, mins):
"""
Keep the apartment door open for n minutes.
"""
self._thread = OpenApartmentDoorThread(self._logger, loops=mins, delay=59, pipe=pipe)
self._thread.start()
def stop_open_apartment_door_for(self):
self._thread.stop()
self._thread.join()
self._thread = None
def delayed_open_front_door(self):
"""
Keep the door open for a few secs, but wait a few secs
before doing so.
"""
thread = OpenFrontDoorThread(self._logger, True, self._test_mode)
thread.start()
def door_ring(self):
"""
Check if someone rang the door bell at least once.
"""
if self._doorbell_rang:
self._logger.debug("Ringing detected (via Interrupt) - Disabled for 14 sec.")
return self._doorbell_rang
def door_ring_release(self):
"""
Release ring_detected.
"""
self._doorbell_rang = False
self._logger.debug("Release auto open ringing.")
def main():
# Be sure we have root privileges
if os.geteuid() != 0:
exit("You need to have root privileges. Exiting.")
logger = Logger.get(verbose = True)
gpio = Port(logger)
i = 0
while True:
if gpio.door_ring():
gpio.door_ring_release()
# every 1 sec ... we should not miss any door bells
print("running %d sec." % i)
i += 1
time.sleep(1)
if __name__ == "__main__":
main()