diff --git a/src/automation/src/main/java/eu/teraflow/automation/AutomationServiceImpl.java b/src/automation/src/main/java/eu/teraflow/automation/AutomationServiceImpl.java index 773c99de6d94b5f8806a8a354b2371c0a6748f9f..54255cf78481bbaa1ecdd81a097a335292d81ed5 100644 --- a/src/automation/src/main/java/eu/teraflow/automation/AutomationServiceImpl.java +++ b/src/automation/src/main/java/eu/teraflow/automation/AutomationServiceImpl.java @@ -50,33 +50,34 @@ public class AutomationServiceImpl implements AutomationService { device -> { final var id = deviceId; - if (!device.isEnabled()) { - LOGGER.infof(MESSAGE, device); - - final var initialConfiguration = - deviceService.getInitialConfiguration(device.getDeviceId()); - - device.enableDevice(); - LOGGER.infof("Enabled device [%s]", id); - - initialConfiguration - .subscribe() - .with( - deviceConfig -> { - device.setDeviceConfiguration(deviceConfig); - final var configuredDeviceIdUni = deviceService.configureDevice(device); - - configuredDeviceIdUni - .subscribe() - .with( - configuredDeviceId -> - LOGGER.infof( - "Device [%s] has been enabled and configured successfully with %s.\n", - id, deviceConfig)); - }); - } else { - LOGGER.infof("%s has been already enabled. Ignoring...", device); + if (device.isEnabled()) { + LOGGER.warnf("%s has already been enabled. Ignoring...", device); + return; } + + LOGGER.infof(MESSAGE, device); + + final var initialConfiguration = + deviceService.getInitialConfiguration(device.getDeviceId()); + + device.enableDevice(); + LOGGER.infof("Enabled device [%s]", id); + + initialConfiguration + .subscribe() + .with( + deviceConfig -> { + device.setDeviceConfiguration(deviceConfig); + final var configuredDeviceIdUni = deviceService.configureDevice(device); + + configuredDeviceIdUni + .subscribe() + .with( + configuredDeviceId -> + LOGGER.infof( + "Device [%s] has been successfully enabled and configured with %s.\n", + id, deviceConfig)); + }); }); 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)); }); diff --git a/src/automation/src/main/java/eu/teraflow/automation/ContextSubscriber.java b/src/automation/src/main/java/eu/teraflow/automation/ContextSubscriber.java index c4d636b6b4dca7241808ade421f32a77861e4d3f..2fc3a3356456b3c1bc55137f686a7e82570a3171 100644 --- a/src/automation/src/main/java/eu/teraflow/automation/ContextSubscriber.java +++ b/src/automation/src/main/java/eu/teraflow/automation/ContextSubscriber.java @@ -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; diff --git a/src/automation/src/main/java/eu/teraflow/automation/context/model/Device.java b/src/automation/src/main/java/eu/teraflow/automation/context/model/Device.java index 77bd3ca5c861713b43faf178c6450e35e6032b3c..1e5563917625a9679feb9e9491990885cc4a3c22 100644 --- a/src/automation/src/main/java/eu/teraflow/automation/context/model/Device.java +++ b/src/automation/src/main/java/eu/teraflow/automation/context/model/Device.java @@ -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; }