-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiygmserveredition.py
428 lines (352 loc) · 13.8 KB
/
diygmserveredition.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#Written by Ryan Kim
#DIYgm code adapted from code from Lukas which was adapted from code by
#James Seekamp, Jeffery Xiao, Issa El-Amir, Regina Tuey, Max Li, Andrew Kent
#Aug 12. 2022
from code import compile_command
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import threading
import signal
import sys
from unicodedata import name
import pigpio
import os
import datetime
import ssl
import zipfile
import zlib
host = "192.168.4.1"
port = 8080
# Pins
pi = pigpio.pi()
PWM_GPIO = 18 # reg edition make sure to use a PWM pin (check RPi datasheet)
#PWM_GPIO = 13 # this is for diygm rws lite edition
MEAS_GPIO = 21
PULLUP_GPIO = 12
global filename
filename = ""
# Disables error message
#GPIO.setwarnings(False)
# PWM Pin
pi.hardware_PWM(PWM_GPIO, 1000, 100000) # Set the frequency and instantiate PWM control on pin
# random number generation
randnum = 0
last_rand = 0
# Measurement Pullup Pin
pi.write(PULLUP_GPIO, 1)
# Preventing race conditions
lock = threading.Lock()
'''
def runrand():
while True:
lock.acquire()
if randnum == 10:
randnum = 0
else:
randnum += 1
lock.release()
time.sleep(.1)
'''
# This method is called every second and handles the pushing and popping for the cpm lists
# this runs on a separate thread
def reset():
while True:
lock.acquire()
global last_count
global counts
global cpm_fast
global cpm_slow
global fast_min
global slow_min
global count_min
# cpm fast averages over 4 seconds, so make only four values in the array
if len(cpm_fast) < 4:
cpm_fast.append(counts)
else:
cpm_fast.pop(0)
cpm_fast.append(counts)
# cpm fast averages over 22 seconds, so make only 22 values in the array
if len(cpm_slow) < 22:
cpm_slow.append(counts)
else:
cpm_slow.pop(0)
cpm_slow.append(counts)
if len(fast_min) < 60:
fast_min.append(int(sum(cpm_fast) * 60/len(cpm_fast)))
slow_min.append(int(sum(cpm_slow) * 60/len(cpm_slow)))
count_min.append(counts)
else:
fast_min.append(int(sum(cpm_fast) * 60/len(cpm_fast)))
fast_min.pop(0)
slow_min.append(int(sum(cpm_slow) * 60/len(cpm_slow)))
slow_min.pop(0)
count_min.append(counts)
count_min.pop(0)
last_count = counts
counts = 0
lock.release()
time.sleep(1)
# This is called every count, don't change arguments of function
def detection_callback(gpio, level, tick):
lock.acquire()
global counts
counts += 1
#last_rand = randnum
#dir = os.path.dirname(os.path.abspath(__file__)) + f"/logs/rand.csv"
#with open(dir, "a+") as log:
# log.write(f"{last_rand}\n")
lock.release()
def log():
global last_lon
global last_lat
global stopLog
global filename
global log_start
while True:
lock.acquire()
if (stopLog):
lock.release()
break
dir = os.path.dirname(os.path.abspath(__file__)) + f"/logs/{filename}.csv"
with open(dir, "a+") as log:
log.write(f"{datetime.datetime.now()-log_start},{last_lat},{last_lon},{last_count},{fast_min[len(fast_min)-1]},{slow_min[len(slow_min)-1]}\n")
lock.release()
time.sleep(1)
# This is a class built on top of http handler that serves as the wireless access point for the diygm
# interface
class Server(BaseHTTPRequestHandler):
def do_GET(self):
global log
global stopLog
global loggingThread
if self.path == "/data":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
lock.acquire()
self.wfile.write(bytes(f"{{\"counts\": {count_min}, \"cpm_fast\": {fast_min}, \"cpm_slow\": {slow_min}}}", "utf-8"))
lock.release()
elif self.path == "/plotly-2.14.0.min.js":
self.send_response(200)
self.send_header("Content-type", "text/javascript")
self.end_headers()
res = open("plotly-2.14.0.min.js", "rb")
self.wfile.write(res.read())
elif self.path == "/end":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes("{}", "utf-8"))
# stop the thread and null out the file handle to the log
lock.acquire()
if (loggingThread.is_alive()):
stopLog = True
lock.release()
elif self.path == "/download":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
res = open("download.html", "rb")
self.wfile.write(res.read())
elif self.path == "/update":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
res = open("update.html", "rb")
self.wfile.write(res.read())
elif self.path == "/getStatus":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
bool = 'false'
# i know this is terrible, but json doesn't play nice with True instead of true
if not loggingThread.is_alive():
bool = 'true'
lock.acquire()
self.wfile.write(bytes(f"{{\"log\": {bool}}}", "utf-8"))
lock.release()
else:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
res = open("diygmserveredition.html", "rb")
self.wfile.write(res.read())
def do_POST(self):
global log
global loggingThread
global filename
global stopLog
global log_start
if len(self.path) > 12 and self.path[0:12] == "/logs/delete":
filename = self.path[12:]
path = os.path.dirname(os.path.abspath(__file__)) + f"/logs/{filename}"
lock.acquire()
if os.path.exists(path):
os.remove(path)
lock.release()
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes("{}", "utf-8"))
elif len(self.path) > 5 and self.path[0:5] == "/logs":
self.send_response(200)
self.send_header("Content-type", "text/csv")
self.end_headers()
filename = self.path[5:]
lock.acquire()
path = os.path.dirname(os.path.abspath(__file__)) + f"/logs/{filename}"
if len(filename) >= 4 and filename[len(filename)-4:] == ".csv" and os.path.exists(path):
ret = open(path, "rb")
self.wfile.write(ret.read())
lock.release()
elif self.path == "/downloadall":
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
filename = self.path[5:]
lock.acquire()
with zipfile.ZipFile(os.path.dirname(os.path.abspath(__file__)) + "/logs/all-logs.zip", "w", zipfile.ZIP_DEFLATED) as zip:
lognames = os.listdir(os.path.dirname(os.path.abspath(__file__)) + "/logs")
for name in lognames:
if name[-3:] != "zip":
zip.write(os.path.dirname(os.path.abspath(__file__)) + "/logs/" + name, arcname=name)
with open(os.path.dirname(os.path.abspath(__file__)) + "/logs/all-logs.zip", "rb") as ret:
self.wfile.write(ret.read())
lock.release()
elif self.path == "/logs":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
lognames = os.listdir(os.path.dirname(os.path.abspath(__file__)) + "/logs")
lognames.sort()
res = "{\"logs\": ["
#this is a workaround for single vs double quotes
if lognames:
for name in lognames:
if name[-4:] == '.csv':
res += "\"" + name + "\","
res = res[0:len(res)-1]
res += "]}"
self.wfile.write(bytes(res, "utf-8"))
elif self.path == "/start":
if int(self.headers['Content-Length']):
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.data_string = self.data_string.decode("utf-8")
filename = self.data_string
if filename[-4:] == '.csv':
filename = filename[:-4]
else:
filename = str(datetime.datetime.now()).replace(" ", "_")
# create log pointer
lock.acquire()
if not loggingThread.is_alive():
dir = os.path.dirname(os.path.abspath(__file__)) + f"/logs/{filename}.csv"
with open(dir, "w") as log_file:
log_file.write("Time,Latitude,Longitude,Counts,CPM Fast,CPM Slow\n")
lock.release()
# start logging thread, should come after the log pointer
if not loggingThread.is_alive():
stopLog = False
log_start = datetime.datetime.now()
loggingThread = threading.Thread(target=log, daemon=True)
loggingThread.start()
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes(f"{{\"name\": \"{filename}.csv\"}}", "utf-8"))
elif self.path[0:7] == "/update": # this is the system updater
with open(self.path[8:], "w") as file:
lock.acquire()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.data_string = self.data_string.decode("utf-8")
file.write(self.data_string)
lock.release()
# restart the system if the python file was updated
if self.path[8:] == os.path.basename(__file__):
os.system("sudo reboot")
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes("{}", "utf-8"))
elif self.path == "/restart": #restart the pi
os.system("sudo reboot")
elif self.path == "/shutdown": #shutdown the raspberry pi
os.system("sudo shutdown -h now")
elif self.path == "/host": #handles changing the hostname
with open("/etc/hostapd/hostapd.conf", "w") as file:
lock.acquire()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.data_string = self.data_string.decode("utf-8")
#this might not work outside of the united states
file.write(f"country_code=US\ninterface=wlan0\nssid={self.data_string}\nhw_mode=g\nchannel=7\nmacaddr_acl=0\nauth_algs=1\nignore_broadcast_ssid=0")
lock.release()
os.system("sudo reboot")
elif self.path == "/pwm": #change the pwm stuff
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.data_string = self.data_string.decode("utf-8")
self.data_string = self.data_string.split(',')
duty_cycle = int(self.data_string[0])
frequency = int(self.data_string[1])
pi.hardware_PWM(PWM_GPIO, frequency, duty_cycle * 10000) # Set the frequency and instantiate PWM control on pin
else:
global last_lat
global last_lon
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
self.data_string = self.data_string.decode("utf-8")
self.data_string = self.data_string.split(',')
lock.acquire()
last_lat = self.data_string[0]
last_lon = self.data_string[1]
lock.release()
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes("{}", "utf-8"))
if __name__=="__main__":
# Globals
global counts
global last_count
global cpm_fast
global cpm_slow
global fast_min
global slow_min
global count_min
global stopLog
global last_lon
global last_lat
global loggingThread
global log_start
log_start = None
last_lon = 0
last_lat = 0
stopLog = False
loggingThread = threading.Thread(target=log, daemon=True)
last_count = 0
counts = 0
cpm_fast = [0]
cpm_slow = [0]
fast_min = [0]
slow_min = [0]
count_min = [0]
global QUIT
QUIT = False
# creates log directory if it doesn't already exist
dir = os.path.dirname(os.path.abspath(__file__)) + "/logs"
if os.path.exists(dir):
pass
else:
os.mkdir(dir)
# sets interrupt for measurement pin
pi.callback(MEAS_GPIO, pigpio.FALLING_EDGE, detection_callback)
# create thread to handle cpm lists
t = threading.Thread(target=reset, daemon=True)
t.start()
# initialize the web server
webServer = HTTPServer((host, port), Server)
# this needs to be exist to handle communication over https so location can be access
webServer.socket = ssl.wrap_socket(webServer.socket,
keyfile="forgedkey.pem",
certfile="forgedcert.pem", server_side=True)
webServer.serve_forever()
t.stop()
Server.server_close()