-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (53 loc) · 1.9 KB
/
app.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
from flask import Flask, render_template, jsonify
from dotenv import load_dotenv
import os
import json
import time
from flask_sse import sse
from threading import Thread
load_dotenv()
app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost:6379"
app.register_blueprint(sse, url_prefix='/stream')
json_file_path = os.getenv('JSON_FILE_PATH')
def check_for_updates():
last_data = None
while True:
try:
with open(json_file_path, 'r') as file:
cameras_data = json.load(file)
data = [
{"location": details['location'], "number": details['number']}
for details in cameras_data.values()
]
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error reading input.json: {e}")
continue
if data != last_data:
sse.publish({"coordinates": data}, type='update_coordinates')
last_data = data
time.sleep(5)
@app.route('/')
def index():
api_key = os.getenv('GOOGLE_MAPS_API_KEY')
map_id = os.getenv('GOOGLE_MAPS_MAP_ID')
# Load coordinates and numbers from input.json
data = []
try:
with open(json_file_path, 'r') as file:
cameras_data = json.load(file)
data = [
{"location": details['location'], "number": details['number']}
for details in cameras_data.values()
]
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error reading input.json: {e}")
return render_template('index.html', api_key=api_key, map_id=map_id, coordinates=data)
if __name__ == '__main__':
thread = Thread(target=check_for_updates)
thread.daemon = True # Ensures the thread will exit when the main program exits
thread.start()
try:
app.run(debug=True, port=5001)
except KeyboardInterrupt:
print("Shutting down...")