forked from gm-stack/overlook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorlock.py
284 lines (247 loc) · 7.12 KB
/
doorlock.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
import serial
import RPi.GPIO as GPIO
import time, io, datetime
import ConfigParser
import asyncore
import thread
import pygame
import pyaudio, wave
import os
import random
import subprocess
import shlex
import socket
from ircasync import *
from rdm880 import *
import sqlite3
try:
import json
except ImportError: # python <2.6
import simplejson as json
config = ConfigParser.ConfigParser()
config.read("doorlock.ini")
LOCK_PIN = config.getint("gpio","lock_pin")
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False) # disable warning for already having GPIO open
GPIO.setup(LOCK_PIN, GPIO.OUT)
GPIO.output(LOCK_PIN, GPIO.LOW)
EXIT_PIN = config.getint("gpio","exit_pin")
AUX_PIN = config.getint("gpio", "aux_pin")
FRIDGE_PIN = config.getint("gpio","fridge_pin")
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(EXIT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(AUX_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(FRIDGE_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
ser = serial.Serial(config.get("rfid","port"), config.getint("rfid","baud"), timeout=1)
print "opening cards db"
conn = sqlite3.connect("cards.db")
cursor = conn.cursor()
print "cards db opened"
opentime = config.getint("gpio","opentime")
MCAST_GROUP = (config.get('mcast', 'ip'), config.getint('mcast', 'port'))
mcast_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
mcast_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
try:
pygame.mixer.pre_init(44100,-16,2,256)
pygame.mixer.init()
sounds = {}
wavlist = os.listdir("sounds")
for wav in wavlist:
if wav.endswith(".wav"):
sounds[wav] = pygame.mixer.Sound("sounds/" + wav)
ping = pygame.mixer.Sound("Ping.wav")
ping.play()
tea_sounds = {}
tea_wavlist = os.listdir("ingress")
for snd in tea_wavlist:
if snd.endswith(".ogg"):
snd_name = snd
if snd_name.startswith("speech_number_"):
snd_name = int(snd_name[14:17])
else:
snd_name = snd_name[7:-4]
tea_sounds[snd_name] = pygame.mixer.Sound("ingress/" + snd)
ping.play()
except:
print "Error initializing sound"
#todo: actually print the error itself
ALERT_COMMAND = shlex.split(config.get("alert","command"))
ALERT_PRESS_TIME = config.getint("alert","presstime")
AUX_PRESS_TIME = 10
def send_mcast_message(**kwargs):
"""
Sends a multicast UDP notification to the local network.
This encodes all arguments as a dict, and serialises it as JSON.
This function silently fails.
"""
global mcast_sock
global MCAST_GROUP
d = json.dumps(kwargs)
try:
mcast_sock.send(d, MCAST_GROUP)
except:
pass
def playwait(sound):
tea_sounds[sound].play()
while pygame.mixer.get_busy():
time.sleep(0.1)
def saytime(remaintime):
minutes = int(remaintime/60)
seconds = remaintime % 60
if remaintime == 60:
seconds = 60
minutes = 0
if minutes:
playwait(minutes)
playwait('minutes')
if seconds:
playwait(seconds)
playwait('seconds')
if minutes or seconds:
playwait('remaining')
else:
playwait('complete')
def timerThread():
starttime = int(time.time())
duration = 180
remaintime = duration
while runTimer and remaintime > 0:
elapsed = (int(time.time())-starttime)
remaintime = duration - elapsed
print "time left: %i" % remaintime
time.sleep(1)
if ((remaintime % 60 == 0) or
((remaintime < 180) and (remaintime % 30 == 0)) or
((remaintime < 60) and (remaintime % 10 == 0)) or
((remaintime < 30) and (remaintime % 5 == 0))):
saytime(remaintime)
doorTime = 0.0
fridgebell = pygame.mixer.Sound("bell.wav")
fridgebell_playing = False
runTimer = False
def doorThread():
global doorTime
global runTimer
fridgeTime = 0
doorPress = 0
auxPress = 0
alertSubproc = None
while True:
if GPIO.input(EXIT_PIN):
doorPress += 1
if doorPress == ALERT_PRESS_TIME:
if alertSubproc:
alertSubproc.kill()
alertSubproc = None
else:
try:
ping.play()
except:
print "sound play failed"
alertSubproc = subprocess.Popen(ALERT_COMMAND, stdout=subprocess.PIPE, shell=False)
if not ((time.time() - doorTime) < 0):
doorTime = time.time() + 2
print "button pushed, unlocking door"
try:
irc.tell(channel, "Unlocking door for exiting user")
except:
pass
send_mcast_message(event='exitButton')
else:
doorPress = 0
if (time.time() - doorTime) < 0:
GPIO.output(LOCK_PIN, GPIO.HIGH)
else:
GPIO.output(LOCK_PIN, GPIO.LOW)
if GPIO.input(FRIDGE_PIN):
fridgeTime += 1
print "fridge has been open for %f seconds" % (fridgeTime/20.0)
send_mcast_message(event='fridgeAlarm', time=fridgeTime/20.0)
if fridgeTime == 30*20:
print "playing bell"
try:
fridgebell.play(loops=-1)
except:
print "sound play failed"
else:
if fridgeTime > 0:
send_mcast_message(event='fridgeAlarmStop')
fridgeTime = 0.0
try:
fridgebell.stop()
except:
print "sound stop failed"
if GPIO.input(AUX_PIN):
auxPress += 1
if auxPress == AUX_PRESS_TIME:
if runTimer:
runTimer = False
else:
runTimer = True
thread.start_new_thread(timerThread, ())
print "starting timer"
else:
auxPress = 0
time.sleep(0.05)
thread.start_new_thread(doorThread, ())
irc_connected = False
def handle_welcome(_,__):
global irc_connected
irc_connected = True
try:
channel = config.get("irc","channel")
irc = IRC(nick=config.get("irc","ircnick"), start_channels=[channel], version="1.0")
irc.make_conn(config.get("irc","ircserver"), config.getint("irc","ircport"))
irc.bind(handle_welcome, RPL_WELCOME)
except:
print "IRC start failed"
thread.start_new_thread(asyncore.loop, ()) # i really should work the serial bit into asyncore, shouldn't i?
soundList = []
def checkCard(tagID):
global doorTime
global soundList
cursor.execute("SELECT `enabled`,`username`,`description` FROM `rfid` WHERE `card_id`=?", (tagID,))
result = cursor.fetchone()
if result:
if result[0]:
try:
if not soundList:
print "creating new soundList"
soundList = sounds.keys()
random.shuffle(soundList)
sounds[soundList.pop()].play()
print "valid card, unlocking door for %s" % result[1]
irc.tell(channel, ("Unlocking door for user %s with a %s" % (result[1], result[2])).encode("ascii",errors="ignore"))
send_mcast_message(event='doorUnlock', user=result[1], label=result[2], id=tagID)
except:
print sys.exc_info()
#if result['soundfile']:
# print "soundfile is %s" % result['soundfile']
doorTime = time.time() + opentime
else:
print "usage of disabled card %s for user %s" % (tagID, result[1])
send_mcast_message(event='disabledCard', user=result[1], label=result[2], id=tagID)
else:
print "unknown card %s" % tagID
try:
irc.tell(channel, ("Unknown card %s" % tagID).encode("ascii",errors="ignore"))
except:
print sys.exc_info()
send_mcast_message(event='unknownCard', id=tagID)
prevcardid = None
while True:
p = Packet(ISO14443A.Request)
reply = p.execute(ser)
p = Packet(ISO14443A.Anticollision)
reply = p.execute(ser)
cardid = reply.data
if cardid:
cardid_hex = "".join(map(lambda x: "%.2X" % x , cardid))
if cardid_hex != prevcardid:
prevcardid = cardid_hex
checkCard(cardid_hex)
else:
prevcardid = None
time.sleep(0.1)