use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.
the class DeviceManagementController method sendConfirmDeviceRegistrationCommand.
@RequestMapping(value = COMMAND_REGISTER_CONFIRM_URL, method = RequestMethod.POST)
@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;
}
}
use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.
the class AutonomousDeviceReboot method run.
@Override
public void run() {
if (this.deviceManagementService.getDevReboot()) {
LOGGER.info("Rebooting devices");
final List<Device> devices = this.deviceRepository.findAll();
for (final Device device : devices) {
LOGGER.info("Autonomous device reboot for : {}: {} ", device.getId(), device.getDeviceIdentification());
// registering device with hasSchedule as false
LOGGER.info("device registration for : {}: {} ", device.getId(), device.getDeviceIdentification());
this.registerDevice.sendRegisterDeviceCommand(device.getId(), false);
// Confirm device registration
LOGGER.info("device register confirmation for : {}: {} ", device.getId(), device.getDeviceIdentification());
this.registerDevice.sendConfirmDeviceRegistrationCommand(device.getId());
// Sending events on device reboot
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_VALUE event occurred on Device reboot ", null);
LOGGER.info("Sending LIGHT_EVENTS_LIGHT_OFF event for device : {}: {} ", device.getId(), device.getDeviceIdentification());
this.registerDevice.sendEventNotificationCommand(device.getId(), Oslp.Event.LIGHT_EVENTS_LIGHT_OFF_VALUE, "LIGHT_EVENTS_LIGHT_OFF event occurred on Device reboot ", null);
LOGGER.info("Sending DIAG_EVENTS_GENERAL event for device : {}: {} ", device.getId(), device.getDeviceIdentification());
this.registerDevice.sendEventNotificationCommand(device.getId(), Oslp.Event.DIAG_EVENTS_GENERAL_VALUE, "DIAG_EVENTS_GENERAL event occurred on Device reboot ", null);
}
}
}
use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.
the class EveningMorningBurnersLightSwitchingOn method run.
@Override
public void run() {
if (this.deviceManagementService.getLightSwitching()) {
LOGGER.info("Publiclighting Switching on for devices with Evening/Morning Burners");
final List<Device> devices = this.deviceRepository.findByHasEveningMorningBurner(true);
for (final Device device : devices) {
LOGGER.info("Light switching for : {}: {} ", device.getId(), device.getDeviceIdentification());
// Switching on Light
this.switchingServices.lightSwitchOn(device.getId());
// Send EventNotifications for LightSwitching On
LOGGER.info("Sending LIGHT_EVENTS_LIGHT_ON event for device : {}: {} ", device.getId(), device.getDeviceIdentification());
// The event index for Evening/Morning Burners is 3.
this.registerDevice.sendEventNotificationCommand(device.getId(), Oslp.Event.LIGHT_EVENTS_LIGHT_ON_VALUE, "LIGHT_EVENTS_LIGHT_ON event occurred on Light Switching on ", 3);
}
}
}
use of com.alliander.osgp.webdevicesimulator.domain.entities.Device 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;
}
use of com.alliander.osgp.webdevicesimulator.domain.entities.Device in project Protocol-Adapter-OSLP by OSGP.
the class RegisterDevice method sendRegisterDeviceCommand.
public DeviceMessageStatus sendRegisterDeviceCommand(final long deviceId, final Boolean hasSchedule) {
// 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 {
// Create new deviceUID. This is a temporary fix for devices that
// have been created in the past (with a 10 byte deviceUID).
// Alternative would be to 1) change the deviceUID in the database
// or 2) delete all devices and create new devices (with a 12 byte
// deviceUID).
// There seems no problem with creating a new deviceUID for every
// registration attempt of the device.
// However, NOTE: THIS BEHAVIOUR IS NOT EQUAL TO THE REAL SSLD/PSLD.
device.setDeviceUid(this.createRandomDeviceUid());
device = this.deviceManagementService.updateDevice(device);
// 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.
device = 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;
}
Aggregations