Search in sources :

Example 1 with GpsCoordinatesDto

use of com.alliander.osgp.dto.valueobjects.GpsCoordinatesDto in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandlerServer method handleRegisterDeviceRequest.

private Oslp.Message handleRegisterDeviceRequest(final byte[] deviceUid, final byte[] sequenceNumber, final Oslp.RegisterDeviceRequest registerRequest) throws UnknownHostException {
    final String deviceIdentification = registerRequest.getDeviceIdentification();
    final String deviceType = registerRequest.getDeviceType().toString();
    final boolean hasSchedule = registerRequest.getHasSchedule();
    InetAddress inetAddress;
    // set, the values will be used to set an IP address for a device.
    if (this.testDeviceIps != null && this.testDeviceIps.containsKey(deviceIdentification)) {
        final String testDeviceIp = this.testDeviceIps.get(deviceIdentification);
        LOGGER.info("Using testDeviceId: {} and testDeviceIp: {}", deviceIdentification, testDeviceIp);
        inetAddress = InetAddress.getByName(testDeviceIp);
    } else {
        inetAddress = InetAddress.getByAddress(registerRequest.getIpAddress().toByteArray());
    }
    // Send message to OSGP-CORE to save IP Address, device type and has
    // schedule values in OSGP-CORE database.
    this.deviceRegistrationService.sendDeviceRegisterRequest(inetAddress, deviceType, hasSchedule, deviceIdentification);
    OslpDevice oslpDevice = this.oslpDeviceSettingsService.getDeviceByDeviceIdentification(registerRequest.getDeviceIdentification());
    // Save the security related values in the OSLP database.
    oslpDevice.updateRegistrationData(deviceUid, registerRequest.getDeviceType().toString(), Integer.valueOf(registerRequest.getRandomDevice()));
    oslpDevice.setSequenceNumber(SequenceNumberUtils.convertByteArrayToInteger(sequenceNumber));
    oslpDevice = this.oslpDeviceSettingsService.updateDevice(oslpDevice);
    // Return current date and time in UTC so the device can sync the clock.
    final Oslp.RegisterDeviceResponse.Builder responseBuilder = Oslp.RegisterDeviceResponse.newBuilder().setStatus(Oslp.Status.OK).setCurrentTime(Instant.now().toString(format)).setRandomDevice(registerRequest.getRandomDevice()).setRandomPlatform(oslpDevice.getRandomPlatform());
    // Return local time zone information of the platform. Devices can use
    // this to convert UTC times to local times.
    final LocationInfo.Builder locationInfo = LocationInfo.newBuilder();
    locationInfo.setTimeOffset(this.timeZoneOffsetMinutes);
    // Get the GPS values from OSGP-CORE database.
    final GpsCoordinatesDto gpsCoordinates = this.deviceDataService.getGpsCoordinatesForDevice(deviceIdentification);
    if (gpsCoordinates != null && gpsCoordinates.getLatitude() != null && gpsCoordinates.getLongitude() != null) {
        // Add GPS information when available in meta data.
        locationInfo.setLatitude(this.convertGpsCoordinateFromFloatToInt(gpsCoordinates.getLatitude())).setLongitude(this.convertGpsCoordinateFromFloatToInt(gpsCoordinates.getLongitude()));
    } else {
        // Otherwise use default GPS information.
        locationInfo.setLatitude(this.convertGpsCoordinateFromFloatToInt(this.defaultLatitude)).setLongitude(this.convertGpsCoordinateFromFloatToInt(this.defaultLongitude));
    }
    responseBuilder.setLocationInfo(locationInfo);
    return Oslp.Message.newBuilder().setRegisterDeviceResponse(responseBuilder.build()).build();
}
Also used : GpsCoordinatesDto(com.alliander.osgp.dto.valueobjects.GpsCoordinatesDto) InetAddress(java.net.InetAddress) OslpDevice(com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice) LocationInfo(com.alliander.osgp.oslp.Oslp.LocationInfo)

Aggregations

OslpDevice (com.alliander.osgp.adapter.protocol.oslp.elster.domain.entities.OslpDevice)1 GpsCoordinatesDto (com.alliander.osgp.dto.valueobjects.GpsCoordinatesDto)1 LocationInfo (com.alliander.osgp.oslp.Oslp.LocationInfo)1 InetAddress (java.net.InetAddress)1