• ProcessDB实时/时序数据库——C/C++之数据订阅


    目录

    前言

    一、添加订阅

    二、查询订阅数据

    三、移除订阅

    四、订阅演示


    前言

    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

    清空订阅

    四、订阅演示

    我们这里演示通过订阅的方式,查询多个数据点的实时数据,代码如下:

    1. // ProcessDB_API_TEST.cpp : ProcessDB数据库示例
    2. //
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "processdb_v2.h"
    10. using namespace std;
    11. void printRecord(char* pointName, MemSampleRecord& memSampleRecord_)
    12. {
    13. switch (memSampleRecord_.nTagType)
    14. {
    15. case TAG_TYPE_FLOAT32:
    16. printf("Name:%s, Float32 value: %f\n", pointName, memSampleRecord_.value.fFloat32);
    17. break;
    18. case TAG_TYPE_DIGITAL:
    19. printf("Name:%s, Digital value: %d\n", pointName, memSampleRecord_.value.nDigital);
    20. break;
    21. case TAG_TYPE_BOOL:
    22. printf("Name:%s, Bool value: %d\n", pointName, memSampleRecord_.value.bBool);
    23. break;
    24. case TAG_TYPE_UINT8:
    25. printf("Name:%s, UInt8 value: %d\n", pointName, memSampleRecord_.value.nuInt8);
    26. break;
    27. case TAG_TYPE_INT8:
    28. printf("Name:%s, Int8 value: %d\n", pointName, memSampleRecord_.value.nInt8);
    29. break;
    30. case TAG_TYPE_UINT16:
    31. printf("Name:%s, UInt16 value: %d\n", pointName, memSampleRecord_.value.nuInt16);
    32. break;
    33. case TAG_TYPE_INT16:
    34. printf("Name:%s, Int16 value: %d\n", pointName, memSampleRecord_.value.nInt16);
    35. break;
    36. case TAG_TYPE_UINT32:
    37. printf("Name:%s, UInt32 value: %d\n", pointName, memSampleRecord_.value.nuInt32);
    38. break;
    39. case TAG_TYPE_INT32:
    40. printf("Name:%s, Int32 value: %d\n", pointName, memSampleRecord_.value.nInt32);
    41. break;
    42. case TAG_TYPE_INT64:
    43. printf("Name:%s, Int64 value: %ll\n", pointName, memSampleRecord_.value.nInt64);
    44. break;
    45. case TAG_TYPE_UINT64:
    46. printf("Name:%s, UInt64 value: %ll\n", pointName, memSampleRecord_.value.nuInt64);
    47. break;
    48. case TAG_TYPE_FLOAT64:
    49. printf("Name:%s, Float64 value: %lf\n", pointName, memSampleRecord_.value.fFloat64);
    50. break;
    51. case TAG_TYPE_TIME:
    52. printf("Name:%s, Time value: %ll\n", pointName, memSampleRecord_.value.nTime);
    53. break;
    54. }
    55. }
    56. int main(int argc, char* argv[])
    57. {
    58. EBASE_RES result = NULL;
    59. int res = 0;
    60. EBASE ebase = { 0 };
    61. /* 初始化连接控制块 */
    62. res = ebase2_init(&ebase);
    63. if (0 != res)
    64. {
    65. cout << "ebase2_init failed !" << endl;
    66. getchar();
    67. exit(1);
    68. }
    69. /*设置超时时间*/
    70. int timeout = 30;
    71. ebase2_set_options(&ebase, EBASE_CONNECTION_TIMEOUT, &timeout);
    72. /* 建立连接 */
    73. res = ebase2_connect(&ebase, "127.0.0.1", 8301, "root", "root");
    74. if (0 != res)
    75. {
    76. cout << "login failed, \n";
    77. getchar();
    78. exit(1);
    79. }
    80. /*准备条件
    81. * 点名:D99.T99.P99 ID:1003000009
    82. * 点名:PUBLIC.SYS.PDB_OS_CPU_USAGE ID:1001000001
    83. */
    84. //根据名称添加查询点
    85. res = ebase2_add_name_to_subscribe_builder(&ebase, "D99.T99.P99");
    86. if (0 > res)
    87. {
    88. printf("add D99.T99.P99 to subscribe builder failed, error code: %d.\n", res);
    89. getchar();
    90. exit(1);
    91. }
    92. //根据id添加查询点
    93. res = ebase2_add_id_to_subscribe_builder(&ebase, 1001000001);
    94. if (0 > res)
    95. {
    96. printf("add D1.T1.P1 to subscribe builder failed, error code: %d.\n", res);
    97. getchar();
    98. exit(1);
    99. }
    100. //根据数据点id的集合,开始批量查询实时数据
    101. res = ebase2_start_subscribe_realtime(&ebase);
    102. if (0 > res)
    103. {
    104. printf("Start subscribe data failed, error code: %d.\n", res);
    105. getchar();
    106. exit(1);
    107. }
    108. for (int i = 0; i < 1000; i++)
    109. {
    110. Sleep(1000);
    111. MemSampleRecord memSampleRecord_ = { 0 };
    112. //根据数据点名,展示数据
    113. res = ebase2_get_subscribe_data_by_name(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &memSampleRecord_);
    114. if (0 > res)
    115. {
    116. printf("Get subscribe data failed, error code: %d.\n", res);
    117. // getchar();
    118. // exit(1);
    119. }
    120. char name[64] = "PUBLIC.SYS.PDB_OS_CPU_USAGE";
    121. printRecord(name, memSampleRecord_);
    122. memSampleRecord_ = { 0 };
    123. //根据数据点id,展示数据
    124. res = ebase2_get_subscribe_data_by_id(&ebase, 1003000009, &memSampleRecord_);
    125. if (0 > res)
    126. {
    127. printf("Get subscribe data failed, error code: %d.\n", res);
    128. // getchar();
    129. // exit(1);
    130. }
    131. char name1[64] = "D99.T99.P99";
    132. printRecord(name1, memSampleRecord_);
    133. }
    134. //关闭批量查询实时数据
    135. res = ebase2_stop_subscribe_realtime(&ebase);
    136. if (0 > res)
    137. {
    138. printf("Stop subscribe data failed, error code: %d.\n", res);
    139. getchar();
    140. exit(1);
    141. }
    142. /* 关闭连接 */
    143. ebase2_close(&ebase);
    144. return 0;
    145. }

    示例运行如下:

    1. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
    2. Name:D99.T99.P99, Float32 value: 666.000000
    3. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
    4. Name:D99.T99.P99, Float32 value: 666.000000
    5. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
    6. Name:D99.T99.P99, Float32 value: 666.000000
    7. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
    8. Name:D99.T99.P99, Float32 value: 666.000000
    9. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 4.026989
    10. Name:D99.T99.P99, Float32 value: 666.000000
    11. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    12. Name:D99.T99.P99, Float32 value: 666.000000
    13. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    14. Name:D99.T99.P99, Float32 value: 666.000000
    15. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    16. Name:D99.T99.P99, Float32 value: 666.000000
    17. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    18. Name:D99.T99.P99, Float32 value: 666.000000
    19. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    20. Name:D99.T99.P99, Float32 value: 666.000000
    21. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    22. Name:D99.T99.P99, Float32 value: 666.000000
    23. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    24. Name:D99.T99.P99, Float32 value: 666.000000
    25. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 1.763187
    26. Name:D99.T99.P99, Float32 value: 666.000000
    27. Name:PUBLIC.SYS.PDB_OS_CPU_USAGE, Float32 value: 3.131282
    28. Name:D99.T99.P99, Float32 value: 666.000000

  • 相关阅读:
    【项目部署-apache】windows系统下apache部署django+channels
    Maven 生命周期,仓库概念和打包命令区别(package、install、deploy)
    (续)SSM整合之spring笔记(AOP 基于注解的AOP之各种通知使用,环绕通知,切面的优先级)(P103—P104)
    CFdiv2-Tournament Countdown-(思维+交互题套路)
    培养现货白银入门学习的逆向思维
    makkefile文件自动化编译以及基础文件命令(补)
    生成对抗网络 GAN——Generative Adversarial Nets
    AI网络爬虫:对网页指定区域批量截图
    SpringMVC的视图
    BUUCTF key不在这里
  • 原文地址:https://blog.csdn.net/sun_race/article/details/128073536