Log system/sensors update

This commit is contained in:
Untriex Programming 2021-03-04 18:36:21 +01:00
parent db96998848
commit 3060dee504
4 changed files with 75 additions and 14 deletions

1
.gitignore vendored

@ -1,3 +1,4 @@
.idea/*
.venv/*
./__pycache__
*.pyc

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

51
main.py

@ -1,7 +1,17 @@
from fastapi import FastAPI
from fastapi import FastAPI, Request
from pydantic import BaseModel
import engine
app = FastAPI()
sensors = {
"teplota": 24,
"vlhkosť": 25,
"počet ľudí": 10,
"doba čakania": 2
}
log = engine.Log(print_m=True, debug=True)
location = "izba"
ID = 55
IP = "192.168.1.25"
@ -28,16 +38,29 @@ class Server_table(BaseModel):
@app.post("/heartbeat")
def heartbeat(s_table: Server_table):
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)] > s_table.last_heartbeat[position]:
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] = s_table.last_heartbeat[position]
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])
print(heartbeat_table)
return heartbeat_table, {"ID": ID, "file_system": filesystem, "location": location}
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)] > s_table.last_heartbeat[
position]:
heartbeat_table["last_heartbeat"][heartbeat_table["ID"].index(server_id)] = s_table.last_heartbeat[
position]
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