bulk_sensor: New add_mux_endpoint() helper function in APIDumpHelper

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2023-12-16 14:31:32 -05:00
parent ffd44c0219
commit acde3720a4
6 changed files with 38 additions and 53 deletions

View File

@ -220,9 +220,9 @@ class ADXL345:
self.api_dump = bulk_sensor.APIDumpHelper(
self.printer, self._api_update, self._api_startstop, API_UPDATES)
self.name = config.get_name().split()[-1]
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("adxl345/dump_adxl345", "sensor", self.name,
self._handle_dump_adxl345)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
self.api_dump.add_mux_endpoint("adxl345/dump_adxl345", "sensor",
self.name, {'header': hdr})
def _build_config(self):
cmdqueue = self.spi.get_command_queue()
self.query_adxl345_cmd = self.mcu.lookup_command(
@ -345,10 +345,6 @@ class ADXL345:
self._start_measurements()
else:
self._finish_measurements()
def _handle_dump_adxl345(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
web_request.send({'header': hdr})
def start_internal_client(self):
cconn = self.api_dump.add_internal_client()
return AccelQueryHelper(self.printer, cconn)

View File

@ -444,9 +444,9 @@ class Angle:
self.api_dump = bulk_sensor.APIDumpHelper(
self.printer, self._api_update, self._api_startstop, 0.100)
self.name = config.get_name().split()[1]
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("angle/dump_angle", "sensor", self.name,
self._handle_dump_angle)
api_resp = {'header': ('time', 'angle')}
self.api_dump.add_mux_endpoint("angle/dump_angle", "sensor", self.name,
api_resp)
def _build_config(self):
freq = self.mcu.seconds_to_clock(1.)
while float(TCODE_ERROR << self.time_shift) / freq < 0.002:
@ -553,10 +553,6 @@ class Angle:
self._start_measurements()
else:
self._finish_measurements()
def _handle_dump_angle(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('time', 'angle')
web_request.send({'header': hdr})
def start_internal_client(self):
return self.api_dump.add_internal_client()

View File

@ -20,6 +20,8 @@ class APIDumpHelper:
self.update_interval = update_interval
self.update_timer = None
self.clients = {}
self.webhooks_start_resp = {}
# Periodic batch processing
def _stop(self):
self.clients.clear()
reactor = self.printer.get_reactor()
@ -52,16 +54,6 @@ class APIDumpHelper:
systime = reactor.monotonic()
waketime = systime + self.update_interval
self.update_timer = reactor.register_timer(self._update, waketime)
def add_client(self, web_request):
cconn = web_request.get_client_connection()
template = web_request.get_dict('response_template', {})
self.clients[cconn] = template
self._start()
def add_internal_client(self):
cconn = InternalDumpClient()
self.clients[cconn] = {}
self._start()
return cconn
def _update(self, eventtime):
try:
msg = self.data_cb(eventtime)
@ -80,6 +72,23 @@ class APIDumpHelper:
tmp['params'] = msg
cconn.send(tmp)
return eventtime + self.update_interval
# Internal clients
def add_internal_client(self):
cconn = InternalDumpClient()
self.clients[cconn] = {}
self._start()
return cconn
# Webhooks registration
def _add_api_client(self, web_request):
cconn = web_request.get_client_connection()
template = web_request.get_dict('response_template', {})
self.clients[cconn] = template
self._start()
web_request.send(self.webhooks_start_resp)
def add_mux_endpoint(self, path, key, value, webhooks_start_resp):
self.webhooks_start_resp = webhooks_start_resp
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint(path, key, value, self._add_api_client)
# An "internal webhooks" wrapper for using APIDumpHelper internally
class InternalDumpClient:

View File

@ -66,9 +66,9 @@ class LIS2DW:
self.api_dump = bulk_sensor.APIDumpHelper(
self.printer, self._api_update, self._api_startstop, API_UPDATES)
self.name = config.get_name().split()[-1]
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("lis2dw/dump_lis2dw", "sensor", self.name,
self._handle_dump_lis2dw)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
self.api_dump.add_mux_endpoint("lis2dw/dump_lis2dw", "sensor",
self.name, {'header': hdr})
def _build_config(self):
cmdqueue = self.spi.get_command_queue()
@ -193,10 +193,6 @@ class LIS2DW:
self._start_measurements()
else:
self._finish_measurements()
def _handle_dump_lis2dw(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
web_request.send({'header': hdr})
def start_internal_client(self):
cconn = self.api_dump.add_internal_client()
return adxl345.AccelQueryHelper(self.printer, cconn)

View File

@ -14,9 +14,9 @@ class DumpStepper:
self.mcu_stepper = mcu_stepper
self.last_api_clock = 0
self.api_dump = bulk_sensor.APIDumpHelper(printer, self._api_update)
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("motion_report/dump_stepper", "name",
mcu_stepper.get_name(), self._add_api_client)
api_resp = {'header': ('interval', 'count', 'add')}
self.api_dump.add_mux_endpoint("motion_report/dump_stepper", "name",
mcu_stepper.get_name(), api_resp)
def get_step_queue(self, start_clock, end_clock):
mcu_stepper = self.mcu_stepper
res = []
@ -62,10 +62,6 @@ class DumpStepper:
"start_mcu_position": mcu_pos, "step_distance": step_dist,
"first_clock": first_clock, "first_step_time": first_time,
"last_clock": last_clock, "last_step_time": last_time}
def _add_api_client(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('interval', 'count', 'add')
web_request.send({'header': hdr})
NEVER_TIME = 9999999999999999.
@ -77,9 +73,10 @@ class DumpTrapQ:
self.trapq = trapq
self.last_api_msg = (0., 0.)
self.api_dump = bulk_sensor.APIDumpHelper(printer, self._api_update)
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("motion_report/dump_trapq", "name", name,
self._add_api_client)
api_resp = {'header': ('time', 'duration', 'start_velocity',
'acceleration', 'start_position', 'direction')}
self.api_dump.add_mux_endpoint("motion_report/dump_trapq", "name", name,
api_resp)
def extract_trapq(self, start_time, end_time):
ffi_main, ffi_lib = chelper.get_ffi()
res = []
@ -130,11 +127,6 @@ class DumpTrapQ:
return {}
self.last_api_msg = d[-1]
return {"data": d}
def _add_api_client(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('time', 'duration', 'start_velocity', 'acceleration',
'start_position', 'direction')
web_request.send({'header': hdr})
STATUS_REFRESH_TIME = 0.250

View File

@ -83,9 +83,9 @@ class MPU9250:
self.api_dump = bulk_sensor.APIDumpHelper(
self.printer, self._api_update, self._api_startstop, API_UPDATES)
self.name = config.get_name().split()[-1]
wh = self.printer.lookup_object('webhooks')
wh.register_mux_endpoint("mpu9250/dump_mpu9250", "sensor", self.name,
self._handle_dump_mpu9250)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
self.api_dump.add_mux_endpoint("mpu9250/dump_mpu9250", "sensor",
self.name, {'header': hdr})
def _build_config(self):
cmdqueue = self.i2c.get_command_queue()
self.mcu.add_config_cmd("config_mpu9250 oid=%d i2c_oid=%d"
@ -207,10 +207,6 @@ class MPU9250:
self._start_measurements()
else:
self._finish_measurements()
def _handle_dump_mpu9250(self, web_request):
self.api_dump.add_client(web_request)
hdr = ('time', 'x_acceleration', 'y_acceleration', 'z_acceleration')
web_request.send({'header': hdr})
def start_internal_client(self):
cconn = self.api_dump.add_internal_client()
return adxl345.AccelQueryHelper(self.printer, cconn)