diff --git a/db.json b/db.json index 8fba4cb..9e31478 100644 --- a/db.json +++ b/db.json @@ -1 +1,364 @@ -{"utilisateur": {"id": "admin", "password": "9ac564e9a29c952a3ddeffd8635adb0c252dbd1d58f3efde265e028f7f808aa9"}, "PIN_ACTIF": false, "CODE_PIN": "0000", "NUM_AUTORISE": "670100036", "TONE_DURATION": 2, "DTMF_CODE": "#", "DTMF_DURATION": 30, "HORAIRES": {"Monday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 1, "06h30": 1, "07h00": 1, "07h30": 1, "08h00": 1, "08h30": 1, "09h00": 1, "09h30": 1, "10h00": 1, "10h30": 1, "11h00": 1, "11h30": 1, "12h00": 1, "12h30": 1, "13h00": 1, "13h30": 1, "14h00": 1, "14h30": 1, "15h00": 1, "15h30": 1, "16h00": 1, "16h30": 1, "17h00": 1, "17h30": 1, "18h00": 1, "18h30": 1, "19h00": 1, "19h30": 1, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Tuesday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 1, "06h30": 1, "07h00": 1, "07h30": 1, "08h00": 1, "08h30": 1, "09h00": 1, "09h30": 1, "10h00": 1, "10h30": 1, "11h00": 1, "11h30": 1, "12h00": 1, "12h30": 1, "13h00": 1, "13h30": 1, "14h00": 1, "14h30": 1, "15h00": 1, "15h30": 1, "16h00": 1, "16h30": 1, "17h00": 1, "17h30": 1, "18h00": 1, "18h30": 1, "19h00": 1, "19h30": 1, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Wednesday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 1, "06h30": 1, "07h00": 1, "07h30": 1, "08h00": 1, "08h30": 1, "09h00": 1, "09h30": 1, "10h00": 1, "10h30": 1, "11h00": 1, "11h30": 1, "12h00": 1, "12h30": 1, "13h00": 1, "13h30": 1, "14h00": 1, "14h30": 1, "15h00": 1, "15h30": 1, "16h00": 1, "16h30": 1, "17h00": 1, "17h30": 1, "18h00": 1, "18h30": 1, "19h00": 1, "19h30": 1, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Thursday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 1, "06h30": 1, "07h00": 1, "07h30": 1, "08h00": 1, "08h30": 1, "09h00": 1, "09h30": 1, "10h00": 1, "10h30": 1, "11h00": 1, "11h30": 1, "12h00": 1, "12h30": 1, "13h00": 1, "13h30": 1, "14h00": 1, "14h30": 1, "15h00": 1, "15h30": 1, "16h00": 1, "16h30": 1, "17h00": 1, "17h30": 1, "18h00": 1, "18h30": 1, "19h00": 1, "19h30": 1, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Friday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 1, "06h30": 1, "07h00": 1, "07h30": 1, "08h00": 1, "08h30": 1, "09h00": 1, "09h30": 1, "10h00": 1, "10h30": 1, "11h00": 1, "11h30": 1, "12h00": 1, "12h30": 1, "13h00": 1, "13h30": 1, "14h00": 1, "14h30": 1, "15h00": 1, "15h30": 1, "16h00": 1, "16h30": 1, "17h00": 1, "17h30": 1, "18h00": 1, "18h30": 1, "19h00": 1, "19h30": 1, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Saturday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 0, "06h30": 0, "07h00": 0, "07h30": 0, "08h00": 0, "08h30": 0, "09h00": 0, "09h30": 0, "10h00": 0, "10h30": 0, "11h00": 0, "11h30": 0, "12h00": 0, "12h30": 0, "13h00": 0, "13h30": 0, "14h00": 0, "14h30": 0, "15h00": 0, "15h30": 0, "16h00": 0, "16h30": 0, "17h00": 0, "17h30": 0, "18h00": 0, "18h30": 0, "19h00": 0, "19h30": 0, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}, "Sunday": {"00h00": 0, "00h30": 0, "01h00": 0, "01h30": 0, "02h00": 0, "02h30": 0, "03h00": 0, "03h30": 0, "04h00": 0, "04h30": 0, "05h00": 0, "05h30": 0, "06h00": 0, "06h30": 0, "07h00": 0, "07h30": 0, "08h00": 0, "08h30": 0, "09h00": 0, "09h30": 0, "10h00": 0, "10h30": 0, "11h00": 0, "11h30": 0, "12h00": 0, "12h30": 0, "13h00": 0, "13h30": 0, "14h00": 0, "14h30": 0, "15h00": 0, "15h30": 0, "16h00": 0, "16h30": 0, "17h00": 0, "17h30": 0, "18h00": 0, "18h30": 0, "19h00": 0, "19h30": 0, "20h00": 0, "20h30": 0, "21h00": 0, "21h30": 0, "22h00": 0, "22h30": 0, "23h00": 0, "23h30": 0}}} \ No newline at end of file +{ + "utilisateur": { + "id": "admin", + "password": "9ac564e9a29c952a3ddeffd8635adb0c252dbd1d58f3efde265e028f7f808aa9" + }, + "PIN_ACTIF": true, + "CODE_PIN": "1234", + "NUM_AUTORISE": "670100036", + "TONE_DURATION": 2, + "DTMF_CODE": "*", + "DTMF_DURATION": 10, + "HORAIRES": { + "Monday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 1, + "h06_30": 1, + "h07_00": 1, + "h07_30": 1, + "h08_00": 1, + "h08_30": 1, + "h09_00": 1, + "h09_30": 1, + "h10_00": 1, + "h10_30": 1, + "h11_00": 1, + "h11_30": 1, + "h12_00": 1, + "h12_30": 1, + "h13_00": 1, + "h13_30": 1, + "h14_00": 1, + "h14_30": 1, + "h15_00": 1, + "h15_30": 1, + "h16_00": 1, + "h16_30": 1, + "h17_00": 1, + "h17_30": 1, + "h18_00": 1, + "h18_30": 1, + "h19_00": 1, + "h19_30": 1, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Tuesday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 1, + "h06_30": 1, + "h07_00": 1, + "h07_30": 1, + "h08_00": 1, + "h08_30": 1, + "h09_00": 1, + "h09_30": 1, + "h10_00": 1, + "h10_30": 1, + "h11_00": 1, + "h11_30": 1, + "h12_00": 1, + "h12_30": 1, + "h13_00": 1, + "h13_30": 1, + "h14_00": 1, + "h14_30": 1, + "h15_00": 1, + "h15_30": 1, + "h16_00": 1, + "h16_30": 1, + "h17_00": 1, + "h17_30": 1, + "h18_00": 1, + "h18_30": 1, + "h19_00": 1, + "h19_30": 1, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Wednesday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 1, + "h06_30": 1, + "h07_00": 1, + "h07_30": 1, + "h08_00": 1, + "h08_30": 1, + "h09_00": 1, + "h09_30": 1, + "h10_00": 1, + "h10_30": 1, + "h11_00": 1, + "h11_30": 1, + "h12_00": 1, + "h12_30": 1, + "h13_00": 1, + "h13_30": 1, + "h14_00": 1, + "h14_30": 1, + "h15_00": 1, + "h15_30": 1, + "h16_00": 1, + "h16_30": 1, + "h17_00": 1, + "h17_30": 1, + "h18_00": 1, + "h18_30": 1, + "h19_00": 1, + "h19_30": 1, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Thursday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 1, + "h06_30": 1, + "h07_00": 1, + "h07_30": 1, + "h08_00": 1, + "h08_30": 1, + "h09_00": 1, + "h09_30": 1, + "h10_00": 1, + "h10_30": 1, + "h11_00": 1, + "h11_30": 1, + "h12_00": 1, + "h12_30": 1, + "h13_00": 1, + "h13_30": 1, + "h14_00": 1, + "h14_30": 1, + "h15_00": 1, + "h15_30": 1, + "h16_00": 1, + "h16_30": 1, + "h17_00": 1, + "h17_30": 1, + "h18_00": 1, + "h18_30": 1, + "h19_00": 1, + "h19_30": 1, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Friday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 1, + "h06_30": 1, + "h07_00": 1, + "h07_30": 1, + "h08_00": 1, + "h08_30": 1, + "h09_00": 1, + "h09_30": 1, + "h10_00": 1, + "h10_30": 1, + "h11_00": 1, + "h11_30": 1, + "h12_00": 1, + "h12_30": 1, + "h13_00": 1, + "h13_30": 1, + "h14_00": 1, + "h14_30": 1, + "h15_00": 1, + "h15_30": 1, + "h16_00": 1, + "h16_30": 1, + "h17_00": 1, + "h17_30": 1, + "h18_00": 1, + "h18_30": 1, + "h19_00": 1, + "h19_30": 1, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Saturday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 0, + "h06_30": 0, + "h07_00": 0, + "h07_30": 0, + "h08_00": 0, + "h08_30": 0, + "h09_00": 0, + "h09_30": 0, + "h10_00": 0, + "h10_30": 0, + "h11_00": 0, + "h11_30": 0, + "h12_00": 0, + "h12_30": 0, + "h13_00": 0, + "h13_30": 0, + "h14_00": 0, + "h14_30": 0, + "h15_00": 0, + "h15_30": 0, + "h16_00": 0, + "h16_30": 0, + "h17_00": 0, + "h17_30": 0, + "h18_00": 0, + "h18_30": 0, + "h19_00": 0, + "h19_30": 0, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + }, + "Sunday": { + "h00_00": 0, + "h00_30": 0, + "h01_00": 0, + "h01_30": 0, + "h02_00": 0, + "h02_30": 0, + "h03_00": 0, + "h03_30": 0, + "h04_00": 0, + "h04_30": 0, + "h05_00": 0, + "h05_30": 0, + "h06_00": 0, + "h06_30": 0, + "h07_00": 0, + "h07_30": 0, + "h08_00": 0, + "h08_30": 0, + "h09_00": 0, + "h09_30": 0, + "h10_00": 0, + "h10_30": 0, + "h11_00": 0, + "h11_30": 0, + "h12_00": 0, + "h12_30": 0, + "h13_00": 0, + "h13_30": 0, + "h14_00": 0, + "h14_30": 0, + "h15_00": 0, + "h15_30": 0, + "h16_00": 0, + "h16_30": 0, + "h17_00": 0, + "h17_30": 0, + "h18_00": 0, + "h18_30": 0, + "h19_00": 0, + "h19_30": 0, + "h20_00": 0, + "h20_30": 0, + "h21_00": 0, + "h21_30": 0, + "h22_00": 0, + "h22_30": 0, + "h23_00": 0, + "h23_30": 0 + } + } +} diff --git a/src/app.py b/src/app.py index e4e196c..71a9f84 100644 --- a/src/app.py +++ b/src/app.py @@ -21,6 +21,7 @@ from src.config import DefaultConfig from src.auth import auth from src.account import account from src.params import params +from src.schedule import schedule from src.log import log as logs ######################################################### @@ -132,5 +133,5 @@ def configure_blueprints(app=None): :return None: ''' - for bp in [auth, logs, account, params]: + for bp in [auth, logs, account, params, schedule]: app.register_blueprint(bp) diff --git a/src/schedule/__init__.py b/src/schedule/__init__.py new file mode 100644 index 0000000..177a9db --- /dev/null +++ b/src/schedule/__init__.py @@ -0,0 +1,6 @@ +# -*- encoding: utf-8 -*- + +# @author : vincent.benoit@benserv.fr +# @brief : schedule views and models + +from .views import schedule diff --git a/src/schedule/views.py b/src/schedule/views.py new file mode 100644 index 0000000..c011f18 --- /dev/null +++ b/src/schedule/views.py @@ -0,0 +1,129 @@ +# -*- encoding: utf-8 -*- + +# @author : vincent.benoit@benserv.fr +# @brief : Scheduler routes + +######################################################### +# Importation de modules externes # + +import sys, re, os +import logging as log +from datetime import datetime, timezone + +from flask import Flask, Blueprint, request, abort, jsonify, current_app +from flask_api import status +from flask_jwt_extended import create_access_token +from flask_jwt_extended import get_jwt +from flask_jwt_extended import set_access_cookies +from flask_jwt_extended import unset_jwt_cookies +from flask_jwt_extended import get_jwt_identity +from flask_jwt_extended import jwt_required + +import json +import shutil +import hashlib +from werkzeug.exceptions import HTTPException + +######################################################### +# Class et Methods # + +schedule = Blueprint('schedule', __name__, url_prefix='/api/configurateur') + +@schedule.errorhandler(HTTPException) +def handle_exception(e): + ''' return JSON instead of HTML for HTTP errors ''' + response = e.get_response() + # replace the body with JSON + response.data = json.dumps({ + 'code': e.code, + 'name': e.name, + 'description': e.description, + }) + response.content_type = "application/json" + return response + +@schedule.after_request +def refresh_expiring_tokens(response): + ''' Using an 'after_request' callback, we refresh any token that is within + 30 minutes of expiring.''' + try: + exp_timestamp = get_jwt()['exp'] + now = datetime.now(timezone.utc) + target_timestamp = datetime.timestamp(now + current_app.config['DELTA']) + if target_timestamp > exp_timestamp: + current_app.logger.warning("On doit recréer un token JWT ....") + access_token = create_access_token(identity=get_jwt_identity()) + # refresh token in storage place + if os.path.exists(os.path.join("/tmp", current_app.config['PROJECT'])): + with open(os.path.join("/tmp", current_app.config['PROJECT'], get_jwt_identity()['id']), 'w') as f: + f.write(access_token) + # Modifiy a Flask Response to set a cookie containing the access JWT. + set_access_cookies(response, access_token) + return response + except (RuntimeError, KeyError): + return response + +@schedule.route('/scheduler', methods=['GET']) +@jwt_required() +def retreive_scheduler(): + ''' Récupération des paramètres de l'intercom + ''' + current_app.logger.info("Récupération des paramètres de l'intercom") + current_user = get_jwt_identity() + # load data from JSON database + with open(current_app.config['DB_PATH'], 'r') as f: + data = json.load(f) + content = [] + if 'HORAIRES' in data: + for day in data['HORAIRES']: + content.append({'name':day, 'horaires':data['HORAIRES'][day]}) + current_app.logger.debug("{}".format(content)) + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="horaire manquant en base de données") + + return content, status.HTTP_200_OK + +@schedule.route('/update_scheduler', methods=['POST']) +@jwt_required() +def update_scheduler(): + ''' Mise à jour des paramètres de l'intercom + ''' + current_app.logger.info("Mise à jour des paramètres de l'intercom") + current_user = get_jwt_identity() + # recuperation des attributs JSON de la requete + data_req = request.get_json() + current_app.logger.debug("request: {}".format(data_req)) + # load data from JSON database + with open(current_app.config['DB_PATH'], 'r') as f: + data = json.load(f) + + if 'pin_actif' in data_req: + data['PIN_ACTIF'] = data_req['pin_actif'] + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + if 'code_pin' in data_req: + data['CODE_PIN'] = data_req['code_pin'] + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + if 'num_autorized' in data_req: + data['NUM_AUTORISE'] = data_req['num_autorized'] + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + if 'tone_duration' in data_req: + data['TONE_DURATION'] = int(data_req['tone_duration']) + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + if 'dtmf_code' in data_req: + data['DTMF_CODE'] = data_req['dtmf_code'] + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + if 'dtmf_duration' in data_req: + data['DTMF_DURATION'] = int(data_req['dtmf_duration']) + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant") + + with open(current_app.config['DB_PATH'], 'w') as f: + json.dump(data, f) + content = {'message':'maj parameters successful!'} + return content, status.HTTP_200_OK +