目录
前言
上文已经介绍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类型的值 |
二、查询实时数据
根据数据点id的集合,批量查询实时数据
// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include "processdb_v2.h"
using namespace std;
int main(int argc, char* argv[])
{
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);
}
//根据数据点id的集合,批量查询实时数据
int status_code[2] = { 0 };
OBJECT_ID point_id[2] = { 0 };
MemSampleRecord record[2] = { 0 };
point_id[0] = 1003000009;
point_id[1] = 1001000001;
res = ebase2_query_realtime_data_ex2(&ebase, point_id, 2, record, status_code);
if (0>res)
{
printf("ebase2_query_realtime_data_ex2 failed, error code: %d.\n", res);
getchar();
exit(1);
}
else
{
for (int i = 0; i < sizeof(status_code) / sizeof(int); i++)
{
if (status_code[i] != 0)
{
printf("point id: %d,error code: %d\n", point_id[i], status_code[i]);
}
else
{
printf("point id: %d,value: %f\n", point_id[i], record[i].value.fFloat32);
}
}
}
/* 关闭连接 */
ebase2_close(&ebase);
return 0;
}
示例运行如下:
point id: 1003000009,value: 666.000000
point id: 1001000001,value: 10.891659
三、更新实时数据
// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include "processdb_v2.h"
using namespace std;
int refreshRealtimeData(EBASE *ebase, const char* fullPointName, char dataType, short nQuality, float value)
{
MemSampleRecord memSampleRecord = { 0 };
memSampleRecord.nSecond = time(NULL);
memSampleRecord.nQuality = nQuality;
memSampleRecord.nTagType = dataType;
switch (memSampleRecord.nTagType)
{
case TAG_TYPE_FLOAT32:
memSampleRecord.value.fFloat32 = value;
break;
case TAG_TYPE_DIGITAL:
memSampleRecord.value.nDigital = (char)value;
break;
case TAG_TYPE_BOOL:
memSampleRecord.value.bBool = (bool)(memSampleRecord.nSecond%2);
break;
case TAG_TYPE_UINT8:
case TAG_TYPE_INT8:
memSampleRecord.value.nInt8 = (char)value;
break;
case TAG_TYPE_UINT16:
case TAG_TYPE_INT16:
memSampleRecord.value.nInt16 = (short)value;
break;
case TAG_TYPE_UINT32:
case TAG_TYPE_INT32:
memSampleRecord.value.nInt32 = (int)value;
break;
case TAG_TYPE_UINT64:
case TAG_TYPE_INT64:
case TAG_TYPE_TIME:
memSampleRecord.value.nInt64 = (long long)value;
break;
case TAG_TYPE_FLOAT64:
memSampleRecord.value.fFloat64 = value;
break;
}
return ebase2_add_realtime_data_to_update_builder_by_name(ebase, fullPointName, &memSampleRecord);
}
typedef struct parameter_st
{
char name[128];
OBJECT_ID id;
DATA_TYPE dataType;
float value;
}Parameter;
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);
}
/*
准备要更新的数据点
*/
Parameter pointList[2] =
{
{ "D99.T99.P99", 1002000001, TAG_TYPE_FLOAT32, 2.22 },
{ "D77.T77.P77", 1007000006, TAG_TYPE_FLOAT32, 2 }
};
//插入实时数据TCP
for (int i = 0; i < sizeof(pointList) / sizeof(Parameter); i++)
{
val = pointList[i].value;
//更新实时数据
res = refreshRealtimeData(&ebase, pointList[i].name, pointList[i].dataType, 1, pointList[i].value);
if (0 != res)
{
cout << "refreshRealtimeData failed, error code: " << res << endl;
getchar();
exit(1);
}
else
{
printf("%s, val:%f, prepared。\n", pointList[i].name, val);
}
}
//执行更新操作
res = ebase2_exec_update_realtime(&ebase);
if (0 != res)
{
printf("execute update failed, error code: %d\n", res);
getchar();
exit(1);
}
/* 关闭连接 */
ebase2_close(&ebase);
return 0;
}