pyh0n/pyhon/parameter.py

145 lines
3.8 KiB
Python
Raw Normal View History

2023-04-10 20:29:10 +05:30
def str_to_float(string):
2023-04-10 22:18:12 +05:30
try:
return int(string)
except ValueError:
2023-04-11 20:39:02 +05:30
return float(str(string).replace(",", "."))
2023-04-10 20:29:10 +05:30
2023-02-13 06:11:38 +05:30
class HonParameter:
def __init__(self, key, attributes):
self._key = key
self._category = attributes.get("category")
self._typology = attributes.get("typology")
self._mandatory = attributes.get("mandatory")
2023-02-19 02:55:51 +05:30
@property
def key(self):
return self._key
2023-02-13 06:11:38 +05:30
@property
def value(self):
return self._value if self._value is not None else "0"
2023-03-05 02:49:48 +05:30
@property
def category(self):
return self._category
@property
def typology(self):
return self._typology
@property
def mandatory(self):
return self._mandatory
2023-02-13 06:11:38 +05:30
class HonParameterFixed(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
self._value = attributes.get("fixedValue", None)
2023-02-13 06:11:38 +05:30
2023-02-20 00:13:41 +05:30
def __repr__(self):
return f"{self.__class__} (<{self.key}> fixed)"
2023-02-19 02:55:51 +05:30
@property
def value(self):
2023-03-03 22:54:19 +05:30
return self._value if self._value is not None else "0"
2023-02-19 02:55:51 +05:30
@value.setter
def value(self, value):
2023-02-19 08:00:48 +05:30
if not value == self._value:
raise ValueError("Can't change fixed value")
2023-02-19 02:55:51 +05:30
2023-02-13 06:11:38 +05:30
class HonParameterRange(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
2023-04-10 20:29:10 +05:30
self._min = str_to_float(attributes["minimumValue"])
self._max = str_to_float(attributes["maximumValue"])
self._step = str_to_float(attributes["incrementValue"])
self._default = str_to_float(attributes.get("defaultValue", self._min))
2023-02-19 02:55:51 +05:30
self._value = self._default
def __repr__(self):
2023-02-20 00:13:41 +05:30
return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])"
2023-02-19 02:55:51 +05:30
2023-02-19 08:00:48 +05:30
@property
def min(self):
return self._min
@property
def max(self):
return self._max
@property
def step(self):
return self._step
2023-02-19 02:55:51 +05:30
@property
def value(self):
return self._value if self._value is not None else self._min
@value.setter
def value(self, value):
2023-04-10 20:29:10 +05:30
value = str_to_float(value)
2023-02-19 08:00:48 +05:30
if self._min <= value <= self._max and not value % self._step:
2023-03-05 23:33:03 +05:30
self._value = value
2023-02-19 08:00:48 +05:30
else:
2023-04-10 00:25:36 +05:30
raise ValueError(
f"Allowed: min {self._min} max {self._max} step {self._step}"
)
2023-02-13 06:11:38 +05:30
class HonParameterEnum(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
2023-02-19 02:55:51 +05:30
self._default = attributes.get("defaultValue")
2023-02-19 08:00:48 +05:30
self._value = self._default or "0"
2023-02-19 02:55:51 +05:30
self._values = attributes.get("enumValues")
def __repr__(self):
2023-02-20 00:13:41 +05:30
return f"{self.__class__} (<{self.key}> {self.values})"
2023-02-19 02:55:51 +05:30
@property
def values(self):
2023-04-08 07:36:36 +05:30
return [str(value) for value in self._values]
2023-02-19 02:55:51 +05:30
@property
def value(self):
2023-03-03 22:54:19 +05:30
return self._value if self._value is not None else self.values[0]
2023-02-19 02:55:51 +05:30
@value.setter
def value(self, value):
2023-02-19 08:00:48 +05:30
if value in self.values:
2023-03-05 23:33:03 +05:30
self._value = value
2023-02-19 08:00:48 +05:30
else:
raise ValueError(f"Allowed values {self._value}")
2023-02-19 02:55:51 +05:30
class HonParameterProgram(HonParameterEnum):
2023-04-16 03:41:50 +05:30
_FILTER = ["iot_recipe", "iot_guided"]
2023-02-19 08:00:48 +05:30
def __init__(self, key, command):
super().__init__(key, {})
self._command = command
2023-04-16 02:32:37 +05:30
self._value = command.program
self._values = command.programs
2023-03-07 00:15:46 +05:30
self._typology = "enum"
2023-04-08 07:36:36 +05:30
self._filter = ""
2023-02-19 08:00:48 +05:30
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if value in self.values:
2023-04-16 02:32:37 +05:30
self._command.program = value
2023-02-19 08:00:48 +05:30
else:
2023-03-05 23:33:03 +05:30
raise ValueError(f"Allowed values {self._values}")
2023-04-08 07:36:36 +05:30
@property
def values(self):
2023-04-16 03:41:50 +05:30
values = [v for v in self._values if all(f not in v for f in self._FILTER)]
2023-04-10 00:59:29 +05:30
return sorted(values)