/// <summary> /// Convert from <see cref="DataPoint{T}"/> to <see cref="Runtime.DataPoint"/>. /// </summary> /// <typeparam name="TValue">Type of <see cref="IMeasurement">measurement</see> for the <see cref="DataPoint{T}"/>.</typeparam> /// <param name="dataPoint"><see cref="DataPoint{T}"/> to convert from.</param> /// <returns>Converted <see cref="Runtime.DataPoint"/>.</returns> public static Runtime.DataPoint ToRuntime <TValue>(this DataPoint <TValue> dataPoint) where TValue : IMeasurement { var converted = new Runtime.DataPoint { TimeSeries = dataPoint.TimeSeries?.ToProtobuf() ?? Guid.Empty.ToProtobuf(), Timestamp = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(dataPoint.Timestamp) }; switch (dataPoint.Measurement) { case Single single: converted.SingleValue = single.ToRuntime(); break; case Vector2 vector2: converted.Vector2Value = vector2.ToRuntime(); break; case Vector3 vector3: converted.Vector3Value = vector3.ToRuntime(); break; } return(converted); }
/// <summary> /// Convert from <see cref="Runtime.DataPoint"/> to <see cref="DataPoint{T}"/>. /// </summary> /// <param name="dataPoint"><see cref="Runtime.DataPoint"/> to convert from.</param> /// <returns>Converted <see cref="DataPoint{T}"/> represented as <see cref="object"/>.</returns> public static object ToDataPoint(this Runtime.DataPoint dataPoint) { Type valueType = typeof(object); object valueInstance = null; switch (dataPoint.MeasurementCase) { case Runtime.DataPoint.MeasurementOneofCase.SingleValue: valueType = typeof(Single); valueInstance = dataPoint.SingleValue.ToSingle(); break; case Runtime.DataPoint.MeasurementOneofCase.Vector2Value: valueType = typeof(Vector2); valueInstance = dataPoint.Vector2Value.ToVector2(); break; case Runtime.DataPoint.MeasurementOneofCase.Vector3Value: valueType = typeof(Vector3); valueInstance = dataPoint.Vector3Value.ToVector3(); break; } var dataPointType = typeof(DataPoint <>).MakeGenericType(new[] { valueType }); var dataPointInstance = Activator.CreateInstance(dataPointType); var valueProperty = dataPointType.GetProperty("Measurement", BindingFlags.Instance | BindingFlags.Public); valueProperty.SetValue(dataPointInstance, valueInstance); var timestamp = (Timestamp)dataPoint.Timestamp.ToDateTimeOffset(); var timestampProperty = dataPointType.GetProperty("Timestamp", BindingFlags.Instance | BindingFlags.Public); timestampProperty.SetValue(dataPointInstance, timestamp); var timeSeriesProperty = dataPointType.GetProperty("TimeSeries", BindingFlags.Instance | BindingFlags.Public); timeSeriesProperty.SetValue(dataPointInstance, dataPoint.TimeSeries.To <TimeSeriesId>()); return(dataPointInstance); }