devices.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:ulnoiot 作者: ulno 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号