gtdatasource.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Created by lloyd on 2020/9/28.
  3. //
  4. #include <iostream>
  5. #include <vector>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <map>
  9. #include <iomanip>
  10. #include "string"
  11. #include "gtdatasource.h"
  12. #include "gtrunconf.h"
  13. #include "modbus.h"
  14. #include "DataMapping/dgns_gtgktype.h"
  15. #include "DataMapping/gtdgnsdao.h"
  16. std::vector<std::vector<double>> gtdatasource::getGTTest() {
  17. std::string sgt = "000025360010257700102610001026470020269500302744004027840050282700602866007029020090293901002978012030150130305501503097017031370190317802003225022032710240331402603358028034020310344403303490035035380380358704003636042036890450373904703793050038450520389605503945057039940600404506204090065041230670413807004138073041230750410007804080081040720830407208604081088040990910412209404141096041510990415310204146104041351070412010904106112040981140409611704100119041091210412012404130126041381280413913004135133041271350411713704107139041001410409914304102145041091470411814904128150041331520413515404136155041331570412715904119160041131610410816304107164041071650410816604112167041151690411617004116171041161720411517204112173041111740411017404114175041231750414017604155176041711760418317704189177041901770417417704156177041311760409717604055175040181740397717303929172038801710383417003790170037501690371116703672166036301650359116303554162035291600349715903430157033591550329215403218152031521500309914803031146029491440286814102787139027221370266913402615132025661290253712702522125025271220254811902573117025871140258911102578108025611060253910302518100025030970249909402502092025100890252008602530083025370800253607702530075025200720250906902499066024930630249106102492058024960550249905302503050025050470250204502499043024950400248903702484035024840330248203002481028024830260248402402483022024850200248601802483017024830150248501302482012024780100248200902481007024780060248200502489004024860030249100202497001024940010248900002495000024920000248700002489000024900000248000002486000025020000251400002536";
  18. int unitwidth = 8;
  19. int sgt_point_num = sgt.size() / unitwidth;
  20. std::vector<std::vector<double>> gt;//[sgt_point_num];
  21. std::cout << "num1: " << sgt_point_num << std::endl;
  22. for (int i = 0; i < sgt_point_num; i++) {
  23. std::string s = sgt.substr(i * unitwidth, 3);
  24. std::string l = sgt.substr(i * unitwidth + 3, 5);
  25. // std::cout<<s<<std::endl;
  26. // std::cout<<l<<std::endl;
  27. double s_d = atof(s.c_str()) / 100.0;
  28. double l_d = atof(l.c_str()) / 100.0;
  29. std::vector<double> point;
  30. point.push_back(s_d);
  31. point.push_back(l_d);
  32. gt.push_back(point);
  33. }
  34. return gt;
  35. }
  36. /*double **gtdatasource::getGTTest2() {
  37. std::string sgt = "000025360010257700102610001026470020269500302744004027840050282700602866007029020090293901002978012030150130305501503097017031370190317802003225022032710240331402603358028034020310344403303490035035380380358704003636042036890450373904703793050038450520389605503945057039940600404506204090065041230670413807004138073041230750410007804080081040720830407208604081088040990910412209404141096041510990415310204146104041351070412010904106112040981140409611704100119041091210412012404130126041381280413913004135133041271350411713704107139041001410409914304102145041091470411814904128150041331520413515404136155041331570412715904119160041131610410816304107164041071650410816604112167041151690411617004116171041161720411517204112173041111740411017404114175041231750414017604155176041711760418317704189177041901770417417704156177041311760409717604055175040181740397717303929172038801710383417003790170037501690371116703672166036301650359116303554162035291600349715903430157033591550329215403218152031521500309914803031146029491440286814102787139027221370266913402615132025661290253712702522125025271220254811902573117025871140258911102578108025611060253910302518100025030970249909402502092025100890252008602530083025370800253607702530075025200720250906902499066024930630249106102492058024960550249905302503050025050470250204502499043024950400248903702484035024840330248203002481028024830260248402402483022024850200248601802483017024830150248501302482012024780100248200902481007024780060248200502489004024860030249100202497001024940010248900002495000024920000248700002489000024900000248000002486000025020000251400002536";
  38. int unitwidth = 8;
  39. int sgt_point_num = sgt.size() / unitwidth;
  40. double **gt;
  41. gt = (double **) malloc(sgt_point_num * sizeof(double *));
  42. //std::cout << "num1: " << sgt_point_num << std::endl;
  43. for (int i = 0; i < sgt_point_num; i++) {
  44. std::string s = sgt.substr(i * unitwidth, 3);
  45. std::string l = sgt.substr(i * unitwidth + 3, 5);
  46. // std::cout<<s<<std::endl;
  47. // std::cout<<l<<std::endl;
  48. double s_d = atof(s.c_str()) / 100.0;
  49. double l_d = atof(l.c_str()) / 100.0;
  50. double point[] = {s_d, l_d};
  51. gt[i] = (double *) malloc(2 * sizeof(double));
  52. gt[i][0] = s_d;
  53. gt[i][1] = l_d;
  54. }
  55. return gt;
  56. }*/
  57. /*void gtdatasource::getGTTest3(std::string sgtstr) {
  58. std::string sgt = sgtstr;
  59. int unitwidth = SGTSTRUNITWIDTH;
  60. sgtpointnum = sgt.size() / unitwidth;
  61. gtformatedata = new double *[sgtpointnum];
  62. for (int i = 0; i < sgtpointnum; i++) {
  63. std::string s = sgt.substr(i * unitwidth, 3);
  64. std::string l = sgt.substr(i * unitwidth + 3, 5);
  65. double s_d = atof(s.c_str()) / 100.0;
  66. double l_d = atof(l.c_str()) / 100.0;
  67. double point[] = {s_d, l_d};
  68. gtformatedata[i] = new double[2];
  69. gtformatedata[i][0] = s_d;
  70. gtformatedata[i][1] = l_d;
  71. }
  72. N = 1.2;
  73. //申请空间
  74. *//* int** a2 = new int*[rows];
  75. for(int i=0;i<rows;i++)
  76. a2[i] = new int[columns];*//*
  77. //释放空间
  78. *//*for(int i=0;i<rows;i++)
  79. delete []a2[i];
  80. delete []a2;*//*
  81. }*/
  82. void gtdatasource::getGTFromModbus(dgns_gtbaseparam *gt_data) {
  83. const char *chIp = rf_modbus_server_ip;
  84. int nPort = rf_modbus_server_port;
  85. modbus_t *pmbs_ctx = modbus_new_tcp(chIp, nPort);
  86. int nRet = modbus_connect(pmbs_ctx);
  87. if (-1 == nRet) {
  88. printf("connect failed: %s\n", modbus_strerror(errno));
  89. modbus_free(pmbs_ctx);
  90. }
  91. // 设置Modbus超时时间
  92. struct timeval tv;
  93. tv.tv_sec = 0;
  94. tv.tv_usec = rf_modbus_server_response_timeout;
  95. if (rf_modbus_server_response_timeout_setting_enable) //设置modbus超时时间为1000毫秒
  96. modbus_set_response_timeout(pmbs_ctx, &tv);
  97. printf("----------------read gt data from modbus----------------------------------\n");
  98. // 读取保持寄存器的值,起始地址为 ,寄存器个数为 ,读取到 数组中
  99. uint16_t pointnum;
  100. modbus_set_slave(pmbs_ctx, 1);
  101. int regs = modbus_read_registers(pmbs_ctx, 40984, 1, &pointnum);
  102. printf("所取得功图点数: %d \n", pointnum);
  103. if (regs != 1) return;
  104. //printf("gt pointnum: %d \n", pointnum);
  105. //sgtpointnum = pointnum;
  106. gt_data->sgtpointnum = pointnum;
  107. uint16_t timebyte[5];
  108. modbus_set_slave(pmbs_ctx, 1);
  109. regs = modbus_read_registers(pmbs_ctx, 40985, 6, timebyte);
  110. if (regs != 6) return;
  111. //const char *gtdate = "12453";// 获取的时间有bug
  112. /*printf("%04X-%02X-%02X %02X:%02X:%02X \n", timebyte[0], timebyte[1], timebyte[2], timebyte[3],
  113. timebyte[4], timebyte[5]);*/
  114. std::stringstream ss;
  115. ss << std::hex << timebyte[0] << "-" << std::setw(2) << std::setfill('0') << std::hex << timebyte[1] << "-"
  116. << std::setw(2) << std::setfill('0') << std::hex << timebyte[2] << " " << std::setw(2) << std::setfill('0')
  117. << std::hex << timebyte[3] << ":" << std::setw(2) << std::setfill('0') << std::hex << timebyte[4] << ":"
  118. << std::setw(2) << std::setfill('0') << std::hex << timebyte[5];
  119. // ss>>gt_data->recivetime;
  120. gt_data->recivetime = ss.str();
  121. /* gtprocdate =
  122. std::to_string(timebyte[0]) + "-" + std::to_string(timebyte[1]) + "-" + std::to_string(timebyte[2]) + " " +
  123. std::to_string(timebyte[3]) + ":" + std::to_string(timebyte[4]) + ":" +
  124. std::to_string(timebyte[5]);//std::string (gtdate);*/
  125. uint16_t chongci;
  126. modbus_set_slave(pmbs_ctx, 1);
  127. regs = modbus_read_registers(pmbs_ctx, 40991, 1, &chongci);
  128. if (regs != 1) return;
  129. //printf("gt chongci: %5.2f \n", 60.0 / (chongci / 1000.0));
  130. // N = 60.0 / (chongci / 1000.0);
  131. gt_data->n = 60.0 / (chongci / 1000.0);
  132. uint16_t chongcheng;
  133. modbus_set_slave(pmbs_ctx, 1);
  134. regs = modbus_read_registers(pmbs_ctx, 40993, 1, &chongcheng);
  135. if (regs != 1) return;
  136. //printf("gt chongcheng: %5.2f \n", chongcheng / 100.0);
  137. // S = chongcheng / 100.0;
  138. gt_data->s_pre = chongcheng / 100.0;
  139. uint16_t WeiYi[250];
  140. uint16_t ZaiHe[250];
  141. //weiyi
  142. modbus_set_slave(pmbs_ctx, 1);
  143. regs = modbus_read_registers(pmbs_ctx, 41001, 125, &WeiYi[0]);
  144. if (regs != 125) return;
  145. modbus_set_slave(pmbs_ctx, 1);
  146. regs = modbus_read_registers(pmbs_ctx, 41126, 125, &WeiYi[125]);
  147. if (regs != 125) return;
  148. //zaihe
  149. modbus_set_slave(pmbs_ctx, 1);
  150. regs = modbus_read_registers(pmbs_ctx, 41251, 125, &ZaiHe[0]);
  151. if (regs != 125) return;
  152. modbus_set_slave(pmbs_ctx, 1);
  153. regs = modbus_read_registers(pmbs_ctx, 41376, 125, &ZaiHe[125]);
  154. if (regs != 125) return;
  155. //printf("gt data :");
  156. //std::vector<std::vector<double>> gtformatedata = new double *[sgtpointnum];
  157. gt_data->sgt.clear();
  158. for (int i = 0; i < gt_data->sgtpointnum; i++) {
  159. std::vector<double> p;
  160. p.push_back(WeiYi[i] / 100.0);
  161. p.push_back(ZaiHe[i] / 100.0);
  162. gt_data->sgt.push_back(p);
  163. //printf("[%2.2lf,%5.2lf]\t", p[0], p[1]);
  164. }
  165. printf("\n");
  166. printf("--------------------------------------------------\n");
  167. // 关闭modbus连接
  168. modbus_close(pmbs_ctx);
  169. //释放modbus资源,使用完libmodbus需要释放掉
  170. modbus_free(pmbs_ctx);
  171. }
  172. void gtdatasource::get_std_gt(dgns_gtbaseparam &gt_data, std::string sgtprodate) {
  173. //gt_data_std=NULL;
  174. //gt_data=NULL;
  175. /* std::vector<dgns_gtstandard> ls=dao.get_dgns_gtstandard_list();
  176. if(ls.empty()||ls.size()==0) return;
  177. gt_data_std=ls[0];*/
  178. if (sgtprodate.empty()) return;
  179. gtdgnsdao::get_dgns_gtbaseparam_single(gt_data, "", sgtprodate);
  180. return;
  181. //have completed !!! in 2020-11-19
  182. /**
  183. * 1、从PG数据库中获取到 标准功图 ok using gtdgnsdao::get_dgns_gtstandard_list()
  184. * 2 根据获取到的标准功图的时间,查得所需的gtbaseparam
  185. * 1 not null and 2 not null
  186. * 1 NULL and 2 NULL
  187. */
  188. }
  189. /**
  190. * 标准工况类型编码后内容输入到modbus server
  191. * @param buff 工况既定标准输出编码数组
  192. * @param buffnum 个数
  193. * @return
  194. */
  195. bool gtdatasource::write_gtcvr_out_content_to_modbus_tcp_server(uint16_t *buff, unsigned int buffnum) {
  196. //TODO:完成工况类型标准输入modbus server--测试还没有通过
  197. //like ::::
  198. //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff);
  199. if ((buff == NULL) || (buffnum < 1)) return false;
  200. int n = 0;
  201. modbus_t *mb;
  202. mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port);
  203. modbus_connect(mb);
  204. /* Read 5 registers from the address 0 */
  205. n = modbus_write_registers(mb, 375, buffnum, buff);
  206. return n > 0;//返回写入成功与否
  207. }
  208. /**
  209. * 标准工况类型编码后内容输入到modbus server
  210. * @param buff 工况既定标准输出编码数组 using vector as container
  211. * @return
  212. */
  213. bool gtdatasource::write_gtcvr_out_content_to_modbus_tcp_server(uint16_t buff) {
  214. //TODO:完成工况类型标准输入modbus server--测试还没有通过
  215. //like ::::
  216. //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff);
  217. if ((buff < 1)) return false;
  218. int n = 0;
  219. modbus_t *mb;
  220. mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port);
  221. modbus_connect(mb);
  222. /* Read 5 registers from the address 0 */
  223. n = modbus_write_register(mb, 375, buff);
  224. return n > 0;//返回写入成功与否
  225. }
  226. bool gtdatasource::send_gtcvr_out_content_to_modbuscode_rf(std::string gk_name_CH) {
  227. std::map<std::string, dgns_gtgktype> gktypes_;
  228. dgns_gtgktype type_gkout;
  229. bool r = false;
  230. if (gtdgnsdao::get_dgns_gtgktype_single(type_gkout, -1, gk_name_CH)) {
  231. int s = 0;
  232. std::vector<int> buffcodes;
  233. std::string code_str;
  234. for (int i = 0; i < type_gkout.modbustranscode.length(); i++) { //将字符串分割成字符串数组
  235. if (type_gkout.modbustranscode[i] == '[') { //以空格作为分隔符
  236. continue;
  237. }
  238. if (type_gkout.modbustranscode[i] == ']') {
  239. if (!code_str.empty())
  240. buffcodes.push_back(std::atoi(code_str.c_str()));
  241. code_str = "";
  242. continue;
  243. }
  244. code_str += type_gkout.modbustranscode[i]; //将分割好的字符串放到K数组里
  245. }
  246. uint16_t *buff = new uint16_t[buffcodes.size()];
  247. for (int i = 0; i < buffcodes.size(); ++i) {
  248. buff[i] = buffcodes[i];
  249. }
  250. r = write_gtcvr_out_content_to_modbus_tcp_server(buff, buffcodes.size());
  251. }
  252. return r;
  253. }
  254. bool gtdatasource::write_gtaoid_out_singlegtproc_to_modbus_tcp_server(uint16_t buff) {
  255. //TODO:完成工况类型标准输入modbus server--测试还没有通过
  256. //like ::::
  257. //int nRet = modbus_write_registers(pmbs_ctx, 42001, 5, tmpbuff);
  258. if ((buff < 1)) return false;
  259. int n = 0;
  260. modbus_t *mb;
  261. mb = modbus_new_tcp(rf_modbus_server_ip, rf_modbus_server_port);
  262. modbus_connect(mb);
  263. /* Read 5 registers from the address 0 */
  264. n = modbus_write_register(mb, 385, buff);
  265. return n > 0;//返回写入成功与否
  266. }
  267. bool gtdatasource::send_gtaoid_out_singlegtproc_to_modbuscode_rf(double liqproc,int gtnumdaily) {
  268. bool r = false;
  269. uint16_t buff=(int)(liqproc/gtly_val_effect_hours*(1/(gtnumdaily/24))*std::pow(10,gtly_val_to_rf_RTU_SD));
  270. r = write_gtaoid_out_singlegtproc_to_modbus_tcp_server(buff);
  271. return r;
  272. }