41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import asyncio
|
|
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"])
|
|
mqtt_client.connect(config["mqtt"]["broker"], config["mqtt"]["port"])
|
|
|
|
while True:
|
|
item = await queue.get()
|
|
for i in item.data:
|
|
return_code, _ = mqtt_client.publish(f"{config['mqtt']['topic']}/{item.name}/{i}", item.data[i])
|
|
if return_code == 4:
|
|
print("Invalid credentials, check your configuration.")
|
|
exit(1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|