pyhOn/pyhon/parameter.py

151 lines
4.4 KiB
Python
Raw Normal View History

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