-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
72 lines (53 loc) · 1.9 KB
/
server.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
from flask import Flask, send_from_directory, json, Response
from os.path import exists
import os
import threading
import inspect
SERVER_PORT = 1234
class DummyClass:
def nothing():
print("nothing")
file_path = os.path.dirname(os.path.abspath(inspect.getsourcefile(DummyClass)))
class MapServer:
app = Flask(__name__)
last_rover_coords = [-1, 0]
debug = True
def register_routes(self):
@self.app.route("/")
def serve_index():
return send_from_directory("frontend", "index.html")
@self.app.route("/tile/<z>/<x>/<y>")
def serve_tile(z, x, y):
tileFilePath = file_path + "/tiles/z{}, x{}, y{}.jpg".format(z, x, y)
if self.debug:
print(f"{file_path}")
if exists(tileFilePath):
tileFD = open(tileFilePath, "rb")
img = tileFD.read()
tileFD.close()
return img
return Response(status=404)
@self.app.route("/roverCoords")
def return_rover_coords():
if self.debug:
print(f"lastcoords {self.last_rover_coords}")
return json.dumps(self.last_rover_coords)
@self.app.route("/<path:path>")
def serve_static(path):
return send_from_directory("frontend", path)
def update_rover_coords(self, lat_lng_array):
if self.debug:
print("updating rover coords")
self.last_rover_coords = lat_lng_array
if self.debug:
print(f"updated {self.last_rover_coords}")
def start(self, debug=True):
self.debug = debug
print("starting server")
def thread_target():
self.app.run(debug=False, host="0.0.0.0", port=SERVER_PORT)
threading.Thread(target=thread_target).start()
if __name__ == "__main__":
mapServer = MapServer()
mapServer.register_routes()
mapServer.start()