diff --git a/server/log.txt b/server/log.txt index 98dfe6e..3aa293a 100644 --- a/server/log.txt +++ b/server/log.txt @@ -156,4 +156,8 @@ 2021-04-14 14:09:58.696666 -> Warning: Address(host='192.168.1.99', port=58182) created new sensor. SENSOR: name='sensor1' value='test' 2021-04-14 14:10:06.190377 -> Warning: Address(host='192.168.1.99', port=58192) created new sensor. - SENSOR: name='sensor2' value='123456789' \ No newline at end of file + SENSOR: name='sensor2' value='123456789' +2021-04-22 10:15:53.540044 -> Warning: Address(host='192.168.1.99', port=43642) created new sensor. + SENSOR: name='test1' value='juhuu' +2021-04-22 10:16:02.865022 -> Warning: Address(host='192.168.1.99', port=43644) created new sensor. + SENSOR: name='test2' value='eeeh' \ No newline at end of file diff --git a/server/main.py b/server/main.py index f1ec39e..4514300 100644 --- a/server/main.py +++ b/server/main.py @@ -75,6 +75,11 @@ class Sensor(BaseModel): value: str +class Message(BaseModel): + m_sender: str + message: str + + @app.get("/") def read_root(): return "wikispot" @@ -265,9 +270,12 @@ def register(): @app.post("/messages/post") -def get_messages(m_sender: str = None, message: str = None): - if m_sender and message: - messages.append({"sender": m_sender, "message": message, "timestamp": time.time()}) +def post_messages(data: Message): + log.debug(f"Message was posted. Sender: {data.m_sender}\n MESSAGE: {data.message}") + if len(messages) >= settings["max_mess"]: + del messages[:len(messages) - settings["max_mess"]] + if data.m_sender and data.message: + messages.append({"sender": data.m_sender, "message": data.message, "timestamp": time.time()}) return "successful" else: return "Empty message/sender" diff --git a/server/settings.json b/server/settings.json index ef576d3..3ef59d2 100644 --- a/server/settings.json +++ b/server/settings.json @@ -6,6 +6,7 @@ "time_to_heartbeat_offline": 25, "save_table": false, "time_to_save": 60, + "max_mess": 20, "log": { "save_error": true, "print_error": true, diff --git a/server/test.py b/server/test.py index e69de29..83f866d 100644 --- a/server/test.py +++ b/server/test.py @@ -0,0 +1,9 @@ +import requests +import time +send_n = 100 +wait_t = 0.5 + +for x in range(send_n): + r = requests.post("http://192.168.1.99:8000/messages/post", json={"m_sender": "server", "message": str(x)}) + print(f"Sent message {x+1}/{send_n}\nResponse: {r.text}") + time.sleep(wait_t) \ No newline at end of file