This commit is contained in:
Untriex Programming 2021-04-01 12:08:36 +02:00
parent 4161f49870
commit 79b5537440
6 changed files with 99 additions and 37 deletions

@ -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"
}
]
}

@ -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
# Todo settings for easy adding/editing files/id/text

BIN
server/server.zip Normal file

Binary file not shown.

@ -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": []
}
}
}
}

24
server/test.py Normal file

@ -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}")

@ -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"
}