You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

862 lines
36 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using InfluxData.Net.Common;
using InfluxData.Net.InfluxDb;
namespace Mesnac.Basic
{
/// <summary>
/// 时序数据库InfluxDb辅助类
/// </summary>
public class InfluxDbHelper
{
#region 字段定义
private int _errCount = 0; //失败异常的次数
private string influxDbPath = @"D:\influxdb-1.4.3-1"; //时序数据库InfluxDb的根目录
private string endpointUri = "http://localhost:8086"; //时序数据库服务器的访问Uri
private string username = "root"; //时序数据库服务器用户名
private string password = "root"; //时序数据服务器密码
private bool _isStart = false; //时序数据库服务器是否启动
private InfluxDbClient _client = null; //时序数据库客户端对象
private readonly string backViewDbName = "MesnacBackView"; //回放数据库的名称
private readonly string backViewTableName = "Monitor"; //回放数据表名称
private readonly string curveDbName = "MesnacCurve"; //曲线数据库的名称
private readonly string mixCurveTableName = "MixCurveData"; //密炼曲线数据表名称
private readonly string millCurveTableName = "MillCurveData"; //开炼曲线数据表名称
private readonly string backViewRetentionPolicyName = "BackViewDataSaveDays"; //回放数据库保留策略名称
private int backViewDataSaveDays = 30; //回放数据保留的天数
#endregion
#region 单例实现
private static InfluxDbHelper _instance = null;
private InfluxDbHelper()
{
this.influxDbPath = AppConfigHelper.GetAppSettingValue("InfluxDb_Path", @"D:\influxdb-1.4.3-1");
this.endpointUri = AppConfigHelper.GetAppSettingValue("InfluxDb_EndPointUri", "http://localhost:8086");
this.username = AppConfigHelper.GetAppSettingValue("InfluxDb_UserName", "root");
this.password = AppConfigHelper.GetAppSettingValue("InfluxDb_Password", "root");
this.backViewDataSaveDays = AppConfigHelper.GetAppSettingValue("BackViewDataSaveDays", 30);
//this.StopDbServer();
//System.Threading.Thread.Sleep(10);
//this.StartDbServer();
//System.Threading.Thread.Sleep(50);
if (!this.IsStartDbServer)
{
this.StartDbServer();
}
}
public static InfluxDbHelper Instance
{
get
{
lock (String.Empty)
{
if (_instance == null)
{
_instance = new InfluxDbHelper();
}
return _instance;
}
}
}
#endregion
#region 属性定义
/// <summary>
/// 回放数据库名称
/// </summary>
public string BackViewDbName
{
get
{
return this.backViewDbName;
}
}
/// <summary>
/// 返回数据保存策略名称
/// </summary>
public string BackViewRetentionPolicyName
{
get
{
return this.backViewRetentionPolicyName;
}
}
/// <summary>
/// 回放数据表名称
/// </summary>
public string BackViewTableName
{
get
{
return this.backViewTableName;
}
}
/// <summary>
/// 密炼曲线数据表名称
/// </summary>
public string MixCurveTableName
{
get
{
return this.mixCurveTableName;
}
}
/// <summary>
/// 开炼曲线数据表名称
/// </summary>
public string MillCurveTableName
{
get
{
return this.millCurveTableName;
}
}
/// <summary>
/// 服务是否启动
/// </summary>
public bool StartState
{
get
{
return this._isStart;
}
}
/// <summary>
/// InfluxDb客户端对象用于执行查询
/// </summary>
public InfluxDbClient Client
{
get
{
if (_client == null)
{
if (!this.StartState)
{
this.StartDbServer();
}
if (this.StartState)
{
_client = new InfluxDbClient(this.endpointUri, this.username, this.password, InfluxData.Net.Common.Enums.InfluxDbVersion.Latest);
}
}
return _client;
}
}
#endregion
#region 方法定义
#region 判断InfluxDbServer是否已启动
/// <summary>
/// 判断InfluxDbServer是否已启动
/// </summary>
public bool IsStartDbServer
{
get
{
this._isStart = WinCmdHelper.CheckProcessExists("influxd", String.Empty);
if (this._isStart)
{
this.CreateBackViewDb(); //创建回放数据库
this.CreateBackViewRetentionPolicy(); //创建回放数据保留策略
this.CreateCurveDb();
}
return this._isStart;
}
}
#endregion
#region 启动InfluxDbServer
/// <summary>
/// 启动InfluxDbServer
/// </summary>
public void StartDbServer()
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.DoStartDbServerTask));
t.Start();
this._isStart = true;
this.CreateBackViewDb(); //创建回放数据库
this.CreateBackViewRetentionPolicy(); //创建回放数据保留策略
this.CreateCurveDb(); //创建曲线数据库
}
private void DoStartDbServerTask()
{
string cmd = String.Format("{0} -config {1}", System.IO.Path.Combine(this.influxDbPath, "influxd.exe"), System.IO.Path.Combine(this.influxDbPath, "influxdb.conf"));
bool result = Mesnac.Basic.WinCmdHelper.ExcuteDosCommand(cmd, false, false);
if (result)
{
this._isStart = true;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("The InfluxDb Server start success!");
}
else
{
this._isStart = false;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("The InfluxDb Server Start failure!");
}
}
#endregion
#region 停止InfluxDbServer
/// <summary>
/// 停止InfluxDbServer
/// </summary>
public void StopDbServer()
{
bool result = WinCmdHelper.KillProcessExists("influxd", String.Empty);
if (result)
{
this._isStart = false;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("The InfluxDb Server Stop success!");
}
else
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("The InfluxDb Server Stop failure!");
}
}
#endregion
#region 创建回放数据库
/// <summary>
/// 创建回放数据库
/// </summary>
public void CreateBackViewDb()
{
try
{
if (!this.StartState)
{
return;
}
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Database.CreateDatabaseAsync(this.backViewDbName);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Db Result success = " + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Db Result Body = " + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Db Result StatusCode =" + task.Result.StatusCode);
}
catch (Exception ex)
{
this._errCount++;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("创建回放数据库异常:" + ex.Message, ex);
}
}
#endregion
#region 创建曲线数据库
/// <summary>
/// 创建曲线数据库
/// </summary>
public void CreateCurveDb()
{
try
{
if (!this.StartState)
{
return;
}
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Database.CreateDatabaseAsync(this.curveDbName);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create Curve Db Result success = " + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create Curve Db Result Body = " + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create Curve Db Result StatusCode =" + task.Result.StatusCode);
}
catch (Exception ex)
{
this._errCount++;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("创建曲线数据库异常:" + ex.Message, ex);
}
}
#endregion
#region 创建回放数据库的保留策略
/// <summary>
/// 创建回放数据库的保留策略
/// </summary>
public void CreateBackViewRetentionPolicy()
{
try
{
if (!this.StartState)
{
return;
}
string duration = String.Format("{0}d", this.backViewDataSaveDays);
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Retention.CreateRetentionPolicyAsync(this.backViewDbName, this.backViewRetentionPolicyName, duration, 1);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Retention Policy Result success = " + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Retention Policy Result Body = " + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("Create BackView Retention Policy Result StatusCode =" + task.Result.StatusCode);
}
catch (Exception ex)
{
this._errCount++;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("创建回放数据库的保留策略异常:" + ex.Message, ex);
}
}
#endregion
#region 写数据至回放数据库
/// <summary>
/// 写数据至回放数据库
/// </summary>
/// <param name="data">要写入的数据</param>
public void WriteDataToBackViewDb(Dictionary<string, object> data)
{
try
{
if (!this.StartState)
{
return;
}
//ICSharpCode.Core.LoggingService<InfluxDbHelper>.Info("field count = " + data.Count);
InfluxData.Net.InfluxDb.Models.Point point = new InfluxData.Net.InfluxDb.Models.Point();
point.Name = this.backViewTableName; //表名
//point.Tags = new Dictionary<string, object>() { { "EquipCode", "01001" } };
point.Fields = data;
point.Timestamp = DateTime.Now;
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Client.WriteAsync(point, this.backViewDbName, this.backViewRetentionPolicyName, InfluxData.Net.Common.Constants.TimeUnit.Seconds);
if (task != null && task.Result != null && !task.Result.Success)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to backView db result success =" + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to backView db result Body =" + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to backView db result StatusCode =" + task.Result.StatusCode);
}
}
catch (Exception ex)
{
this._errCount++;
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("保存回放数据异常:" + ex.Message, ex);
}
}
#endregion
#region 写数据至曲线数据库
/// <summary>
/// 写数据至曲线数据库
/// </summary>
/// <param name="equipCode">机台编号</param>
/// <param name="data">曲线数据</param>
public void WriteDataToCurveDb(string equipCode, Dictionary<string, object> data)
{
try
{
if (!this.StartState)
{
return;
}
InfluxData.Net.InfluxDb.Models.Point point = new InfluxData.Net.InfluxDb.Models.Point();
point.Name = this.mixCurveTableName; //表名
point.Tags = new Dictionary<string, object>() { { "EquipCode", equipCode } };
point.Fields = data;
point.Timestamp = DateTime.Now;
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Client.WriteAsync(point, this.curveDbName, null, InfluxData.Net.Common.Constants.TimeUnit.Seconds);
if (task != null && task.Result != null && !task.Result.Success)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result success =" + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result Body =" + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result StatusCode =" + task.Result.StatusCode);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("保存曲线数据异常:" + ex.Message, ex);
}
}
/// <summary>
/// 写曲线数据至曲线数据库表
/// </summary>
/// <param name="tableName">要写入的表名</param>
/// <param name="equipCode">机台编号</param>
/// <param name="unitNum">设备编号</param>
/// <param name="data">曲线数据</param>
public void WriteDataToCurveDb(string tableName, string equipCode, string unitNum, Dictionary<string, object> data, DateTime? time)
{
try
{
if (!this.StartState)
{
return;
}
InfluxData.Net.InfluxDb.Models.Point point = new InfluxData.Net.InfluxDb.Models.Point();
point.Name = tableName; //表名
point.Tags = new Dictionary<string, object>() { { "EquipCode", equipCode }, { "UnitNum", unitNum } };
point.Fields = data;
if (time == null)
{
point.Timestamp = DateTime.Now;
}
else
{
point.Timestamp = time;
}
Task<InfluxData.Net.Common.Infrastructure.IInfluxDataApiResponse> task = this.Client.Client.WriteAsync(point, this.curveDbName, null, InfluxData.Net.Common.Constants.TimeUnit.Seconds);
if (task != null && task.Result != null && !task.Result.Success)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result success =" + task.Result.Success);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result Body =" + task.Result.Body);
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Warn("Write Data to curve db result StatusCode =" + task.Result.StatusCode);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("保存曲线数据异常:" + ex.Message, ex);
}
}
#endregion
#region 从曲线数据库获取数据
/// <summary>
/// 从曲线数据库获取数据
/// </summary>
/// <param name="equipCode">机台号</param>
/// <param name="columnList">字段列表</param>
/// <param name="begin">开始时间</param>
/// <param name="end">结束时间</param>
/// <returns>返回对应的曲线数据</returns>
public Dictionary<string, IList<object>> GetDataFromCurveDb(string equipCode, List<string> columnList, DateTime begin, DateTime end)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1} where EquipCode = '{2}' and time >= '{3:yyyy-MM-dd HH:mm:ss}' and time <= '{4:yyyy-MM-dd HH:mm:ss}'";
query = String.Format(query, String.Join(",", columnList.ToArray()), this.mixCurveTableName, equipCode, begin, end);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.curveDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从曲线数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
/// <summary>
/// 从曲线数据库获取数据
/// </summary>
/// <param name="tableName">要读取的表名</param>
/// <param name="equipCode">机台编号</param>
/// <param name="unitNum">设备编号</param>
/// <param name="columnList">要查询的字段列表</param>
/// <param name="begin">开始时间</param>
/// <param name="end">结束时间</param>
/// <returns>返回对应的曲线数据</returns>
public Dictionary<string, IList<object>> GetDataFromCurveDb(string tableName, string equipCode, string unitNum, List<string> columnList, DateTime begin, DateTime end)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1} where EquipCode = '{2}' and UnitNum = '{3}' and time >= '{4:yyyy-MM-dd HH:mm:ss}' and time <= '{5:yyyy-MM-dd HH:mm:ss}'";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, tableName, equipCode, unitNum, begin, end);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.curveDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从曲线数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
/// <summary>
/// 从曲线数据库获取数据
/// </summary>
/// <param name="columnList">字段列表</param>
/// <param name="begin">开始时间</param>
/// <param name="end">结束时间</param>
/// <returns>返回对应的曲线数据</returns>
public Dictionary<string, IList<object>> GetDataFromCurveDb(List<string> columnList, DateTime begin, DateTime end)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1} where time >= '{2:yyyy-MM-dd HH:mm:ss}' and time <= '{3:yyyy-MM-dd HH:mm:ss}'";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, this.mixCurveTableName, begin, end);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.curveDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从曲线数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
/// <summary>
/// 从曲线数据库获取数据
/// </summary>
/// <param name="tableName">要读取的表名</param>
/// <param name="unitNum">设备编号</param>
/// <param name="columnList">字段列表</param>
/// <param name="begin">开始时间</param>
/// <param name="end">结束时间</param>
/// <returns>返回对应的曲线数据</returns>
public Dictionary<string, IList<object>> GetDataFromCurveDb(string tableName, string unitNum, List<string> columnList, DateTime begin, DateTime end)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1} where UnitNum = '{2}' and time >= '{3:yyyy-MM-dd HH:mm:ss}' and time <= '{4:yyyy-MM-dd HH:mm:ss}'";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, tableName, unitNum, begin, end);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.curveDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从曲线数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
#endregion
#region 把数据集合转化为DataTable
/// <summary>
/// 把数据集合转化为DataTable
/// </summary>
/// <param name="data">要转化的数据集合</param>
/// <returns>返回转化后的DataTable对象</returns>
public static DataTable ConvertDicToDataTable(Dictionary<string, IList<object>> data)
{
DataTable dt = new DataTable();
if (data != null && data.Count > 0)
{
string lastKey = String.Empty;
foreach(string key in data.Keys)
{
lastKey = key;
dt.Columns.Add(key, typeof(object));
}
for (int i = 0; i < data[lastKey].Count; i++)
{
DataRow row = dt.NewRow();
foreach(string key in data.Keys)
{
if (i < data[key].Count)
{
row[key] = data[key][i];
}
else
{
row[key] = 0;
}
}
dt.Rows.Add(row);
}
}
return dt;
}
#endregion
#region 从回放数据库获取数据
/// <summary>
/// 从回放数据库获取数据
/// </summary>
/// <param name="tableName">回放数据表名称</param>
/// <param name="columnList">字段列表</param>
/// <param name="time">回放时间点</param>
/// <returns>返回对应时间点的回放数据</returns>
public Dictionary<string, IList<object>> GetDataFromBackViewDb(string tableName, List<string> columnList, DateTime time)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1}.{2} where time = '{3:yyyy-MM-dd HH:mm:ss}'";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, this.backViewRetentionPolicyName, tableName, time);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.BackViewDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从回放数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
/// <summary>
/// 从回放数据库获取数据
/// </summary>
/// <param name="time">回放时间点</param>
/// <returns>返回对应回放时间点的回放数据</returns>
public Dictionary<string, IList<object>> GetDataFromBackViewDb(DateTime time)
{
try
{
if (!this.StartState)
{
return null;
}
Dictionary<string, IList<object>> result = new Dictionary<string, IList<object>>();
string query = "select {0} from {1}.{2} where time = '{3:yyyy-MM-dd HH:mm:ss}'";
string fields = "*";
query = String.Format(query, fields, this.backViewRetentionPolicyName, this.backViewTableName, time);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, this.BackViewDbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
for (int i = 0; i < serie.Columns.Count; i++)
{
result.Add(serie.Columns[i], serie.Values[i]);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从回放数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
#endregion
#region 从InfluxDb数据库获取数据
/// <summary>
/// 从InfluxDb数据库获取数据
/// </summary>
/// <param name="dbName">数据库名称</param>
/// <param name="tableName">表名称</param>
/// <param name="columnList">字段列表</param>
/// <param name="time">时间条件yyyy-MM-dd HH:mm:ss</param>
/// <param name="limit">限制返回结果的最大条数</param>
/// <returns>返回符合条件的n条数据</returns>
public List<Dictionary<string, object>> GetData(string dbName, string tableName, List<string> columnList, DateTime time, int limit)
{
try
{
if (!this.StartState)
{
return null;
}
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
string query = "select {0} from {1} where time > '{2:yyyy-MM-dd HH:mm:ss}' limit {3}";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, tableName, time, limit);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, dbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
Dictionary<string, object> data = null;
for (int i = 0; i < limit; i++)
{
data = new Dictionary<string, object>();
for (int colIndex = 0; colIndex < serie.Columns.Count; colIndex++)
{
data.Add(serie.Columns[colIndex], serie.Values[i][colIndex]);
}
result.Add(data);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error("从InfluxDb数据库获取数据异常:" + ex.Message, ex);
}
return null;
}
/// <summary>
/// 从InfluxDb数据库获取数据
/// </summary>
/// <param name="dbName">数据库名称</param>
/// <param name="retentionPolicyName">数据保存策略</param>
/// <param name="tableName">数据表</param>
/// <param name="columnList">字段列表</param>
/// <param name="time">时间条件yyyy-MM-dd HH:mm:ss</param>
/// <param name="limit">限制返回结果的最大条数</param>
/// <returns>返回符合条件的n条数据</returns>
public List<Dictionary<string, object>> GetData(string dbName, string retentionPolicyName, string tableName, List<string> columnList, DateTime time, int limit)
{
try
{
if (!this.StartState)
{
return null;
}
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
string query = "select {0} from {1}.{2} where time > '{3:yyyy-MM-dd HH:mm:ss}' limit {4}";
string fields = "*";
if (columnList != null && columnList.Count > 0)
{
fields = String.Join(",", columnList.ToArray());
}
query = String.Format(query, fields, retentionPolicyName, tableName, time, limit);
Task<IEnumerable<InfluxData.Net.InfluxDb.Models.Responses.Serie>> task = this.Client.Client.QueryAsync(query, dbName, null, null);
if (task != null && task.Result != null)
{
InfluxData.Net.InfluxDb.Models.Responses.Serie serie = task.Result.FirstOrDefault<InfluxData.Net.InfluxDb.Models.Responses.Serie>();
if (serie != null)
{
Dictionary<string, object> data = null;
for (int i = 0; i < limit; i++)
{
data = new Dictionary<string, object>();
for (int colIndex = 0; colIndex < serie.Columns.Count; colIndex++)
{
data.Add(serie.Columns[colIndex], serie.Values[i][colIndex]);
}
result.Add(data);
}
}
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<InfluxDbHelper>.Error(String.Format("从InfluxDb数据库{0}.{1}.{2}中获取数据异常: {3}", dbName, retentionPolicyName, tableName, ex.Message), ex);
}
return null;
}
#endregion
#endregion
}
}