ProcessDB实时/时序数据库在使用C/C++进行开发时,支持订阅的功能来进行数据操作
我们使用订阅功能的时候,需要先把所要操作的数据点加入到订阅中,支持通过数据点id和数据点名加入订阅
函数 | 注释 |
ebase2_add_id_to_subscribe_builder | 根据数据点id添加订阅 |
ebase2_add_name_to_subscribe_builder | 根据数据点名添加订阅 |
把数据点添加进订阅后,就可以启动订阅了
函数 | 注释 |
ebase2_start_subscribe_realtime | 启动订阅 |
然后使用不同的订阅函数,来获取我们所需的数据
函数 | 注释 |
ebase2_get_subscribe_builder_size | 获取订阅的数据数量 |
ebase2_get_subscribe_data_by_id | 通过数据点id获取订阅数据 |
ebase2_get_subscribe_data_by_name | 通过数据点名获取订阅数据 |
查询结束后,我们关闭数据订阅
函数 | 注释 |
ebase2_stop_subscribe_realtime | 关闭订阅 |
processDB针对订阅数据点的移除,提供了三种方式.
函数 | 注释 |
ebase2_remove_id_from_subscribe_builder | 根据数据点id移除订阅 |
ebase2_remove_name_from_subscribe_builder | 通过数据点名移除订阅数据 |
ebase2_clear_subscribe_builder | 清空订阅 |
我们这里演示通过订阅的方式,查询多个数据点的实时数据,代码如下:
- // ProcessDB_API_TEST.cpp : ProcessDB数据库示例
- //
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include "processdb_v2.h"
- using namespace std;
- void printRecord(char* pointName, MemSampleRecord& memSampleRecord_)
- {
-
- switch (memSampleRecord_.nTagType)
- {
- case TAG_TYPE_FLOAT32:
- printf("Name:%s, Float32 value: %f\n", pointName, memSampleRecord_.value.fFloat32);
- break;
- case TAG_TYPE_DIGITAL:
- printf("Name:%s, Digital value: %d\n", pointName, memSampleRecord_.value.nDigital);
- break;
- case TAG_TYPE_BOOL:
- printf("Name:%s, Bool value: %d\n", pointName, memSampleRecord_.value.bBool);
- break;
- case TAG_TYPE_UINT8:
- printf("Name:%s, UInt8 value: %d\n", pointName, memSampleRecord_.value.nuInt8);
- break;
- case TAG_TYPE_INT8:
- printf("Name:%s, Int8 value: %d\n", pointName, memSampleRecord_.value.nInt8);
- break;
- case TAG_TYPE_UINT16:
- printf("Name:%s, UInt16 value: %d\n", pointName, memSampleRecord_.value.nuInt16);
- break;
- case TAG_TYPE_INT16:
- printf("Name:%s, Int16 value: %d\n", pointName, memSampleRecord_.value.nInt16);
- break;
- case TAG_TYPE_UINT32:
- printf("Name:%s, UInt32 value: %d\n", pointName, memSampleRecord_.value.nuInt32);
- break;
- case TAG_TYPE_INT32:
- printf("Name:%s, Int32 value: %d\n", pointName, memSampleRecord_.value.nInt32);
- break;
- case TAG_TYPE_INT64:
- printf("Name:%s, Int64 value: %ll\n", pointName, memSampleRecord_.value.nInt64);
- break;
- case TAG_TYPE_UINT64:
- printf("Name:%s, UInt64 value: %ll\n", pointName, memSampleRecord_.value.nuInt64);
- break;
- case TAG_TYPE_FLOAT64:
- printf("Name:%s, Float64 value: %lf\n", pointName, memSampleRecord_.value.fFloat64);
- break;
- case TAG_TYPE_TIME:
- printf("Name:%s, Time value: %ll\n", pointName, memSampleRecord_.value.nTime);
- break;
- }
- }
-
- 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);
- }
- /*准备条件
- * 点名:D99.T99.P99 ID:1003000009
- * 点名:PUBLIC.SYS.PDB_OS_CPU_USAGE ID:1001000001
- */
- //根据名称添加查询点
- res = ebase2_add_name_to_subscribe_builder(&ebase, "D99.T99.P99");
- if (0 > res)
- {
- printf("add D99.T99.P99 to subscribe builder failed, error code: %d.\n", res);
- getchar();
- exit(1);
- }
- //根据id添加查询点
- res = ebase2_add_id_to_subscribe_builder(&ebase, 1001000001);
- if (0 > res)
- {
- printf("add D1.T1.P1 to subscribe builder failed, error code: %d.\n", res);
- getchar();
- exit(1);
- }
- //根据数据点id的集合,开始批量查询实时数据
- res = ebase2_start_subscribe_realtime(&ebase);
- if (0 > res)
- {
- printf("Start subscribe data failed, error code: %d.\n", res);
- getchar();
- exit(1);
- }
- for (int i = 0; i < 1000; i++)
- {
- Sleep(1000);
- MemSampleRecord memSampleRecord_ = { 0 };
- //根据数据点名,展示数据
- res = ebase2_get_subscribe_data_by_name(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &memSampleRecord_);
- if (0 > res)
- {
- printf("Get subscribe data failed, error code: %d.\n", res);
- // getchar();
- // exit(1);
- }
- char name[64] = "PUBLIC.SYS.PDB_OS_CPU_USAGE";
- printRecord(name, memSampleRecord_);
- memSampleRecord_ = { 0 };
- //根据数据点id,展示数据
- res = ebase2_get_subscribe_data_by_id(&ebase, 1003000009, &memSampleRecord_);
- if (0 > res)
- {
- printf("Get subscribe data failed, error code: %d.\n", res);
- // getchar();
- // exit(1);
- }
- char name1[64] = "D99.T99.P99";
- printRecord(name1, memSampleRecord_);
- }
- //关闭批量查询实时数据
- res = ebase2_stop_subscribe_realtime(&ebase);
- if (0 > res)
- {
- printf("Stop subscribe data failed, error code: %d.\n", res);
- getchar();
- exit(1);
- }
-
- /* 关闭连接 */
- ebase2_close(&ebase);
-
- return 0;
-
- }
示例运行如下:
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
- Name:D99.T99.P99, Float32 value: 666.000000
- Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 3.131282
- Name:D99.T99.P99, Float32 value: 666.000000