import std/[asyncdispatch] import std/[os, posix] import std/[tables, json, options] import std/math import nmqtt import types, solax var mqttContext* {.threadvar.} : MqttCtx proc ctrlCHook* () {.noconv.} = echo "Ctrl+C fired! \nStopping Server now!" waitFor mqttContext.disconnect() quit() proc updateData (config: Config) {.async.} = await sleepAsync(500) while true: try: let solaxData = await getSolaxData(config.ip, config.password) if mqttContext.isConnected: await mqttContext.publish(config.mqtt.topic, $solaxData, 2, true) for key, value in solaxData: await sleepAsync(250) await mqttContext.publish(config.mqtt.topic & "/" & key, $value, 2, true) except: echo "Error[updateData]:\n", getCurrentExceptionMsg() await sleepAsync(int(config.updateInterval * 1000)) proc main () {.async.} = setControlCHook(ctrlCHook) onSignal(SIGTERM): echo "Got SIGTERM! \nStopping Server now!" waitFor mqttContext.disconnect() quit() var configFile = "./config.json" if getEnv("CONFIG_PATH") != "": configFile = getEnv("CONFIG_PATH") if not fileExists(configFile): echo "Config file not found" quit() let config = parseFile(configFile).to(Config) mqttContext = newMqttCtx("solax2mqtt") mqttContext.set_host(config.mqtt.host, config.mqtt.port) mqttContext.set_verbosity(1) if (config.mqtt.username.isSome and config.mqtt.password.isSome): mqttContext.set_auth(config.mqtt.username.get, config.mqtt.password.get) await mqttContext.start() asyncCheck updateData(config) runForever() waitFor main()