From 073b1ef5c6f368f0fcd9ec1c8486914339f49c34 Mon Sep 17 00:00:00 2001 From: Anthony Romaniello Date: Tue, 5 Mar 2024 17:24:39 -0500 Subject: [PATCH] add calibrate_on_startup --- env.template | 2 +- src/initialization/sensor_loader.py | 30 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/env.template b/env.template index 2bdf6157..7bf49093 100644 --- a/env.template +++ b/env.template @@ -92,7 +92,7 @@ SSL_KEY_PATH=sensor01.pem # sensor calibration on startup, if no onboard calibration data # is available on startup. The specified action must be available. CALIBRATE_ON_STARTUP=true -CALIBRATION_ACTION=SEA_CBRS_Calibrate_Baseline +STARTUP_CALIBRATION_ACTION=SEA_CBRS_Calibrate_Baseline # Debug dependant settings if $DEBUG; then diff --git a/src/initialization/sensor_loader.py b/src/initialization/sensor_loader.py index 63e130ba..d1281033 100644 --- a/src/initialization/sensor_loader.py +++ b/src/initialization/sensor_loader.py @@ -5,6 +5,7 @@ from typing import Optional, Union from django.conf import settings +from environs import Env from its_preselector.configuration_exception import ConfigurationException from its_preselector.controlbyweb_web_relay import ControlByWebWebRelay from its_preselector.preselector import Preselector @@ -17,7 +18,10 @@ from utils.signals import register_component_with_status +from ..capabilities import actions_by_name + logger = logging.getLogger(__name__) +env = Env() class SensorLoader: @@ -72,9 +76,7 @@ def load_sensor(sensor_capabilities: dict) -> Sensor: check_for_required_sigan_settings() sigan_module_setting = settings.SIGAN_MODULE sigan_module = importlib.import_module(sigan_module_setting) - logger.info( - "Creating " + settings.SIGAN_CLASS + " from " + settings.SIGAN_MODULE - ) + logger.info(f"Creating {settings.SIGAN_CLASS} from {settings.SIGAN_MODULE}") sigan_constructor = getattr(sigan_module, settings.SIGAN_CLASS) sigan = sigan_constructor(switches=switches) register_component_with_status.send(sigan, component=sigan) @@ -106,10 +108,24 @@ def load_sensor(sensor_capabilities: dict) -> Sensor: sensor_cal = get_calibration(settings.SENSOR_CALIBRATION_FILE, "sensor") if sensor_cal is not None: sensor.sensor_calibration = sensor_cal - # Now run the calibration action defined in the environment - # TODO - # This will create an onboard_cal file if needed, and set it - # as the sensor's sensor_calibration. + + # Now run the calibration action defined in the environment + # This will create an onboard_cal file if needed, and set it + # as the sensor's sensor_calibration. + if env("CALIBRATE_ON_STARTUP") or sensor.sensor_calibration is None: + try: + cal_action = actions_by_name[env("STARTUP_CALIBRATION_ACTION")] + cal_action(sensor=sensor, schedule_entry=None, task_id=None) + except KeyError: + logger.exception( + f"Specified startup calibration action does not exist." + ) + else: + logger.debug( + "Skipping startup calibration since sensor_calibration exists and" + + "CALIBRATE_ON_STARTUP environment variable is False" + ) + # Now load the differential calibration, if it exists differential_cal = get_calibration( settings.DIFFERENTIAL_CALIBRATION_FILE,