Search in sources :

Example 6 with ProtocolAdapterException

use of com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850Client method sendCommandWithRetry.

/**
     * Basically the same as sendCommandWithRetry, but with a retry parameter.
     */
private <T> T sendCommandWithRetry(final Function<T> function, final String deviceIdentification, final int retryCount, final DeviceMessageLog deviceMessageLog) throws ProtocolAdapterException {
    T output = null;
    LOGGER.info("retry: {} of {} for deviceIdentification: {}", retryCount, this.maxRetryCount, deviceIdentification);
    try {
        output = function.apply(deviceMessageLog);
    } catch (final ProtocolAdapterException e) {
        if (retryCount >= this.maxRetryCount) {
            throw e;
        } else {
            this.sendCommandWithRetry(function, deviceIdentification, retryCount + 1, deviceMessageLog);
        }
    } catch (final Exception e) {
        throw new ProtocolAdapterException(e == null ? "Could not execute command" : e.getMessage(), e);
    }
    return output;
}
Also used : ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) SclParseException(org.openmuc.openiec61850.SclParseException) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) NodeReadException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) IOException(java.io.IOException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)

Example 7 with ProtocolAdapterException

use of com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850Client method sendCommandWithRetry.

/**
     * Executes the apply method of the given {@link Function} with retries and
     * message logging.
     *
     * @return The given T.
     */
public <T> T sendCommandWithRetry(final Function<T> function, final String functionName, final String deviceIdentification) throws ProtocolAdapterException {
    T output = null;
    final DeviceMessageLog deviceMessageLog = new DeviceMessageLog(IED.FLEX_OVL, LogicalDevice.LIGHTING, functionName);
    try {
        output = function.apply(deviceMessageLog);
    } catch (final NodeWriteException | NodeReadException e) {
        if (ConnectionState.OK.equals(e.getConnectionState())) {
            // ServiceError means we have to retry.
            LOGGER.error("Caught ServiceError, retrying", e);
            this.sendCommandWithRetry(function, deviceIdentification, 1, deviceMessageLog);
        } else {
            LOGGER.error("Caught IOException, connection with device is broken.", e);
        }
    } catch (final ConnectionFailureException e) {
        throw e;
    } catch (final Exception e) {
        throw new ProtocolAdapterException(e == null ? "Could not execute command" : e.getMessage(), e);
    }
    return output;
}
Also used : DeviceMessageLog(com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.DeviceMessageLog) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) NodeReadException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) SclParseException(org.openmuc.openiec61850.SclParseException) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) NodeReadException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException) ConnectionFailureException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException) IOException(java.io.IOException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)

Example 8 with ProtocolAdapterException

use of com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850Client method readServerModelFromDevice.

/**
     * Read the device model from the device.
     *
     * @param clientAssociation
     *            The {@link ClientAssociation} instance.
     *
     * @return A {@link ServerModel} instance.
     * @throws ProtocolAdapterException
     */
public ServerModel readServerModelFromDevice(final ClientAssociation clientAssociation) throws ProtocolAdapterException {
    try {
        LOGGER.debug("Start reading server model from device");
        // RetrieveModel() will call all GetDirectory and GetDefinition ACSI
        // services needed to get the complete server model.
        final ServerModel serverModel = clientAssociation.retrieveModel();
        LOGGER.debug("Completed reading server model from device");
        return serverModel;
    } catch (final ServiceError e) {
        clientAssociation.close();
        throw new ProtocolAdapterException("Service Error requesting model.", e);
    } catch (final IOException e) {
        throw new ProtocolAdapterException("Fatal IOException requesting model.", e);
    }
}
Also used : ServiceError(org.openmuc.openiec61850.ServiceError) ServerModel(org.openmuc.openiec61850.ServerModel) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) IOException(java.io.IOException)

Example 9 with ProtocolAdapterException

use of com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850ClientRTUEventListener method processReport.

private void processReport(final Report report, final String reportDescription, final Iec61850ReportHandler reportHandler) throws ProtocolAdapterException {
    if (report.getDataSet() == null) {
        this.logger.warn("No DataSet available for {}", reportDescription);
        return;
    }
    final List<FcModelNode> members = report.getDataSet().getMembers();
    if ((members == null) || members.isEmpty()) {
        this.logger.warn("No members in DataSet available for {}", reportDescription);
        return;
    }
    final List<MeasurementDto> measurements = new ArrayList<>();
    for (final FcModelNode member : members) {
        if (member == null) {
            this.logger.warn("Member == null in DataSet for {}", reportDescription);
            continue;
        }
        this.logger.info("Handle member {} for {}", member.getReference(), reportDescription);
        try {
            final MeasurementDto dto = reportHandler.handleMember(new ReadOnlyNodeContainer(this.deviceIdentification, member));
            if (dto != null) {
                measurements.add(dto);
            } else {
                this.logger.warn("Unsupprted member {}, skipping", member.getName());
            }
        } catch (final Exception e) {
            this.logger.error("Error adding event notification for member {} from {}", member.getReference(), reportDescription, e);
        }
    }
    final GetDataSystemIdentifierDto systemResult = reportHandler.createResult(measurements);
    final List<GetDataSystemIdentifierDto> systems = new ArrayList<>();
    systems.add(systemResult);
    final ReportDto reportDto = new ReportDto(report.getSqNum(), new DateTime(report.getTimeOfEntry().getTimestampValue() + IEC61850_ENTRY_TIME_OFFSET), report.getRptId());
    this.deviceManagementService.sendMeasurements(this.deviceIdentification, new GetDataResponseDto(systems, reportDto));
}
Also used : GetDataResponseDto(com.alliander.osgp.dto.valueobjects.microgrids.GetDataResponseDto) GetDataSystemIdentifierDto(com.alliander.osgp.dto.valueobjects.microgrids.GetDataSystemIdentifierDto) ArrayList(java.util.ArrayList) FcModelNode(org.openmuc.openiec61850.FcModelNode) ReportDto(com.alliander.osgp.dto.valueobjects.microgrids.ReportDto) ReadOnlyNodeContainer(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.ReadOnlyNodeContainer) MeasurementDto(com.alliander.osgp.dto.valueobjects.microgrids.MeasurementDto) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException) DateTime(org.joda.time.DateTime)

Example 10 with ProtocolAdapterException

use of com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850SetLightCommand method switchLightRelay.

public Boolean switchLightRelay(final Iec61850Client iec61850Client, final DeviceConnection deviceConnection, final int index, final boolean on) throws ProtocolAdapterException {
    // Commands don't return anything, so returnType is Void.
    final Function<Boolean> function = new Function<Boolean>() {

        @Override
        public Boolean apply(final DeviceMessageLog deviceMessageLog) throws Exception {
            try {
                final LogicalNode logicalNode = LogicalNode.getSwitchComponentByIndex(index);
                // Check if CfSt.enbOper [CF] is set to true. If it is not
                // set to true, the relay can not be operated.
                final NodeContainer masterControl = deviceConnection.getFcModelNode(LogicalDevice.LIGHTING, logicalNode, DataAttribute.MASTER_CONTROL, Fc.CF);
                iec61850Client.readNodeDataValues(deviceConnection.getConnection().getClientAssociation(), masterControl.getFcmodelNode());
                final BdaBoolean enbOper = masterControl.getBoolean(SubDataAttribute.ENABLE_OPERATION);
                if (enbOper.getValue()) {
                    LOGGER.info("masterControl.enbOper is true, switching of relay {} is enabled", index);
                } else {
                    LOGGER.info("masterControl.enbOper is false, switching of relay {} is disabled", index);
                    // Set the value to true.
                    masterControl.writeBoolean(SubDataAttribute.ENABLE_OPERATION, true);
                    LOGGER.info("set masterControl.enbOper to true to enable switching of relay {}", index);
                    deviceMessageLog.addVariable(logicalNode, DataAttribute.MASTER_CONTROL, Fc.CF, SubDataAttribute.ENABLE_OPERATION, Boolean.toString(true));
                }
                // Switch the relay using Pos.Oper.ctlVal [CO].
                final NodeContainer position = deviceConnection.getFcModelNode(LogicalDevice.LIGHTING, logicalNode, DataAttribute.POSITION, Fc.CO);
                iec61850Client.readNodeDataValues(deviceConnection.getConnection().getClientAssociation(), position.getFcmodelNode());
                final NodeContainer operation = position.getChild(SubDataAttribute.OPERATION);
                final BdaBoolean controlValue = operation.getBoolean(SubDataAttribute.CONTROL_VALUE);
                LOGGER.info(String.format("Switching relay %d %s", index, on ? "on" : "off"));
                controlValue.setValue(on);
                operation.write();
                deviceMessageLog.addVariable(logicalNode, DataAttribute.POSITION, Fc.CO, SubDataAttribute.OPERATION, SubDataAttribute.CONTROL_VALUE, Boolean.toString(on));
                DeviceMessageLoggingService.logMessage(deviceMessageLog, deviceConnection.getDeviceIdentification(), deviceConnection.getOrganisationIdentification(), false);
                return true;
            } catch (final Exception e) {
                LOGGER.error("Exception during switchLightRelay()", e);
                return false;
            }
        }
    };
    return iec61850Client.sendCommandWithRetry(function, "SetLight", deviceConnection.getDeviceIdentification());
}
Also used : Function(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.Function) DeviceMessageLog(com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.DeviceMessageLog) BdaBoolean(org.openmuc.openiec61850.BdaBoolean) NodeContainer(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.NodeContainer) LogicalNode(com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.LogicalNode) BdaBoolean(org.openmuc.openiec61850.BdaBoolean) ProtocolAdapterException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)

Aggregations

ProtocolAdapterException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ProtocolAdapterException)22 DeviceMessageLog (com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.DeviceMessageLog)16 Function (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.Function)15 NodeContainer (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.NodeContainer)11 ArrayList (java.util.ArrayList)9 DateTime (org.joda.time.DateTime)7 ConnectionFailureException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.ConnectionFailureException)6 DeviceOutputSetting (com.alliander.osgp.core.db.api.iec61850.entities.DeviceOutputSetting)6 NodeWriteException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException)5 DeviceConnection (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.DeviceConnection)5 LogicalNode (com.alliander.osgp.adapter.protocol.iec61850.infra.networking.helper.LogicalNode)5 FunctionalException (com.alliander.osgp.shared.exceptionhandling.FunctionalException)5 BdaBoolean (org.openmuc.openiec61850.BdaBoolean)5 Ssld (com.alliander.osgp.core.db.api.iec61850.entities.Ssld)4 LightValueDto (com.alliander.osgp.dto.valueobjects.LightValueDto)4 IOException (java.io.IOException)4 List (java.util.List)4 JMSException (javax.jms.JMSException)4 ScheduleEntry (com.alliander.osgp.adapter.protocol.iec61850.domain.valueobjects.ScheduleEntry)3 NodeReadException (com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeReadException)3