added inserting inverter unique identifier to separate DB collection
parent
260ce4efec
commit
dfb858fb77
16
src/main.py
16
src/main.py
|
@ -21,21 +21,31 @@ async def main():
|
|||
print(f"Connecting to inverter: {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)
|
||||
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:
|
||||
print("")
|
||||
print("------------------------------------------------")
|
||||
data = await read_inverter(inverter=connected_inverter)
|
||||
data["inverter_unique_tag"] = config.inverter_unique_tag
|
||||
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}")
|
||||
|
||||
except:
|
||||
mongodb_client.close()
|
||||
|
||||
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except:
|
||||
print("Error --> script restart")
|
||||
time.sleep(1)
|
||||
time.sleep(1)
|
||||
|
|
Loading…
Reference in New Issue