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;
}
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;
}
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);
}
}
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));
}
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());
}
Aggregations