Skip to content

Config Parser

get_sensors(config)

Get the list of Sensor objects from the previously loaded config

Parameters:

Name Type Description Default
config object

The config object

required

Returns:

Type Description
list[GenericSensor]

Returns a list of Sensors (GenericSensor)

Source code in software/sailowtech_ctd/methods/config_parser.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def get_sensors(config: object) -> list[GenericSensor]:
    """
    Get the list of Sensor objects from the previously loaded config
    :param config: The config object
    :return: Returns a list of Sensors (GenericSensor)
    """
    if "sensors" not in config: logger.error("No sensors configured in Config!"); exit(1)
    sensors = []
    for sensor in config["sensors"]:
        n = sensor
        sensor = config["sensors"][sensor]
        if "sensor-type" not in sensor: logger.error(f"Attribute sensor-type not defined for sensor {n}"); exit(1)
        if "name" not in sensor: logger.error(f"Attribute name not defined for sensor {n}"); exit(1)
        if "address" not in sensor: logger.error(f"Attribute address not defined for sensor {n}"); exit(1)
        min_delay = sensor["min-delay"] if "min-delay" in sensor else 0
        match sensor["sensor-type"]:
            case "ATLAS_EZO_DO":
                s = AtlasSensor(Sensor.ATLAS_EZO_DO, sensor["name"], sensor["address"], min_delay)
                sensors.append(s)
            case "ATLAS_EZO_CONDUCTIVITY":
                s = AtlasSensor(Sensor.ATLAS_EZO_CONDUCTIVITY, sensor["name"], sensor["address"], min_delay)
                sensors.append(s)
            case "ATLAS_EZO_TEMP":
                s = AtlasSensor(Sensor.ATLAS_EZO_TEMP, sensor["name"], sensor["address"], min_delay)
                sensors.append(s)
            case "BLUEROBOTICS_BAR30_DEPTH":
                s = DepthSensor(sensor["name"], sensor["address"], min_delay)
                sensors.append(s)
            case "MOCK_SENSOR":
                if "metric-type" not in sensor: logger.error(f"Attribute metric-type not defined for sensor {n}"); exit(1)
                s = MockSensor(SensorBrand.MockBrand, Sensor.MOCK_SENSOR, sensor["name"], sensor["address"], sensor["min"], sensor["max"], min_delay, Metric(sensor["metric-type"].lower()))
                sensors.append(s)
            case _:
                logger.warning(f"Sensor {n} was not set up. Wrong name?")

    return sensors

is_mock(config)

Check if the config file has the mock attribute, which means the software is running in a test environment

Parameters:

Name Type Description Default
config object

The config object

required

Returns:

Type Description
bool

Returns a boolean to check if it is a mock config

Source code in software/sailowtech_ctd/methods/config_parser.py
29
30
31
32
33
34
35
36
37
38
def is_mock(config: object) -> bool:
    """
    Check if the config file has the mock attribute, which means the software is running in a test environment
    :param config: The config object
    :return: Returns a boolean to check if it is a mock config
    """
    if "mock" in config:
        return config["mock"]
    else:
        return False

load_file_to_yaml(path)

Tries to load the config file at the specified path

Parameters:

Name Type Description Default
path str

The path to the config file

required

Returns:

Type Description
object

Returns a python object of the yaml file to be further used

Source code in software/sailowtech_ctd/methods/config_parser.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def load_file_to_yaml(path: str) -> object:
    """
    Tries to load the config file at the specified path
    :param path: The path to the config file
    :return: Returns a python object of the yaml file to be further used
    """
    try:
        path = os.path.abspath(os.path.join(pathlib.Path(os.path.dirname(__file__)).parent.parent.parent, path))
        config = open(path)
    except FileNotFoundError:
        logger.error(f"Config file not found at {path}")
        exit(1)
    else:
        with config:
            return yaml.safe_load(config)