adding update time very hastily to the top right

This commit is contained in:
Lucas
2021-10-09 20:12:44 -04:00
parent 1a7c2c9a9c
commit 10c2b9ffca
4 changed files with 38 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ certifi==2020.12.5
chardet==4.0.0 chardet==4.0.0
click==8.0.1 click==8.0.1
colorama==0.4.4 colorama==0.4.4
deepdiff==5.5.0
dnspython==1.16.0 dnspython==1.16.0
Flask==2.0.1 Flask==2.0.1
greenlet==1.1.0 greenlet==1.1.0
@@ -13,6 +14,7 @@ itsdangerous==2.0.1
Jinja2==3.0.1 Jinja2==3.0.1
MarkupSafe==2.0.1 MarkupSafe==2.0.1
numpy==1.21.0 numpy==1.21.0
ordered-set==4.0.2
pandas==1.3.0 pandas==1.3.0
protobuf==3.16.0 protobuf==3.16.0
protobuf3-to-dict==0.1.5 protobuf3-to-dict==0.1.5

View File

@@ -1,12 +1,12 @@
import logging
import os import os
import threading import threading
from flask import Flask, jsonify, render_template, request, abort
from mta_manager import MTA
import pandas as pd import pandas as pd
from deepdiff import DeepDiff
from datetime import datetime
from dotenv import load_dotenv from dotenv import load_dotenv
from flask import Flask, jsonify, render_template, request, abort
from mta_manager import MTA
load_dotenv() load_dotenv()
@@ -49,8 +49,10 @@ def get_mta_data():
global subway_data global subway_data
station = request.json["station"] station = request.json["station"]
if station in subway_data: if station in subway_data:
mta_data = subway_data[station]
mta_data["LastUpdated"] = subway_data["LastUpdated"]
return jsonify( return jsonify(
subway_data[station] mta_data
) )
else: else:
abort(404) abort(404)
@@ -71,16 +73,20 @@ def get_stop_id():
if __name__ == "__main__": if __name__ == "__main__":
api_key = os.getenv('MTA_API_KEY', '') api_key = os.getenv('MTA_API_KEY', '')
mtaController = MTA(
api_key,
["A", "C", "E", "1", "2", "3"],
["127S", "127N", "A27N", "A27S"]
)
old_data = None
last_updated = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
async def mta_callback(routes): async def mta_callback(routes):
global subway_data global subway_data, old_data, last_updated
subway_data = link_to_station(mtaController.convert_routes_to_station_first(routes)) subway_data = link_to_station(mtaController.convert_routes_to_station_first(routes))
subway_data["LastUpdated"] = last_updated
if old_data is None:
old_data = subway_data
data_diff = DeepDiff(old_data, subway_data, ignore_order=True)
if data_diff != {}:
old_data = subway_data
last_updated = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
app.logger.info(f"Updated Subway Data - {subway_data}") app.logger.info(f"Updated Subway Data - {subway_data}")
@@ -93,15 +99,20 @@ if __name__ == "__main__":
self.run() self.run()
mtaController = MTA(
api_key,
["A", "C", "E", "1", "2", "3"],
["127S", "127N", "A27N", "A27S"]
)
mtaController.add_callback(mta_callback)
def start_mta(): def start_mta():
mtaController.add_callback(mta_callback)
while True: while True:
try: try:
mtaController.start_updates() mtaController.start_updates()
except Exception as e: except Exception as e:
app.logger.info(f"Exception found in update function - {e}") app.logger.info(f"Exception found in update function - {e}")
threadLock = threading.Lock() threadLock = threading.Lock()
threads = [threadWrapper(start_mta)] threads = [threadWrapper(start_mta)]
@@ -109,10 +120,8 @@ if __name__ == "__main__":
t.start() t.start()
debug = os.getenv("DEBUG", 'False').lower() in ('true', '1', 't') debug = os.getenv("DEBUG", 'False').lower() in ('true', '1', 't')
app.run(host="localhost", debug= True, port=5000)
app.run(host="localhost", debug= False, port=5000)
# Wait for all threads to complete
for t in threads: for t in threads:
t.join() t.join()
print("Exiting Main Thread") print("Exiting Main Thread")

View File

@@ -17,9 +17,9 @@
src="https://code.jquery.com/jquery-3.6.0.min.js" src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script src="/static/js/DataRequests.js"></script> <script src="/static/js/MtaData.js"></script>
<title>Hello, world!</title> <title>Pi MTA Display!</title>
</head> </head>
<body class="dark"> <body class="dark">
<nav class="navbar navbar-dark bg-dark py-2"> <nav class="navbar navbar-dark bg-dark py-2">
@@ -31,6 +31,9 @@
Pi MTA Display! Pi MTA Display!
</h1> </h1>
</a> </a>
<h3>
Last Updated:&nbsp;&nbsp;<span id="last_updated"></span>
</h3>
</div> </div>
</nav> </nav>
<div id="station_1"> <div id="station_1">
@@ -205,8 +208,5 @@
</div> </div>
</div> </div>
</div> </div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
</body> </body>
</html> </html>

View File

@@ -28,6 +28,12 @@ $(document).ready(function () {
function updateStation(station, data) { function updateStation(station, data) {
updateDirections(station, data, "North"); updateDirections(station, data, "North");
updateDirections(station, data, "South"); updateDirections(station, data, "South");
updateTime(data["LastUpdated"])
}
function updateTime(lastUpdated){
console.log(lastUpdated)
$("#last_updated").text(lastUpdated)
} }
function updateDirections(station, data, direction) { function updateDirections(station, data, direction) {