def my_stat(): time.sleep(3) """ if my_check_date() is False: global SET_DATE mylib.my_log('try to set_date') SET_DATE = mylib.set_time() """ if my_check_ip() is False: global PUBLIC_IP mylib.my_log('try to get public ip', 1) PUBLIC_IP = mylib.get_public_ip() payload = { "id": DEVICE_ID, "memtotal": mylib.get_total_mem(), "memused": mylib.get_used_mem(), "disktotal": mylib.get_sd_size(), "diskfree": mylib.get_free_space(), "cputemp": mylib.get_cpu_temp(), "cpuload": mylib.get_cpu_usage(), "internalip": mylib.get_ip_addr(), "publicip": PUBLIC_IP, "uptime": mylib.get_uptime(), "time": int(time.time()), "timestr": time.ctime() } mylib.my_log('status: ' + str(payload), 1) rclient.publish(TOPIC + "/out/status", mylib.my_json(payload)) th = threading.Timer(9, my_stat) # interval th.daemon = True th.start()
def l_connect(client, userdata, flags, rc): mylib.my_log("Connected local client with result code = " + str(rc), 1) payload = { "conn": True, "uptime": mylib.get_uptime(), "time": int(time.time()), "timestr": time.ctime() } mylib.my_log("Local client: " + str(payload), 1) lclient.publish("/hjlocal/out/start", mylib.my_json(payload))
def run_lclient(): payload = { "conn": False, "uptime": mylib.get_uptime(), "time": int(time.time()), "timestr": time.ctime() } lclient.will_set("/hjlocal/out/start", mylib.my_json(payload)) lclient.connect("localhost", 1883, 60) lclient.on_connect = l_connect lclient.on_message = l_message lclient.loop_forever()
def r_connect(client, userdata, flags, rc): mylib.my_log("Connected remote client with result code = " + str(rc), 1) payload = { "conn": True, "uptime": mylib.get_uptime(), "time": int(time.time()), "id": DEVICE_ID, "timestr": time.ctime() } mylib.my_log("Remote client: " + str(payload), 1) rclient.publish(TOPIC + "/out/start", mylib.my_json(payload), qos=1, retain=True)
def run_rclient(arg=None): payload = { "conn": False, "uptime": mylib.get_uptime(), "time": int(time.time()), "id": DEVICE_ID, "timestr": time.ctime() } rclient.will_set(TOPIC + "/out/start", mylib.my_json(payload), qos=1, retain=True) if arg == '-l': rclient.connect("localhost", 1883, 60) else: rclient.connect("test.mosquitto.org", 1883, 60) rclient.on_connect = r_connect rclient.on_message = r_message rclient.loop_forever()