Added range checks
This commit is contained in:
parent
3fa12f65bc
commit
1f3a71ba96
@ -130,10 +130,12 @@ class Nucon(Enum):
|
|||||||
STEAM_TURBINE_1_PRESSURE = ("STEAM_TURBINE_1_PRESSURE", float, False)
|
STEAM_TURBINE_1_PRESSURE = ("STEAM_TURBINE_1_PRESSURE", float, False)
|
||||||
STEAM_TURBINE_2_PRESSURE = ("STEAM_TURBINE_2_PRESSURE", float, False)
|
STEAM_TURBINE_2_PRESSURE = ("STEAM_TURBINE_2_PRESSURE", float, False)
|
||||||
|
|
||||||
def __init__(self, id: str, param_type: Type, is_writable: bool):
|
def __init__(self, id: str, param_type: Type, is_writable: bool, min_val: Optional[Union[int, float]] = None, max_val: Optional[Union[int, float]] = None):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.param_type = param_type
|
self.param_type = param_type
|
||||||
self.is_writable = is_writable
|
self.is_writable = is_writable
|
||||||
|
self.min_val = min_val
|
||||||
|
self.max_val = max_val
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.id
|
return self.id
|
||||||
@ -156,6 +158,17 @@ class Nucon(Enum):
|
|||||||
def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
|
def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
|
||||||
Nucon.set(self, new_value, force)
|
Nucon.set(self, new_value, force)
|
||||||
|
|
||||||
|
def check_in_range(self, value: Union[int, float], raise_on_oob=False) -> None:
|
||||||
|
if self.min_val is not None and value < self.min_val:
|
||||||
|
if raise_on_oob:
|
||||||
|
raise ValueError(f"Value {value} is below the minimum allowed value {self.min_val} for {self.name}")
|
||||||
|
return False
|
||||||
|
if self.max_val is not None and value > self.max_val:
|
||||||
|
if raise_on_oob:
|
||||||
|
raise ValueError(f"Value {value} is above the maximum allowed value {self.max_val} for {self.name}")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_base_url(cls, url: str) -> None:
|
def set_base_url(cls, url: str) -> None:
|
||||||
NuconConfig.base_url = url
|
NuconConfig.base_url = url
|
||||||
@ -220,11 +233,16 @@ class Nucon(Enum):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def set(cls, parameter: Union['Nucon', str], value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
|
def set(cls, parameter: Union['Nucon', str], value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
|
||||||
if isinstance(parameter, str):
|
if isinstance(parameter, str):
|
||||||
parameter = cls[parameter]
|
parameter = next((param for param in cls if param.id == parameter), None)
|
||||||
|
if parameter is None:
|
||||||
|
raise ValueError(f"No parameter found with id '{parameter}'")
|
||||||
|
|
||||||
if not force and not parameter.is_writable:
|
if not force and not parameter.is_writable:
|
||||||
raise ValueError(f"Parameter {parameter.name} is not writable")
|
raise ValueError(f"Parameter {parameter.name} is not writable")
|
||||||
|
|
||||||
|
if not force:
|
||||||
|
parameter.check_in_range(value, raise_on_oob=True)
|
||||||
|
|
||||||
if parameter.enum_type and isinstance(value, parameter.enum_type):
|
if parameter.enum_type and isinstance(value, parameter.enum_type):
|
||||||
value = value.value
|
value = value.value
|
||||||
|
|
||||||
@ -237,7 +255,6 @@ class Nucon(Enum):
|
|||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}")
|
raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}")
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
# Example usage
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Enable dummy mode for testing
|
# Enable dummy mode for testing
|
||||||
|
Loading…
Reference in New Issue
Block a user