use of org.opensmartgridplatform.adapter.protocol.iec61850.exceptions.ProtocolAdapterException in project Protocol-Adapter-IEC61850 by OSGP.
the class OsgpResponseMessageListener method onMessage.
@Override
public void onMessage(final Message message) {
try {
LOGGER.info("Received message of type: {}", message.getJMSType());
final ObjectMessage objectMessage = (ObjectMessage) message;
final String messageType = objectMessage.getJMSType();
final String deviceIdentification = objectMessage.getStringProperty(Constants.DEVICE_IDENTIFICATION);
final ResponseMessage responseMessage = (ResponseMessage) objectMessage.getObject();
final String result = responseMessage == null ? null : responseMessage.getResult().toString();
final OsgpException osgpException = responseMessage == null ? null : responseMessage.getOsgpException();
if (DeviceFunctionDto.valueOf(messageType).equals(DeviceFunctionDto.REGISTER_DEVICE)) {
this.handleDeviceRegistration(result, deviceIdentification, messageType, osgpException);
} else {
throw new UnknownMessageTypeException("Unknown JMSType: " + messageType);
}
} catch (final JMSException ex) {
LOGGER.error("Exception: {} ", ex.getMessage(), ex);
} catch (final ProtocolAdapterException e) {
LOGGER.error("ProtocolAdapterException", e);
} catch (final UnknownMessageTypeException e) {
LOGGER.error("UnknownMessageTypeException", e);
}
}
use of org.opensmartgridplatform.adapter.protocol.iec61850.exceptions.ProtocolAdapterException 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);
}
use of org.opensmartgridplatform.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 org.opensmartgridplatform.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 org.opensmartgridplatform.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);
}
}
Aggregations