forked from ridiculousfish/one-second-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorbell.py
executable file
·42 lines (37 loc) · 1.29 KB
/
doorbell.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
#!/usr/bin/python
import os
import signal
import subprocess
import sys
import time
# Ignore SIGCHLD
# This will prevent zombies
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
# After ringing, how many seconds before we allow ourselves to ring again
DEBOUNCE_INTERVAL = 7
# The token to look for in tcpdump's output
# This should be your unique network SSID
SSID_TOKEN = sys.argv[1] if len(sys.argv) > 1 else 'Free Public Wifi'
DEVNULL = open(os.devnull, 'wb')
def do_ring():
""" Play the doorbell.wav file. Don't wait for it to finish. """
# cmd = 'alsaplayer -o alsa --quiet ./doorbell.wav'
# soundproc = subprocess.Popen(cmd.split(), close_fds=True,
# stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
subprocess.call('bash ./lights.sh >/dev/null', shell=True)
cmd = 'tcpdump -l -K -q -i DoorbellMonitor -n -s 256'
proc = subprocess.Popen(cmd.split(), close_fds=True,
bufsize=0, stdout=subprocess.PIPE)
last_played = 0
while True:
line = proc.stdout.readline()
if not line:
print "tcpdump exited"
break
if SSID_TOKEN in line:
now = time.time()
if now - last_played > DEBOUNCE_INTERVAL:
last_played = now
sys.stdout.write(line)
sys.stdout.flush()
do_ring()