Search in sources :

Example 6 with Device

use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.

the class TariffSwitchingLow method run.

@Override
public void run() {
    if (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(com.alliander.osgp.webdevicesimulator.domain.entities.Device) RegisterDevice(com.alliander.osgp.webdevicesimulator.service.RegisterDevice)

Example 7 with Device

use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.

the class RegisterDevice method sendEventNotificationCommand.

public DeviceMessageStatus sendEventNotificationCommand(final Long id, final Integer event, final String description, final Integer index, final boolean hasTimestamp) {
    // Find device.
    Device device = this.deviceManagementService.findDevice(id);
    if (device == null) {
        // Set the DeviceMessageStatus NOT_FOUND as the device is not found.
        return DeviceMessageStatus.NOT_FOUND;
    }
    this.errorMessage = "";
    try {
        // Set index when provided in request.
        final int sequenceNumber = device.doGetNextSequence();
        final String timestamp = this.getFormattedCurrentTimestamp();
        final Oslp.Event oslpEvent = Oslp.Event.valueOf(event);
        // Create request and write outgoing request to log.
        final OslpEnvelope request = this.createEventNotificationRequest(device, sequenceNumber, oslpEvent, description, index, timestamp, hasTimestamp);
        this.writeOslpLogItem(request, device, false);
        // Send event notification message and receive response.
        final OslpEnvelope response = this.sendRequest(device, request);
        // Write incoming response to log.
        this.writeOslpLogItem(response, device, true);
        // Get the sequence number from the response envelope and check it.
        this.checkSequenceNumber(response.getSequenceNumber(), sequenceNumber);
        // Success, update the sequence number of the device.
        device.setSequenceNumber(sequenceNumber);
        device = this.deviceManagementService.updateDevice(device);
        // Set the DeviceMessageStatus OK as the SendEvent is Success.
        return DeviceMessageStatus.OK;
    } catch (final Exception e) {
        LOGGER.error("send event notification exception", e);
        this.errorMessage = e.getMessage();
        // successful.
        return DeviceMessageStatus.FAILURE;
    }
}
Also used : Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) ByteString(com.google.protobuf.ByteString) Oslp(com.alliander.osgp.oslp.Oslp) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DeviceSimulatorException(com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 8 with Device

use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.

the class RegisterDevice method sendConfirmDeviceRegistrationCommand.

public DeviceMessageStatus sendConfirmDeviceRegistrationCommand(final long deviceId) {
    // Find device.
    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 {
        final Integer sequenceNumber = device.doGetNextSequence();
        // Create registration confirmation message.
        final OslpEnvelope oslpRequest = this.createEnvelopeBuilder(device.getDeviceUid(), sequenceNumber).withPayloadMessage(Message.newBuilder().setConfirmRegisterDeviceRequest(Oslp.ConfirmRegisterDeviceRequest.newBuilder().setRandomDevice(device.getRandomDevice()).setRandomPlatform(device.getRandomPlatform())).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);
        // 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(device.getRandomDevice(), response.getPayloadMessage().getConfirmRegisterDeviceResponse().getRandomDevice(), device.getRandomPlatform(), response.getPayloadMessage().getConfirmRegisterDeviceResponse().getRandomPlatform());
        // Successful.
        device.setSequenceNumber(sequenceNumber);
        device = this.deviceManagementService.updateDevice(device);
        // Check if there has been an out of sequence security event.
        OutOfSequenceEvent outOfSequenceEvent = this.oslpChannelHandler.hasOutOfSequenceEventForDevice(device.getId());
        while (outOfSequenceEvent != null) {
            // An event has occurred, send
            // SECURITY_EVENTS_OUT_OF_SEQUENCE_VALUE event notification.
            this.sendEventNotificationCommand(outOfSequenceEvent.getDeviceId(), Oslp.Event.SECURITY_EVENTS_OUT_OF_SEQUENCE_VALUE, "out of sequence event occurred at time stamp: " + outOfSequenceEvent.getTimestamp().toString() + " for request: " + outOfSequenceEvent.getRequest(), null);
            // Check if there has been another event, this will return null
            // if no more events are present in the list.
            outOfSequenceEvent = this.oslpChannelHandler.hasOutOfSequenceEventForDevice(device.getId());
        }
        // successful.
        return DeviceMessageStatus.OK;
    } catch (final Exception e) {
        LOGGER.error("confirm device registration exception", e);
        this.errorMessage = e.getMessage();
        // is NOT successful.
        return DeviceMessageStatus.FAILURE;
    }
}
Also used : Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) OutOfSequenceEvent(com.alliander.osgp.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DeviceSimulatorException(com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 9 with Device

use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.

the class DeviceManagementController method editDevice.

@RequestMapping(value = DEVICE_EDIT_URL, method = RequestMethod.POST)
public String editDevice(@ModelAttribute(MODEL_ATTRIBUTE_DEVICE) final Device updated, @PathVariable final Long id, final BindingResult bindingResult, final RedirectAttributes attributes, final Model model) {
    if (bindingResult.hasErrors()) {
        return DEVICE_EDIT_VIEW;
    }
    // Find device
    final Device deviceToUpdate = this.deviceManagementService.findDevice(id);
    if (deviceToUpdate == null) {
        return DEVICE_EDIT_VIEW;
    }
    // Update data
    deviceToUpdate.setIpAddress(updated.getIpAddress());
    deviceToUpdate.setDeviceType(updated.getDeviceType());
    deviceToUpdate.setActualLinkType(updated.getActualLinkType());
    deviceToUpdate.setTariffOn(updated.isTariffOn());
    deviceToUpdate.setProtocol(updated.getProtocol());
    // Store device
    final Device device = this.deviceManagementService.updateDevice(deviceToUpdate);
    this.addFeedbackMessage(attributes, FEEDBACK_MESSAGE_KEY_DEVICE_UPDATED, device.getDeviceIdentification());
    model.addAttribute(MODEL_ATTRIBUTE_DEVICE, deviceToUpdate);
    return DEVICE_EDIT_VIEW;
}
Also used : Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) RegisterDevice(com.alliander.osgp.webdevicesimulator.service.RegisterDevice) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Device

use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.

the class DeviceManagementController method getSequenceNumber.

@RequestMapping(value = COMMAND_GET_SEQUENCE_NUMBER_URL, method = RequestMethod.POST)
@ResponseBody
public Integer getSequenceNumber(@RequestBody final GetSequenceNumberRequest getSequenceNumberRequest) {
    final Long deviceId = getSequenceNumberRequest.getDeviceId();
    final Device device = this.deviceManagementService.findDevice(deviceId);
    if (device != null) {
        return device.getSequenceNumber();
    } else {
        return -1;
    }
}
Also used : Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) RegisterDevice(com.alliander.osgp.webdevicesimulator.service.RegisterDevice) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Device (com.alliander.osgp.webdevicesimulator.domain.entities.Device)23 RegisterDevice (com.alliander.osgp.webdevicesimulator.service.RegisterDevice)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 DeviceSimulatorException (com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException)4 OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)3 DeviceMessageStatus (com.alliander.osgp.webdevicesimulator.domain.entities.DeviceMessageStatus)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Oslp (com.alliander.osgp.oslp.Oslp)2 ByteString (com.google.protobuf.ByteString)2 Message (com.alliander.osgp.oslp.Oslp.Message)1 OutOfSequenceEvent (com.alliander.osgp.webdevicesimulator.service.OslpChannelHandler.OutOfSequenceEvent)1