X-Git-Url: https://the.earth.li/gitweb/?p=energenie-attiny.git;a=blobdiff_plain;f=mqtt-power;h=d2723cba9a6f58e48a1d93fff537494429cddaa8;hp=ee7b67246332803ca39e2f994f38b4c025fc1509;hb=96d1571590812770023026aed69ca6d4c948d44d;hpb=a1b18b29238c880ecefb578a460bcdb81aa5433c diff --git a/mqtt-power b/mqtt-power index ee7b672..d2723cb 100755 --- a/mqtt-power +++ b/mqtt-power @@ -20,6 +20,7 @@ import time import sys import hid +import json import paho.mqtt.client as mqtt debug = False @@ -61,6 +62,19 @@ def mqtt_message(client, device, message): control_relay(device, relay, state) +def uptime(): + with open('/proc/uptime', 'r') as f: + seconds = int(float(f.readline().split()[0])) + + days = seconds // 86400 + seconds %= 86400 + hours = seconds // 3600 + seconds %= 3600 + minutes = seconds // 60 + seconds %= 60 + + return "{}T{}:{}:{}".format(days, hours, minutes, seconds) + relay = None for dev in hid.enumerate(0x16c0, 0x05df): if dev['manufacturer_string'] == 'www.dcttech.com': @@ -86,4 +100,9 @@ client.subscribe(prefix.format(serno_str)) client.loop_start() while True: - time.sleep(60) + state = { + 'Time': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime()), + 'Uptime': uptime() + } + client.publish("relay/{}/state".format(serno_str), json.dumps(state)) + time.sleep(300)