To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 4506bb0f authored by mikolajr's avatar mikolajr
Browse files

In PfeifferTPG device: refactor set full scale methods common code

parent 3eb28829
......@@ -13,7 +13,7 @@ measurement/activeline/controllers/
import logging
from enum import Enum, IntEnum
from typing import List, Union, Tuple
from typing import Dict, List, Tuple, Union
from .base import SingleCommDevice
from ..comm import SerialCommunication, SerialCommunicationConfig
......@@ -150,10 +150,25 @@ class PfeifferTPGConfig:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.full_scale_ranges_reversed = {
self.full_scale_ranges_reversed: Dict[int, int] = {
v: k for k, v in self.full_scale_ranges.items()
}
def is_valid_scale_range_reversed_str(self, v: str) -> bool:
"""
Check if given string represents a valid reversed scale range of a model.
:param v: Reversed scale range string.
:return: `True` if valid, `False` otherwise.
"""
# Explicit check because otherwise we get `True` for instance for `float`
if not isinstance(v, str):
raise TypeError(f"Expected `str`, got `{type(v)}` instead.")
try:
return int(v) in self.full_scale_ranges_reversed
except ValueError:
return False
# model of the TPG (determines which lookup table to use for the
# full scale range)
model: Union[str, Model] = Model.TPG25xA # type: ignore
......@@ -386,37 +401,44 @@ class PfeifferTPG(SingleCommDevice):
logging.info(f"Reading all sensors with result: {ret}.")
return ret
def set_full_scale_unitless(self, fsr: List[int]) -> None:
"""
Set the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:param fsr: list of full scale range values, like [0, 1, 3, 3, 2, 0]
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
wrong_values = [
v for v in fsr if v not in self.config.model.full_scale_ranges_reversed
]
def _set_full_scale(self, fsr: List[int], unitless: bool) -> None:
if len(fsr) != self.number_of_sensors:
raise ValueError(
f"Argument fsr should be of length "
f"{self.number_of_sensors}. Received length {len(fsr)}."
)
elif wrong_values:
possible_values_map = (
self.config.model.full_scale_ranges_reversed if unitless
else self.config.model.full_scale_ranges
)
wrong_values = [v for v in fsr if v not in possible_values_map]
if wrong_values:
raise ValueError(
f"Argument fsr contains invalid values: {wrong_values}. Accepted "
f"values are {list(self.config.model.full_scale_ranges.values())}"
)
str_fsr = ",".join([str(f) for f in fsr])
str_fsr = ",".join([
str(f if unitless else possible_values_map[f]) for f in fsr
])
try:
self.com.send_command(f"FSR,{str_fsr}")
logging.info(f"Set sensors full scale to {fsr} respectively.")
except PfeifferTPGError:
except PfeifferTPGError as e:
logging.error("Setting sensors full scale failed.")
raise
raise e
def set_full_scale_unitless(self, fsr: List[int]) -> None:
"""
Set the full scale range of the attached sensors. See lookup table between
command and corresponding pressure in the device user manual.
:param fsr: list of full scale range values, like [0, 1, 3, 3, 2, 0]
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
self._set_full_scale(fsr, True)
def get_full_scale_unitless(self) -> List[int]:
"""
......@@ -434,20 +456,19 @@ class PfeifferTPG(SingleCommDevice):
logging.error("Query full scale range of all sensors failed.")
raise
answer_values = answer.split(",")
wrong_values = [
v
for v in answer.split(",")
if not v.isdigit()
or int(v) not in self.config.model.full_scale_ranges_reversed
v for v in answer_values
if not self.config.model.is_valid_scale_range_reversed_str(v)
]
if wrong_values:
raise PfeifferTPGError(
f"The controller returned the full scale range values: {answer}. The "
f"values {wrong_values} are invalid. Accepted values are "
f"{list(self.config.model.full_scale_ranges.values())}."
f"{list(self.config.model.full_scale_ranges_reversed.keys())}."
)
fsr = [int(i) for i in answer.split(",")]
fsr = [int(v) for v in answer_values]
logging.info(f"Obtained full scale range of all sensors as {fsr}.")
return fsr
......@@ -459,27 +480,7 @@ class PfeifferTPG(SingleCommDevice):
:raises SerialCommunicationIOError: when communication port is not opened
:raises PfeifferTPGError: if command fails
"""
wrong_values = [v for v in fsr if v not in self.config.model.full_scale_ranges]
if len(fsr) != self.number_of_sensors:
raise ValueError(
f"Argument fsr should be of length {self.number_of_sensors}. "
f"Received length {len(fsr)}."
)
elif wrong_values:
raise ValueError(
f"Argument fsr contains invalid values: {wrong_values}. Accepted "
f"values are {list(self.config.model.full_scale_ranges.keys())}"
)
str_fsr = ",".join([str(self.config.model.full_scale_ranges[f]) for f in fsr])
try:
self.com.send_command(f"FSR,{str_fsr}")
logging.info(f"Set sensors full scale to {fsr} respectively.")
except PfeifferTPGError:
logging.error("Setting sensors full scale range failed.")
raise
self._set_full_scale(fsr, False)
def get_full_scale_mbar(self) -> List[Number]:
"""
......@@ -496,11 +497,10 @@ class PfeifferTPG(SingleCommDevice):
logging.info("Query full scale range of all sensors failed.")
raise
answer_values = answer.split(",")
wrong_values = [
v
for v in answer.split(",")
if not v.isdigit()
or int(v) not in self.config.model.full_scale_ranges_reversed
v for v in answer_values
if not self.config.model.is_valid_scale_range_reversed_str(v)
]
if wrong_values:
raise PfeifferTPGError(
......@@ -510,8 +510,7 @@ class PfeifferTPG(SingleCommDevice):
)
fsr = [
self.config.model.full_scale_ranges_reversed[int(i)]
for i in answer.split(",")
self.config.model.full_scale_ranges_reversed[int(i)] for i in answer_values
]
logging.info(f"Obtained full scale range of sensors as {fsr} mbar.")
return fsr
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment