Skip to content
Snippets Groups Projects
Commit b4ef3fe7 authored by Vasilis Katopodis's avatar Vasilis Katopodis
Browse files

fix: remove duplicate subscription in automation service

parent 8cd8e0d9
No related branches found
No related tags found
No related merge requests found
...@@ -42,53 +42,51 @@ public class AutomationServiceImpl implements AutomationService { ...@@ -42,53 +42,51 @@ public class AutomationServiceImpl implements AutomationService {
@Override @Override
public Uni<Device> addDevice(String deviceId) { public Uni<Device> addDevice(String deviceId) {
final var deserializedDeviceUni = contextService.getDevice(deviceId); final var deserializedDeviceUni = contextService.getDevice(deviceId);
deserializedDeviceUni deserializedDeviceUni
.onFailure() .onFailure()
.recoverWithNull() .recoverWithNull()
.subscribe() .invoke(device -> processDevice(device, deviceId));
.with(
device -> {
final var id = deviceId;
if (device == null) {
LOGGER.warnf("%s is null. Ignoring...", device);
return;
}
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; return deserializedDeviceUni;
} }
private void processDevice(Device device, String deviceId) {
if (device == null) {
LOGGER.warnf("%s is null. Ignoring...", device);
return;
}
if (device.isEnabled()) {
LOGGER.warnf("%s has already been enabled. Ignoring...", device);
return;
}
enableAndConfigureDevice(device, deviceId);
}
private void enableAndConfigureDevice(Device device, String deviceId) {
final var initialConfiguration = deviceService.getInitialConfiguration(device.getDeviceId());
device.enableDevice();
LOGGER.infof("Enabled device [%s]", deviceId);
initialConfiguration
.subscribe()
.with(deviceConfig -> setDeviceConfiguration(device, deviceId, deviceConfig));
}
private void setDeviceConfiguration(Device device, String deviceId, DeviceConfig 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",
deviceId, deviceConfig));
}
@Override @Override
public Uni<Device> deleteDevice(String deviceId) { public Uni<Device> deleteDevice(String deviceId) {
final var deserializedDeviceUni = contextService.getDevice(deviceId); final var deserializedDeviceUni = contextService.getDevice(deviceId);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment