public DeviceState Deserialize(byte[] payload, ILogger log) { DroneState restored = serializer.Deserialize(payload); log.LogInformation("Process message for device ID", restored.DeviceId); var deviceState = new DeviceState(); deviceState.DeviceId = restored.DeviceId; if (restored.Battery != null) { deviceState.Battery = restored.Battery; } if (restored.FlightMode != null) { deviceState.FlightMode = (int)restored.FlightMode; } if (restored.Position != null) { deviceState.Latitude = restored.Position.Value.Latitude; deviceState.Longitude = restored.Position.Value.Longitude; deviceState.Altitude = restored.Position.Value.Altitude; } if (restored.Health != null) { deviceState.AccelerometerOK = restored.Health.Value.AccelerometerOK; deviceState.GyrometerOK = restored.Health.Value.GyrometerOK; deviceState.MagnetometerOK = restored.Health.Value.MagnetometerOK; } return(deviceState); }
public void Serialized_Deserialized_DroneState_Are_Identical() { DroneState droneState = new DroneState() { DeviceId = "some_random_device_id", FlightMode = DroneFlightMode.Inflight, Battery = 0.99, Position = (44.5, 44.6, 500), Health = (true, false, false), IsKeyFrame = true }; var byteArraySeg = _serializer.Serialize(droneState); DroneState restored = _serializer.Deserialize(byteArraySeg.Array); Assert.Equal(droneState.DeviceId, restored.DeviceId); Assert.Equal(droneState.FlightMode, restored.FlightMode); Assert.Equal(droneState.Battery, restored.Battery); Assert.Equal(droneState.IsKeyFrame, restored.IsKeyFrame); Assert.Equal(droneState.Position, restored.Position); Assert.Equal(droneState.Health, restored.Health); } }