forked from RichardHabeeb/InsecureBuildingAutomationSystem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalarm.py
More file actions
78 lines (62 loc) · 2.07 KB
/
alarm.py
File metadata and controls
78 lines (62 loc) · 2.07 KB
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
################################################################################
# Alarm
################################################################################
################################################################################
# IMPORTS
################################################################################
import threading
import zmq
import socket
import json
import os
from gpio import *
################################################################################
# CLASSES
################################################################################
################################################################################
# VARIABLES
################################################################################
alarm_status = 0
g = gpio(9)
led_status = 0
################################################################################
# FUNCTIONS
################################################################################
def blink():
"""
Periodically toggle the alarm led
"""
global alarm_status
global g
global led_status
if alarm_status:
#toggle
led_status ^= 1
g.set_value(led_status)
else:
g.set_value(0)
threading.Timer(0.1, blink).start()
################################################################################
# MAIN
################################################################################
def main():
global alarm_status
global g
if not os.path.exists("/tmp/feeds"):
os.makedirs("/tmp/feeds")
context = zmq.Context()
tc_socket = context.socket(zmq.SUB)
tc_socket.setsockopt(zmq.SUBSCRIBE, "")
tc_socket.bind("ipc:///tmp/feeds/2")
g.set_direction(gpio.Direction.Output)
g.set_value(led_status)
blink()
while True:
# Wait for next request from client
message = tc_socket.recv()
print "ALARM: ", message
message = json.loads(message)
if "enable" in message:
alarm_status = message["enable"]
if __name__ == "__main__":
main()