-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
428 lines (303 loc) · 13.8 KB
/
main.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
from mapping_util import *
from pygame import *
from math import hypot
import firebase_manager
import datetime
import copy
import firebase_admin
import wifi_manager
from firebase_admin import credentials
import calculation_manager
import multiprocessing
import time as t
manager = multiprocessing.Manager()
shared_dict = manager.dict()
cred = credentials.Certificate('firebase_key.json')
firebase_admin.initialize_app(cred)
def update_position(shared_dict):
while True:
shared_dict['data'] = wifi_manager.dump_aps('wlp0s20f3', 3)
def draw_screen(shared_dict):
def firebase_sync():
nonlocal last_synced, unsaved_changes
print('FIREBASE SYNCED!')
last_synced = str(datetime.datetime.now())
unsaved_changes = False
firebase_manager.set_nodes(points)
firebase_manager.set_edge(edges)
def center(p):
nonlocal pos, new_map_rect
# pos[0] += screen_size[0] // 2 - (pos[0] + int(new_map_img.get_width() * p[0]))
# pos[1] += screen_size[1] // 2 - (pos[1] + int(new_map_img.get_height() * p[1]))
x_movement = int(screen_size[0] // 2 - (pos[0] + int(new_map_img.get_width() * p[0])))
y_movement = int(screen_size[1] // 2 - (pos[1] + int(new_map_img.get_height() * p[1])))
print(x_movement, y_movement, p)
while abs(x_movement) > 2 or abs(y_movement) > 2:
pos[0] += x_movement / 2
pos[1] += y_movement / 2
x_movement /= 2
y_movement /= 2
update_map()
display.update()
print('gei')
# pos is top left corner
#
# print('moving x')
# for x in range(abs(x_movement) // 3):
# pos[0] += 3 * x_movement / abs(x_movement)
# update_map()
# display.update()
#
# print('moving y')
# for y in range(abs(y_movement) // 3):
# pos[1] += 3 * y_movement / abs(y_movement)
# update_map()
# display.update()
new_map_rect.topleft = pos
def update_map():
nonlocal new_map_img, new_map_rect, markup_surf
new_map_img = transform.rotozoom(map_img, angle, screen_zoom * manual_zoom)
new_map_rect.size = new_map_img.get_size()
new_map_rect.topleft = pos
markup_surf = Surface(new_map_img.get_size(), SRCALPHA, 32)
screen.fill(0)
screen.blit(new_map_img, pos)
init()
map_img = image.load("notcoding.jpg")
logo = transform.scale(image.load("InterNAV.png"), (600, 450))
screen_size = get_start_dims(map_img)
img_w, img_h = map_img.get_size()
p_w, p_h = img_w * 0.05, img_h * 0.05
screen = display.set_mode(screen_size, RESIZABLE)
display.set_caption("InterNAV")
clock = time.Clock()
screen_zoom = get_zoom(map_img, *screen_size)
manual_zoom = 1
angle = 0
pos = [0, 0]
mode = 'data'
# MODES
# 1 - Viewing map and getting location
# 2 - Adding/Modifying nodes and graph
new_map_img = transform.rotozoom(map_img, angle, screen_zoom * manual_zoom)
new_map_rect = new_map_img.get_rect()
new_map_rect.topleft = pos
markup_surf = Surface(new_map_img.get_size(), SRCALPHA, 32)
text = font.SysFont("Bahnschrift", 18)
click_and_drag = False
points = firebase_manager.get_nodes()
edges = firebase_manager.get_edges()
current_edge = []
if not points:
points = {}
highlighted_point = ''
unsaved_changes = False
last_synced = 'Never'
data_list = []
view_setup = False
to_be_scanned = {}
auto_zoomed = False
point_recorded = False
navigation_path = None
last_position = None
screen.fill((200, 200, 255))
screen.blit(logo, ((screen_size[0] - logo.get_width()) // 2, (screen_size[1] - logo.get_height()) // 2))
display.flip()
t.sleep(0.05)
if not points:
mode = 'data'
while True:
mb = mouse.get_pressed()
for e in event.get():
if e.type == QUIT:
break
elif e.type == MOUSEBUTTONDOWN:
if e.button == 3 and mode == 'data':
mx, my = e.pos
mx = (mx - pos[0]) / ((screen_zoom * manual_zoom) * map_img.get_width())
my = (my - pos[1]) / ((screen_zoom * manual_zoom) * map_img.get_height())
minpoint = ()
minval = 99999
for pointid in points:
p = points[pointid]['location']
dist = hypot(p[0] - mx, p[1] - my)
# print(dist)
if dist < 0.005 and dist < minval:
minpoint = p
minval = dist
if minpoint != ():
draw.circle(markup_surf, (0, 0, 0, 0), (
int(new_map_img.get_width() * minpoint[0]),
int(new_map_img.get_height() * minpoint[1])), 5)
hitlist = []
for i in edges:
if firebase_manager.generate_id(minpoint) in i:
hitlist.append(i)
for z in hitlist:
edges.remove(z)
del points[firebase_manager.generate_id(minpoint)]
update_map()
unsaved_changes = True
elif e.type == MOUSEBUTTONUP:
if e.button in [1, 2]:
mx, my = e.pos
mx = (mx - pos[0]) / ((screen_zoom * manual_zoom) * map_img.get_width())
my = (my - pos[1]) / ((screen_zoom * manual_zoom) * map_img.get_height())
minpoint = ()
minval = 99999
for pointid in points:
p = points[pointid]['location']
dist = hypot(p[0] - mx, p[1] - my)
# print(dist)
if dist < 0.005 and dist < minval:
minpoint = p
minval = dist
highlighted_point = firebase_manager.generate_id(p)
if minpoint == ():
if not click_and_drag and new_map_rect.collidepoint(e.pos) and mode == 'data':
mx, my = e.pos
mx = (mx - pos[0]) / ((screen_zoom * manual_zoom) * map_img.get_width())
my = (my - pos[1]) / ((screen_zoom * manual_zoom) * map_img.get_height())
points[firebase_manager.generate_id([mx, my])] = {
'location': [mx, my]
}
else:
if e.button == 1 and mode == 'viewing':
center(minpoint)
navigation_path = calculation_manager.getPath(firebase_manager.generate_id(minpoint))
if e.button == 2:
current_edge.append(firebase_manager.generate_id(minpoint))
if len(current_edge) == 2:
edges.append(current_edge)
current_edge = []
unsaved_changes = True
elif e.button == 4:
if manual_zoom < 2:
manual_zoom += 0.05
pos[0] -= int((abs(e.pos[0]) / img_w) * p_w)
pos[1] -= int((abs(e.pos[1]) / img_h) * p_h)
update_map()
elif e.button == 5:
if manual_zoom > 0.25:
manual_zoom -= 0.05
pos[0] += int((abs(e.pos[0]) / img_w) * p_w)
pos[1] += int((abs(e.pos[1]) / img_h) * p_h)
update_map()
click_and_drag = False
elif e.type == MOUSEMOTION:
if mb[0]:
click_and_drag = True
pos[0] += e.rel[0]
pos[1] += e.rel[1]
new_map_rect.topleft = pos
elif e.type == VIDEORESIZE:
screen_size[0] = max(800, e.size[0])
screen_size[1] = max(600, e.size[1])
screen_zoom = get_zoom(map_img, *screen_size)
update_map()
display.set_mode(screen_size, RESIZABLE)
else:
keys = key.get_pressed()
if keys[K_1]:
mode = 'viewing'
navigation_path = None
view_setup = False
elif keys[K_2]:
mode = 'data'
elif keys[K_3]:
mode = 'scanning'
auto_zoomed = False
to_be_scanned = copy.deepcopy(points)
unsaved_changes = True
for i in copy.deepcopy(to_be_scanned):
if 'aps' in to_be_scanned[i]:
del to_be_scanned[i]
elif keys[K_4]:
firebase_sync()
elif keys[K_5] and mode == 'scanning' and not point_recorded:
point_recorded = True
points[highlighted_point]['aps'] = wifi_manager.dump_aps('wlp0s20f3', 3)
del to_be_scanned[highlighted_point]
elif not keys[K_5]:
point_recorded = False
if mode == 'viewing' and not view_setup:
view_setup = True
calculation_manager.Initialize(points, None, None, edges)
if mode == 'viewing':
calculatedPosition = calculation_manager.findLocation(
shared_dict['data'] if ('data' in shared_dict) else wifi_manager.dump_aps('wlp0s20f3', 3))
if calculatedPosition != last_position and navigation_path:
navigation_path = calculation_manager.getPath(firebase_manager.generate_id(minpoint))
last_position = copy.deepcopy(calculatedPosition)
update_map()
#print('POSITION!: ', calculatedPosition)
data_list = [
'Keyboard commands: 1-View/Navigation 2-Configuration 3-Scanning 4-Sync with Firebase 5-Record RP',
'MODE: ' + mode,
'LAST SYNCED: ' + last_synced,
'UNSAVED CHANGES: ' + str(unsaved_changes)
]
if mode == 'scanning':
if len(to_be_scanned):
data_list.append('PLEASE MOVE TO DESIGNATED POSITION AND PRESS 5 TO RECORD POINT!')
data_list.append('POINTS REMAINING: ' + str(len(to_be_scanned.keys())))
highlighted_point = list(to_be_scanned.keys())[0]
print('hey')
if not auto_zoomed:
manual_zoom = 2
pos[0] -= int(to_be_scanned[highlighted_point]['location'][0] * p_w)
pos[1] -= int(to_be_scanned[highlighted_point]['location'][1] * p_h)
auto_zoomed = True
center(to_be_scanned[highlighted_point]['location'])
update_map()
else:
data_list.append('SCANNING COMPLETE!')
data_list.append('PRESS 1 TO RETURN TO VIEW MODE')
if unsaved_changes:
firebase_sync()
screen.fill((255,255,255))
pos[0] = max(min(pos[0], screen_size[0] - 160), 160 - new_map_img.get_width())
pos[1] = max(min(pos[1], screen_size[1] - 160), 160 - new_map_img.get_height())
screen.blit(new_map_img, pos)
for id1, id2 in edges:
try:
(rx1, ry1), (rx2, ry2) = points[id1]['location'], points[id2]['location']
draw.line(markup_surf, (0, 255, 255),
(int(new_map_img.get_width() * rx1), int(new_map_img.get_height() * ry1)),
(int(new_map_img.get_width() * rx2), int(new_map_img.get_height() * ry2)))
except:
update_map()
if navigation_path:
for id1, id2 in navigation_path:
(rx1, ry1), (rx2, ry2) = id1, id2
draw.line(markup_surf, (255, 0, 255),
(int(new_map_img.get_width() * rx1), int(new_map_img.get_height() * ry1)),
(int(new_map_img.get_width() * rx2), int(new_map_img.get_height() * ry2)), 5)
for pointid in points:
rx, ry = points[pointid]['location']
if firebase_manager.generate_id([rx, ry]) == highlighted_point:
color = (0, 255, 0)
else:
color = (255, 0, 0)
draw.circle(markup_surf, color,
(int(new_map_img.get_width() * rx), int(new_map_img.get_height() * ry)), 5)
if mode == 'viewing':
draw.circle(markup_surf, (0, 0, 255),
(int(new_map_img.get_width() * calculatedPosition[0]),
int(new_map_img.get_height() * calculatedPosition[1])), 5)
screen.blit(markup_surf, pos)
for i, data in enumerate(data_list):
screen.blit(text.render(data, True, (0, 0, 0), (220, 220, 220)), (20, 20 + 30 * i))
clock.tick()
#display.set_caption(f"InterNAV | FPS: {int(clock.get_fps())}")
display.set_caption(f"InterNAV")
display.update()
continue
break
display.quit()
up = multiprocessing.Process(target=update_position, args=(shared_dict,))
up.start()
dp = multiprocessing.Process(target=draw_screen, args=(shared_dict,))
dp.start()
print('Running!')
input('Press any key to end')