def _publish_status(device_list=None, ignore_time=False):
global _client, _last_publish
if device_list is None:
device_list = _devlist.values()
current_time = time.ticks_us()
if ignore_time or \
time.ticks_diff(current_time,
_last_publish) >= MIN_PUBLISH_TIME_US:
_last_publish = current_time
try:
for d in device_list:
v = d.value()
if v is not None:
rt = (_topic + "/" + d.name).encode()
for s in d.getters:
if s == "":
t = rt
else:
t = rt + "/" + s.encode()
my_value = d.getters[s]()
print('Publishing', t, my_value)
if type(my_value) is bytes or type(
my_value) is bytearray:
_client.publish(t, my_value)
else:
_client.publish(t, str(my_value).encode())
except Exception as e:
print('Trouble publishing, re-init network.')
print(e)
_init_mqtt()
# ======= Setup and execution
评论列表
文章目录