-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomesecurity.py
48 lines (40 loc) · 1.4 KB
/
homesecurity.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
import subprocess
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
acceptableIP = ['192.168.0.20', '192.168.0.21']
def shell(command):
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
except Exception, e:
output = str(e.output)
finished = output.split('\n')
for line in finished:
print line
return
class HomeSecurityServer(WebSocket):
armed = False;
def handleMessage(self):
print(self.data + " " + str(HomeSecurityServer.armed))
if(self.data == "STATUS"):
if HomeSecurityServer.armed:
self.sendMessage("1")
elif not HomeSecurityServer.armed:
self.sendMessage("0")
elif(self.data == "1"):
HomeSecurityServer.armed = True
shell('killall motion')
shell('motion -b')
self.sendMessage("1")
elif(self.data == "0"):
HomeSecurityServer.armed = False
shell('killall motion')
self.sendMessage("0")
def handleConnected(self):
print(self.address, 'connected')
if self.address[0] not in acceptableIP:
print('Rejected')
self.close()
def handleClose(self):
print(self.address, 'closed')
shell('killall motion')
server = SimpleWebSocketServer('', 8081, HomeSecurityServer)
server.serveforever()