added inserting inverter unique identifier to separate DB collection

main
Jan Beníček 2024-07-17 09:47:23 +02:00
parent 260ce4efec
commit dfb858fb77
1 changed files with 13 additions and 3 deletions

View File

@ -21,18 +21,28 @@ async def main():
print(f"Connecting to inverter: {config.inverter_host}") print(f"Connecting to inverter: {config.inverter_host}")
connected_inverter = await connect_to_inverter(ip=config.inverter_host) connected_inverter = await connect_to_inverter(ip=config.inverter_host)
mongodb_client = await get_mongodb_client(host=config.mongodb_host, port=config.mongodb_port, username=config.mongodb_user, password=config.mongodb_pass) mongodb_client = await get_mongodb_client(host=config.mongodb_host, port=config.mongodb_port, username=config.mongodb_user, password=config.mongodb_pass)
db_collection = mongodb_client[config.mongodb_db]["inverters_data"]
mongodb_inverters_list = mongodb_client[config.mongodb_db]["inverters_list"]
if(await mongodb_inverters_list.count_documents({"inverter_unique_tag" : f"{config.inverter_unique_tag}"}) == 0):
await mongodb_inverters_list.insert_one({"inverter_unique_tag" : f"{config.inverter_unique_tag}"})
print("inserted")
mongodb_inverrters_data = mongodb_client[config.mongodb_db]["inverters_data"]
while True: while True:
print("") print("")
print("------------------------------------------------") print("------------------------------------------------")
data = await read_inverter(inverter=connected_inverter) data = await read_inverter(inverter=connected_inverter)
data["inverter_unique_tag"] = config.inverter_unique_tag data["inverter_unique_tag"] = config.inverter_unique_tag
print(json.dumps(data, default=str)) print(json.dumps(data, default=str))
inserted = await db_collection.insert_one(data) inserted = await mongodb_inverrters_data.insert_one(data)
print(f"data saved with id: {inserted.inserted_id}") print(f"data saved with id: {inserted.inserted_id}")
except: except:
mongodb_client.close() mongodb_client.close()
while True: while True:
try: try:
asyncio.run(main()) asyncio.run(main())