import sys
import hid
+import json
import paho.mqtt.client as mqtt
debug = False
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':
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)