From 3060dee504da6c7f3d036ad9a2f55cfa09ee82d3 Mon Sep 17 00:00:00 2001 From: Untriex Programming Date: Thu, 4 Mar 2021 18:36:21 +0100 Subject: [PATCH] Log system/sensors update --- .gitignore | 1 + engine.py | 37 +++++++++++++++++++++++++++++++++++++ log.txt | 0 main.py | 51 +++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 engine.py create mode 100644 log.txt diff --git a/.gitignore b/.gitignore index 3107f59..49030a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* .venv/* ./__pycache__ +*.pyc diff --git a/engine.py b/engine.py new file mode 100644 index 0000000..85e8ed2 --- /dev/null +++ b/engine.py @@ -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}") \ No newline at end of file diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py index 3b1b4cc..5257884 100644 --- a/main.py +++ b/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} \ No newline at end of file +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