using System; using System.Threading.Tasks; using InfluxDB.Client; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Core.Flux.Domain; using InfluxDB.Client.Writes; namespace Examples { public static class CustomDomainMapping { /// /// Define Domain Object /// private class Sensor { /// /// Type of sensor. /// public string Type { get; set; } /// /// Version of sensor. /// public string Version { get; set; } /// /// Measured value. /// public double Value { get; set; } public DateTimeOffset Timestamp { get; set; } public override string ToString() { return $"{Timestamp:MM/dd/yyyy hh:mm:ss.fff tt} {Type}, {Version} value: {Value}"; } } /// /// Define Custom Domain Object Converter /// private class DomainEntityConverter : IDomainObjectMapper { /// /// Convert to DomainObject. /// public T ConvertToEntity(FluxRecord fluxRecord) { return (T)ConvertToEntity(fluxRecord, typeof(T)); } public object ConvertToEntity(FluxRecord fluxRecord, Type type) { if (type != typeof(Sensor)) { throw new NotSupportedException($"This converter doesn't supports: {type}"); } var customEntity = new Sensor { Type = Convert.ToString(fluxRecord.GetValueByKey("type")), Version = Convert.ToString(fluxRecord.GetValueByKey("version")), Value = Convert.ToDouble(fluxRecord.GetValueByKey("data")), Timestamp = fluxRecord.GetTime().GetValueOrDefault().ToDateTimeUtc() }; return Convert.ChangeType(customEntity, type); } /// /// Convert to Point /// public PointData ConvertToPointData(T entity, WritePrecision precision) { if (!(entity is Sensor sensor)) { throw new NotSupportedException($"This converter doesn't supports: {entity}"); } var point = PointData .Measurement("sensor") .Tag("type", sensor.Type) .Tag("version", sensor.Version) .Field("data", sensor.Value) .Timestamp(sensor.Timestamp, precision); return point; } } public static async Task Main() { const string host = "http://localhost:9999"; const string token = "my-token"; const string bucket = "my-bucket"; const string organization = "my-org"; var options = new InfluxDBClientOptions(host) { Token = token, Org = organization, Bucket = bucket }; var converter = new DomainEntityConverter(); using var client = new InfluxDBClient(options); // // Prepare data to write // var time = new DateTimeOffset(2020, 11, 15, 8, 20, 15, new TimeSpan(3, 0, 0)); var entity1 = new Sensor { Timestamp = time, Type = "temperature", Version = "v0.0.2", Value = 15 }; var entity2 = new Sensor { Timestamp = time.AddHours(1), Type = "temperature", Version = "v0.0.2", Value = 15 }; var entity3 = new Sensor { Timestamp = time.AddHours(2), Type = "humidity", Version = "v0.13", Value = 74 }; var entity4 = new Sensor { Timestamp = time.AddHours(3), Type = "humidity", Version = "v0.13", Value = 82 }; // // Write data // await client.GetWriteApiAsync(converter) .WriteMeasurementsAsync(new[] { entity1, entity2, entity3, entity4 }, WritePrecision.S); // // Query Data to Domain object // var queryApi = client.GetQueryApiSync(converter); // // Select ALL // var query = $"from(bucket:\"{bucket}\") " + "|> range(start: 0) " + "|> filter(fn: (r) => r[\"_measurement\"] == \"sensor\")" + "|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"; var sensors = queryApi.QuerySync(query); // // Print result // sensors.ForEach(it => Console.WriteLine(it.ToString())); } } }