Search in sources :

Example 16 with Ssld

use of org.opensmartgridplatform.domain.core.entities.Ssld in project open-smart-grid-platform by OSGP.

the class SsldConverter method convertFrom.

@Override
public Ssld convertFrom(final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Device source, final Type<Ssld> destinationType, final MappingContext context) {
    if (source == null) {
        return null;
    }
    final Ssld destination = this.helper.initEntity(source);
    final List<org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting> deviceOutputSettings = new ArrayList<>();
    for (final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.DeviceOutputSetting deviceOutputSetting : source.getOutputSettings()) {
        final org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting newDeviceOutputSetting = new org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting(deviceOutputSetting.getInternalId(), deviceOutputSetting.getExternalId(), deviceOutputSetting.getRelayType() == null ? null : org.opensmartgridplatform.domain.core.valueobjects.RelayType.valueOf(deviceOutputSetting.getRelayType().name()), deviceOutputSetting.getAlias());
        deviceOutputSettings.add(newDeviceOutputSetting);
    }
    destination.updateOutputSettings(deviceOutputSettings);
    if (source.isPublicKeyPresent() != null) {
        destination.setPublicKeyPresent(source.isPublicKeyPresent());
    }
    if (source.isHasSchedule() != null) {
        destination.setHasSchedule(source.isHasSchedule());
    }
    if (source.isActivated() != null) {
        destination.setActivated(source.isActivated());
    }
    if (source.getDeviceLifecycleStatus() != null) {
        destination.setDeviceLifecycleStatus(DeviceLifecycleStatus.valueOf(source.getDeviceLifecycleStatus().name()));
    }
    // clearing the existing Eans to prevent duplication
    destination.setEans(new ArrayList<Ean>());
    for (final org.opensmartgridplatform.adapter.ws.schema.core.devicemanagement.Ean ean : source.getEans()) {
        final Ean newEan = new Ean(destination, ean.getCode(), ean.getDescription());
        destination.getEans().add(newEan);
    }
    return destination;
}
Also used : ArrayList(java.util.ArrayList) DeviceOutputSetting(org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting) DeviceOutputSetting(org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting) Ean(org.opensmartgridplatform.domain.core.entities.Ean) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld)

Example 17 with Ssld

use of org.opensmartgridplatform.domain.core.entities.Ssld in project open-smart-grid-platform by OSGP.

the class EventNotificationMessageService method handleSwitchDeviceEvents.

private void handleSwitchDeviceEvents(final Device device, final List<Event> switchDeviceEvents) throws UnknownEntityException {
    LOGGER.info("handleSwitchDeviceEvents() called for device: {} with lightSwitchingEvents.size(): {}", device.getDeviceIdentification(), switchDeviceEvents.size());
    if (switchDeviceEvents.isEmpty()) {
        return;
    }
    // Determine light and tariff relays for SSLD.
    final Ssld ssld = this.eventNotificationHelperService.findSsld(device.getId());
    final Set<Integer> indexesLightRelays = new TreeSet<>();
    final Set<Integer> indexesTariffRelays = new TreeSet<>();
    for (final DeviceOutputSetting deviceOutputSetting : ssld.getOutputSettings()) {
        if (deviceOutputSetting.getOutputType().equals(RelayType.LIGHT)) {
            indexesLightRelays.add(deviceOutputSetting.getExternalId());
        }
        if (deviceOutputSetting.getOutputType().equals(RelayType.TARIFF) || deviceOutputSetting.getOutputType().equals(RelayType.TARIFF_REVERSED)) {
            indexesTariffRelays.add(deviceOutputSetting.getExternalId());
        }
    }
    this.printRelayIndexes(indexesLightRelays, indexesTariffRelays, device.getDeviceIdentification());
    final Map<Integer, RelayStatus> lastRelayStatusPerIndex = new TreeMap<>();
    for (final Event lightSwitchingEvent : switchDeviceEvents) {
        final Integer index = lightSwitchingEvent.getIndex();
        if (index == 0) {
            this.handleLightSwitchingEventForIndex0(indexesLightRelays, device, lightSwitchingEvent, lastRelayStatusPerIndex);
        } else {
            this.createRelayStatus(device, lightSwitchingEvent, index, lastRelayStatusPerIndex);
        }
    }
    this.printRelayStatuses(lastRelayStatusPerIndex, device.getDeviceIdentification());
    if (!lastRelayStatusPerIndex.isEmpty()) {
        LOGGER.info("calling ssld.updateSwitchingEventRelayStatuses() for device: {} with lastRelayStatusPerIndex.size(): {}", ssld.getDeviceIdentification(), lastRelayStatusPerIndex.size());
        ssld.updateSwitchingEventRelayStatuses(lastRelayStatusPerIndex);
        this.eventNotificationHelperService.saveSsld(ssld);
        this.sendRequestMessageToDomainCore(MessageType.RELAY_STATUS_UPDATED_EVENTS.name(), ssld.getDeviceIdentification(), null);
    }
}
Also used : RelayStatus(org.opensmartgridplatform.domain.core.entities.RelayStatus) TreeSet(java.util.TreeSet) Event(org.opensmartgridplatform.domain.core.entities.Event) DeviceOutputSetting(org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting) TreeMap(java.util.TreeMap) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld)

Example 18 with Ssld

use of org.opensmartgridplatform.domain.core.entities.Ssld in project open-smart-grid-platform by OSGP.

the class EventNotificationMessageService method handleSwitchingEvent.

private void handleSwitchingEvent(final String deviceIdentification, final Date dateTime, final EventType eventType, final int index) throws UnknownEntityException {
    final Ssld ssld = this.eventNotificationHelperService.findSsld(deviceIdentification);
    // index.
    if (index == 0) {
        for (final DeviceOutputSetting deviceOutputSetting : ssld.getOutputSettings()) {
            if (deviceOutputSetting.getOutputType().equals(RelayType.LIGHT)) {
                this.updateRelayStatusForEvent(deviceOutputSetting.getExternalId(), ssld, dateTime, eventType);
            }
        }
    } else {
        this.updateRelayStatusForEvent(index, ssld, dateTime, eventType);
    }
    this.eventNotificationHelperService.saveSsld(ssld);
    this.sendRequestMessageToDomainCore(MessageType.RELAY_STATUS_UPDATED_EVENTS.name(), deviceIdentification, null);
}
Also used : DeviceOutputSetting(org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld)

Example 19 with Ssld

use of org.opensmartgridplatform.domain.core.entities.Ssld in project open-smart-grid-platform by OSGP.

the class DeviceDomainService method searchActiveDevice.

public Device searchActiveDevice(@Identification final String deviceIdentification, final ComponentType osgpComponent) throws FunctionalException {
    final Device device = this.searchDevice(deviceIdentification);
    // For smartmeters, we want to able to communicate no matter what the
    // device life cycle status is.
    final Optional<SmartMeter> smartMeter = this.smartMeterRepository.findById(device.getId());
    if (smartMeter.isPresent()) {
        return device;
    }
    if (!device.isActivated() || !device.getDeviceLifecycleStatus().equals(DeviceLifecycleStatus.IN_USE)) {
        throw new FunctionalException(FunctionalExceptionType.INACTIVE_DEVICE, osgpComponent, new InactiveDeviceException(deviceIdentification));
    }
    // Note: since this code is still specific for SSLD / PSLD, this null
    // check is needed.
    final Optional<Ssld> ssld = this.ssldRepository.findById(device.getId());
    if (ssld.isPresent() && !ssld.get().isPublicKeyPresent()) {
        throw new FunctionalException(FunctionalExceptionType.UNREGISTERED_DEVICE, osgpComponent, new UnregisteredDeviceException(deviceIdentification));
    }
    return device;
}
Also used : UnregisteredDeviceException(org.opensmartgridplatform.domain.core.exceptions.UnregisteredDeviceException) Device(org.opensmartgridplatform.domain.core.entities.Device) SmartMeter(org.opensmartgridplatform.domain.core.entities.SmartMeter) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) InactiveDeviceException(org.opensmartgridplatform.domain.core.exceptions.InactiveDeviceException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld)

Example 20 with Ssld

use of org.opensmartgridplatform.domain.core.entities.Ssld in project open-smart-grid-platform by OSGP.

the class DeviceManagementService method setDeviceAlias.

@Transactional(value = "writableTransactionManager")
public void setDeviceAlias(@Identification final String organisationIdentification, final String deviceIdentification, final String deviceAlias, final List<DeviceOutputSetting> newDeviceOutputSettings) throws FunctionalException {
    final Ssld existingSsld = this.writableSsldRepository.findByDeviceIdentification(deviceIdentification);
    if (existingSsld == null) {
        // device does not exist
        LOGGER.info("Device does not exist, cannot set Alias.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceIdentification));
    }
    // Check to see if the organization is authorized for SET_DEVICE_ALIASES
    final Organisation organisation = this.organisationRepository.findByOrganisationIdentification(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, existingSsld, DeviceFunction.SET_DEVICE_ALIASES);
    if (deviceAlias != null) {
        existingSsld.setAlias(deviceAlias);
        this.writableDeviceRepository.save(existingSsld);
    }
    if (newDeviceOutputSettings != null && !newDeviceOutputSettings.isEmpty()) {
        this.updateRelayAliases(newDeviceOutputSettings, existingSsld);
    }
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) Ssld(org.opensmartgridplatform.domain.core.entities.Ssld) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)50 DeviceOutputSetting (org.opensmartgridplatform.domain.core.entities.DeviceOutputSetting)16 Device (org.opensmartgridplatform.domain.core.entities.Device)13 ArrayList (java.util.ArrayList)12 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)11 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)10 Test (org.junit.jupiter.api.Test)9 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)9 Given (io.cucumber.java.en.Given)8 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)8 RelayType (org.opensmartgridplatform.domain.core.valueobjects.RelayType)6 Transactional (org.springframework.transaction.annotation.Transactional)6 Date (java.util.Date)5 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)5 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)5 RelayStatus (org.opensmartgridplatform.domain.core.entities.RelayStatus)4 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)4 Address (org.opensmartgridplatform.domain.core.valueobjects.Address)4 GpsCoordinates (org.opensmartgridplatform.domain.core.valueobjects.GpsCoordinates)4 NoDeviceResponseException (org.opensmartgridplatform.shared.exceptionhandling.NoDeviceResponseException)4