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 111d955..27c3069 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,49 @@ 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"] = {} + print(schedules_data.keys()) + + for category in schedules_data.keys() : + data['schedule'][category] = [] + for hour in schedules_data[category] : + 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"}) 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 +244,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 +262,7 @@ def get(): actions = { "config":get_config, "horaires":get_horaires, + "look":get_look, } content = request.args.get("content") if content in actions.keys(): @@ -298,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, @@ -343,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 19a92c0..943ab86 100644 --- a/static/js/horaires.js +++ b/static/js/horaires.js @@ -1,8 +1,97 @@ -var onLoad = function () { +var getSchedules = function (fontSize=1) { + 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 = "
"+schedule["PublicCode"]+"
{{ schedules[0].PublicCode }}
-