Commit 064ffb26 authored by Luis de la Cal's avatar Luis de la Cal
Browse files

Merge branch 'develop' into feat/l3-components

parents d2fbb35b 82ba0a55
Loading
Loading
Loading
Loading
+44 −28
Original line number Diff line number Diff line
@@ -50,7 +50,11 @@ public class AutomationServiceImpl implements AutomationService {
                        device -> {
                            final var id = deviceId;

                            if (!device.isEnabled()) {
                            if (device.isEnabled()) {
                                LOGGER.warnf("%s has already been enabled. Ignoring...", device);
                                return;
                            }

                            LOGGER.infof(MESSAGE, device);

                            final var initialConfiguration =
@@ -71,12 +75,9 @@ public class AutomationServiceImpl implements AutomationService {
                                                        .with(
                                                                configuredDeviceId ->
                                                                        LOGGER.infof(
                                                                                    "Device [%s] has been enabled and configured successfully with %s.\n",
                                                                                "Device [%s] has been successfully enabled and configured with %s.\n",
                                                                                id, deviceConfig));
                                            });
                            } else {
                                LOGGER.infof("%s has been already enabled. Ignoring...", device);
                            }
                        });

        return deserializedDeviceUni;
@@ -92,13 +93,23 @@ public class AutomationServiceImpl implements AutomationService {
                        device -> {
                            final var id = deviceId;

                            if (device.isDisabled()) {
                                LOGGER.warnf("%s has already been disabled. Ignoring...", device);
                                return;
                            }

                            device.disableDevice();
                            LOGGER.infof("Disabled device [%s]", id);

                            LOGGER.infof(MESSAGE, device);

                            final var empty = deviceService.deleteDevice(device.getDeviceId());

                            empty
                                    .subscribe()
                                    .with(emptyMessage -> LOGGER.infof("Device [%s] has been deleted.\n", id));
                                    .with(
                                            emptyMessage ->
                                                    LOGGER.infof("Device [%s] has been successfully deleted.\n", id));
                        });

        return deserializedDeviceUni;
@@ -114,6 +125,11 @@ public class AutomationServiceImpl implements AutomationService {
                        device -> {
                            final var id = deviceId;

                            if (!device.isEnabled()) {
                                LOGGER.warnf("Cannot update disabled device %s. Ignoring...", device);
                                return;
                            }

                            LOGGER.infof(MESSAGE, device);
                            device.setDeviceConfiguration(deviceConfig);
                            final var updatedDeviceIdUni = deviceService.configureDevice(device);
@@ -123,7 +139,7 @@ public class AutomationServiceImpl implements AutomationService {
                                    .with(
                                            configuredDeviceId ->
                                                    LOGGER.infof(
                                                            "Device [%s] has been updated successfully with %s.\n",
                                                            "Device [%s] has been successfully updated with %s.\n",
                                                            id, deviceConfig));
                        });

+5 −3
Original line number Diff line number Diff line
@@ -78,9 +78,11 @@ public class ContextSubscriber {
                                    automationService.deleteDevice(deviceEvent.getDeviceId());
                                    break;
                                case UPDATE:
                                    LOGGER.infof("Received %s for device [%s]", event, deviceId);
                                    automationService.updateDevice(
                                            deviceEvent.getDeviceId(), deviceEvent.getDeviceConfig().orElse(null));
                                    LOGGER.warnf(
                                        "Received %s for device [%s]. " +
                                            "No automation action on an already updated device",
                                            event, deviceId);
                                    break;
                                case UNDEFINED:
                                    logWarningMessage(event, deviceId, eventType);
                                    break;
+8 −0
Original line number Diff line number Diff line
@@ -61,10 +61,18 @@ public class Device {
        return deviceOperationalStatus == DeviceOperationalStatus.ENABLED;
    }

    public boolean isDisabled() {
        return deviceOperationalStatus == DeviceOperationalStatus.DISABLED;
    }

    public void enableDevice() {
        this.deviceOperationalStatus = DeviceOperationalStatus.ENABLED;
    }

    public void disableDevice() {
        this.deviceOperationalStatus = DeviceOperationalStatus.DISABLED;
    }

    public String getDeviceId() {
        return deviceId;
    }
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ from common.rpc_method_wrapper.ServiceExceptions import AlreadyExistsException,
from common.tools.grpc.Tools import grpc_message_to_json, grpc_message_to_json_string
from context.client.ContextClient import ContextClient
from pathcomp.frontend.client.PathCompClient import PathCompClient
from service.service.tools.ContextGetters import get_service
from .tools.ContextGetters import get_service
from .service_handler_api.ServiceHandlerFactory import ServiceHandlerFactory
from .task_scheduler.TaskScheduler import TasksScheduler

+9 −6
Original line number Diff line number Diff line
@@ -14,21 +14,23 @@

import logging, operator
from enum import Enum
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Set, Tuple
from common.proto.context_pb2 import Device, Service
from common.tools.grpc.Tools import grpc_message_to_json_string
from service.service.service_handler_api._ServiceHandler import _ServiceHandler
from .Exceptions import (
    UnsatisfiedFilterException, UnsupportedServiceHandlerClassException, UnsupportedFilterFieldException,
    UnsupportedFilterFieldValueException)
from .FilterFields import FILTER_FIELD_ALLOWED_VALUES, FilterFieldEnum

if TYPE_CHECKING:
    from service.service.service_handler_api._ServiceHandler import _ServiceHandler

LOGGER = logging.getLogger(__name__)

class ServiceHandlerFactory:
    def __init__(self, service_handlers : List[Tuple[type, List[Dict[FilterFieldEnum, Any]]]]) -> None:
        # Dict{field_name => Dict{field_value => Set{ServiceHandler}}}
        self.__indices : Dict[str, Dict[str, Set[_ServiceHandler]]] = {}
        self.__indices : Dict[str, Dict[str, Set['_ServiceHandler']]] = {}

        for service_handler_class,filter_field_sets in service_handlers:
            for filter_fields in filter_field_sets:
@@ -36,6 +38,7 @@ class ServiceHandlerFactory:
                self.register_service_handler_class(service_handler_class, **filter_fields)

    def register_service_handler_class(self, service_handler_class, **filter_fields):
        from service.service.service_handler_api._ServiceHandler import _ServiceHandler
        if not issubclass(service_handler_class, _ServiceHandler):
            raise UnsupportedServiceHandlerClassException(str(service_handler_class))

@@ -59,12 +62,12 @@ class ServiceHandlerFactory:
                field_indice_service_handlers = field_indice.setdefault(field_value, set())
                field_indice_service_handlers.add(service_handler_class)

    def get_service_handler_class(self, **filter_fields) -> _ServiceHandler:
    def get_service_handler_class(self, **filter_fields) -> '_ServiceHandler':
        supported_filter_fields = set(FILTER_FIELD_ALLOWED_VALUES.keys())
        unsupported_filter_fields = set(filter_fields.keys()).difference(supported_filter_fields)
        if len(unsupported_filter_fields) > 0: raise UnsupportedFilterFieldException(unsupported_filter_fields)

        candidate_service_handler_classes : Dict[_ServiceHandler, int] = None # num. filter hits per service_handler
        candidate_service_handler_classes : Dict['_ServiceHandler', int] = None # num. filter hits per service_handler
        for field_name, field_values in filter_fields.items():
            field_indice = self.__indices.get(field_name)
            if field_indice is None: continue
@@ -109,7 +112,7 @@ def get_common_device_drivers(drivers_per_device : List[Set[int]]) -> Set[int]:

def get_service_handler_class(
    service_handler_factory : ServiceHandlerFactory, service : Service, connection_devices : Dict[str, Device]
) -> Optional[_ServiceHandler]:
) -> Optional['_ServiceHandler']:

    str_service_key = grpc_message_to_json_string(service.service_id)

Loading