#!/usr/bin/env python3

from json import loads
from os import getpid
from sys import argv, stderr
from time import sleep

import paho.mqtt.client as mqtt

BROKER_HOST = argv[1]
BROKER_TOPIC = argv[2]


def log(msg):
    print(msg, file=stderr, flush=True)


def out(msg):
    print(msg, flush=True)


def on_connect(client, userdata, flags, rc):
    log(f'connected to mqtt broker at {BROKER_HOST} with status: {mqtt.connack_string(rc)}')

    client.subscribe(BROKER_TOPIC)


def on_disconnect(client, userdata, rc):
    log(f'disconnected from mqtt broker at {BROKER_HOST} with status: {mqtt.error_string(rc)}')


def on_message(client, userdata, msg):
    try:
        message = msg.payload.decode()

        if not any(message.startswith(i) for i in ['{', '[']):
            # probably not json
            return

        json = loads(message)
        topic_split = msg.topic.strip('/').split('/')
        sensor_name = '_'.join(topic_split[:-1])

        if topic_split[-1] == 'STATE':
            # /switch/wohnzimmer/ambilight/STATE {"Time":"2021-05-15T07:40:16","Uptime":"4T18:48:43","Vcc":3.108,"Heap":19,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"OFF","Wifi":{"AP":1,"SSId":"NSA Surveillance Van 2342","BSSId":"DA:CC:46:87:58:F7","Channel":6,"RSSI":100,"LinkCount":2,"Downtime":"0T00:00:27"}}
            power_state = bool(json['POWER'] == 'ON')
            out(f'tasmota,device={sensor_name} state={power_state}')
            out(f'tasmota,device={sensor_name} rssi={json["Wifi"]["RSSI"]}i')

        if topic_split[-1] == 'SENSOR':
            # /switch/wohnzimmer/ambilight/SENSOR {"Time":"2021-05-15T07:40:16","ENERGY":{"TotalStartTime":"2019-08-02T13:56:56","Total":2.324,"Yesterday":0.004,"Today":0.000,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0.00,"Voltage":237,"Current":0.000}}
            out(f'tasmota,device={sensor_name} energy_total={json["ENERGY"]["Total"]}')
            out(f'tasmota,device={sensor_name} energy_today={json["ENERGY"]["Today"]}')
            out(f'tasmota,device={sensor_name} energy_yesterday={json["ENERGY"]["Yesterday"]}')
            out(f'tasmota,device={sensor_name} power={json["ENERGY"]["Power"]}')
            out(f'tasmota,device={sensor_name} apparent_power={json["ENERGY"]["ApparentPower"]}')
            out(f'tasmota,device={sensor_name} reactive_power={json["ENERGY"]["ReactivePower"]}')
            out(f'tasmota,device={sensor_name} powerfactor={json["ENERGY"]["Factor"]}')
            out(f'tasmota,device={sensor_name} voltage={json["ENERGY"]["Voltage"]}i')
            out(f'tasmota,device={sensor_name} current={json["ENERGY"]["Current"]}')
    except Exception as e:
        log(f'parse error while parsing a message on topic {msg.topic}:')
        log(repr(e))
        log(repr(msg.payload))


def on_subscribe(client, userdata, mid, granted_qos):
    log(f'subscription {mid} was successful')


if __name__ == '__main__':
    client = mqtt.Client(
        client_id=f'tasmota-telegraf-plugin_{getpid()}'
    )

    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_message = on_message
    client.on_subscribe = on_subscribe

    client.connect(BROKER_HOST, 1883, 30)

    client.loop_forever()
