2023-02-18 21:25:51 +00:00
|
|
|
from pyhon.parameter import HonParameterFixed, HonParameterEnum, HonParameterRange, HonParameterProgram
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HonCommand:
|
2023-02-18 21:25:51 +00:00
|
|
|
def __init__(self, name, attributes, connector, device, multi=None, category=""):
|
2023-02-13 00:41:38 +00:00
|
|
|
self._connector = connector
|
|
|
|
self._device = device
|
|
|
|
self._name = name
|
2023-02-18 21:25:51 +00:00
|
|
|
self._multi = multi or {}
|
|
|
|
self._category = category
|
2023-02-13 00:41:38 +00:00
|
|
|
self._description = attributes.get("description", "")
|
|
|
|
self._parameters = self._create_parameters(attributes.get("parameters", {}))
|
|
|
|
self._ancillary_parameters = self._create_parameters(attributes.get("ancillaryParameters", {}))
|
2023-02-18 21:25:51 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"{self._name} command"
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
def _create_parameters(self, parameters):
|
|
|
|
result = {}
|
|
|
|
for parameter, attributes in parameters.items():
|
|
|
|
match attributes.get("typology"):
|
|
|
|
case "range":
|
|
|
|
result[parameter] = HonParameterRange(parameter, attributes)
|
|
|
|
case "enum":
|
|
|
|
result[parameter] = HonParameterEnum(parameter, attributes)
|
|
|
|
case "fixed":
|
|
|
|
result[parameter] = HonParameterFixed(parameter, attributes)
|
2023-02-18 21:25:51 +00:00
|
|
|
if self._multi:
|
|
|
|
result["program"] = HonParameterProgram("program", {"current": self._category, "values": list(self._multi)})
|
2023-02-13 00:41:38 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parameters(self):
|
2023-02-18 21:25:51 +00:00
|
|
|
result = {key: parameter.value for key, parameter in self._parameters.items()}
|
|
|
|
return result | {"program": self._category}
|
2023-02-13 00:41:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ancillary_parameters(self):
|
|
|
|
return {key: parameter.value for key, parameter in self._ancillary_parameters.items()}
|
|
|
|
|
|
|
|
async def send(self):
|
|
|
|
return await self._connector.send_command(self._device, self._name, self.parameters,
|
|
|
|
self.ancillary_parameters)
|
|
|
|
|
|
|
|
async def get_programs(self):
|
|
|
|
return self._multi
|
|
|
|
|
|
|
|
async def set_program(self, program):
|
|
|
|
self._device.commands[self._name] = self._multi[program]
|
2023-02-18 21:25:51 +00:00
|
|
|
|
|
|
|
def _get_settings_keys(self, command=None):
|
|
|
|
command = command or self
|
|
|
|
keys = []
|
|
|
|
for key, parameter in command._parameters.items():
|
|
|
|
if isinstance(parameter, HonParameterFixed):
|
|
|
|
continue
|
|
|
|
if key not in keys:
|
|
|
|
keys.append(key)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
@property
|
|
|
|
def setting_keys(self):
|
|
|
|
if not self._multi:
|
|
|
|
return self._get_settings_keys()
|
|
|
|
result = [key for cmd in self._multi.values() for key in self._get_settings_keys(cmd)]
|
|
|
|
return list(set(result + ["program"]))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def settings(self):
|
|
|
|
return {s: self._parameters[s] for s in self.setting_keys}
|
|
|
|
|
|
|
|
def set_setting(self, key, value):
|
|
|
|
if key == "program":
|
|
|
|
self.set_program(key)
|
|
|
|
else:
|
|
|
|
self.parameters[key].value = value
|