ProcessDB实时/时序数据库——C#查询历史时序数据

目录

前言

一、历史时序数据字段介绍

二、根据点名查询历史时序数据

 三、根据id查询历史时序数据


前言

ProcessDB实时/时序数据库支持C#语言开发,本文将针对C#操作ProcessDB库的历史时序数据的相关操作进行介绍


一、历史时序数据字段介绍

字段注释
id数据点id
name数据点名

nTagType

数据类型

nQuality

数据质量

nMilliSecond

数据时间(毫秒)

nSecond

数据时间(秒)

nDigital

数字量

bBool

bool类型的值

nuInt8

uint8类型的值

nInt8

in8类型的值

nuInt16

uint16类型的值

nInt16

int16类型的值

nuInt32

uint32类型的值

nInt32

int32类型的值

nuInt64

uint64类型的值

nInt64

Int64类型的值

nTime

time时间类型的值

fFloat32

float32类型的值

fFloat64

float64类型的值

二、根据点名查询历史时序数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProcessDB;

namespace Data_History
{
    class Program
    {
        static void Main(string[] args)
        {
            TestHistory testHistory = new TestHistory();
            int res = testHistory.HistoryTest();
        }
    }

    public class TestHistory {
        public class pointlist
        {
            public string pointname { get; set; }
            public int pointid { get; set; }
            public DATA_TYPE datatype { get; set; }
        }

        static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        public int HistoryTest() {
            int res = 0;
            /* 初始化连接控制块 */
            res = ProcessDBDao.getInstance().init();
            if (0 != res)
            {
                Console.WriteLine("ProcessDBDao init failed! error code: {0}", res);
                return res;
            }
            /* 建立连接 */
            res = ProcessDBDao.getInstance().connect("127.0.0.1", 8301, "root", "root");
            //res = ProcessDBDao.getInstance().connect("192.168.0.186", 8301, "root", "root");
            if (0 != res)
            {
                Console.WriteLine("login failed! error code: {0}", res);
                return res;
            }
            Dictionary<string, int> map = new Dictionary<string, int>();
            var pointlist = new List<pointlist>
            {
                new pointlist{ pointname = "D99.T99.P99", datatype = DATA_TYPE.TAG_TYPE_FLOAT32},

            };

            /* 查询时序数据 */
            long dt = 0;
            TimeRegion timeRegion = new TimeRegion();
            TimeSpan ts = DateTime.Now - DateTime.Parse("1970-01-01 08:00:00");
            timeRegion.tmBegin = System.Convert.ToInt32(ts.TotalSeconds) - 18000;//将数字的指定字符串表示形式转换为等效的 32 位有符号整数。
            ts = DateTime.Now - DateTime.Parse("1970-01-01 08:00:00");
            //结束时间
            timeRegion.tmEnd = System.Convert.ToInt32(ts.TotalSeconds);
            if (timeRegion.tmBegin >= timeRegion.tmEnd)
            {
                Console.WriteLine("开始时间应小于结束时间");
                return res;
            }
            int sample_interval = 0;
            dt = timeRegion.tmEnd - timeRegion.tmBegin;
            IntPtr ebase_res;
            MemSampleRecord memSampleRecord = new MemSampleRecord();
            //获取时间
            int count = 0;
            string pointName = "D99.T99.P99";
            DateTime timeStr = new DateTime();
            for (int loop = 0; loop < pointlist.Count; loop++)
            {
                Console.WriteLine("ebase_exec_his_sample_query succeed timeRegion  {0}!", timeRegion.tmBegin);
                res = ProcessDBDao.getInstance().exec_his_sample_query(pointName, ref timeRegion, sample_interval, out ebase_res);
                if (0 != res)
                {
                    Console.WriteLine("ebase_exec_his_sample_query failed! error code: {0}", res);
                    return res;
                }
                else {
                    Console.WriteLine("ebase_exec_his_sample_query succeed!");
                }
                Console.WriteLine("History simple query ---------------begin-----------------\n");
                ProcessDBDao.getInstance().get_result_count(ebase_res, out count);
                Console.WriteLine("ebase_exec_his_sample_query succeed count  {0}!", count);
                for (int dwLoop = 0; dwLoop < count; dwLoop++)
                {
                    res = ProcessDBDao.getInstance().get_sample_record(ebase_res, dwLoop, ref memSampleRecord);
                    if (res < 0)
                    {
                        Console.WriteLine("get_sample_record failed! error code: {0}", res);
                        return res;
                    }
                    else {
                        Console.WriteLine("get_sample_record succeed!");
                    }
                    timeStr = GetTime(Convert.ToString(memSampleRecord.nSecond));
                    switch (memSampleRecord.nTagType)
                    {
                        case (byte)DATA_TYPE.TAG_TYPE_FLOAT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.fFloat32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_DIGITAL:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nDigital);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT8:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt8);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT8:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt8);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT16:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt16);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT16:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt16);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt64);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt64);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_FLOAT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.fFloat64);
                            break;
                    }
                }
                Console.WriteLine("History simple query ---------------end-------------------\n");
                Console.WriteLine("His simple record num:{0},used:{1}:", count, dt);
                ProcessDBDao.getInstance().free_result(ebase_res);
            }
            res = ProcessDBDao.getInstance().close();
            return res;
        }
    }

}

示例运行如下:

ebase_exec_his_sample_query succeed!
History simple query ---------------begin-----------------

ebase_exec_his_sample_query count  2!
get_sample_record succeed!
nTagType: 0, dateTime: 2022/12/12 11:04:19, value: 6666
get_sample_record succeed!
nTagType: 0, dateTime: 2022/12/12 11:04:26, value: 7777
History simple query ---------------end-------------------

His simple record num:2,used:18000

 三、根据id查询历史时序数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProcessDB;

namespace Data_History
{
    class Program
    {
        static void Main(string[] args)
        {
            TestHistory testHistory = new TestHistory();
            int res = testHistory.HistoryTest();
        }
    }

    public class TestHistory
    {
        public class pointlist
        {
            public string pointname { get; set; }
            public int pointid { get; set; }
            public DATA_TYPE datatype { get; set; }
        }

        static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        public int HistoryTest()
        {
            int res = 0;
            /* 初始化连接控制块 */
            res = ProcessDBDao.getInstance().init();
            if (0 != res)
            {
                Console.WriteLine("ProcessDBDao init failed! error code: {0}", res);
                return res;
            }
            /* 建立连接 */
            res = ProcessDBDao.getInstance().connect("127.0.0.1", 8301, "root", "root");
            //res = ProcessDBDao.getInstance().connect("192.168.0.186", 8301, "root", "root");
            if (0 != res)
            {
                Console.WriteLine("login failed! error code: {0}", res);
                return res;
            }
            var pointlist = new List<pointlist>
            {
                new pointlist{ pointname = "D99.T99.P99",pointid = 1003000009 , datatype = DATA_TYPE.TAG_TYPE_FLOAT32},

            };

            /* 查询历史时序数据 */
            long dt = 0;
            //结束时间
            long endTime = 1670823552000;
            //开始时间
            long startTime = 1670812752000;
            int sample_interval = 0;
            IntPtr ebase_res;
            MemSampleRecord memSampleRecord = new MemSampleRecord();
            //获取时间
            int count = 0;
            DateTime timeStr = new DateTime();
            for (int loop = 0; loop < pointlist.Count; loop++)
            {
                res = ProcessDBDao.getInstance().ebase2_exec_his_sample_query_id(pointlist[loop].pointid,ref  startTime,ref  endTime, sample_interval, out ebase_res);
                if (0 != res)
                {
                    Console.WriteLine("ebase_exec_his_sample_query failed! error code: {0}", res);
                    return res;
                }
                else
                {
                    Console.WriteLine("ebase_exec_his_sample_query succeed!");
                }
                Console.WriteLine("History simple query ---------------begin-----------------\n");
                ProcessDBDao.getInstance().get_result_count(ebase_res, out count);
                Console.WriteLine("ebase_exec_his_sample_query succeed count  {0}!", count);
                for (int dwLoop = 0; dwLoop < count; dwLoop++)
                {
                    res = ProcessDBDao.getInstance().get_sample_record(ebase_res, dwLoop, ref memSampleRecord);
                    if (res < 0)
                    {
                        Console.WriteLine("get_sample_record failed! error code: {0}", res);
                        return res;
                    }
                    else
                    {
                        Console.WriteLine("get_sample_record succeed!");
                    }
                    timeStr = GetTime(Convert.ToString(memSampleRecord.nSecond));
                    switch (memSampleRecord.nTagType)
                    {
                        case (byte)DATA_TYPE.TAG_TYPE_FLOAT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.fFloat32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_DIGITAL:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nDigital);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT8:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt8);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT8:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt8);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT16:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt16);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT16:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt16);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT32:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt32);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_UINT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nUInt64);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_INT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.nInt64);
                            break;
                        case (byte)DATA_TYPE.TAG_TYPE_FLOAT64:
                            Console.WriteLine("nTagType: {0}, dateTime: {1}, value: {2}", memSampleRecord.nTagType, timeStr, memSampleRecord.fFloat64);
                            break;
                    }
                }
                Console.WriteLine("History simple query ---------------end-------------------\n");
                Console.WriteLine("His simple record num:{0},used:{1}:", count, dt);
                ProcessDBDao.getInstance().free_result(ebase_res);
            }
            res = ProcessDBDao.getInstance().close();
            return res;
        }
    }

}

示例运行如下:

ebase_exec_his_sample_query succeed!
History simple query ---------------begin-----------------

ebase_exec_his_sample_query count  2!
get_sample_record succeed!
nTagType: 0, dateTime: 2022/12/12 11:04:19, value: 6666
get_sample_record succeed!
nTagType: 0, dateTime: 2022/12/12 11:04:26, value: 7777
History simple query ---------------end-------------------

His simple record num:2,used:0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值