-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_tag.py
52 lines (35 loc) · 1.19 KB
/
main_tag.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
#!/usr/bin/env python3.5
import RPi.GPIO as io
import threading
import time
from modules import logger
from modules.message_client import MessageClient
from modules.counter import Counter
from modules.siren_listener import SirenListener
import config
message_client = MessageClient(config.control_center_ip, config.control_center_port)
counter = Counter()
siren_listener = SirenListener()
io.setmode(io.BCM)
door_pin = 23
vibration_pin = 25
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)
io.setup(vibration_pin, io.IN, pull_up_down=io.PUD_UP)
def opened_callback(channel):
message_client.send('door opened')
def hit_callback(channel):
if counter.incr():
message_client.send('door hit')
def main():
while True:
time.sleep(1)
io.add_event_detect(door_pin, io.RISING, callback=opened_callback, bouncetime=2000)
io.add_event_detect(vibration_pin, io.RISING, callback=hit_callback, bouncetime=500)
# start listening for siren call
new_siren_thread = threading.Thread(target=siren_listener.listen)
new_siren_thread.daemon = True # stop if the program exits
new_siren_thread.start()
logger.logger.info('TAG STARTED!')
# start main loop
if __name__ == '__main__':
main()