From 93e2e739c8adc42f7c51ee2f814dd57dfb7afe7f Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 27 Jun 2021 16:18:24 +0200 Subject: [PATCH 1/3] Moved horaires query to js instead of html page constant reload (in progress) --- reflectobus.py | 106 +++++++++++++++++++-------------------- static/js/horaires.js | 88 +++++++++++++++++++++++++++++++- static/styles/mirror.css | 5 ++ templates/horaires.html | 48 ++---------------- 4 files changed, 146 insertions(+), 101 deletions(-) diff --git a/reflectobus.py b/reflectobus.py index 111d955..fc421cf 100644 --- a/reflectobus.py +++ b/reflectobus.py @@ -16,6 +16,16 @@ # On initialise le serveur Flask app = Flask(__name__) +def get_ip() : + localip = "127.0.0.1" + if not offline: # We open a socket to get our ip on the local network + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("8.8.8.8", 80)) + localip = (s.getsockname()[0]) + s.close() + + return localip + def fake_schedules(datatype="object"): # Générateur de fausses lignes de bus, gagnerait à être amélioré @@ -146,12 +156,10 @@ def get_horaires(): data['config'] = {} data['config']['refresh_time'] = configParser['ADVANCED']['refresh_time'] - data['config']['background_color'] = configParser['ADVANCED']['background_color'] - data['config']['font_size'] = eval(configParser['ADVANCED']['font_size']) data['config']['hide_category'] = eval(configParser['ADVANCED']['hide_category']) - - if configParser['ADVANCED']['background_type'] == "image" : - data['config']['background_url'] = configParser['ADVANCED']['background_url'] + data['config']['pass_colors'] = eval(configParser['ADVANCED']['pass_colors']) + data['config']['lines_color'] = configParser['ADVANCED']['lines_color'] + data['config']['localip'] = get_ip() if not offline : global config_changed @@ -159,13 +167,45 @@ def get_horaires(): if config_changed : schedules_object = schedules.Schedules() config_changed = False - data['schedule'] = schedules_object.__main__() - print(data['schedule']) - return (data) + schedules_data = schedules_object.__main__() + data["schedule"] = {} + + for category in schedules_data.keys() : + for hour in schedules_data[category] : + data["schedule"][category] = { + 'name':hour.name, + 'id':hour.id, + 'Carrier':hour.Carrier, + 'Operator':hour.Operator, + 'PublicCode':hour.PublicCode, + 'TypeOfLine':hour.TypeOfLine, + 'VehicleType':hour.VehicleType, + 'night':hour.night, + 'lepiloteId':hour.lepiloteId, + 'color':hour.color, + 'sqliType':hour.sqliType + } + + + return ({"data":data, "content":"Horaires"}) else: data['schedule'] = fake_schedules(datatype="JSON") - return ({"data":data, "content":None}) + return ({"data":data, "content":"Horaires"}) + +def get_look(): + data = {} + + data['background_color'] = configParser['ADVANCED']['background_color'] + data['font_size'] = eval(configParser['ADVANCED']['font_size']) + data['refresh_time'] = eval(configParser['ADVANCED']['refresh_time']) + + if configParser['ADVANCED']['background_type'] == "image" : + data['background_url'] = configParser['ADVANCED']['background_url'] + + return ({"data":data,"content":"look"}) + + def set_wlan(data): # NotImplementedError @@ -200,60 +240,17 @@ def index(): @app.route("/boot") def boot(): - localip = "127.0.0.1" - if not offline: # We open a socket to get our ip on the local network - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - localip = (s.getsockname()[0]) - s.close() - data = {"version":ver, "rtm_version":rtm_ver, "boot_time":configParser['ADVANCED']['boot_time'], - "local_ip":localip + "local_ip":get_ip() } data['background_color'] = configParser['ADVANCED']['background_color'] return render_template('boot.html', data=data) @app.route("/horaires") def horaires(): - data={} - # fetching parsed data - - if not offline : - global config_changed - global schedules_object - if config_changed : - schedules_object = schedules.Schedules() - config_changed = False - data['schedule'] = schedules_object.__main__() - - else : # We create fake Lines to be able to test the app when offline - data['schedule'] = fake_schedules(datatype="object") - - data['refresh_time'] = configParser['ADVANCED']['refresh_time'] - data['background_color'] = configParser['ADVANCED']['background_color'] - data['font_size'] = eval(configParser['ADVANCED']['font_size']) - data['hide_category'] = eval(configParser['ADVANCED']['hide_category']) - - if configParser['ADVANCED']['background_type'] == "image" : - data['background_url'] = configParser['ADVANCED']['background_url'] - - data['total_schedules'] = 0 - for i in data['schedule'].keys(): - data['total_schedules'] += len(data['schedule'][i]) - - if data['total_schedules'] == 0 : - localip = "127.0.0.1" - if not offline: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - localip = (s.getsockname()[0]) - s.close() - - data['localip'] = localip - - return render_template('horaires.html',data=data) + return render_template('horaires.html') # not in use yet @app.route('/get') @@ -261,6 +258,7 @@ def get(): actions = { "config":get_config, "horaires":get_horaires, + "look":get_look, } content = request.args.get("content") if content in actions.keys(): diff --git a/static/js/horaires.js b/static/js/horaires.js index 19a92c0..6a82429 100644 --- a/static/js/horaires.js +++ b/static/js/horaires.js @@ -1,8 +1,92 @@ -var onLoad = function () { +var getSchedules = function (fontSize=1) { + console.log('qsc') + getJSON("/get?content=horaires", function(data){ + data = data["data"]; + // Variables initialisation dependingnon the request content + var numberOfCategories = Object.keys( data["schedule"] ).length; + var numberOfSchedules = 0; + for (var category of Object.keys( data["schedule"] ) ) { + numberOfSchedules += data["schedule"][category].length; + } + if (numberOfSchedules == 0) { + // If no schedules are available, we set te text to this sentence + document.body.innerHTML = "
Aucun horaire en temps réel n'est disponible,
Configurez cela à l'URL suivante : http://"+data["config"]["localip"]+":5000
"; + } else { + section = "
" + for (var category of Object.keys( data["schedule"] ) ) { + // We set the beginning of the section + section += "
" + if ( ! data["config"]["hide_category"]){ + // If the variable hide_category is set to + // true, we don't display the title + section += "

"+category+"

" + } + if ( data["schedule"][category].length > 0 ) { + for (var schedule of data["schedule"][category]) { + section += "
"; + section += ""+schedule["hour"]+"min" + } + } else { + section += "Il n'y a pas d'horaires disponibles pour cette catégorie" + } + section += "
" + } + section += "" + document.body.innerHTML = section + } + }); // On remplace les horaires "0min" par "En Vue" for (var i of document.getElementsByClassName('time')) { if (i.innerHTML == "0min") { i.innerHTML = "En Vue"} } } -onLoad() \ No newline at end of file +var loadLook = function() { + getJSON("/get?content=look", function(data){ + data = data["data"]; + + if (typeof data["background_url"] != "undefined") { + document.body.style.background = "url(/static/walls/"+data["background_url"]+")"; + } else { + document.body.style.background = "url(/not_an_image.png)" + } + document.body.style.backgroundColor = data["background_color"]; + + for (element of document.getElementsByClassName("time")) { + element.style.fontSize = data["font_size"]+"rem"; + } + if (window.cpt == 3) { + getSchedules(fontSize=data["font_size"]); + window.cpt = 0; + } else { + window.cpt += 1; + } + setTimeout(() => { loadLook("loadLook"); }, data["refresh_time"]/3 ); + }); +} + +var onLoad = function() { + getSchedules(); + loadLook() +} + +window.cpt = 0; +onLoad() + + +// pas d'horaires ne fonctionne pas (issue serveur) +// 509 ne fonctionne pas (ne parvient pas à identifier self.name/id/...) +// N'affiche pas Internal Server Error si c'est le cas \ No newline at end of file diff --git a/static/styles/mirror.css b/static/styles/mirror.css index fd03a83..a77baae 100644 --- a/static/styles/mirror.css +++ b/static/styles/mirror.css @@ -9,4 +9,9 @@ background-size: 0.8125rem; display: inline-block; padding-right: 0.8125rem; + background-image: url("/static/src/realtime.gif"); +} + +.time { + font-size:1rem; } \ No newline at end of file diff --git a/templates/horaires.html b/templates/horaires.html index 0f96334..f9f6a17 100644 --- a/templates/horaires.html +++ b/templates/horaires.html @@ -10,52 +10,10 @@ href="https://fonts.googleapis.com/css?family=Roboto"> -
- {% if data['total_schedules'] > 0 %} - {% for n in data['schedule'].keys() %} -
- {% if not data['hide_category'] %} -

{{ n }}

- {% endif %} - {% if data['schedule'][n]|length > 0 %} - {% for schedules in data['schedule'][n] %} -
-

{{ schedules[0].PublicCode }}

-
- {{ schedules[1] }}min - {% endfor %} - {% else %} - Il n'y a pas d'horaires en temps réel disponibles pour cette catégorie - {% endif %} -
- {% endfor %} - {% else %} -
- Aucun horaire en temps réel n'est disponible,
- Configurez cela à l'URL suivante : http://{{ data['localip'] }}:5000/config -
- {% endif %} - +
- + - - - + From 44880f1c32e0705a658a7bd1e7f20a25c8569899 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 7 Aug 2021 22:45:08 +0200 Subject: [PATCH 2/3] Minor changes --- modules/schedules.py | 6 +++--- reflectobus.py | 30 +++++++++++++++++------------- static/js/horaires.js | 9 +++++---- static/styles/main.css | 2 +- templates/config.html | 2 +- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/modules/schedules.py b/modules/schedules.py index aae7563..91e12a8 100644 --- a/modules/schedules.py +++ b/modules/schedules.py @@ -10,13 +10,10 @@ home = str(Path.home()) configpath = PurePath(home).joinpath('.config/reflect-o-bus/') -configParser = configparser.ConfigParser() - if not os.path.isfile(configpath.joinpath('config')) : os.makedirs(configpath, exist_ok=True) shutil.copy('./examples/default',configpath.joinpath('config')) - class Configuration(): def __init__(self): configParser.read(configpath.joinpath('config')) @@ -138,6 +135,9 @@ def get_schedules(config): class Schedules(): def __init__(self): + global configParser + configParser = configparser.ConfigParser() + self.config = Configuration() self.config.update() print(" * Config Initialized") diff --git a/reflectobus.py b/reflectobus.py index fc421cf..d3a4bb1 100644 --- a/reflectobus.py +++ b/reflectobus.py @@ -169,22 +169,26 @@ def get_horaires(): config_changed = False schedules_data = schedules_object.__main__() data["schedule"] = {} + print(schedules_data.keys()) for category in schedules_data.keys() : + data['schedule'][category] = [] for hour in schedules_data[category] : - data["schedule"][category] = { - 'name':hour.name, - 'id':hour.id, - 'Carrier':hour.Carrier, - 'Operator':hour.Operator, - 'PublicCode':hour.PublicCode, - 'TypeOfLine':hour.TypeOfLine, - 'VehicleType':hour.VehicleType, - 'night':hour.night, - 'lepiloteId':hour.lepiloteId, - 'color':hour.color, - 'sqliType':hour.sqliType - } + data["schedule"][category].append({ + 'name':hour[0].name, + 'id':hour[0].id, + 'Carrier':hour[0].Carrier, + 'Operator':hour[0].Operator, + 'PublicCode':hour[0].PublicCode, + 'TypeOfLine':hour[0].TypeOfLine, + 'VehicleType':hour[0].VehicleType, + 'night':hour[0].night, + 'lepiloteId':hour[0].lepiloteId, + 'color':hour[0].color, + 'sqliType':hour[0].sqliType, + 'hour':hour[1], + 'isRealTime':hour[2] + }) return ({"data":data, "content":"Horaires"}) diff --git a/static/js/horaires.js b/static/js/horaires.js index 6a82429..651be0a 100644 --- a/static/js/horaires.js +++ b/static/js/horaires.js @@ -1,5 +1,4 @@ var getSchedules = function (fontSize=1) { - console.log('qsc') getJSON("/get?content=horaires", function(data){ data = data["data"]; // Variables initialisation dependingnon the request content @@ -37,7 +36,11 @@ var getSchedules = function (fontSize=1) { if ( schedule["isRealTime"] ) { section += " realtime" } - section += "'>"+schedule["hour"]+"min" + if (schedule['hour'] == 0) { + section += "'>En Vue" + } else { + section += "'>"+schedule["hour"]+"min" + } } } else { section += "Il n'y a pas d'horaires disponibles pour cette catégorie" @@ -87,6 +90,4 @@ window.cpt = 0; onLoad() -// pas d'horaires ne fonctionne pas (issue serveur) -// 509 ne fonctionne pas (ne parvient pas à identifier self.name/id/...) // N'affiche pas Internal Server Error si c'est le cas \ No newline at end of file diff --git a/static/styles/main.css b/static/styles/main.css index b7bb970..99d6536 100644 --- a/static/styles/main.css +++ b/static/styles/main.css @@ -7,7 +7,7 @@ line-height:3rem; width:3rem; height:3rem; - font-size:2rem; + font-size:1.6rem; transition:color .333s; } #squircle::after { diff --git a/templates/config.html b/templates/config.html index e48e107..557c9b1 100644 --- a/templates/config.html +++ b/templates/config.html @@ -58,7 +58,6 @@ Durée de l'écran de démarrage (ms)
-
Cacher les noms de catégories:
Taille d'affichage des horaires
+
From a0430710d2ed1a868c40bd0479168448f0c0b524 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 8 Aug 2021 22:20:26 +0200 Subject: [PATCH 3/3] Affichage des erreurs sur la page --- reflectobus.py | 9 +++++++-- static/js/horaires.js | 4 ++++ static/js/main.js | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/reflectobus.py b/reflectobus.py index d3a4bb1..27c3069 100644 --- a/reflectobus.py +++ b/reflectobus.py @@ -300,7 +300,8 @@ def get_logs(): with zipfile.ZipFile(data, mode='w') as zf: for file in Path(logs_path).rglob('*'): # Cette solution supprime zf.write(file, file.name) # la possibilité d'utiliser des dossiers dans - data.seek(0) # les logs, mais ce n'est pas quelque chose de nécessaire + data.seek(0) # les logs, car elle prend tous les fichiers + # sans différencier leur chemin d'accès return send_file( data, @@ -345,7 +346,11 @@ def view_logs(): @app.errorhandler(500) def server_error_handler(e): print("error:",e) - return render_template('error_500.html',data=e) + print(request.base_url) + if request.is_json or request.base_url.split('/')[-1] == "get": + return(e) + else: + return render_template('error_500.html',data=e) # Development option to run tests without internet access diff --git a/static/js/horaires.js b/static/js/horaires.js index 651be0a..943ab86 100644 --- a/static/js/horaires.js +++ b/static/js/horaires.js @@ -50,6 +50,10 @@ var getSchedules = function (fontSize=1) { section += "" document.body.innerHTML = section } + }, error_handler=function(err){ + document.body.innerHTML=err; + console.log(err); + setTimeout(() => {document.location=document.location},1000) }); // On remplace les horaires "0min" par "En Vue" for (var i of document.getElementsByClassName('time')) { diff --git a/static/js/main.js b/static/js/main.js index ea2a522..464541c 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -26,11 +26,11 @@ function sendJSON(data,callback){ xhr.send(data); } -var getJSON = function(url, callback) { +var getJSON = function(url, callback, error_handler=function(err){throw err}) { fetch(url) .then(res => res.json()) .then((out) => { callback(out); }) - .catch(err => { throw err }); + .catch(err => { error_handler(err) }); }