Skip to content

Commit

Permalink
refactor sensors' data parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nlef committed May 3, 2022
1 parent 9d9046f commit 4363bee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
20 changes: 12 additions & 8 deletions bot/klippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,29 +264,33 @@ def update_sensror(self, name: str, value) -> None:
self.sensors_dict[name]["speed"] = value["speed"]
if "rpm" in value:
self.sensors_dict[name]["rpm"] = value["rpm"]
else:
elif value:
self.sensors_dict[name] = value

@staticmethod
def sensor_message(name: str, value) -> str:
sens_name = re.sub(r"([A-Z]|\d|_)", r" \1", name).replace("_", "")
message = ""
if "power" in value:
message = emoji.emojize(" :hotsprings: ", language="alias") + f"{sens_name.title()}: {round(value['temperature'])}"
if "target" in value and value["target"] > 0.0 and abs(value["target"] - value["temperature"]) > 2:
message += emoji.emojize(" :arrow_right: ", language="alias") + f"{round(value['target'])}"
if value["power"] > 0.0:
message += emoji.emojize(" :fire: ", language="alias")
elif "speed" in value:
message = emoji.emojize(" :tornado: ", language="alias") + f"{sens_name.title()}: {round(value['temperature'])}"
message = emoji.emojize(" :tornado: ", language="alias") + f"{sens_name.title()}:"
if "temperature" in value:
message += f" {round(value['temperature'])}"
if "target" in value and value["target"] > 0.0 and abs(value["target"] - value["temperature"]) > 2:
message += emoji.emojize(" :arrow_right: ", language="alias") + f"{round(value['target'])}"
if "speed" in value and value["speed"] > 0.0:
message += f" {round(value['speed'])}%"
if "rpm" in value and value["rpm"] > 0.0:
message += f" {round(value['rpm'])}RPM"
else:
if "speed" in value:
message += f" {round(value['speed']*100)}%"
if "rpm" in value and value["rpm"] is not None:
message += f" {round(value['rpm']*100)}RPM"
elif "temperature" in value:
message = emoji.emojize(" :thermometer: ", language="alias") + f"{sens_name.title()}: {round(value['temperature'])}"
message += "\n"
if message:
message += "\n"
return message

def _get_sensors_message(self) -> str:
Expand Down
44 changes: 25 additions & 19 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,21 +1085,7 @@ def status_response(status_resp):
if "virtual_sdcard" in status_resp:
klippy.vsd_progress = status_resp["virtual_sdcard"]["progress"]

# Todo: add sensors & heaters parsing
for sens in [key for key in status_resp if "temperature_sensor" in key]:
if status_resp[sens]:
klippy.update_sensror(sens.replace("temperature_sensor ", ""), status_resp[sens])

for fan in [key for key in status_resp if "temperature_fan" in key]:
if status_resp[fan]:
klippy.update_sensror(fan.replace("temperature_fan ", ""), status_resp[fan])

for heater in [key for key in status_resp if "extruder" in key or "heater_bed" in key or "heater_generic" in key]:
if status_resp[heater]:
klippy.update_sensror(
heater.replace("extruder ", "").replace("heater_bed ", "").replace("heater_generic ", ""),
status_resp[heater],
)
parse_sensors(status_resp)


def notify_gcode_reponse(message_params):
Expand Down Expand Up @@ -1163,13 +1149,33 @@ def notify_status_update(message_params):
if "print_stats" in message_params_loc:
parse_print_stats(message_params)

for sens in [key for key in message_params_loc if "temperature_sensor" in key]:
klippy.update_sensror(sens.replace("temperature_sensor ", ""), message_params_loc[sens])
parse_sensors(message_params_loc)


def parse_sensors(message_parts_loc):
for sens in [key for key in message_parts_loc if key.startswith("temperature_sensor")]:
klippy.update_sensror(sens.replace("temperature_sensor ", ""), message_parts_loc[sens])

for heater_fan in [key for key in message_parts_loc if key.startswith("heater_fan")]:
if message_parts_loc[heater_fan]:
klippy.update_sensror(heater_fan.replace("heater_fan ", ""), message_parts_loc[heater_fan])

for controller_fan in [key for key in message_parts_loc if key.startswith("controller_fan")]:
if message_parts_loc[controller_fan]:
klippy.update_sensror(controller_fan.replace("controller_fan ", ""), message_parts_loc[controller_fan])

for temperature_fan in [key for key in message_parts_loc if key.startswith("temperature_fan")]:
if message_parts_loc[temperature_fan]:
klippy.update_sensror(temperature_fan.replace("temperature_fan ", ""), message_parts_loc[temperature_fan])

for generic_fan in [key for key in message_parts_loc if key.startswith("fan_generic")]:
if message_parts_loc[generic_fan]:
klippy.update_sensror(generic_fan.replace("fan_generic ", ""), message_parts_loc[generic_fan])

for heater in [key for key in message_params_loc if "extruder" in key or "heater_bed" in key or "heater_generic" in key]:
for heater in [key for key in message_parts_loc if key.startswith("extruder") or key.startswith("heater_bed") or key.startswith("heater_generic")]:
klippy.update_sensror(
heater.replace("extruder ", "").replace("heater_bed ", "").replace("heater_generic ", ""),
message_params_loc[heater],
message_parts_loc[heater],
)


Expand Down

0 comments on commit 4363bee

Please sign in to comment.