def run(updates=5, sleepms=1, poll_rate_inputs=4, poll_rate_network=10):
# updates: send out a status every this amount of seconds.
# If 0, never send time based status updates only when change happened.
# sleepms: going o sleep for how long between each loop run
# poll_rate_network: how often to evaluate incoming network commands
# poll_rate_inputs: how often to evaluate inputs
_init_mqtt()
if updates == 0:
overflow = 1000
else:
overflow = updates * 1000
overflow *= poll_rate_network * poll_rate_inputs / sleepms
overflow = int(overflow)
# print("Overflow:",overflow)
counter = 0
while True:
if counter % poll_rate_network == 0:
_poll_subscripton()
if counter % poll_rate_inputs == 0:
device_list = []
for d in _devlist.values():
if d.update():
device_list.append(d)
if len(device_list) > 0:
_publish_status(device_list)
if updates != 0 and counter % (updates * 1000) == 0:
print("Publishing full update.")
_publish_status(ignore_time=True)
# execute things on timestack
now = time.ticks_ms()
for id in list(_timestack):
t, cb = _timestack[id]
if time.ticks_diff(now, t) >= 0:
del (_timestack[id])
cb(id)
time.sleep_ms(sleepms)
counter += 1
if counter >= overflow:
counter = 0
评论列表
文章目录