Search in sources :

Example 1 with DeviceSimulatorException

use of org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException in project open-smart-grid-platform 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;
    try {
        channelFuture = this.bootstrap.connect(address);
        channelFuture.awaitUninterruptibly(this.connectionTimeout, TimeUnit.MILLISECONDS);
        if (channelFuture.channel() != null && channelFuture.channel().isActive()) {
            LOGGER.info("Connection established to: {}", address);
        } else {
            LOGGER.info("The connnection to OSGP from device {} is not successful", deviceIdentification);
            LOGGER.warn("Unable to connect to: {}", address);
            throw new IOException("Unable to connect");
        }
        this.callbacks.put(channelFuture.channel().id().asLongText(), callback);
        channelFuture.channel().writeAndFlush(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.channel().close();
        return response;
    } catch (final IOException | DeviceSimulatorException e) {
        // Remove callback when exception has occurred
        this.callbacks.remove(channelFuture.channel().id().asLongText());
        throw e;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DeviceSimulatorException(org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException) IOException(java.io.IOException) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 2 with DeviceSimulatorException

use of org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException in project open-smart-grid-platform by OSGP.

the class OslpChannelHandler method handleRequest.

private Oslp.Message handleRequest(final OslpEnvelope message, final int sequenceNumber) throws DeviceSimulatorException {
    final Oslp.Message request = message.getPayloadMessage();
    // Create response message
    final Oslp.Message response;
    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 int 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) {
        if (this.reponseDelayRandomRange == null) {
            this.sleep(this.responseDelayTime);
        } else {
            final Long randomDelay = (long) (this.reponseDelayRandomRange * this.random.nextDouble());
            this.sleep(this.responseDelayTime + randomDelay);
        }
    }
    response = this.checkForRequest(request, device);
    // Update device
    device.setSequenceNumber(expectedSequenceNumber);
    this.deviceManagementService.updateDevice(device);
    // Write log entry for response
    LOGGER.debug("Responding: {}", response);
    return response;
}
Also used : Message(org.opensmartgridplatform.oslp.Oslp.Message) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) DeviceSimulatorException(org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException) ByteString(com.google.protobuf.ByteString) Oslp(org.opensmartgridplatform.oslp.Oslp)

Aggregations

DeviceSimulatorException (org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException)2 ByteString (com.google.protobuf.ByteString)1 ChannelFuture (io.netty.channel.ChannelFuture)1 IOException (java.io.IOException)1 Oslp (org.opensmartgridplatform.oslp.Oslp)1 Message (org.opensmartgridplatform.oslp.Oslp.Message)1 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)1 Device (org.opensmartgridplatform.webdevicesimulator.domain.entities.Device)1