InfluxDB v2.0 からのクエリには、次のライブラリを使用します。
https://github.com/influxdata/influxdb-client-csharp
InfluxDB クエリの結果をリストに変換したいと考えています。次のコードでこれを解決しました。面倒な気がしますが、これをもっときれいに解決できる方法はありますか?
namespace InfluxDBRead
{
class Program
{
static async Task Main(string[] args)
{
var influxDBClient = InfluxDBClientFactory.Create("http://localhost:8086", "Qlu0fF8tliDLMK-DCbZ-XwcJ9_PiGY5-Dw-LfTCjcgjy8CUA80KaashqCKR77JUAdbIY-O1A3Ar_mz4jgzZy5g==");
var flux = "from(bucket: \"Development\")\n"
+ "|> range(start: -1d)"
+ "|> filter(fn: (r) => r[\"device\"] == \"Freezer 01\")"
+ "|> filter(fn: (r) => r[\"_field\"] == \"tempBLowAlarm\" or r[\"_field\"] == \"tempBHighAlarm\" or r[\"_field\"] == \"tempB\" or r[\"_field\"] == \"tempALowAlarm\" or r[\"_field\"] == \"tempAHighAlarm\" or r[\"_field\"] == \"tempA\" or r[\"_field\"] == \"levelLowAlarm\" or r[\"_field\"] == \"levelHighAlarm\" or r[\"_field\"] == \"level\" or r[\"_field\"] == \"fillTimeAlarm\" or r[\"_field\"] == \"bypassTimeAlarm\")";
var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "Organisation");
var bypassTimeAlarmRecords = fluxTables[0].Records;
var fillTimeAlarmRecords = fluxTables[1].Records;
var levelRecords = fluxTables[2].Records;
var levelHighAlarmRecords = fluxTables[3].Records;
var levelLowAlarmRecords = fluxTables[4].Records;
var tempARecords = fluxTables[5].Records;
var tempAHighAlarmRecords = fluxTables[6].Records;
var tempALowAlarmRecords = fluxTables[7].Records;
var tempBRecords = fluxTables[8].Records;
var tempBHighAlarmRecords = fluxTables[9].Records;
var tempBLowAlarmRecords = fluxTables[10].Records;
var count = bypassTimeAlarmRecords.Count();
List<Value> Values = new List<Value>();
for(int i = 0; i < count; i++)
{
Value _value = new Value();
_value.DateAndTime = Convert.ToDateTime(bypassTimeAlarmRecords[i].GetTimeInDateTime());
_value.BypassTimeAlarm = Convert.ToBoolean(bypassTimeAlarmRecords[i].GetValue());
_value.FillTimeAlarm = Convert.ToBoolean(fillTimeAlarmRecords[i].GetValue());
_value.Level = Convert.ToDouble(levelRecords[i].GetValue());
_value.LevelHighAlarm = Convert.ToBoolean(levelHighAlarmRecords[i].GetValue());
_value.LevelLowAlarm = Convert.ToBoolean(levelLowAlarmRecords[i].GetValue());
_value.TempA = Convert.ToDouble(tempARecords[i].GetValue());
_value.TempAHighAlarm = Convert.ToBoolean(tempAHighAlarmRecords[i].GetValue());
_value.TempALowAlarm = Convert.ToBoolean(tempALowAlarmRecords[i].GetValue());
_value.TempB = Convert.ToDouble(tempBRecords[i].GetValue());
_value.TempBHighAlarm = Convert.ToBoolean(tempBHighAlarmRecords[i].GetValue());
_value.TempBLowAlarm = Convert.ToBoolean(tempBLowAlarmRecords[i].GetValue());
Values.Add(_value);
}
influxDBClient.Dispose();
}
}
public class Value
{
public DateTime DateAndTime { get; set; }
public double TempA { get; set; }
public double TempB { get; set; }
public double Level { get; set; }
public bool TempAHighAlarm { get; set; }
public bool TempBHighAlarm { get; set; }
public bool LevelHighAlarm { get; set; }
public bool TempALowAlarm { get; set; }
public bool TempBLowAlarm { get; set; }
public bool LevelLowAlarm { get; set; }
public bool FillTimeAlarm { get; set; }
public bool BypassTimeAlarm { get; set; }
}
}