File size: 2,940 Bytes
9502e19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from flask import *
from base64 import b64decode, b64encode
from time import ctime, sleep, time
from threading import Thread
import gzip, requests
from getFreeRoomsFromAde2 import AdeRequest
            
Ade = AdeRequest()
infos = Ade.getRoomsInfos()
freeRooms = Ade.getCurrentsFreeRooms()



def import_allowed():
    tab = []
    
    for room in freeRooms:
        tab.append([room, freeRooms[room]["freeUntil"]])
    return tab

def import_response_data():
    tab = []
    
    for room in freeRooms:
        tab.append([room, freeRooms[room]["capacity"], freeRooms[room]["freeUntil"], freeRooms[room]["busy"]])
    return tab

app = Flask(__name__)
app.config['response_data'] = import_response_data() # [numRoom: str, capacity: int, freeUntil: str, busy: tuple]
app.config['allowed'] = import_allowed() # [numRoom: str, freeUtil: str]

def reloadData():
    while True:
        try:
            sleep(600)
            infos = Ade.getRoomsInfos()
            freeRooms = Ade.getCurrentsFreeRooms()
            app.config['response_data'] = import_response_data() # [numRoom: str, capacity: int, freeUntil: str, busy: tuple]
            app.config['allowed'] = import_allowed() # [numRoom: str, freeUtil: str]
            print("[+] Refresh Data From Ade")
            
        except Exception as e:
            print("Err. When reload:", e)
            

Thread(target=reloadData).start()


@app.route('/')
def index():
    prepPage = ""
    deux = ""
    for ip in app.config["allowed"]:
        prepPage += "<div class=\"ip-item" + deux + "\" onclick=\"PrintResponse(this, '" + ip[0] +"');\">\n"
        ipPadded = ip[0] + ((14 - len(ip[0])) * "&nbsp;")
        prepPage += "<p>Salle n°" + ipPadded + "</p>&nbsp;\n"
        prepPage += "<p>Disponible jusqu'à -> " + ip[1] + "</p>&nbsp;\n"
        
        prepPage += "</div>\n"
        if deux == "":
            deux = "2"
        else:
            deux = ""
    res = open("templates/index.html", "r").read().replace("=====cnt=====", prepPage).replace("\\=====HOME-IP_Addr=====\\", "Get Free Rooms From Ade").encode()
    resp = Response(gzip.compress(res))
    resp.headers['Content-Encoding'] = 'gzip'
    return resp


@app.route("/responsesFrom/<ip>")
def responsesFrom(ip):
    def busyUntil(tab):
        ts, te = tab
        return f'- {str(ts[0]).zfill(2)}h{str(ts[1]).zfill(2)} à {str(te[0]).zfill(2)}h{str(te[1]).zfill(2)}\n'
    responses = "\r\n\r\n"
    for resp in app.config['response_data']:
        if resp[0] == ip:
            responses += str(ip) + "    capacité: " + str(resp[1]) + "    disponible jusqu'à: " + str(resp[2])
            if resp[3] != []:
                responses  += "\noccupée durrant:\n" + str("".join(busyUntil(x) for x in resp[3])) + "\r\n\r\n"
    resp = Response(gzip.compress(responses.encode()))
    resp.headers['Content-Encoding'] = 'gzip'
    return resp



if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)