output-service-tsdb/InfluxDB/InfluxWriter.cs

33 lines
992 B
C#
Raw Normal View History

2020-04-08 02:58:29 +02:00
using System;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
2020-04-08 15:48:56 +02:00
using OutputServiceTSDB.Models;
using OutputServiceTSDB.Utilities;
2020-04-08 02:58:29 +02:00
namespace OutputServiceTSDB.InfluxDB
{
2020-04-08 15:48:56 +02:00
public class InfluxWriter: IDisposable
2020-04-08 02:58:29 +02:00
{
2020-04-08 15:48:56 +02:00
private readonly char[] Token = EnvironmentVariableConfiguration.InfluxDBToken.ToCharArray();
2020-04-08 02:58:29 +02:00
2020-04-08 15:48:56 +02:00
private InfluxDBClient influxDBClient;
public InfluxWriter()
2020-04-08 02:58:29 +02:00
{
2020-04-08 15:48:56 +02:00
influxDBClient = InfluxDBClientFactory.Create(EnvironmentVariableConfiguration.InfluxDBHost, Token);
}
2020-04-08 02:58:29 +02:00
2020-04-08 15:48:56 +02:00
public void Write(MeasurementObject measurementObject)
{
2020-04-08 02:58:29 +02:00
using (var writeApi = influxDBClient.GetWriteApi())
{
2020-04-08 15:48:56 +02:00
writeApi.WriteMeasurement(EnvironmentVariableConfiguration.InfluxDBBucket, EnvironmentVariableConfiguration.InfluxDBOrg, WritePrecision.Ns, measurementObject);
2020-04-08 02:58:29 +02:00
}
2020-04-08 15:48:56 +02:00
}
2020-04-08 02:58:29 +02:00
2020-04-08 15:48:56 +02:00
public void Dispose()
{
2020-04-08 02:58:29 +02:00
influxDBClient.Dispose();
}
}
2020-04-08 15:48:56 +02:00
}