Goodwe_reader/src/main.py

50 lines
1.4 KiB
Python
Raw Normal View History

2024-07-16 13:48:21 +00:00
import goodwe, asyncio, time, json
import motor.motor_asyncio
from configuration import get_config
import motor
config = get_config()
async def connect_to_inverter(ip: str):
return await goodwe.connect(host=ip)
async def read_inverter(inverter):
return await inverter.read_runtime_data()
async def get_mongodb_collection(host: str, port: str, db: str, username: str, password: str):
client = motor.motor_asyncio.AsyncIOMotorClient(f"mongodb://{username}:{password}@{host}:{port}/")
return client[db]["inverters_data"]
async def main():
print(f"Connecting to inverter: {config.inverter_host}")
connected_inverter = await connect_to_inverter(ip=config.inverter_host)
db_collection = await get_mongodb_collection(host=config.mongodb_host, port=config.mongodb_port, db=config.mongodb_db, username=config.mongodb_user, password=config.mongodb_pass)
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)
print(f"data saved with id: {inserted.inserted_id}")
while True:
try:
asyncio.run(main())
except:
print("Error --> script restart")
time.sleep(1)