-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
153 lines (115 loc) · 3.46 KB
/
Copy pathapp.py
File metadata and controls
153 lines (115 loc) · 3.46 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
import json
import os
import signal
import threading
import time
from flask import Flask, request, send_file, Response
from flask_socketio import SocketIO, emit
from flask_cors import CORS, cross_origin
from models import MqttReceiver
from utils import Config
app = Flask(__name__)
if not os.path.isdir('maps'):
os.mkdir('maps')
if not os.path.isdir('profiles'):
os.mkdir('profiles')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins=['http://localhost', 'http://localhost:5173', 'http://192.168.1.0'],
async_mode='threading')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
config = Config()
receiver = MqttReceiver(
config.receiver_config,
config.mqtt_host,
config.mqtt_port,
config.mqtt_topic,
config.status_app)
socketio.start_background_task(receiver.infinite_sender, socketio)
@app.route('/data')
@cross_origin()
def get_data():
"""
:return: all data
"""
return json.dumps(receiver.data, default=vars)
@app.route('/charts/origins')
@cross_origin()
def get_origins():
"""
:return: List of origins
"""
return json.dumps(receiver.get_origins())
@app.route('/maps')
@cross_origin()
def get_maps():
return json.dumps(os.listdir('maps'))
@app.route('/profiles')
@cross_origin()
def get_profiles():
with open('profiles/manifest.json') as f:
return f.read()
@app.route('/maps/origins')
@cross_origin()
def get_map_origins():
return json.dumps(receiver.get_location_origins())
@app.route('/maps/<path:pars>')
def paths(pars):
if '..' in pars:
return "Not ok"
path = os.path.join('maps', *pars.split('/'))
if not os.path.isfile(path):
return Response(f"Path {path} is not file", 404)
return send_file(path)
@app.route('/profiles/<path:pars>')
def profiles(pars):
if '..' in pars:
return "Not ok"
path = os.path.join('profiles', *pars.split('/'))
print(path)
if not os.path.isfile(path):
return Response(f"Path {path} is not file", 404)
return send_file(path)
@socketio.on('charts/configure')
def configure_session(data):
session = receiver.get_session(request.sid)
try:
session.configure(data)
except Exception as e:
print(e)
@socketio.on('maps/configure')
def configure_maps(data):
session_id = request.sid
session = receiver.get_session(session_id)
try:
history = session.configure_locations(data, receiver.location_history)
socketio.emit('maps/history', history, to=session_id)
except Exception as e:
print(e)
@socketio.on('request')
def get_origins(value):
session_id = request.sid
if value == 'charts/origins':
socketio.emit('charts/origins', receiver.get_origins(), to=session_id)
if value == 'maps/origins':
socketio.emit('maps/origins', receiver.get_location_origins(), to=session_id)
@socketio.on('connect')
def my_connect():
receiver.create_session(request.sid)
@socketio.on('disconnect')
def my_disconnect():
receiver.sessions.pop(request.sid)
print(config)
if __name__ == '__main__':
socketio_thread = threading.Thread(
target=socketio.run,
args=(app,),
kwargs={"debug": False, "host": "0.0.0.0", "port": config.pcc_port, "allow_unsafe_werkzeug": True},
daemon=True)
socketio_thread.start()
try:
while True:
input()
except KeyboardInterrupt:
print("Wyłączanie")
os.kill(os.getpid(), signal.SIGINT)