use of com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException in project Protocol-Adapter-OSLP by OSGP.
the class OslpChannelHandler method send.
public OslpEnvelope send(final InetSocketAddress address, final OslpEnvelope request, final String deviceIdentification) throws IOException, DeviceSimulatorException {
LOGGER.info("Sending OSLP request: {}", request.getPayloadMessage());
final Callback callback = new Callback(this.connectionTimeout);
this.lock.lock();
// Open connection and send message
ChannelFuture channelFuture = null;
try {
channelFuture = this.bootstrap.connect(address);
channelFuture.awaitUninterruptibly(this.connectionTimeout, TimeUnit.MILLISECONDS);
if (channelFuture.getChannel() != null && channelFuture.getChannel().isConnected()) {
LOGGER.info("Connection established to: {}", address);
} else {
LOGGER.info("The connnection to the device {} is not successfull", deviceIdentification);
LOGGER.warn("Unable to connect to: {}", address);
throw new IOException("Unable to connect");
}
this.callbacks.put(channelFuture.getChannel().getId(), callback);
channelFuture.getChannel().write(request);
} finally {
this.lock.unlock();
}
// wait for response and close connection
try {
final OslpEnvelope response = callback.get(deviceIdentification);
LOGGER.info("Received OSLP response (after callback): {}", response.getPayloadMessage());
/*
* Devices expect the channel to be closed if (and only if) the
* platform initiated the conversation. If the device initiated the
* conversation it needs to close the channel itself.
*/
channelFuture.getChannel().close();
return response;
} catch (final IOException | DeviceSimulatorException e) {
LOGGER.error("send exception", e);
// Remove callback when exception has occurred
this.callbacks.remove(channelFuture.getChannel().getId());
throw e;
}
}
use of com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException in project Protocol-Adapter-OSLP by OSGP.
the class OslpChannelHandler method handleRequest.
private Oslp.Message handleRequest(final OslpEnvelope message, final int sequenceNumber) throws DeviceSimulatorException, IOException, ParseException {
final Oslp.Message request = message.getPayloadMessage();
// Create response message
Oslp.Message response = null;
final String deviceIdString = Base64.encodeBase64String(message.getDeviceId());
LOGGER.info("request received, sequenceNumber: {}", sequenceNumber);
LOGGER.info("manufacturerId byte[0]: {} byte[1]: {}", message.getDeviceId()[0], message.getDeviceId()[1]);
LOGGER.info("deviceId as BASE 64 STRING: {}", deviceIdString);
// lookup correct device.
final Device device = this.deviceManagementService.findDevice(deviceIdString);
if (device == null) {
throw new DeviceSimulatorException("device with id: " + deviceIdString + " is unknown");
}
// Calculate expected sequence number
final Integer expectedSequenceNumber = device.doGetNextSequence();
// Check sequence number
if (Math.abs(expectedSequenceNumber - sequenceNumber) > this.sequenceNumberWindow) {
this.outOfSequenceList.add(new OutOfSequenceEvent(device.getId(), message.getPayloadMessage().toString(), DateTime.now()));
throw new DeviceSimulatorException("SequenceNumber incorrect for device: " + device.getDeviceIdentification() + " Expected: " + (expectedSequenceNumber == 0 ? this.sequenceNumberMaximum : expectedSequenceNumber - 1) + " Actual: " + (sequenceNumber == 0 ? this.sequenceNumberMaximum : sequenceNumber - 1) + " SequenceNumberWindow: " + this.sequenceNumberWindow + " Request: " + message.getPayloadMessage().toString());
}
// sleep for a little while
if (this.responseDelayTime != null && this.reponseDelayRandomRange == null) {
this.sleep(this.responseDelayTime);
} else if (this.responseDelayTime != null && this.reponseDelayRandomRange != null) {
final Long randomDelay = (long) (this.reponseDelayRandomRange * this.random.nextDouble());
this.sleep(this.responseDelayTime + randomDelay);
}
// Handle only expected messages
if (request.hasStartSelfTestRequest()) {
device.setLightOn(true);
device.setSelftestActive(true);
response = createStartSelfTestResponse();
} else if (request.hasStopSelfTestRequest()) {
device.setLightOn(false);
device.setSelftestActive(false);
response = createStopSelfTestResponse();
} else if (request.hasSetLightRequest()) {
this.handleSetLightRequest(device, request.getSetLightRequest());
response = createSetLightResponse();
} else if (request.hasSetEventNotificationsRequest()) {
this.handleSetEventNotificationsRequest(device, request.getSetEventNotificationsRequest());
response = createSetEventNotificationsResponse();
} else if (request.hasUpdateFirmwareRequest()) {
this.handleUpdateFirmwareRequest(device, request.getUpdateFirmwareRequest());
response = createUpdateFirmwareResponse();
} else if (request.hasGetFirmwareVersionRequest()) {
response = createGetFirmwareVersionResponse(this.firmwareVersion);
} else if (request.hasSwitchFirmwareRequest()) {
response = createSwitchFirmwareResponse();
} else if (request.hasUpdateDeviceSslCertificationRequest()) {
response = createUpdateDeviceSslCertificationResponse();
} else if (request.hasSetDeviceVerificationKeyRequest()) {
response = createSetDeviceVerificationKeyResponse();
} else if (request.hasSetScheduleRequest()) {
this.handleSetScheduleRequest(device, request.getSetScheduleRequest());
response = createSetScheduleResponse();
} else if (request.hasSetConfigurationRequest()) {
this.handleSetConfigurationRequest(device, request.getSetConfigurationRequest());
response = this.createSetConfigurationResponse();
} else if (request.hasGetConfigurationRequest()) {
this.handleGetConfigurationRequest(device, request.getGetConfigurationRequest());
response = this.createGetConfigurationResponse(device);
} else if (request.hasSwitchConfigurationRequest()) {
response = createSwitchConfigurationResponse();
} else if (request.hasGetActualPowerUsageRequest()) {
this.handleGetActualPowerUsageRequest(device, request.getGetActualPowerUsageRequest());
response = createGetActualPowerUsageResponse();
} else if (request.hasGetPowerUsageHistoryRequest()) {
this.handleGetPowerUsageHistoryRequest(device, request.getGetPowerUsageHistoryRequest());
response = createGetPowerUsageHistoryWithDatesResponse(request.getGetPowerUsageHistoryRequest());
} else if (request.hasGetStatusRequest()) {
response = this.createGetStatusResponse(device);
} else if (request.hasResumeScheduleRequest()) {
response = createResumeScheduleResponse();
} else if (request.hasSetRebootRequest()) {
response = createSetRebootResponse();
this.sendDelayedDeviceRegistration(device);
} else if (request.hasSetTransitionRequest()) {
this.handleSetTransitionRequest(device, request.getSetTransitionRequest());
response = createSetTransitionResponse();
} else if (request.hasConfirmRegisterDeviceRequest()) {
response = createConfirmRegisterDeviceResponse(request.getConfirmRegisterDeviceRequest().getRandomDevice(), request.getConfirmRegisterDeviceRequest().getRandomPlatform());
} else {
// Handle errors by logging
LOGGER.error("Did not expect request, ignoring: " + request.toString());
}
// Update device
device.setSequenceNumber(expectedSequenceNumber);
this.deviceManagementService.updateDevice(device);
// Write log entry for response
LOGGER.debug("Responding: " + response);
return response;
}
Aggregations