47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import asyncio
|
|
import logging
|
|
import os
|
|
import toml
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from CloudConnection import CloudConnection
|
|
from Inverter import Inverter
|
|
|
|
|
|
async def main():
|
|
os.path.dirname(__file__)
|
|
with open("config.toml", "r") as f:
|
|
config = toml.load(f)
|
|
|
|
connection = CloudConnection(config["apsystems"]["username"], config["apsystems"]["password"])
|
|
connection.generate_user_token()
|
|
inverter = connection.get_inverter()
|
|
devices = []
|
|
|
|
queue = asyncio.Queue()
|
|
for inv in inverter:
|
|
devices.append(Inverter(queue, connection, inv))
|
|
for device in devices:
|
|
asyncio.create_task(device.start())
|
|
|
|
mqtt_client = mqtt.Client()
|
|
mqtt_client.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"])
|
|
try:
|
|
mqtt_client.connect(config["mqtt"]["broker"], config["mqtt"]["port"])
|
|
except:
|
|
logging.error("Invalid mqtt credentials, check your configuration.")
|
|
exit(2)
|
|
|
|
while True:
|
|
item = await queue.get()
|
|
logging.debug(item)
|
|
for i in item.data:
|
|
return_code, _ = mqtt_client.publish(f"{config['mqtt']['topic']}/{item.name}/{i}", item.data[i])
|
|
if return_code != 0:
|
|
await queue.put(item)
|
|
mqtt_client.reconnect()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|