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

目录

前言

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

二、查询历史时序数据


前言

前文已经介绍C/C++使用ProcessDB的基本操作,本文将针对历史时序数据的相关操作进行介绍


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

字段 注释
id 数据点id
name 数据点名
value 实时数据值

nTagType

数据类型

nQuality

数据质量

nMilliSecond

数据时间(毫秒)

nSecond

数据时间(秒)

历史时序数据value结构

字段 注释

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类型的值

 

二、查询历史时序数据

// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//

#include 
#include 
#include 
#include 
#include 
#include "processdb_v2.h"
using namespace std;

int main(int argc, char* argv[])
{
	float val = 0;
	EBASE_RES result = NULL;
	int res = 0;
	EBASE ebase = { 0 };
	/* 初始化连接控制块 */
	res = ebase2_init(&ebase);
	if (0 != res)
	{
		cout << "ebase2_init failed !" << endl;
		getchar();
		exit(1);
	}
	/*设置超时时间*/
	int timeout = 30;
	ebase2_set_options(&ebase, EBASE_CONNECTION_TIMEOUT, &timeout);
	/* 建立连接 */
	res = ebase2_connect(&ebase, "127.0.0.1", 8301, "root", "root");

	if (0 != res)
	{
		cout << "login failed, \n";
		getchar();
		exit(1);
	}
	/* 查询历史时序数据 */
	char fmt[] = "%Y-%m-%d %H:%M:%S";
	char startTimeStr[] = "2022-11-24 13:25:45";
	char stopTimeStr[] = "2022-11-24 13:26:00";
	struct tm tb = { 0 };
	time_t startTime;
	time_t stopTime;
	if (ebase2_strptime(startTimeStr, fmt, &tb))
	{
		startTime = mktime(&tb);
		if (startTime == -1)
		{
			printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
		}
	}
	if (ebase2_strptime(stopTimeStr, fmt, &tb))
	{
		stopTime = mktime(&tb);
		if (stopTime == -1)
		{
			printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
		}
	}
	/*采样间隔*/
	int sample_interval = 0;
	/* 查找历史时序数据 */
	EBASE_RES ebase2_res;
	TimeRegion timeRegion = { 0 };
	timeRegion.tmBegin = startTime;
	timeRegion.tmEnd = stopTime;
	res = ebase2_exec_his_sample_query(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &timeRegion, sample_interval, &ebase2_res);
	if (0 != res)
	{
		printf("ebase2_exec_his_sample_query failed, error code: %d\n", res);
		getchar();
		exit(1);
	}
	printf("History simple query ---------------begin-----------------\n");
	int count = 0;
	MemSampleRecord memSampleRecord = { 0 };
	ebase2_get_result_count(ebase2_res, &count);
	for (unsigned int dwLoop = 0; dwLoop < count; dwLoop++)
	{
		ebase2_get_sample_record(ebase2_res, dwLoop, &memSampleRecord);
		/* 取得历史记录时间的年月日时分秒 */
		char time_str[32] = { 0 };
		ebase2_decode_time_str(memSampleRecord.nSecond, time_str);
		printf("Time: %s  ", time_str);
		/* 取得历史记录的值 */
		switch (memSampleRecord.nTagType)
		{
		case TAG_TYPE_FLOAT32:
			printf("Val: %6.2f    ", memSampleRecord.value.fFloat32);
			break;
		case TAG_TYPE_DIGITAL:
			printf("Val: %8d    ", memSampleRecord.value.nDigital);
			break;
		case TAG_TYPE_BOOL:
		case TAG_TYPE_UINT8:
		case TAG_TYPE_INT8:
			printf("Val: %8d    ", memSampleRecord.value.nInt8);
			break;
		case TAG_TYPE_UINT16:
		case TAG_TYPE_INT16:
			printf("Val: %8d    ", memSampleRecord.value.nInt16);
			break;
		case TAG_TYPE_UINT32:
		case TAG_TYPE_INT32:
			printf("Val: %8d    ", memSampleRecord.value.nInt32);
			break;
		case TAG_TYPE_UINT64:
		case TAG_TYPE_INT64:
		case TAG_TYPE_TIME:
			printf("Val: %8lld    ", memSampleRecord.value.nInt64);
			break;
		case TAG_TYPE_FLOAT64:
			printf("Val: %6.2lf    ", memSampleRecord.value.fFloat64);
			break;
		default:
			break;
		}
		/* 取得历史记录的状态 */
		printf("Quality: %4d\n", memSampleRecord.nQuality);
	}
	printf("History simple query ---------------end-------------------\n");
	ebase2_free_result(ebase2_res);
	printf("His stat record count: %d, \n", count);
	/* 关闭连接 */
	ebase2_close(&ebase);

	return 0;

}

示例运行如下:

History simple query ---------------begin-----------------
Time: 2022-11-24 13:25:45  Val:   0.00    Quality:    0
Time: 2022-11-24 13:25:46  Val:   1.08    Quality:    0
Time: 2022-11-24 13:25:47  Val:   1.12    Quality:    0
Time: 2022-11-24 13:25:48  Val:   1.42    Quality:    0
Time: 2022-11-24 13:25:49  Val:   0.45    Quality:    0
Time: 2022-11-24 13:25:50  Val:   0.00    Quality:    0
Time: 2022-11-24 13:25:51  Val:   0.91    Quality:    0
Time: 2022-11-24 13:25:52  Val:   0.53    Quality:    0
Time: 2022-11-24 13:25:53  Val:   2.33    Quality:    0
Time: 2022-11-24 13:25:54  Val:   2.53    Quality:    0
Time: 2022-11-24 13:25:55  Val:   1.84    Quality:    0
Time: 2022-11-24 13:25:56  Val:   1.43    Quality:    0
Time: 2022-11-24 13:25:57  Val:   0.33    Quality:    0
Time: 2022-11-24 13:25:58  Val:   1.39    Quality:    0
Time: 2022-11-24 13:25:59  Val:   2.22    Quality:    0
Time: 2022-11-24 13:26:00  Val:   0.42    Quality:    0
History simple query ---------------end-------------------
His stat record count: 16,

你可能感兴趣的:(#,c++,时序数据库,数据库开发,物联网,ProcessDB)