bulk_sensor: Minor code reorg to _stop() in APIDumpHelper()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2023-12-16 21:59:53 -05:00
parent acde3720a4
commit 95c753292d
1 changed files with 19 additions and 19 deletions

View File

@ -22,23 +22,6 @@ class APIDumpHelper:
self.clients = {} self.clients = {}
self.webhooks_start_resp = {} self.webhooks_start_resp = {}
# Periodic batch processing # Periodic batch processing
def _stop(self):
self.clients.clear()
reactor = self.printer.get_reactor()
reactor.unregister_timer(self.update_timer)
self.update_timer = None
if not self.is_started:
return reactor.NEVER
try:
self.startstop_cb(False)
except self.printer.command_error as e:
logging.exception("API Dump Helper stop callback error")
self.clients.clear()
self.is_started = False
if self.clients:
# New client started while in process of stopping
self._start()
return reactor.NEVER
def _start(self): def _start(self):
if self.is_started: if self.is_started:
return return
@ -54,19 +37,36 @@ class APIDumpHelper:
systime = reactor.monotonic() systime = reactor.monotonic()
waketime = systime + self.update_interval waketime = systime + self.update_interval
self.update_timer = reactor.register_timer(self._update, waketime) self.update_timer = reactor.register_timer(self._update, waketime)
def _stop(self):
self.clients.clear()
self.printer.get_reactor().unregister_timer(self.update_timer)
self.update_timer = None
if not self.is_started:
return
try:
self.startstop_cb(False)
except self.printer.command_error as e:
logging.exception("API Dump Helper stop callback error")
self.clients.clear()
self.is_started = False
if self.clients:
# New client started while in process of stopping
self._start()
def _update(self, eventtime): def _update(self, eventtime):
try: try:
msg = self.data_cb(eventtime) msg = self.data_cb(eventtime)
except self.printer.command_error as e: except self.printer.command_error as e:
logging.exception("API Dump Helper data callback error") logging.exception("API Dump Helper data callback error")
return self._stop() self._stop()
return self.printer.get_reactor().NEVER
if not msg: if not msg:
return eventtime + self.update_interval return eventtime + self.update_interval
for cconn, template in list(self.clients.items()): for cconn, template in list(self.clients.items()):
if cconn.is_closed(): if cconn.is_closed():
del self.clients[cconn] del self.clients[cconn]
if not self.clients: if not self.clients:
return self._stop() self._stop()
return self.printer.get_reactor().NEVER
continue continue
tmp = dict(template) tmp = dict(template)
tmp['params'] = msg tmp['params'] = msg