From 1e018e9b92d9eed8242f0528c9de96119a3bc121 Mon Sep 17 00:00:00 2001 From: Arksine Date: Fri, 31 Jul 2020 08:29:33 -0400 Subject: [PATCH] temperature_store: directly request sensor data Sensor information is no longer provided by the response to the "check_ready" remote procedure call. The temperature store now fetches the sensor data itself and sets up the subscription. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 5 +-- moonraker/plugins/temperature_store.py | 53 +++++++++++++++++++------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index d242d79..7b59ed6 100644 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -249,7 +249,7 @@ class Server: if not isinstance(result, ServerError): is_ready = result.get("is_ready", False) if is_ready: - self._set_klippy_ready(result.get('sensors', {})) + self._set_klippy_ready() def _load_config(self, config): self.request_timeout = config.get( @@ -293,10 +293,9 @@ class Server: else: logging.info("No request matching response: " + str(response)) - def _set_klippy_ready(self, sensors): + def _set_klippy_ready(self): logging.info("Klippy ready") self.is_klippy_ready = True - self.send_event("server:refresh_temp_sensors", sensors) self.send_event("server:klippy_state_changed", "ready") def _set_klippy_shutdown(self): diff --git a/moonraker/plugins/temperature_store.py b/moonraker/plugins/temperature_store.py index 511d0fd..96885b9 100644 --- a/moonraker/plugins/temperature_store.py +++ b/moonraker/plugins/temperature_store.py @@ -24,26 +24,51 @@ class TemperatureStore: self.server.register_event_handler( "server:status_update", self._set_current_temps) self.server.register_event_handler( - "server:refresh_temp_sensors", self._init_sensors) + "server:klippy_state_changed", self._init_sensors) # Register endpoint self.server.register_endpoint( "/server/temperature_store", "server_temperature_store", ['GET'], self._handle_temp_store_request) - def _init_sensors(self, sensors): - logging.info("Configuring available sensors: %s" % (str(sensors))) - new_store = {} - for sensor in sensors: - if sensor in self.temperature_store: - new_store[sensor] = self.temperature_store[sensor] - else: - new_store[sensor] = { - 'temperatures': deque(maxlen=TEMPERATURE_STORE_SIZE), - 'targets': deque(maxlen=TEMPERATURE_STORE_SIZE)} - self.temperature_store = new_store - self.temp_update_cb.start() - # XXX - spawn a callback that requests temperature updates? + async def _init_sensors(self, state): + if state != "ready": + return + + # Fetch sensors + request = self.server.make_request( + "objects/status", 'GET', {'heaters': []}) + result = await request.wait() + if isinstance(result, self.server.error): + logging.info("Error Configuring Sensors: %s" % (str(result))) + return + sensors = result.get("heaters", {}).get("available_sensors", []) + + if sensors: + # Add Subscription + sub = {s: [] for s in sensors} + request = self.server.make_request( + "objects/subscription", 'POST', sub) + result = await request.wait() + if isinstance(result, self.server.error): + logging.info("Error subscribing to sensors: %s" %(str(result))) + return + logging.info("Configuring available sensors: %s" % (str(sensors))) + new_store = {} + for sensor in sensors: + if sensor in self.temperature_store: + new_store[sensor] = self.temperature_store[sensor] + else: + new_store[sensor] = { + 'temperatures': deque(maxlen=TEMPERATURE_STORE_SIZE), + 'targets': deque(maxlen=TEMPERATURE_STORE_SIZE)} + self.temperature_store = new_store + self.temp_update_cb.start() + else: + logging.info("No sensors found") + self.last_temps = {} + self.temperature_store = {} + self.temp_update_cb.stop() def _set_current_temps(self, data): for sensor in self.temperature_store: