diff --git a/src/handlers/tests/test_handlers.py b/src/handlers/tests/test_handlers.py index e1a4d792..d4084b92 100644 --- a/src/handlers/tests/test_handlers.py +++ b/src/handlers/tests/test_handlers.py @@ -1,15 +1,22 @@ +import logging import pytest -from django import conf +from handlers import sensors from django.conf import settings - from status.models import Location +from scos_actions.hardware.sensor import Sensor +from scos_actions.metadata.utils import construct_geojson_point +from scos_actions.signals import register_sensor +logger = logging.getLogger(__name__) @pytest.mark.django_db def test_db_location_update_handler(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = {} + location = construct_geojson_point(-105.7, 40.5, 0) + sensor = Sensor(location = location) + register_sensor.send(sensor, sensor = sensor) + logger.debug(f"len(sensors) sensors registered") + logger.debug(f"sigan: {sensors[0].signal_analyzer}") + logger.debug(f"Registered sigan = {sensors}") location = Location() location.gps = False location.height = 10 @@ -18,17 +25,20 @@ def test_db_location_update_handler(): location.description = "test" location.active = True location.save() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "test" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == 100 + assert sensor.location["coordinates"][1] == -1 + assert sensor.location["coordinates"][2] == 10 + @pytest.mark.django_db def test_db_location_update_handler_current_location_none(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = None + sensor = Sensor() + register_sensor.send(sensor, sensor = sensor) + logger.debug(f"len(sensors) sensors registered") + logger.debug(f"sigan: {sensors[0].signal_analyzer}") + logger.debug(f"Registered sigan = {sensors}") location = Location() location.gps = False location.height = 10 @@ -37,50 +47,39 @@ def test_db_location_update_handler_current_location_none(): location.description = "test" location.active = True location.save() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "test" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == 100 + assert sensor.location["coordinates"][1] == -1 + assert sensor.location["coordinates"][2] == 10 @pytest.mark.django_db def test_db_location_update_handler_not_active(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = {} + location = construct_geojson_point(-105.7, 40.5, 0) + sensor = Sensor(location=location) + register_sensor.send(sensor, sensor=sensor) + logger.debug(f"len(sensors) sensors registered") + logger.debug(f"sigan: {sensors[0].signal_analyzer}") + logger.debug(f"Registered sigan = {sensors}") location = Location() location.gps = False location.height = 10 location.longitude = 100 location.latitude = -1 + location.description = "" location.active = False - location.description = "test" - location.save() - assert len(capabilities["sensor"]["location"]) == 0 - - -@pytest.mark.django_db -def test_db_location_update_handler_no_description(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = {} - location = Location() - location.gps = False - location.height = 10 - location.longitude = 100 - location.latitude = -1 location.save() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == -105.7 + assert sensor.location["coordinates"][1] == 40.5 + assert sensor.location["coordinates"][2] == 0 @pytest.mark.django_db def test_db_location_deleted_handler(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = {} + location = construct_geojson_point(-105.7, 40.5, 0) + sensor = Sensor(location=location) + register_sensor.send(sensor, sensor=sensor) location = Location() location.gps = False location.height = 10 @@ -89,19 +88,19 @@ def test_db_location_deleted_handler(): location.description = "test" location.active = True location.save() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "test" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == 100 + assert sensor.location["coordinates"][1] == -1 + assert sensor.location["coordinates"][2] == 10 location.delete() - assert capabilities["sensor"]["location"] is None + assert sensor.location is None @pytest.mark.django_db def test_db_location_deleted_inactive_handler(): - capabilities = settings.CAPABILITIES - capabilities["sensor"] = {} - capabilities["sensor"]["location"] = {} + location = construct_geojson_point(-105.7, 40.5, 0) + sensor = Sensor(location=location) + register_sensor.send(sensor, sensor=sensor) location = Location() location.gps = False location.height = 10 @@ -110,13 +109,13 @@ def test_db_location_deleted_inactive_handler(): location.description = "test" location.active = True location.save() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "test" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == 100 + assert sensor.location["coordinates"][1] == -1 + assert sensor.location["coordinates"][2] == 10 location.active = False location.delete() - assert capabilities["sensor"]["location"]["x"] == 100 - assert capabilities["sensor"]["location"]["y"] == -1 - assert capabilities["sensor"]["location"]["z"] == 10 - assert capabilities["sensor"]["location"]["description"] == "test" + assert sensor.location is not None + assert sensor.location["coordinates"][0] == 100 + assert sensor.location["coordinates"][1] == -1 + assert sensor.location["coordinates"][2] == 10 diff --git a/src/initialization/tests/test_initialization.py b/src/initialization/tests/test_initialization.py index 0225ab39..d74b2f41 100644 --- a/src/initialization/tests/test_initialization.py +++ b/src/initialization/tests/test_initialization.py @@ -1,9 +1,10 @@ from src.initialization import load_preselector + def test_load_preselector(): - preselector = load_preselector( + preselector = load_preselector(preselector_config= {"name": "test", "base_url": "http://127.0.0.1"}, - "its_preselector.web_relay_preselector", - "WebRelayPreselector", + module="its_preselector.web_relay_preselector", + preselector_class_name = "WebRelayPreselector", sensor_definition={} ) assert preselector is not None \ No newline at end of file diff --git a/src/scheduler/tests/utils.py b/src/scheduler/tests/utils.py index 203a7a33..a28e2c47 100644 --- a/src/scheduler/tests/utils.py +++ b/src/scheduler/tests/utils.py @@ -11,10 +11,14 @@ from django.conf import settings from scos_actions.hardware.mocks.mock_sigan import MockSignalAnalyzer +logger = logging.getLogger(__name__) actions = settings.ACTIONS +if actions is not None: + logger.debug(f"Have {len(actions)} actions") +else: + logger.warning("Actions is None") BAD_ACTION_STR = "testing expected failure" -logger = logging.getLogger(__name__) logger.debug("*************** scos-sensor/scheduler/test/utils ***********") @@ -62,7 +66,7 @@ def advance_testclock(iterator, n): def simulate_scheduler_run(n=1): s = Scheduler() - s.signal_analyzer = MockSignalAnalyzer() + s.sensor = Sensor(signal_analyzer=MockSignalAnalyzer()) for _ in range(n): advance_testclock(s.timefn, 1) s.run(blocking=False) @@ -107,7 +111,7 @@ def create_action(): """ flag = threading.Event() - def cb(schedule_entry_json, task_id): + def cb(sensor, schedule_entry_json, task_id): flag.set() return "set flag" @@ -122,7 +126,7 @@ def cb(schedule_entry_json, task_id): def create_bad_action(): - def bad_action(sigan, gps, schedule_entry_json, task_id): + def bad_action(sensor, schedule_entry_json, task_id): raise Exception(BAD_ACTION_STR) actions["bad_action"] = bad_action diff --git a/src/sensor/wsgi.py b/src/sensor/wsgi.py index b725c8e1..d7cabd6e 100644 --- a/src/sensor/wsgi.py +++ b/src/sensor/wsgi.py @@ -15,8 +15,6 @@ import importlib import logging from django.core.wsgi import get_wsgi_application -from environs import Env -from scos_actions.signals import register_component_with_status os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sensor.settings") django.setup() # this is necessary because we need to handle our own thread @@ -31,18 +29,71 @@ faulthandler.enable() application = get_wsgi_application() +logger = logging.get(__name__) if not settings.IN_DOCKER: # Normally scheduler is started by gunicorn worker process - env = Env() - sigan_module_setting = env("SIGAN_MODULE") + from scheduler import scheduler + from initialization import ( + load_preselector, + load_switches, + ) + from initialization import ( + get_sensor_calibration, + get_sigan_calibration + ) + from django.conf import settings + from status.models import Location + from scos_actions.hardware.sensor import Sensor + from scos_actions.metadata.utils import construct_geojson_point + from scos_actions.signals import register_component_with_status + from scos_actions.signals import register_signal_analyzer + from scos_actions.signals import register_sensor + + sigan_module_setting = settings.SIGAN_MODULE sigan_module = importlib.import_module(sigan_module_setting) - logger = logging.getLogger(__name__) - logger.info("Creating " + env("SIGAN_CLASS") + " from " + env("SIGAN_MODULE")) - sigan_constructor = getattr(sigan_module, env("SIGAN_CLASS")) - sigan = sigan_constructor() + logger.info("Creating " + settings.SIGAN_CLASS + " from " + settings.SIGAN_MODULE) + sigan_constructor = getattr(sigan_module, settings.SIGAN_CLASS) + sensor_cal = get_sensor_calibration(settings.SENSOR_CALIBRATION_FILE, settings.DEFAULT_CALIBRATION_FILE) + sigan_cal = get_sigan_calibration(settings.SIGAN_CALIBRATION_FILE, settings.DEFAULT_CALIBRATION_FILE) + sigan = sigan_constructor(sensor_cal=sensor_cal, sigan_cal=sigan_cal) register_component_with_status.send(sigan, component=sigan) - scheduler.thread.signal_analyzer = sigan + register_signal_analyzer.send(sigan, signal_analyzer=sigan) + + switches = load_switches(settings.SWITCH_CONFIGS_DIR) + capabilities = settings.CAPABILITIES + preselector = load_preselector(settings.PRESELECTOR_CONFIG, settings.PRESELECTOR_MODULE, settings.PRESELECTOR_CLASS, capabilities["sensor"]) + location = None + if "location" in capabilities["sensor"]: + try: + sensor_loc = capabilities["sensor"].pop("location") + try: + #if there is an active database location, use it over the value in the sensor def. + db_location = Location.objects.get(active=True) + location = construct_geojson_point(db_location.longitude, db_location.latitude, db_location.height) + except Location.DoesNotExist: + # This should never occur because status/migrations/0003_auto_20211217_2229.py + # will load the No DB location. Use sensor def location and save to DB. + location = construct_geojson_point( + sensor_loc["x"], + sensor_loc["y"], + sensor_loc["z"] if "z" in sensor_loc else None, + ) + #Save the sensor location from the sensor def to the database + db_location = Location() + db_location.longitude = sensor_loc["x"] + db_location.latitude = sensor_loc["y"] + db_location.height = sensor_loc["z"] + db_location.gps = False + db_location.description = sensor_loc["description"] + db_location.save() + except: + logger.exception("Failed to get sensor location from sensor definition.") + + + sensor = Sensor(signal_analyzer=sigan, preselector = preselector, switches = switches, capabilities = capabilities, location = location) + scheduler.thread.sensor = sensor + register_sensor.send(sensor, sensor=sensor) scheduler.thread.start()