Search in sources :

Example 1 with LightValueDto

use of com.alliander.osgp.dto.valueobjects.LightValueDto in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850SsldDeviceService method runSelfTest.

@Override
public void runSelfTest(final DeviceRequest deviceRequest, final DeviceResponseHandler deviceResponseHandler, final boolean startOfTest) throws JMSException {
    // Assuming all goes well.
    final DeviceMessageStatus status = DeviceMessageStatus.OK;
    DeviceConnection deviceConnection = null;
    try {
        deviceConnection = this.connectToDevice(deviceRequest);
        // Getting the SSLD for the device output-settings.
        final Ssld ssld = this.ssldDataService.findDevice(deviceRequest.getDeviceIdentification());
        // This list will contain the external indexes of all light relays.
        // It's used to interpret the deviceStatus data later on.
        final List<Integer> lightRelays = new ArrayList<>();
        LOGGER.info("Turning all lights relays {}", startOfTest ? "on" : "off");
        final Iec61850SetLightCommand iec61850SetLightCommand = new Iec61850SetLightCommand();
        // startOfTest.
        for (final DeviceOutputSetting deviceOutputSetting : this.ssldDataService.findByRelayType(ssld, RelayType.LIGHT)) {
            lightRelays.add(deviceOutputSetting.getExternalId());
            if (!iec61850SetLightCommand.switchLightRelay(this.iec61850Client, deviceConnection, deviceOutputSetting.getInternalId(), startOfTest)) {
                throw new ProtocolAdapterException(String.format("Failed to switch light relay during self-test with internal index: %d for device: %s", deviceOutputSetting.getInternalId(), deviceRequest.getDeviceIdentification()));
            }
        }
        // Sleep and wait.
        this.selfTestSleep();
        // Getting the status.
        final DeviceStatusDto deviceStatus = new Iec61850GetStatusCommand().getStatusFromDevice(this.iec61850Client, deviceConnection, ssld);
        LOGGER.info("Fetching and checking the devicestatus");
        // Checking to see if all light relays have the correct state.
        for (final LightValueDto lightValue : deviceStatus.getLightValues()) {
            if (lightRelays.contains(lightValue.getIndex()) && lightValue.isOn() != startOfTest) {
                // request failed.
                throw new ProtocolAdapterException("not all relays are ".concat(startOfTest ? "on" : "off"));
            }
        }
        LOGGER.info("All lights relays are {}, returning OK", startOfTest ? "on" : "off");
        this.createSuccessfulDefaultResponse(deviceRequest, deviceResponseHandler, status);
    } catch (final ConnectionFailureException se) {
        LOGGER.info("Original ConnectionFailureException message: {}", se.getMessage());
        final ConnectionFailureException seGeneric = new ConnectionFailureException("Connection failure", se);
        this.handleConnectionFailureException(deviceRequest, deviceResponseHandler, seGeneric);
    } catch (final Exception e) {
        LOGGER.info("Selftest failure", e);
        final TechnicalException te = new TechnicalException(ComponentType.PROTOCOL_IEC61850, "Selftest failure - " + e.getMessage());
        this.handleException(deviceRequest, deviceResponseHandler, te);
    }
    this.iec61850DeviceConnectionService.disconnect(deviceConnection, deviceRequest);
}
Also used : TechnicalException(com.alliander.osgp.shared.exceptionhandling.TechnicalException) ArrayList(java.util.ArrayList) DeviceOutputSetting(com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) FunctionalException(com.alliander.osgp.shared.exceptionhandling.FunctionalException) JMSException(javax.jms.JMSException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) TechnicalException(com.alliander.osgp.shared.exceptionhandling.TechnicalException) Iec61850SetLightCommand(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.services.commands.Iec61850SetLightCommand) LightValueDto(com.alliander.osgp.dto.valueobjects.LightValueDto) DeviceMessageStatus(com.alliander.osgp.adapter.protocol.iec61850.device.DeviceMessageStatus) Iec61850GetStatusCommand(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.services.commands.Iec61850GetStatusCommand) DeviceStatusDto(com.alliander.osgp.dto.valueobjects.DeviceStatusDto) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) DeviceConnection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) Ssld(com.alliander.osgp.core.db.api.iec61850.entities.Ssld)

Example 2 with LightValueDto

use of com.alliander.osgp.dto.valueobjects.LightValueDto in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850SetScheduleCommand method createScheduleEntries.

/**
     * Returns a map of schedule entries, grouped by the internal index.
     */
private Map<Integer, List<ScheduleEntry>> createScheduleEntries(final List<ScheduleDto> scheduleList, final Ssld ssld, final RelayTypeDto relayTypeDto, final SsldDataService ssldDataService) throws FunctionalException {
    final Map<Integer, List<ScheduleEntry>> relaySchedulesEntries = new HashMap<>();
    final RelayType relayType = RelayType.valueOf(relayTypeDto.name());
    for (final ScheduleDto schedule : scheduleList) {
        for (final LightValueDto lightValue : schedule.getLightValue()) {
            final List<Integer> indexes = new ArrayList<>();
            if (lightValue.getIndex() == 0 && (RelayType.TARIFF.equals(relayType) || RelayType.TARIFF_REVERSED.equals(relayType))) {
                // Index 0 is not allowed for tariff switching.
                throw new FunctionalException(FunctionalExceptionType.VALIDATION_ERROR, ComponentType.PROTOCOL_IEC61850);
            } else if (lightValue.getIndex() == 0 && RelayType.LIGHT.equals(relayType)) {
                // Index == 0, getting all light relays and adding their
                // internal indexes to the indexes list.
                final List<DeviceOutputSetting> settings = ssldDataService.findByRelayType(ssld, relayType);
                for (final DeviceOutputSetting deviceOutputSetting : settings) {
                    indexes.add(deviceOutputSetting.getInternalId());
                }
            } else {
                // Index != 0, adding just the one index to the list.
                indexes.add(ssldDataService.convertToInternalIndex(ssld, lightValue.getIndex()));
            }
            ScheduleEntry scheduleEntry;
            try {
                scheduleEntry = this.convertToScheduleEntry(schedule, lightValue);
            } catch (final ProtocolAdapterException e) {
                throw new FunctionalException(FunctionalExceptionType.VALIDATION_ERROR, ComponentType.PROTOCOL_IEC61850, e);
            }
            for (final Integer internalIndex : indexes) {
                if (relaySchedulesEntries.containsKey(internalIndex)) {
                    // Internal index already in the Map, adding to the List
                    relaySchedulesEntries.get(internalIndex).add(scheduleEntry);
                } else {
                    // First time we come across this relay, checking its
                    // type.
                    this.checkRelayForSchedules(ssldDataService.getDeviceOutputSettingForInternalIndex(ssld, internalIndex).getRelayType(), relayType, internalIndex);
                    // Adding it to scheduleEntries.
                    final List<ScheduleEntry> scheduleEntries = new ArrayList<>();
                    scheduleEntries.add(scheduleEntry);
                    relaySchedulesEntries.put(internalIndex, scheduleEntries);
                }
            }
        }
    }
    return relaySchedulesEntries;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DeviceOutputSetting(com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting) ScheduleDto(com.alliander.osgp.dto.valueobjects.ScheduleDto) LightValueDto(com.alliander.osgp.dto.valueobjects.LightValueDto) RelayType(com.alliander.osgp.core.db.api.iec61850valueobjects.RelayType) ScheduleEntry(com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.ScheduleEntry) ArrayList(java.util.ArrayList) List(java.util.List) FunctionalException(com.alliander.osgp.shared.exceptionhandling.FunctionalException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)

Example 3 with LightValueDto

use of com.alliander.osgp.dto.valueobjects.LightValueDto in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850SsldDeviceService method createListOfInternalIndicesToSwitch.

private List<LightValueDto> createListOfInternalIndicesToSwitch(final List<DeviceOutputSetting> deviceOutputSettings, final List<LightValueDto> lightValues) throws FunctionalException {
    final List<LightValueDto> relaysWithInternalIdToSwitch = new ArrayList<>();
    LOGGER.info("creating list of internal indices using device output settings and external indices from light values");
    for (final LightValueDto lightValue : lightValues) {
        if (lightValue == null) {
            break;
        }
        DeviceOutputSetting deviceOutputSettingForExternalId = null;
        for (final DeviceOutputSetting deviceOutputSetting : deviceOutputSettings) {
            if (deviceOutputSetting.getExternalId() == lightValue.getIndex()) {
                // You can only switch LIGHT relays that are used.
                this.checkRelay(deviceOutputSetting.getRelayType(), RelayType.LIGHT, deviceOutputSetting.getInternalId());
                deviceOutputSettingForExternalId = deviceOutputSetting;
            }
        }
        if (deviceOutputSettingForExternalId != null) {
            final LightValueDto relayWithInternalIdToSwitch = new LightValueDto(deviceOutputSettingForExternalId.getInternalId(), lightValue.isOn(), lightValue.getDimValue());
            relaysWithInternalIdToSwitch.add(relayWithInternalIdToSwitch);
        }
    }
    return relaysWithInternalIdToSwitch;
}
Also used : ArrayList(java.util.ArrayList) DeviceOutputSetting(com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting) LightValueDto(com.alliander.osgp.dto.valueobjects.LightValueDto)

Example 4 with LightValueDto

use of com.alliander.osgp.dto.valueobjects.LightValueDto in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850GetStatusCommand method getStatusFromDevice.

public DeviceStatusDto getStatusFromDevice(final Iec61850Client iec61850Client, final DeviceConnection deviceConnection, final Ssld ssld) throws ProtocolAdapterException {
    final Function<DeviceStatusDto> function = new Function<DeviceStatusDto>() {

        @Override
        public DeviceStatusDto apply(final DeviceMessageLog deviceMessageLog) throws Exception {
            // getting the light relay values
            final List<LightValueDto> lightValues = new ArrayList<>();
            for (final DeviceOutputSetting deviceOutputSetting : ssld.getOutputSettings()) {
                final LogicalNode logicalNode = LogicalNode.getSwitchComponentByIndex(deviceOutputSetting.getInternalId());
                final NodeContainer position = deviceConnection.getFcModelNode(LogicalDevice.LIGHTING, logicalNode, DataAttribute.POSITION, Fc.ST);
                iec61850Client.readNodeDataValues(deviceConnection.getConnection().getClientAssociation(), position.getFcmodelNode());
                final BdaBoolean state = position.getBoolean(SubDataAttribute.STATE);
                final boolean on = state.getValue();
                lightValues.add(new LightValueDto(deviceOutputSetting.getExternalId(), on, null));
                LOGGER.info(String.format("Got status of relay %d => %s", deviceOutputSetting.getInternalId(), on ? "on" : "off"));
                deviceMessageLog.addVariable(logicalNode, DataAttribute.POSITION, Fc.ST, Boolean.toString(on));
            }
            final NodeContainer eventBuffer = deviceConnection.getFcModelNode(LogicalDevice.LIGHTING, LogicalNode.STREET_LIGHT_CONFIGURATION, DataAttribute.EVENT_BUFFER, Fc.CF);
            iec61850Client.readNodeDataValues(deviceConnection.getConnection().getClientAssociation(), eventBuffer.getFcmodelNode());
            final String filter = eventBuffer.getString(SubDataAttribute.EVENT_BUFFER_FILTER);
            LOGGER.info("Got EvnBuf.enbEvnType filter {}", filter);
            deviceMessageLog.addVariable(LogicalNode.STREET_LIGHT_CONFIGURATION, DataAttribute.EVENT_BUFFER, Fc.CF, filter);
            final Set<EventNotificationTypeDto> notificationTypes = EventType.getNotificationTypesForFilter(filter);
            int eventNotificationsMask = 0;
            for (final EventNotificationTypeDto notificationType : notificationTypes) {
                eventNotificationsMask |= notificationType.getValue();
            }
            final NodeContainer softwareConfiguration = deviceConnection.getFcModelNode(LogicalDevice.LIGHTING, LogicalNode.STREET_LIGHT_CONFIGURATION, DataAttribute.SOFTWARE_CONFIGURATION, Fc.CF);
            iec61850Client.readNodeDataValues(deviceConnection.getConnection().getClientAssociation(), softwareConfiguration.getFcmodelNode());
            String lightTypeValue = softwareConfiguration.getString(SubDataAttribute.LIGHT_TYPE);
            // Fix for Kaifa bug KI-31
            if (lightTypeValue == null || lightTypeValue.isEmpty()) {
                lightTypeValue = "RELAY";
            }
            final LightTypeDto lightType = LightTypeDto.valueOf(lightTypeValue);
            deviceMessageLog.addVariable(LogicalNode.STREET_LIGHT_CONFIGURATION, DataAttribute.SOFTWARE_CONFIGURATION, Fc.CF, lightTypeValue);
            DeviceMessageLoggingService.logMessage(deviceMessageLog, deviceConnection.getDeviceIdentification(), deviceConnection.getOrganisationIdentification(), false);
            /*
                 * The preferredLinkType and actualLinkType are hard-coded to
                 * LinkTypeDto.ETHERNET, other link types do not apply to the
                 * device type in use.
                 */
            return new DeviceStatusDto(lightValues, LinkTypeDto.ETHERNET, LinkTypeDto.ETHERNET, lightType, eventNotificationsMask);
        }
    };
    return iec61850Client.sendCommandWithRetry(function, "GetStatus", deviceConnection.getDeviceIdentification());
}
Also used : LightTypeDto(com.alliander.osgp.dto.valueobjects.LightTypeDto) ArrayList(java.util.ArrayList) DeviceOutputSetting(com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting) NodeContainer(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.NodeContainer) LogicalNode(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.LogicalNode) LightValueDto(com.alliander.osgp.dto.valueobjects.LightValueDto) Function(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.Function) DeviceMessageLog(com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.DeviceMessageLog) DeviceStatusDto(com.alliander.osgp.dto.valueobjects.DeviceStatusDto) BdaBoolean(org.openmuc.openiec61850.BdaBoolean) EventNotificationTypeDto(com.alliander.osgp.dto.valueobjects.EventNotificationTypeDto)

Example 5 with LightValueDto

use of com.alliander.osgp.dto.valueobjects.LightValueDto in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850SsldDeviceService method setLight.

@Override
public void setLight(final SetLightDeviceRequest deviceRequest, final DeviceResponseHandler deviceResponseHandler) throws JMSException {
    DeviceConnection deviceConnection = null;
    try {
        deviceConnection = this.connectToDevice(deviceRequest);
        // Getting the SSLD for the device output-settings.
        final Ssld ssld = this.ssldDataService.findDevice(deviceRequest.getDeviceIdentification());
        final List<DeviceOutputSetting> deviceOutputSettings = this.ssldDataService.findByRelayType(ssld, RelayType.LIGHT);
        final List<LightValueDto> lightValues = deviceRequest.getLightValuesContainer().getLightValues();
        List<LightValueDto> relaysWithInternalIdToSwitch;
        // Check if external index 0 is used.
        final LightValueDto index0LightValue = this.checkForIndex0(lightValues);
        if (index0LightValue != null) {
            // If external index 0 is used, create a list of all light
            // relays according to the device output settings.
            relaysWithInternalIdToSwitch = this.createListOfInternalIndicesToSwitch(deviceOutputSettings, index0LightValue.isOn());
        } else {
            // Else, create a list of internal indices based on the given
            // external indices in the light values list.
            relaysWithInternalIdToSwitch = this.createListOfInternalIndicesToSwitch(deviceOutputSettings, lightValues);
        }
        // Switch light relays based on internal indices.
        final Iec61850SetLightCommand iec61850SetLightCommand = new Iec61850SetLightCommand();
        for (final LightValueDto relayWithInternalIdToSwitch : relaysWithInternalIdToSwitch) {
            LOGGER.info("Trying to switch light relay with internal index: {} for device: {}", relayWithInternalIdToSwitch.getIndex(), deviceRequest.getDeviceIdentification());
            if (!iec61850SetLightCommand.switchLightRelay(this.iec61850Client, deviceConnection, relayWithInternalIdToSwitch.getIndex(), relayWithInternalIdToSwitch.isOn())) {
                throw new ProtocolAdapterException(String.format("Failed to switch light relay with internal index: %d for device: %s", relayWithInternalIdToSwitch.getIndex(), deviceRequest.getDeviceIdentification()));
            }
        }
        this.createSuccessfulDefaultResponse(deviceRequest, deviceResponseHandler);
    } catch (final ConnectionFailureException se) {
        this.handleConnectionFailureException(deviceRequest, deviceResponseHandler, se);
    } catch (final Exception e) {
        this.handleException(deviceRequest, deviceResponseHandler, e);
    }
    this.iec61850DeviceConnectionService.disconnect(deviceConnection, deviceRequest);
}
Also used : ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) DeviceConnection(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection) DeviceOutputSetting(com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) FunctionalException(com.alliander.osgp.shared.exceptionhandling.FunctionalException) JMSException(javax.jms.JMSException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) TechnicalException(com.alliander.osgp.shared.exceptionhandling.TechnicalException) Ssld(com.alliander.osgp.core.db.api.iec61850.entities.Ssld) LightValueDto(com.alliander.osgp.dto.valueobjects.LightValueDto) Iec61850SetLightCommand(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.services.commands.Iec61850SetLightCommand)

Aggregations

DeviceOutputSetting (com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting)6 LightValueDto (com.alliander.osgp.dto.valueobjects.LightValueDto)6 ArrayList (java.util.ArrayList)5 ProtocolAdapterException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)3 FunctionalException (com.alliander.osgp.shared.exceptionhandling.FunctionalException)3 ConnectionFailureException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException)2 NodeWriteException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException)2 DeviceConnection (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection)2 Iec61850SetLightCommand (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.services.commands.Iec61850SetLightCommand)2 Ssld (com.alliander.osgp.core.db.api.iec61850.entities.Ssld)2 DeviceStatusDto (com.alliander.osgp.dto.valueobjects.DeviceStatusDto)2 TechnicalException (com.alliander.osgp.shared.exceptionhandling.TechnicalException)2 JMSException (javax.jms.JMSException)2 DeviceMessageStatus (com.alliander.osgp.adapter.protocol.iec61850.device.DeviceMessageStatus)1 DeviceMessageLog (com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.DeviceMessageLog)1 ScheduleEntry (com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.ScheduleEntry)1 Function (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.Function)1 LogicalNode (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.LogicalNode)1 NodeContainer (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.NodeContainer)1 Iec61850GetStatusCommand (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.services.commands.Iec61850GetStatusCommand)1