diff --git a/server/filesystem.json b/server/filesystem.json index 2031326..c64f093 100644 --- a/server/filesystem.json +++ b/server/filesystem.json @@ -1,22 +1,22 @@ { - "ID": 0, - "location": "izba", - "descrpition": { - "title": "legionrpi", - "description_s": "krátky popis, ktorý bude zobrazený iba v náhladovom okne", - "description_l": "dlhší popis zariadenia, ktorý bude zobrazený po otvorení", - "photo_s": "mala_fotka.png", - "photo_b": "velka fotka.png" - }, - "files": [ - { - "name": "test", - "format": ".jpg", - "description": "toto je jpg test file" - }, { - "name": "test2", - "format": ".txt", - "description": "toto je txt test file" - } - ] + "ID": "0", + "location": "izba", + "description": { + "title": "legionrpi", + "description_s": "krátky popis, ktorý bude zobrazený iba v náhladovom okne", + "description_l": "dlhší popis zariadenia, ktorý bude zobrazený po otvorení", + "photo_s": "mala_fotka.png", + "photo_b": "velka fotka.png" + }, + "files": [ + { + "name": "test", + "format": ".jpg", + "description": "toto je jpg test file" + }, { + "name": "test2", + "format": ".txt", + "description": "toto je txt test file" + } + ] } diff --git a/server/main.py b/server/main.py index 42a6f21..c60cdfa 100644 --- a/server/main.py +++ b/server/main.py @@ -1,13 +1,15 @@ -from fastapi import FastAPI, Request -from fastapi.responses import FileResponse, HTMLResponse -from pydantic import BaseModel -import engine -import requests -import time +import hashlib import json import os import threading -import hashlib +import time +import engine +import requests +import uuid +from fastapi import FastAPI, Request +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import FileResponse +from pydantic import BaseModel with open("settings.json", "r") as f: # loading settings settings = json.load(f) @@ -21,6 +23,16 @@ location = settings["location"] time_to_save = settings["time_to_save"] app = FastAPI() # init of FastAPI + +origins = ["*", ] +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + log = engine.Log(settings["log"]) # init of LOG update = engine.Update() offline = [] @@ -39,6 +51,8 @@ sensors = { # List of "live" data like tempeature, etc. "doba čakania": 2 } +messages = [] + heartbeat_table["ID"].append(ID) heartbeat_table["IP"].append(IP) heartbeat_table["location"].append(location) @@ -46,8 +60,6 @@ heartbeat_table["file_system"].append(filesystem) heartbeat_table["last_heartbeat"].append(time_to_heartbeat) -# Todo better "host" ID handeling - class ServerTable(BaseModel): # table of content for heartbeat request ID: list IP: list @@ -56,6 +68,11 @@ class ServerTable(BaseModel): # table of content for heartbeat request last_heartbeat: list +@app.get("/") +def read_root(): + return {"Hello": "World"} + + @app.post("/heartbeat") def heartbeat(s_table: ServerTable, request: Request): log.message(f"server requested heartbeat {request.client.host}:{request.client.port}") @@ -141,7 +158,7 @@ def comparision(file: str): @app.get("/devices_list") def get_devices_list(): - return heartbeat_table["file_system"] + return [{"connected_id": ID}, *heartbeat_table["file_system"]] @app.get("/admin/{command}") @@ -152,6 +169,25 @@ def admin(command: str): os.system(f"""python3 system.py update -version {command.split("-")[1]}""") +@app.get("/messages/get") +def get_messages(m_from: int = 0, m_to: int = 10): + return messages[m_from:m_to] + + +@app.get("/messages/reqister") +def get_messages(): + return uuid.uuid4().hex[24:] + + +@app.post("/messages/post") +def get_messages(m_sender: str = None, message: str = None): + if m_sender and message: + messages.append({"sender": m_sender, "message": message}) + return "successfull" + else: + return "Empty message/sender" + + def send_heartbeat(ip, id): global heartbeat_table log.message(f"""sending heartbeat to {ip}({"offline" if id in offline else "online"})""") @@ -187,7 +223,8 @@ def mainloop(): log.message(f"""{heartbeat_table["IP"][device_number]} gone online""") heartbeat_table["last_heartbeat"][int(device_number)] = int(time_to_heartbeat) + 5 try: - log.debug(f"""{device_ID} : time to heartbeat : {heartbeat_table["last_heartbeat"][device_number]}""") + log.debug( + f"""{device_ID} : time to heartbeat : {heartbeat_table["last_heartbeat"][device_number]}""") heartbeat_table["last_heartbeat"][device_number] -= 1 except IndexError: pass @@ -204,5 +241,4 @@ def mainloop(): thread_1 = threading.Thread(target=mainloop, daemon=True) thread_1.start() -# Todo new filesystem handeling -# Todo settings for easy adding/editing files/id/text \ No newline at end of file +# Todo settings for easy adding/editing files/id/text diff --git a/server/server.zip b/server/server.zip new file mode 100644 index 0000000..7c03be4 Binary files /dev/null and b/server/server.zip differ diff --git a/server/settings.json b/server/settings.json index ebf1394..2ae1bee 100644 --- a/server/settings.json +++ b/server/settings.json @@ -4,6 +4,8 @@ "location": "izba", "time_to_heartbeat": 20, "time_to_heartbeat_offline": 25, + "save_table": false, + "time_to_save": 60, "log": { "save_error": true, "print_error": true, @@ -19,5 +21,5 @@ "location": [], "file_system": [], "last_heartbeat": [] -} -} + } +} \ No newline at end of file diff --git a/server/test.py b/server/test.py new file mode 100644 index 0000000..9410f45 --- /dev/null +++ b/server/test.py @@ -0,0 +1,24 @@ +import threading +import uuid +import time + +zoznam = [] +rovnake = 0 +run = True +start = time.time() + +def generate(): + global zoznam, rovnake, run + while run: + cache = uuid.uuid4().hex[24:] + if cache in zoznam: + rovnake += 1 + zoznam.append(cache) + pocet = len(zoznam) + print(f"{pocet} : {rovnake} rovnakých - {cache}") + if pocet > 50000: + break + + +generate() +print(f"process lasted {time.time()-start}") \ No newline at end of file diff --git a/server/version.json b/server/version.json index 0d562bd..e18780f 100644 --- a/server/version.json +++ b/server/version.json @@ -1,5 +1,5 @@ { - "version": "0.5", - "id": 3, + "version": "0.6", + "id": 4, "url": "https://raw.githubusercontent.com/UntriexTv/test_directory/main/ver.json" }