Log system/sensors update
This commit is contained in:
parent
db96998848
commit
3060dee504
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.idea/*
|
.idea/*
|
||||||
.venv/*
|
.venv/*
|
||||||
./__pycache__
|
./__pycache__
|
||||||
|
*.pyc
|
||||||
|
37
engine.py
Normal file
37
engine.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class Log():
|
||||||
|
def __init__(self, save_e=True, save_w=False, save_m=False, print_e=True, print_w=True, print_m=False, debug = False):
|
||||||
|
self.save_error = save_e
|
||||||
|
self.save_warning = save_w
|
||||||
|
self.save_messages = save_m
|
||||||
|
self.print_error = print_e
|
||||||
|
self.print_warning = print_w
|
||||||
|
self.print_messages = print_m
|
||||||
|
self.debug_e = debug
|
||||||
|
|
||||||
|
def error(self, error):
|
||||||
|
if self.print_error:
|
||||||
|
print(f"{datetime.now()} -> ERROR: {error}")
|
||||||
|
if self.save_error:
|
||||||
|
with open("log.txt", "a") as file:
|
||||||
|
file.write(f"\n{datetime.now()} -> ERROR: {error}")
|
||||||
|
|
||||||
|
def warning(self, warning):
|
||||||
|
if self.print_warning:
|
||||||
|
print(f"{datetime.now()} -> Warning: {warning}")
|
||||||
|
if self.save_warning:
|
||||||
|
with open("log.txt", "a") as file:
|
||||||
|
file.write(f"\n{datetime.now()} -> Warning: {warning}")
|
||||||
|
|
||||||
|
def message(self, message):
|
||||||
|
if self.print_messages:
|
||||||
|
print(f"{datetime.now()} -> message: {message}")
|
||||||
|
if self.save_messages:
|
||||||
|
with open("log.txt", "a") as file:
|
||||||
|
file.write(f"\n{datetime.now()} -> message: {message}")
|
||||||
|
|
||||||
|
def debug(self, debug):
|
||||||
|
if self.debug_e:
|
||||||
|
print(f"{datetime.now()} -> DEBUG: {debug}")
|
0
log.txt
Normal file
0
log.txt
Normal file
51
main.py
51
main.py
@ -1,7 +1,17 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Request
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
import engine
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
sensors = {
|
||||||
|
"teplota": 24,
|
||||||
|
"vlhkosť": 25,
|
||||||
|
"počet ľudí": 10,
|
||||||
|
"doba čakania": 2
|
||||||
|
}
|
||||||
|
log = engine.Log(print_m=True, debug=True)
|
||||||
|
|
||||||
location = "izba"
|
location = "izba"
|
||||||
ID = 55
|
ID = 55
|
||||||
IP = "192.168.1.25"
|
IP = "192.168.1.25"
|
||||||
@ -28,16 +38,29 @@ class Server_table(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
@app.post("/heartbeat")
|
@app.post("/heartbeat")
|
||||||
def heartbeat(s_table: Server_table):
|
def heartbeat(s_table: Server_table, request: Request):
|
||||||
for position, server_id in enumerate(s_table.ID):
|
log.message(f"heartbeat requested: {request.client.host}:{request.client.port}")
|
||||||
if server_id in heartbeat_table["ID"]:
|
log.debug(f"Recieved server table: {s_table}")
|
||||||
if heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] > s_table.last_heartbeat[position]:
|
try:
|
||||||
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] = s_table.last_heartbeat[position]
|
for position, server_id in enumerate(s_table.ID):
|
||||||
else:
|
if server_id in heartbeat_table["ID"]:
|
||||||
heartbeat_table["ID"].append(s_table.ID[position])
|
if heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] > s_table.last_heartbeat[
|
||||||
heartbeat_table["IP"].append(s_table.IP[position])
|
position]:
|
||||||
heartbeat_table["location"].append(s_table.location[position])
|
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] = s_table.last_heartbeat[
|
||||||
heartbeat_table["file_system"].append(s_table.file_system[position])
|
position]
|
||||||
heartbeat_table["last_heartbeat"].append(s_table.last_heartbeat[position])
|
else:
|
||||||
print(heartbeat_table)
|
heartbeat_table["ID"].append(s_table.ID[position])
|
||||||
return heartbeat_table, {"ID": ID, "file_system": filesystem, "location": location}
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user