Search in sources :

Example 1 with LocationInfo

use of org.opensmartgridplatform.oslp.Oslp.LocationInfo in project open-smart-grid-platform 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();
    final InetAddress inetAddress = this.getInetAddress(registerRequest, deviceIdentification);
    // 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(), 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(org.opensmartgridplatform.dto.valueobjects.GpsCoordinatesDto) InetAddress(java.net.InetAddress) OslpDevice(org.opensmartgridplatform.adapter.protocol.oslp.elster.domain.entities.OslpDevice) LocationInfo(org.opensmartgridplatform.oslp.Oslp.LocationInfo)

Aggregations

InetAddress (java.net.InetAddress)1 OslpDevice (org.opensmartgridplatform.adapter.protocol.oslp.elster.domain.entities.OslpDevice)1 GpsCoordinatesDto (org.opensmartgridplatform.dto.valueobjects.GpsCoordinatesDto)1 LocationInfo (org.opensmartgridplatform.oslp.Oslp.LocationInfo)1