Use NamedTuple for repetier API methods (#56941)

This commit is contained in:
Marc Mueller 2021-10-04 17:41:51 +02:00 committed by GitHub
parent eb9b9c57a4
commit 2f9943fe7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,28 +37,38 @@ UPDATE_SIGNAL = "repetier_update_signal"
TEMP_DATA = {"tempset": "temp_set", "tempread": "state", "output": "output"} TEMP_DATA = {"tempset": "temp_set", "tempread": "state", "output": "output"}
API_PRINTER_METHODS = { @dataclass
"bed_temperature": { class APIMethods:
"offline": {"heatedbeds": None, "state": "off"}, """API methods for properties."""
"state": {"heatedbeds": "temp_data"},
"temp_data": TEMP_DATA, offline: dict[str, str | None]
"attribute": "heatedbeds", state: dict[str, str]
}, temp_data: dict[str, str] | None = None
"extruder_temperature": { attribute: str | None = None
"offline": {"extruder": None, "state": "off"},
"state": {"extruder": "temp_data"},
"temp_data": TEMP_DATA, API_PRINTER_METHODS: dict[str, APIMethods] = {
"attribute": "extruder", "bed_temperature": APIMethods(
}, offline={"heatedbeds": None, "state": "off"},
"chamber_temperature": { state={"heatedbeds": "temp_data"},
"offline": {"heatedchambers": None, "state": "off"}, temp_data=TEMP_DATA,
"state": {"heatedchambers": "temp_data"}, attribute="heatedbeds",
"temp_data": TEMP_DATA, ),
"attribute": "heatedchambers", "extruder_temperature": APIMethods(
}, offline={"extruder": None, "state": "off"},
"current_state": { state={"extruder": "temp_data"},
"offline": {"state": None}, temp_data=TEMP_DATA,
"state": { attribute="extruder",
),
"chamber_temperature": APIMethods(
offline={"heatedchambers": None, "state": "off"},
state={"heatedchambers": "temp_data"},
temp_data=TEMP_DATA,
attribute="heatedchambers",
),
"current_state": APIMethods(
offline={"state": None},
state={
"state": "state", "state": "state",
"activeextruder": "active_extruder", "activeextruder": "active_extruder",
"hasxhome": "x_homed", "hasxhome": "x_homed",
@ -67,10 +77,10 @@ API_PRINTER_METHODS = {
"firmware": "firmware", "firmware": "firmware",
"firmwareurl": "firmware_url", "firmwareurl": "firmware_url",
}, },
}, ),
"current_job": { "current_job": APIMethods(
"offline": {"job": None, "state": "off"}, offline={"job": None, "state": "off"},
"state": { state={
"done": "state", "done": "state",
"job": "job_name", "job": "job_name",
"jobid": "job_id", "jobid": "job_id",
@ -84,25 +94,25 @@ API_PRINTER_METHODS = {
"y": "y", "y": "y",
"z": "z", "z": "z",
}, },
}, ),
"job_end": { "job_end": APIMethods(
"offline": {"job": None, "state": "off", "start": None, "printtime": None}, offline={"job": None, "state": "off", "start": None, "printtime": None},
"state": { state={
"job": "job_name", "job": "job_name",
"start": "start", "start": "start",
"printtime": "print_time", "printtime": "print_time",
"printedtimecomp": "from_start", "printedtimecomp": "from_start",
}, },
}, ),
"job_start": { "job_start": APIMethods(
"offline": { offline={
"job": None, "job": None,
"state": "off", "state": "off",
"start": None, "start": None,
"printedtimecomp": None, "printedtimecomp": None,
}, },
"state": {"job": "job_name", "start": "start", "printedtimecomp": "from_start"}, state={"job": "job_name", "start": "start", "printedtimecomp": "from_start"},
}, ),
} }
@ -251,17 +261,17 @@ class PrinterAPI:
"""Get data from the state cache.""" """Get data from the state cache."""
printer = self.printers[printer_id] printer = self.printers[printer_id]
methods = API_PRINTER_METHODS[sensor_type] methods = API_PRINTER_METHODS[sensor_type]
for prop, offline in methods["offline"].items(): for prop, offline in methods.offline.items():
state = getattr(printer, prop) state = getattr(printer, prop)
if state == offline: if state == offline:
# if state matches offline, sensor is offline # if state matches offline, sensor is offline
return None return None
data = {} data = {}
for prop, attr in methods["state"].items(): for prop, attr in methods.state.items():
prop_data = getattr(printer, prop) prop_data = getattr(printer, prop)
if attr == "temp_data": if attr == "temp_data":
temp_methods = methods["temp_data"] temp_methods = methods.temp_data or {}
for temp_prop, temp_attr in temp_methods.items(): for temp_prop, temp_attr in temp_methods.items():
data[temp_attr] = getattr(prop_data[temp_id], temp_prop) data[temp_attr] = getattr(prop_data[temp_id], temp_prop)
else: else:
@ -290,8 +300,8 @@ class PrinterAPI:
continue continue
methods = API_PRINTER_METHODS[sensor_type] methods = API_PRINTER_METHODS[sensor_type]
if "temp_data" in methods["state"].values(): if "temp_data" in methods.state.values():
prop_data = getattr(printer, methods["attribute"]) prop_data = getattr(printer, methods.attribute or "")
if prop_data is None: if prop_data is None:
continue continue
for idx, _ in enumerate(prop_data): for idx, _ in enumerate(prop_data):