using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ropin.Core.Common
{
public class InfluxDBHelper
{
string _baseAddress = "http://localhost:8086"; //127.0.0.1
string _username = "admin";
string _password = "admin";
///
/// 读
///
///
///
/// sql = "CREATE DATABASE mydb";
/// sql = "select * from test";
///
///
public string Query(string database, string sql)
{
string pathAndQuery = string.Format("/query?db={0}&q={1}", database, sql);
string url = _baseAddress + pathAndQuery;
string result = HttpHelper.Get(url, _username, _password);
return result;
}
///
/// 写
///
///
///
/// string sql = "test,name=测试,count=1 value=10";
/// 插入表test 索引name 列count,value
///
public string Write(string database, string sql)
{
string pathAndQuery = string.Format("/write?db={0}&precision=s", database);
string url = _baseAddress + pathAndQuery;
string result = HttpHelper.Post(url, sql, _username, _password);
return result;
}
}
// public class InfluxDBClient
// {
// //声明InfluxDbClient
// private InfluxDbClient clientDb;
// public InfluxDBClient()
// {
// //连接InfluxDb的API地址、账号、密码
// var infuxUrl = "http://localhost:8086/";
// var infuxUser = "admin";
// var infuxPwd = "admin";
// //创建InfluxDbClient实例
// clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest);
// }
// ///
// /// 从InfluxDB中读取数据
// ///
// public async void GetData()
// {
// //传入查询命令,支持多条
// var queries = new[]
// {
//" SELECT * FROM Reading WHERE time> now() - 24h "
//};
// var dbName = "code-hub";
// //从指定库中查询数据
// var response = await clientDb.Client.QueryAsync(queries, dbName);
// //得到Serie集合对象(返回执行多个查询的结果)
// var series = response.ToList();
// //取出第一条命令的查询结果,是一个集合
// var list = series[0].Values;
// //从集合中取出第一条数据
// var info_model = list.FirstOrDefault();
// }
// ///
// /// 往InfluxDB中写入数据
// ///
// public async void AddData()
// {
// //基于InfluxData.Net.InfluxDb.Models.Point实体准备数据
// var point_model = new Point()
// {
// Name = "Reading",//表名
// Tags = new Dictionary()
//{
// { "Id", 158}
//},
// Fields = new Dictionary()
//{
// { "Val", "webInfo" }
//},
// Timestamp = DateTime.UtcNow
// };
// var dbName = "code-hub";
// //从指定库中写入数据,支持传入多个对象的集合
// var response = await clientDb.Client.WriteAsync(point_model, dbName);
// }
// }
public static class InfluxDBExpress
{
///
/// 将当前时间转换成unix时间戳形式
///
///
///
public static long ToUnixTimestamp(this DateTime datetime)
{
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
}
}