From: Max Value Date: Fri, 25 Jul 2025 18:41:32 +0000 (+0100) Subject: Implemented websockets for the display and HUD pages X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=f4a4fc74924513163a899f67e751db5318f0353c;p=shopping-channel Implemented websockets for the display and HUD pages --- diff --git a/TODO b/TODO new file mode 100644 index 0000000..cc33ad5 --- /dev/null +++ b/TODO @@ -0,0 +1,5 @@ +- websockets for the api +- update the doomsday clock repo +- better solution to loading static data + - potentially in the database? + - could open it up for sqlite browser diff --git a/main.py b/main.py index abcdd9a..887cdb3 100755 --- a/main.py +++ b/main.py @@ -1,16 +1,17 @@ #!.venv/bin/python -from flask import Flask +from flask_socketio import SocketIO, emit from flask_httpauth import HTTPBasicAuth from datetime import datetime, timezone from os import path, environ, system -from markupsafe import escape from flask_cors import CORS +from flask import Flask,request import json auth = HTTPBasicAuth() app = Flask(__name__) CORS(app) +socketio = SocketIO(app, logger=True, engineio_logger=True) try: app.root_path = ROOT = environ["XMDV_PATH"] @@ -67,5 +68,13 @@ app.route("/api/generate")(docs_generate) # utils app.before_request(teleshopping.check_database) +# if the socket is connecting for the first time, send data +@socketio.on('connect') +def initial(): + print("conected!") + request.method = "internal" + emit("apiUpdate", teleshopping.data(), json=True) + + if __name__ == "__main__": - app.run(host='127.0.0.1', port=8000, debug=True) + socketio.run(app, host='127.0.0.1', port=8000, debug=True) diff --git a/requirements.txt b/requirements.txt index 19c1262..e25dc38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flask_httpauth +flask_socketio markupsafe flask_cors werkzeug diff --git a/static/display.css b/static/display.css new file mode 100644 index 0000000..c85b420 --- /dev/null +++ b/static/display.css @@ -0,0 +1,41 @@ +/* +CSS stylesheet for styling the HUD's including the '/hud' page and the +'/autocue' page +*/ + +body { + font-family: sans-serif; + color: white; + background-color: black; +} + +#slider { + position: fixed; + top: 10px; + left: 10px; +} + +#notes, #message { + margin: 20px; +} + +#timer { + position: absolute; + bottom: 0; + left: 0; + margin: 20px; +} + +#numbers { + position: absolute; + top: 20px; + left: 0; + margin: 20px; +} +#discountBox { + opacity: 0; + transition: opacity 1s; +} +#discountBox.show { + opacity: 1; +} diff --git a/static/utils.js b/static/utils.js new file mode 100644 index 0000000..828e96f --- /dev/null +++ b/static/utils.js @@ -0,0 +1,16 @@ +export function makeTime(end, offset, strike) { + let current = Math.round((Date.now() + offset) / 1000); + var time = end - current; + + if (Math.sign(time) == -1) {time = 0;} + var minutes = Math.floor(time / 60); + var seconds = (time - (minutes * 60)); + + var minutesString = minutes.toString().padStart(2, "0"); + var secondsString = seconds.toString().padStart(2, "0"); + if (strike) { + return `${minutesString}:${secondsString}`; + } else { + return `${minutesString}:${secondsString}`; + } +} diff --git a/teleshopping/admin.py b/teleshopping/admin.py index 9602368..ad1d1d0 100644 --- a/teleshopping/admin.py +++ b/teleshopping/admin.py @@ -1,6 +1,20 @@ +from flask import Flask, Response, request, render_template, send_from_directory from math import radians, cos, sin +from .api.data import data as api +from flask_socketio import emit +from markupsafe import escape +from os import environ +import sqlite3 +import json INCREMENT = 18 +ROOT = environ["XMDV_PATH"] + +with open(f"{ROOT}/static/static.json", "r", encoding="utf-8") as f: + static_data = json.loads(f.read()) + +with open(f"{ROOT}/static/info.json", "r", encoding="utf-8") as f: + static_info = json.loads(f.read()) def admin(): return Response(render_template("admin.html", mimetype="text/html")) @@ -31,7 +45,7 @@ def admin_page(page): else: aggrigate_data.update({key: post_data[key]}) - with sqlite3.connect(path.join(app.root_path, "data.db")) as connection: + with sqlite3.connect(f"{ROOT}/data.db") as connection: cursor = connection.cursor() result = cursor.execute("SELECT * FROM pragma_table_info('state');").fetchall()[1:] @@ -47,12 +61,15 @@ def admin_page(page): WHERE id = 1; """ - print(query) - cursor.execute(query) connection.commit() - request.method = "internal" # set method to internal so that the api call returns only the python object + request.method = "internal" # set method to internal so that the api call returns only the python object + emit('apiUpdate', api(), broadcast=True, namespace="/") + + else: + request.method = "internal" # set method to internal so that the api call returns only the python object + if page == "clock": coords = [{ "i":i, diff --git a/teleshopping/api/data.py b/teleshopping/api/data.py index d96949a..7be9dc3 100644 --- a/teleshopping/api/data.py +++ b/teleshopping/api/data.py @@ -27,6 +27,9 @@ def data(): else: return "", 404 +def handle_message(message): + send(message) + def items(): if request.method == "GET": return jsonify(static_data) diff --git a/templates/autocue.html b/templates/autocue.html index 31caa05..cd3e934 100644 --- a/templates/autocue.html +++ b/templates/autocue.html @@ -4,85 +4,60 @@ XMDV Autocue - - + + + +
{{item.notes|safe}}
+
{{data.note}}
+ + - - - -
{{item.notes|safe}}
-
{{data.note}}
diff --git a/templates/display.html b/templates/display.html index 7474dfd..57be682 100644 --- a/templates/display.html +++ b/templates/display.html @@ -3,128 +3,94 @@ XMDV Display - - - - - -
- T1: 00:00 - - T2: 00:00 - - T3: 00:00
- Left: 0 out of 0
- Start price: 0.00 - ⤓00% - ⤈00%
- Discount: 00% (Now 0.00) -