Search in sources :

Example 1 with Device

use of org.opensmartgridplatform.webdevicesimulator.domain.entities.Device in project open-smart-grid-platform by OSGP.

the class AutonomousDeviceRegister method run.

@Override
public void run() {
    if (this.deviceManagementService.getDevRegistration()) {
        LOGGER.info("Registering devices");
        final List<Device> devices = this.deviceRepository.findAll();
        for (final Device device : devices) {
            LOGGER.info("Autonomous device register for : {}: {} ", device.getId(), device.getDeviceIdentification());
            this.registerDevice.sendRegisterDeviceCommand(device.getId(), false);
            LOGGER.info("Autonomous device register confirmation for : {}: {} ", device.getId(), device.getDeviceIdentification());
            this.registerDevice.sendConfirmDeviceRegistrationCommand(device.getId());
        }
    }
}
Also used : Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) RegisterDevice(org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice)

Example 2 with Device

use of org.opensmartgridplatform.webdevicesimulator.domain.entities.Device in project open-smart-grid-platform by OSGP.

the class TariffSwitchingLow method run.

@Override
public void run() {
    if (Boolean.TRUE.equals(this.deviceManagementService.getTariffSwitching())) {
        LOGGER.info("traiff Switching off for devices");
        final List<Device> devices = this.deviceRepository.findAll();
        for (final Device device : devices) {
            LOGGER.info("Tariff switching for : {}: {} ", device.getId(), device.getDeviceIdentification());
            // Switching off Tariff
            this.switchingServices.tariffSwitchLow(device.getId());
            // Send EventNotifications for TariffSwitching Off
            LOGGER.info("Sending TARIFF_EVENTS_TARIFF_OFF event for device : {}: {} ", device.getId(), device.getDeviceIdentification());
            this.registerDevice.sendEventNotificationCommand(device.getId(), Oslp.Event.TARIFF_EVENTS_TARIFF_OFF_VALUE, "TARIFF_EVENTS_TARIFF_OFF event occurred on Tariff Switching low ", 1);
        }
    }
}
Also used : Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) RegisterDevice(org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice)

Example 3 with Device

use of org.opensmartgridplatform.webdevicesimulator.domain.entities.Device in project open-smart-grid-platform by OSGP.

the class RegisterDevice method sendRegisterDeviceCommand.

public DeviceMessageStatus sendRegisterDeviceCommand(final long deviceId, final Boolean hasSchedule) {
    // Find device.
    final Device device = this.deviceManagementService.findDevice(deviceId);
    if (device == null) {
        // Set the DeviceMessageStatus NOT_FOUND as the Device is not found.
        return DeviceMessageStatus.NOT_FOUND;
    }
    this.errorMessage = "";
    try {
        // Generate random sequence number and random device number.
        final Integer sequenceNumber = device.doGenerateRandomNumber();
        final Integer randomDevice = device.doGenerateRandomNumber();
        // Create registration message.
        final OslpEnvelope oslpRequest = this.createEnvelopeBuilder(device.getDeviceUid(), sequenceNumber).withPayloadMessage(Message.newBuilder().setRegisterDeviceRequest(Oslp.RegisterDeviceRequest.newBuilder().setDeviceIdentification(device.getDeviceIdentification()).setIpAddress(ByteString.copyFrom(InetAddress.getByName(device.getIpAddress()).getAddress())).setDeviceType(device.getDeviceType().isEmpty() ? DeviceType.PSLD : DeviceType.valueOf(device.getDeviceType())).setHasSchedule(hasSchedule).setRandomDevice(randomDevice)).build()).build();
        // Write outgoing request to log.
        this.writeOslpLogItem(oslpRequest, device, false);
        final OslpEnvelope response = this.sendRequest(device, oslpRequest);
        // Write incoming response to log.
        this.writeOslpLogItem(response, device, true);
        this.currentTime = response.getPayloadMessage().getRegisterDeviceResponse().getCurrentTime();
        // Get the sequence number from the response envelope and check it.
        this.checkSequenceNumber(response.getSequenceNumber(), sequenceNumber);
        // Get the two random numbers and check them both.
        this.checkRandomDeviceAndRandomPlatform(randomDevice, response.getPayloadMessage().getRegisterDeviceResponse().getRandomDevice(), response.getPayloadMessage().getRegisterDeviceResponse().getRandomPlatform());
        // Set the sequence number and persist it.
        device.setSequenceNumber(sequenceNumber);
        // Get the two random numbers and persist them both.
        device.setRandomDevice(response.getPayloadMessage().getRegisterDeviceResponse().getRandomDevice());
        device.setRandomPlatform(response.getPayloadMessage().getRegisterDeviceResponse().getRandomPlatform());
        // Save the entity.
        this.deviceManagementService.updateDevice(device);
        // Set the DeviceMessageStatus OK as the registration is successful.
        return DeviceMessageStatus.OK;
    } catch (final UnknownHostException ex) {
        LOGGER.error("incorrect IP address format", ex);
    } catch (final Exception e) {
        LOGGER.error("register device exception", e);
        this.errorMessage = e.getMessage();
        // successful.
        return DeviceMessageStatus.FAILURE;
    }
    return DeviceMessageStatus.NOT_FOUND;
}
Also used : UnknownHostException(java.net.UnknownHostException) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) DeviceSimulatorException(org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) OslpEnvelope(org.opensmartgridplatform.oslp.OslpEnvelope)

Example 4 with Device

use of org.opensmartgridplatform.webdevicesimulator.domain.entities.Device in project open-smart-grid-platform by OSGP.

the class DeviceManagementController method sendEventNotificationCommandAll.

@PostMapping(value = COMMAND_SENDNOTIFICATION_URL)
@ResponseBody
public String sendEventNotificationCommandAll(@RequestBody final SendEventNotificationRequest request) {
    // Find device
    final Device device = this.deviceManagementService.findDevice(request.getDeviceId());
    final DeviceMessageStatus status = this.registerDevice.sendEventNotificationCommand(request.getDeviceId(), request.getEvent(), request.getDescription(), request.getIndex(), request.getHasTimestamp());
    if (status == DeviceMessageStatus.OK) {
        return this.getFeedbackMessage(FEEDBACK_MESSAGE_KEY_EVENTNOTIFICATION_SENT, device.getDeviceIdentification());
    } else if (status == DeviceMessageStatus.FAILURE) {
        return this.getFeedbackMessage(FEEDBACK_MESSAGE_KEY_DEVICE_ERROR, device.getDeviceIdentification(), this.registerDevice.getErrorMessage());
    } else {
        return FAILURE_URL;
    }
}
Also used : DeviceMessageStatus(org.opensmartgridplatform.webdevicesimulator.domain.entities.DeviceMessageStatus) RegisterDevice(org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with Device

use of org.opensmartgridplatform.webdevicesimulator.domain.entities.Device in project open-smart-grid-platform by OSGP.

the class DeviceManagementController method sendConfirmDeviceRegistrationCommand.

@PostMapping(value = COMMAND_REGISTER_CONFIRM_URL)
@ResponseBody
public String sendConfirmDeviceRegistrationCommand(@RequestBody final ConfirmDeviceRegistrationRequest request) {
    // Find device
    final Device device = this.deviceManagementService.findDevice(request.getDeviceId());
    final DeviceMessageStatus status = this.registerDevice.sendConfirmDeviceRegistrationCommand(request.getDeviceId());
    if (status == DeviceMessageStatus.OK) {
        return this.getFeedbackMessage(FEEDBACK_MESSAGE_KEY_DEVICE_REGISTERED_CONFIRM, device.getDeviceIdentification());
    } else if (status == DeviceMessageStatus.FAILURE) {
        return this.getFeedbackMessage(FEEDBACK_MESSAGE_KEY_DEVICE_ERROR, device.getDeviceIdentification(), this.registerDevice.getErrorMessage());
    } else {
        return FAILURE_URL;
    }
}
Also used : DeviceMessageStatus(org.opensmartgridplatform.webdevicesimulator.domain.entities.DeviceMessageStatus) RegisterDevice(org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice) Device(org.opensmartgridplatform.webdevicesimulator.domain.entities.Device) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Device (org.opensmartgridplatform.webdevicesimulator.domain.entities.Device)31 RegisterDevice (org.opensmartgridplatform.webdevicesimulator.service.RegisterDevice)23 OslpEnvelope (org.opensmartgridplatform.oslp.OslpEnvelope)7 PostMapping (org.springframework.web.bind.annotation.PostMapping)7 Channel (io.netty.channel.Channel)6 SocketChannel (io.netty.channel.socket.SocketChannel)6 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)6 Test (org.junit.jupiter.api.Test)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 OslpLogItem (org.opensmartgridplatform.webdevicesimulator.domain.entities.OslpLogItem)4 DeviceSimulatorException (org.opensmartgridplatform.webdevicesimulator.exceptions.DeviceSimulatorException)4 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 DeviceMessageStatus (org.opensmartgridplatform.webdevicesimulator.domain.entities.DeviceMessageStatus)3 ByteString (com.google.protobuf.ByteString)2 Oslp (org.opensmartgridplatform.oslp.Oslp)2 OutOfSequenceEvent (org.opensmartgridplatform.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent)2 ArrayList (java.util.ArrayList)1 DeviceType (org.opensmartgridplatform.oslp.Oslp.DeviceType)1 Message (org.opensmartgridplatform.oslp.Oslp.Message)1