Mabasej_Team/main.py

109 lines
3.9 KiB
Python
Raw Normal View History

2021-03-04 18:36:21 +01:00
from fastapi import FastAPI, Request
2021-03-04 12:13:46 +01:00
from pydantic import BaseModel
2021-03-04 18:36:21 +01:00
import engine
import requests
import time
import json
import threading
2021-03-04 12:13:46 +01:00
app = FastAPI()
2021-03-04 18:36:21 +01:00
sensors = {
"teplota": 24,
"vlhkosť": 25,
"počet ľudí": 10,
"doba čakania": 2
}
log = engine.Log(print_m=True, debug=True)
time_to_heartbeat = 60 #Seconds
location = "2"
ID = 2
IP = "192.168.1.99"
2021-03-04 12:13:46 +01:00
filesystem = {
"otvaracie_hod": ["t", {"pon": "10-25"}, {"uto": "10-25"}],
"prehliadka": ["pdf", "/files/prehliadka.pdf"],
"fotky_hrad": ["png_z", ["/files/hrad1.png", "/files/hrad2.png"]]
}
heartbeat_table = {
"ID": [1],
"IP": ["192.168.1.231"],
"location": ["1"],
"file_system": ["x"],
"last_heartbeat": [7]
2021-03-04 12:13:46 +01:00
}
heartbeat_table["ID"].append(ID)
heartbeat_table["IP"].append(IP)
heartbeat_table["location"].append(location)
heartbeat_table["file_system"].append(filesystem)
heartbeat_table["last_heartbeat"].append(time_to_heartbeat)
# Todo better "host" ID handeling
2021-03-04 12:13:46 +01:00
class Server_table(BaseModel):
ID: list
IP: list
location: list
file_system: list
last_heartbeat: list
@app.post("/heartbeat")
2021-03-04 18:36:21 +01:00
def heartbeat(s_table: Server_table, request: Request):
log.message(f"heartbeat requested: {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"]:
if heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] < \
2021-03-05 19:09:00 +01:00
s_table.last_heartbeat[position]:
2021-03-04 18:36:21 +01:00
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] = s_table.last_heartbeat[
position]
log.debug(f"updated {server_id}`s heartbeat to {s_table.last_heartbeat[position]}")
#Todo update filesystem too. Now updating only last heartbeat
elif server_id == ID:
log.debug(f"Updated my heartbeat from {s_table.last_heartbeat[position]} to {time_to_heartbeat}")
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(ID)] = time_to_heartbeat
2021-03-04 18:36:21 +01:00
else:
heartbeat_table["ID"].append(s_table.ID[position])
heartbeat_table["IP"].append(s_table.IP[position])
heartbeat_table["location"].append(s_table.location[position])
heartbeat_table["file_system"].append(s_table.file_system[position])
heartbeat_table["last_heartbeat"].append(s_table.last_heartbeat[position])
except Exception as error:
log.error(f"heartbeat > {error}")
return heartbeat_table, {"ID": ID, "file_system": filesystem, "location": location}
@app.get("/sensors")
def get_sensors(request: Request):
log.message(f"sensor data sent to {request.client.host}:{request.client.port}")
log.debug(f"sensor data: {sensors}")
return sensors
2021-03-05 19:09:00 +01:00
@app.get("/files/{file}")
def get_file(file: str):
pass
#Todo Get files function for client (phone/ther rpi)
2021-03-05 19:09:00 +01:00
def send_heartbeat(ip):
log.message(f"requesting heartbeat from {ip}")
cache_request = requests.post(f"http://{ip}:8000/heartbeat", data=json.dumps(heartbeat_table))
log.debug(json.dumps(cache_request.json(), indent=4))
def mainloop():
while True:
for device_number, device_ID in enumerate(heartbeat_table["ID"]):
if device_ID != ID:
if heartbeat_table["last_heartbeat"][device_number] < 0:
send_heartbeat(heartbeat_table["IP"][device_number])
heartbeat_table["last_heartbeat"][device_number] = time_to_heartbeat + 5
print(f"""{device_ID} : time to heartbeat : {heartbeat_table["last_heartbeat"][device_number]}""")
heartbeat_table["last_heartbeat"][device_number] -= 1
time.sleep(1)
2021-03-05 19:09:00 +01:00
thread_1 = threading.Thread(target=mainloop, daemon=True)
thread_1.start()