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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-07-31 08:29:33 -04:00
parent 973b1ffa7d
commit 1e018e9b92
2 changed files with 41 additions and 17 deletions

View File

@ -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):

View File

@ -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: