Update of files
!not tested! merged because of addon of app
This commit is contained in:
parent
1df6405a3d
commit
169abb06ca
@ -1,7 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class Log():
|
||||
class Log:
|
||||
def __init__(self, settings=None):
|
||||
if settings is None:
|
||||
settings = {"save_error": True, "print_error": True, "save_warning": True, "print_warning": True,
|
||||
|
22
filesystem.json
Normal file
22
filesystem.json
Normal file
@ -0,0 +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"
|
||||
}
|
||||
]
|
||||
}
|
3
log.txt
3
log.txt
@ -0,0 +1,3 @@
|
||||
|
||||
2021-03-15 10:13:26.660898 -> Warning: 192.168.1.232 disconnected/is not available
|
||||
2021-03-15 11:23:33.589998 -> Warning: 192.168.1.231 disconnected/is not available
|
53
main.py
53
main.py
@ -7,20 +7,22 @@ import time
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import hashlib
|
||||
|
||||
with open("settings.json", "r") as f: #loading settings
|
||||
with open("settings.json", "r") as f: # loading settings
|
||||
settings = json.load(f)
|
||||
|
||||
IP = settings["IP"]
|
||||
ID = settings["ID"]
|
||||
location = settings["location"]
|
||||
|
||||
app = FastAPI() #init of FastAPI
|
||||
app = FastAPI() # init of FastAPI
|
||||
log = engine.Log(settings["log"]) # init of LOG
|
||||
offline = []
|
||||
|
||||
time_to_heartbeat = settings["time_to_heartbeat"] # Raspberry will be requesting heartbeat every __ seconds
|
||||
time_to_heartbeat_offline = settings["time_to_heartbeat_offline"] # Raspberry will be requesting heartbeat every __ seconds from offline rpi
|
||||
time_to_heartbeat_offline = settings[
|
||||
"time_to_heartbeat_offline"] # Raspberry will be requesting heartbeat every __ seconds from offline rpi
|
||||
|
||||
# json variables
|
||||
filesystem = { # Here will be files saved on this raspberry
|
||||
@ -45,7 +47,7 @@ heartbeat_table["last_heartbeat"].append(time_to_heartbeat)
|
||||
|
||||
# Todo better "host" ID handeling
|
||||
|
||||
class Server_table(BaseModel): # table of content for heartbeat request
|
||||
class ServerTable(BaseModel): # table of content for heartbeat request
|
||||
ID: list
|
||||
IP: list
|
||||
location: list
|
||||
@ -54,9 +56,10 @@ class Server_table(BaseModel): # table of content for heartbeat request
|
||||
|
||||
|
||||
@app.post("/heartbeat")
|
||||
def heartbeat(s_table: Server_table, request: Request):
|
||||
def heartbeat(s_table: ServerTable, request: Request):
|
||||
log.message(f"server requested heartbeat {request.client.host}:{request.client.port}")
|
||||
log.debug(f"Recieved server table: {s_table}")
|
||||
|
||||
try:
|
||||
for position, server_id in enumerate(s_table.ID):
|
||||
if server_id in heartbeat_table["ID"]:
|
||||
@ -78,9 +81,11 @@ def heartbeat(s_table: Server_table, request: Request):
|
||||
heartbeat_table["last_heartbeat"].append(s_table.last_heartbeat[position])
|
||||
except Exception as error:
|
||||
log.error(f"heartbeat > {error}")
|
||||
|
||||
if heartbeat_table["ID"][heartbeat_table["IP"].index(request.client.host)] in offline:
|
||||
offline.remove(heartbeat_table["ID"][heartbeat_table["IP"].index(request.client.host)])
|
||||
log.message(f"{request.client.host} gone online")
|
||||
|
||||
return heartbeat_table, {"ID": ID, "file_system": filesystem, "location": location}
|
||||
|
||||
|
||||
@ -93,21 +98,26 @@ def get_sensors(request: Request):
|
||||
|
||||
@app.get("/files/{IDx}/{file}")
|
||||
def get_file(IDx: int, file: str):
|
||||
server_ip = heartbeat_table["IP"][heartbeat_table["ID"].index(IDx)]
|
||||
if IDx == ID:
|
||||
return FileResponse(f"files/{file}")
|
||||
elif IDx in heartbeat_table["ID"]:
|
||||
r = requests.get(
|
||||
f"""http://{heartbeat_table["IP"][heartbeat_table["ID"].index(IDx)]}:8000/files/{IDx}/{file}""")
|
||||
r.encoding = "utf-8"
|
||||
if os.path.isdir(f"cache/{IDx}"):
|
||||
if os.path.isfile(f"cache/{IDx}/{file}"):
|
||||
pass
|
||||
# Todo cache time to live/compare files on server and cache with not resource heavy function
|
||||
with open(f"cache/{IDx}/{file}", "rb") as compared_file:
|
||||
m = hashlib.md5()
|
||||
for line in compared_file:
|
||||
m.update(line)
|
||||
rr = requests.get(f"""http://{server_ip}:8000/compare/{file}""")
|
||||
if rr.text.strip('"') != str(m.hexdigest()):
|
||||
log.message(f"{file} on server {server_ip} is changed.")
|
||||
else:
|
||||
with open(f"cache/{IDx}/{file}", "wb") as save:
|
||||
save.write(bytes(r.content))
|
||||
log.debug(f"returning cached file cache/{IDx}{file}")
|
||||
return FileResponse(f"cache/{IDx}/{file}")
|
||||
else:
|
||||
os.mkdir(f"cache/{IDx}")
|
||||
log.message(f"downloading {file} from {server_ip}")
|
||||
r = requests.get(f"http://{server_ip}:8000/files/{IDx}/{file}")
|
||||
with open(f"cache/{IDx}/{file}", "wb") as save:
|
||||
save.write(bytes(r.content))
|
||||
return FileResponse(f"cache/{IDx}/{file}")
|
||||
@ -119,6 +129,20 @@ def update_sensors():
|
||||
# Todo Make option to upload "live data" manually to rpi
|
||||
|
||||
|
||||
@app.get("/compare/{file}")
|
||||
def comparision(file: str):
|
||||
with open(f"files/{file}", "rb") as compared_file:
|
||||
m = hashlib.md5()
|
||||
for line in compared_file:
|
||||
m.update(line)
|
||||
return m.hexdigest()
|
||||
|
||||
|
||||
@app.get("/devices_list")
|
||||
def get_devices_list():
|
||||
return heartbeat_table
|
||||
|
||||
|
||||
def send_heartbeat(ip, id):
|
||||
global heartbeat_table
|
||||
log.message(f"""sending heartbeat to {ip}({"offline" if id in offline else "online"})""")
|
||||
@ -153,4 +177,7 @@ thread_1 = threading.Thread(target=mainloop, daemon=True)
|
||||
thread_1.start()
|
||||
|
||||
# Todo in next release: disconnect offline client after set time
|
||||
# Todo better formating code + comments
|
||||
# Todo send to mobile
|
||||
# Todo new filesystem handeling
|
||||
# Todo implement update system
|
||||
# Todo settings for easy adding/editing files/id/text
|
@ -12,12 +12,5 @@
|
||||
"save_message": false,
|
||||
"print_message": true,
|
||||
"enable_debug": false
|
||||
},
|
||||
"heartbeat_table": {
|
||||
"ID": [],
|
||||
"IP": [],
|
||||
"location": [],
|
||||
"file_system": [],
|
||||
"last_heartbeat": []
|
||||
}
|
||||
}
|
||||
}
|
4
version.json
Normal file
4
version.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"version": "0.1",
|
||||
"type": "Alpha"
|
||||
}
|
Loading…
Reference in New Issue
Block a user