-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
188 lines (156 loc) · 4.81 KB
/
Copy pathserver.py
File metadata and controls
188 lines (156 loc) · 4.81 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
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
import json
import uuid
import socket
import urllib
import httplib
import sqlite3
import datetime
# input:
# type: D_COMMAND, command, name
# type: D_REG, name
# type: D_NOTIFY, did, name, msg
# type: D_DELETE, name
# output:
# type: D_STATUS, status, msg
client_ip_port = 5050
def alert_client(msg):
conn = httplib.HTTPSConnection('api.pushover.net:443')
conn.request('POST', '/1/messages.json',
urllib.urlencode({
'token': 'aW34oVj5RHAgJ2C7bZC4VPE4haCAUs',
'user': 'uaFuTcZz62wvmspFi8VMenx24My28Z',
'message': msg,
}), { 'Content-type': 'application/x-www-form-urlencoded' })
conn.getresponse()
def create_status(status, msg):
return {'type': 'D_STATUS', 'status': status, 'msg': msg}
def send_client_command(ip, command):
# Connect to the client
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, client_ip_port))
# Send the data
dev_data = {'type': 'D_COMMAND', 'command': command}
message = json.dumps(dev_data)
print 'Sending : "%s"' % message
len_sent = s.send(message)
if len_sent < 1:
print 'something went wrong in send_client_command!'
# Receive a response
resp = s.recv(1024)
response = json.loads(resp)
print 'Received: "%s"' % response
# Clean up
s.close()
return response
# verify Dev_id
def verify_did(conn, did):
cur = conn.cursor()
cur.execute("SELECT did FROM devices")
conn.commit()
row = cur.fetchone()
cur.close()
if row == None:
return False
return True
# register a device with the server
# return the device id
def register_handle(client_ip, name):
ret = 'OK'
ret_msg = str(uuid.uuid4())
try:
conn = sqlite3.connect('HAB.db')
conn.execute('''pragma foreign_keys=ON''')
conn.execute("INSERT INTO devices VALUES (?,?,?)", (ret_msg, name, client_ip))
conn.commit()
except Exception as e:
# Roll back any change if something goes wrong
conn.rollback()
ret = 'FAIL'
ret_msg = repr(e)
print e
finally:
conn.close()
return create_status(ret, ret_msg)
# notify user of the new event
def notify_handle(data):
ret = 'OK'
ret_msg = ''
try:
conn=sqlite3.connect('HAB.db')
conn.execute('''pragma foreign_keys=ON''')
if verify_did(conn, data['did']) == False:
ret = 'FAIL'
ret_msg = 'DID Not Found'
else:
# checks out; add event
cur_time = datetime.datetime.now().strftime("%H:%M:%S:%f %m-%d-%Y")
conn.execute("INSERT INTO events VALUES (?,?,?,?)", (data['did'], data['name'], data['msg'], cur_time))
conn.commit()
# alert user
alert_client(data['name'] + ' says: ' + data['msg']);
except Exception as e:
# Roll back any change if something goes wrong
conn.rollback()
ret = 'FAIL'
ret_msg = repr(e)
print e
finally:
conn.close()
return create_status(ret, ret_msg)
def delete_handle(data):
ret = 'OK'
ret_msg = ''
try:
conn=sqlite3.connect('HAB.db')
conn.execute('''pragma foreign_keys=ON''')
# find the device
cur = conn.cursor()
cur.execute("SELECT CIP FROM devices WHERE NAME = ?", (data['name'],))
conn.commit()
row = cur.fetchone()
if row == None:
ret = 'OK'
ret_msg = data['name'] + ' not found!'
else:
print 'Send Delete to Client ' + row[0]
send_client_command(row[0], 'DELETE')
# checks out; delete device
conn.execute("DELETE FROM devices WHERE NAME = ?", (data['name'],))
conn.commit()
except Exception as e:
# Roll back any change if something goes wrong
conn.rollback()
ret = 'FAIL'
ret_msg = repr(e)
print e
finally:
cur.close()
conn.close()
return create_status(ret, ret_msg)
def command_handle(data):
ret = 'OK'
ret_msg = ''
try:
conn=sqlite3.connect('HAB.db')
conn.execute('''pragma foreign_keys=ON''')
# find the device
cur = conn.cursor()
cur.execute("SELECT CIP FROM devices WHERE NAME = ?", (data['name'],))
conn.commit()
row = cur.fetchone()
if row == None:
ret = 'FAIL'
ret_msg = data['name'] + ' not found!'
else:
print 'Send Data to Client ' + row[0]
send_client_command(row[0], data['command'])
except Exception as e:
# Roll back any change if something goes wrong
conn.rollback()
ret = 'FAIL'
ret_msg = repr(e)
print e
finally:
cur.close()
conn.close()
return create_status(ret, ret_msg)