Initial commit, Json formatting, engine, user, producer und meter erstellt.
This commit is contained in:
79
client/JsonHandling.py
Normal file
79
client/JsonHandling.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import json
|
||||
import os
|
||||
import serverrequests as serverrequests
|
||||
import JsonHandling
|
||||
import energymeter
|
||||
import energyproducer
|
||||
import energyuser
|
||||
import datetime
|
||||
|
||||
class JsonHandling:
|
||||
|
||||
@staticmethod
|
||||
def check_config_has_changed(json_data):
|
||||
data = json.loads(json_data)
|
||||
return data["metadata"]["config_has_changed"] == "1"
|
||||
|
||||
|
||||
class DataCollector:
|
||||
def __init__(self):
|
||||
self.data = {"timestamps": []}
|
||||
self.now = datetime.datetime.now()
|
||||
|
||||
|
||||
def collect_and_store_data(self, producer_values, meter_values, user_values):
|
||||
|
||||
print("a")
|
||||
if self.now.minute == datetime.datetime.now().minute:
|
||||
print("b")
|
||||
|
||||
return
|
||||
self.now = datetime.datetime.now()
|
||||
print("cccccccc")
|
||||
|
||||
timestamp = self.now.strftime("%Y-%m-%d %H:%M")
|
||||
self.data["timestamps"].append(timestamp)
|
||||
# Werte für Produzenten hinzufügen
|
||||
for producer_id, values in producer_values.items():
|
||||
if producer_id not in self.data:
|
||||
self.data[producer_id] = {}
|
||||
self.data[producer_id]["values"] = self.update_values(self.data[producer_id].get("values", {}), values)
|
||||
|
||||
# Werte für Meter hinzufügen
|
||||
for meter_id, values in meter_values.items():
|
||||
if meter_id not in self.data:
|
||||
self.data[meter_id] = {}
|
||||
self.data[meter_id]["values"] = self.update_values(self.data[meter_id].get("values", {}), values)
|
||||
|
||||
# Werte für Benutzer hinzufügen
|
||||
for user_id, values in user_values.items():
|
||||
if user_id not in self.data:
|
||||
self.data[user_id] = {}
|
||||
self.data[user_id]["values"] = self.update_values(self.data[user_id].get("values", {}), values)
|
||||
|
||||
# Begrenzung der Daten auf die letzten 24 Stunden (1440 Minuten)
|
||||
if len(self.data["timestamps"]) > 1440:
|
||||
self.data["timestamps"].pop(0)
|
||||
for key in self.data.keys():
|
||||
if key != "timestamps":
|
||||
for value_key in self.data[key]["values"]:
|
||||
self.data[key]["values"][value_key].pop(0)
|
||||
|
||||
|
||||
if os.path.exists("data.json"):
|
||||
with open("data.json", "r") as file:
|
||||
existing_data = json.load(file)
|
||||
existing_data.update(self.data)
|
||||
self.data = existing_data
|
||||
|
||||
# Daten als JSON speichern
|
||||
with open("data.json", "w") as file:
|
||||
json.dump(self.data, file)
|
||||
|
||||
|
||||
def update_values(self, current_values, new_values):
|
||||
for key, value in new_values.items():
|
||||
if key not in current_values:
|
||||
current_values[key] = []
|
||||
current_values[key].append(value)
|
||||
return current_values
|
||||
Reference in New Issue
Block a user