main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include <iostream>
  2. #include "opencv2/opencv.hpp"
  3. #include "gtdatasource.h"
  4. #include "gtbaseparamanalysize.h"
  5. #include "gtstandardlization.h"
  6. #include "gtcvcore.h"
  7. #include "DataMapping/gtdgnsdao.h"
  8. #include "DataMapping/dgns_gtbaseparam.h"
  9. #include "DataMapping/dgns_gtmloutcode.h"
  10. #include "Test/modbustest.hpp"
  11. #include "Test/jsontest.hpp"
  12. #include "pthread.h"
  13. #include "gtsecondanalysize.h"
  14. #include "gtselectstandardgt.h"
  15. #include "gtrunconf.h"
  16. #include "DataMapping/dgns_gtcvrresult.h"
  17. #include "gtaoidcore.h"
  18. #include "DataMapping/aoid_config_data.h"
  19. #include "DataMapping/aoid_gk_model.h"
  20. #include "DataMapping/aoid_singlewell_config.h"
  21. #include "DataMapping/gtaoiddao.h"
  22. #include "DataMapping/aoid_singlegt_yield.h"
  23. void *cal_liqproc(const std::string &recitime) {
  24. /*dgns_gtbaseparam *_baseparam = (dgns_gtbaseparam *) args[0];
  25. dgns_gtcvrresult *_cvrresult = (dgns_gtcvrresult *) args[1];*/
  26. std::cout<<"----------------------------cal liq---------------------"<<std::endl;
  27. dgns_gtbaseparam _baseparam;
  28. if(!gtdgnsdao::get_dgns_gtbaseparam_single(_baseparam,"",recitime)) {
  29. std::cout << "Waring:未找到对应的功图" << std::endl;
  30. return 0;
  31. }
  32. std::vector<dgns_gtcvrresult> _cvrresultlist=gtdgnsdao::getDgns_gtcvrresultContent("",_baseparam.recivetime);
  33. if (_cvrresultlist.empty() || _cvrresultlist.size() == 0) {
  34. std::cout << "Waring:无诊断结果已跳过" << std::endl;
  35. return 0;
  36. }
  37. dgns_gtcvrresult _cvrresult=_cvrresultlist[0];
  38. gtaoidcore::gtrevision rev;
  39. rev.sgt_encode(&_baseparam);
  40. gtaoidcore::calbgt cbgt;
  41. cbgt.cal_bgt(rev, _cvrresult.result2);
  42. aoid_config_data cdata;
  43. if(!gtaoiddao::get_aoid_config_data_and_open_hotupdate(cdata)){
  44. std::cout<<"Warning:没有配置井筒基础数据";
  45. return 0;
  46. }
  47. aoid_gk_model gkModel;
  48. gtaoiddao::get_aoid_gk_model_fromDB(_cvrresult.result2, gkModel);
  49. aoid_singlewell_config singlewellConfig;
  50. gtaoiddao::get_aoid_singlewell_config_fromDB(gtly_cal_effectstroke_fun, singlewellConfig);
  51. gtaoidcore _gtaoidcore;
  52. std::vector<double> cal_ly_r = _gtaoidcore.cal_ylandon(_baseparam.sgt,
  53. cbgt.bgt, _baseparam.s, _baseparam.n,
  54. _cvrresult.result2, cdata.bj,
  55. cdata.bs, cdata.hsl, cdata.smd, cdata.ymd, cdata.qyb,
  56. cdata.dym, gkModel.gkxs,
  57. singlewellConfig.gradient_1, singlewellConfig.gradient_10,
  58. singlewellConfig.gradient_2,
  59. singlewellConfig.gradient_3,
  60. singlewellConfig.gradient_4, singlewellConfig.gradient_5,
  61. singlewellConfig.gradient_6,
  62. singlewellConfig.gradient_7,
  63. singlewellConfig.gradient_8, singlewellConfig.gradient_9,
  64. singlewellConfig.dividenum_1,
  65. singlewellConfig.dividenum_2,
  66. singlewellConfig.dividenum_3, singlewellConfig.dividenum_4,
  67. singlewellConfig.dividenum_5,
  68. singlewellConfig.dividenum_6,
  69. singlewellConfig.dividenum_7, singlewellConfig.dividenum_8,
  70. singlewellConfig.dividenum_9,
  71. singlewellConfig.dividenum_10);
  72. if (cal_ly_r.empty() || cal_ly_r.size() == 0) {
  73. std::cout << "Error:量液结果计算出错!" << std::endl;
  74. return 0;
  75. }
  76. {
  77. aoid_singlegt_yield singlegtYield;
  78. singlegtYield.qyb = cdata.qyb;
  79. singlegtYield.hsl = cdata.hsl;
  80. singlegtYield.bj = cdata.bj;
  81. singlegtYield.sgt = _baseparam.sgt;
  82. singlegtYield.bgt=cbgt.bgt;
  83. singlegtYield.bdxs = gkModel.gkxs;
  84. singlegtYield.bs = cdata.bs;
  85. singlegtYield.cmd = cdata.dym - cdata.bs;
  86. singlegtYield.eczdjg = _cvrresult.result2;
  87. singlegtYield.frequence = _baseparam.n;
  88. singlegtYield.stroke = _baseparam.s;
  89. singlegtYield.yy = cdata.yy;
  90. singlegtYield.ty = cdata.ty;
  91. singlegtYield.hy = cdata.hy;
  92. singlegtYield.prod_date = _baseparam.recivetime;
  93. singlegtYield.refrencesgt = _baseparam.sgt;
  94. singlegtYield.zdjg = _cvrresult.result2;
  95. singlegtYield.scsj = 24;//TODO::修改计算部分的时间,根据一天多少功图平均一下,直接写24也没事
  96. singlegtYield.pumpdepth = cdata.bs;
  97. singlegtYield.yzzj = cdata.bj;
  98. //日产液量 日产油量 日产水量 日产气量 泵效 理论排量 实际冲程 有效冲程 左冲程 右冲程 冲程损失 漏失损失 gyss 有效冲程比例
  99. singlegtYield.liq_prod_daily = cal_ly_r[0];
  100. singlegtYield.oil_prod_daily = cal_ly_r[1];
  101. singlegtYield.water_prod_daily = cal_ly_r[2];
  102. singlegtYield.gas_prod_daily = cal_ly_r[3];
  103. singlegtYield.bx = cal_ly_r[4];
  104. singlegtYield.llpl = cal_ly_r[5];
  105. singlegtYield.sjcc = cal_ly_r[6];
  106. singlegtYield.yxcc = cal_ly_r[8];
  107. singlegtYield.zcc = cal_ly_r[9];
  108. singlegtYield.ycc = cal_ly_r[10];
  109. singlegtYield.ccss = cal_ly_r[11];
  110. singlegtYield.lsss = cal_ly_r[12];
  111. singlegtYield.gyss = cal_ly_r[13];
  112. singlegtYield.stroke_ratio = cal_ly_r[14];
  113. if (!gtaoiddao::add_aoid_singlegt_yield(singlegtYield)) {
  114. std::cout << "Error:单功图量液计算结果保存失败" << std::endl;
  115. }
  116. else std::cout << "液量计算完成" << std::endl;
  117. }
  118. {
  119. gtdatasource kit_gtsource;
  120. if (kit_gtsource.send_gtaoid_out_singlegtproc_to_modbuscode_rf(cal_ly_r[0], cdata.totalgtnum)) {
  121. std::cout << "量液数据写入modbus成功" << std::endl;
  122. }else std::cout << "Error:量液数据写入modbus失败" << std::endl;
  123. }
  124. }
  125. void *cal_main(void *args) {
  126. gtcvcore::calgray kit_cgray;
  127. gtdatasource kit_gtsource;
  128. using kit_dao = typeof(gtdgnsdao);// kit_dao;
  129. gtbaseparamanalysize kit_gtparam;
  130. gtselectstandardgt kit_stdgt;
  131. gtcvcore kit_gtcvcore;
  132. kit_gtcvcore.initSVM(gtrunconf_svmmodel_load);
  133. dgns_gtbaseparam gt_data_cur;
  134. kit_gtsource.getGTFromModbus(&gt_data_cur);
  135. kit_gtparam.analysisParam(&gt_data_cur);
  136. if (gt_data_cur.islegal != 1) {
  137. std::cout << "Error:没有输入正确的功图!!!" << std::endl;
  138. return 0;
  139. }
  140. dgns_gtbaseparam gt_data_his;
  141. dgns_gtstandard gt_data_his_std;
  142. kit_stdgt.select_bzt_from_db(gt_data_his_std);
  143. kit_gtsource.get_std_gt(gt_data_his, gt_data_his_std.sgtprodate);
  144. //TODO:这里数据库的实时功图数据定时将3个月之前的功图删除,保证数据库占用空间稳定
  145. if (!kit_dao::add_dgns_gtbaseparam_single(gt_data_cur)) {
  146. //数据库有唯一主键(功图产生时间)控制,插入失败人为是已诊断过功图,跳过本次诊断
  147. return 0;
  148. }
  149. dgns_gtstandard gt_data_cur_std;
  150. if (!kit_cgray.calGrayCharacters(gt_data_cur_std, gt_data_cur)) {
  151. std::cout << "gt cal gray error!!!" << std::endl;
  152. return 0;
  153. }
  154. gtstandardlization _gtstandardization;
  155. _gtstandardization.standardization(&gt_data_cur);
  156. _gtstandardization.paddingMap();
  157. std::cout << "-----------------The first diagnose---------------" << std::endl;
  158. std::string predictresult_code_str = "1";
  159. predictresult_code_str = kit_gtcvcore.HOGSVMPredict(_gtstandardization.mapGT_Standard1,
  160. _gtstandardization.mapGT_Standard2, gtrunconf_cvcore_regnfunc,
  161. gtrunconf_svmmodel_load, "test");
  162. std::map<int, dgns_gtmloutcode> dgns_gtmloutcode_map = kit_dao::get_dgns_gtmloutcode_map();
  163. int predictresult_code = std::atoi(predictresult_code_str.c_str());
  164. std::map<int, dgns_gtmloutcode>::iterator iter = dgns_gtmloutcode_map.find(predictresult_code);
  165. std::string predictresult_std = "";
  166. if (iter != dgns_gtmloutcode_map.end()) predictresult_std = iter->second.gkdec;
  167. std::cout << std::endl << "图形特征识别:" << predictresult_std << std::endl;
  168. std::cout << "----------------------------------------------" << std::endl;
  169. std::cout << "-----------------The compare GTSimillar---------------" << std::endl;
  170. /* cv::Scalar siml = _gtcvcore.C_GTSimillar_HOG(_gtstandardization.mapGT_Standard1,
  171. _gtstandardization.mapGT_Standard2,
  172. _gtstandardization2.mapGT_Standard1,
  173. _gtstandardization2.mapGT_Standard2);*/
  174. //std::cout << "the two gt img similar (MSSIM):" << siml << std::endl;
  175. double siml2 = kit_cgray.compareCharacters(gt_data_cur_std.sgtcharacters, gt_data_his_std.sgtcharacters);
  176. std::cout << "功图相似度变化解释 (Gray):" << siml2 << std::endl;
  177. std::cout << "----------------------------------------------" << std::endl;
  178. std::cout << "-----------------The sec func diagnose---------------" << std::endl;
  179. gtsecondanalysize secfunc;
  180. std::string secresult = secfunc.secondanalysize_main(gt_data_his, gt_data_cur, predictresult_code,
  181. predictresult_std,
  182. siml2);
  183. std::cout << std::endl << "最终工况解释结果:" << secresult << std::endl;
  184. std::cout << "----------------------------------------------" << std::endl;
  185. {
  186. dgns_gtcvrresult gtcvrresult;
  187. gtcvrresult.sgt = gt_data_cur.sgt;
  188. gtcvrresult.result2 = secresult;
  189. gtcvrresult.result1 = predictresult_std;
  190. gtcvrresult.isexpected = 1;
  191. gtcvrresult.isproblem = "0";//TODO:是否是问题配置到gk code表里
  192. gtcvrresult.processparam= "{\"sgt_recivetime\":\""+gt_data_cur.recivetime+"\"}";
  193. if (kit_dao::addDgns_gtcvrresultContent(gtcvrresult)) {
  194. std::cout << "诊断结果保存成功" << std::endl;
  195. } else
  196. std::cout << "诊断结果保存失败" << std::endl;
  197. }
  198. {
  199. std::cout << "----------------write modbus rf_server----------------" << std::endl;
  200. //写回modbus
  201. if (kit_gtsource.send_gtcvr_out_content_to_modbuscode_rf(secresult)) {
  202. std::cout << "写入瑞飞RTU成功!" << std::endl;
  203. } else
  204. std::cout << "写入瑞飞RTU失败!" << std::endl;
  205. std::cout << "----------------------------------------------" << std::endl;
  206. }
  207. {
  208. std::cout << "----------------cal bzt----------------" << std::endl;
  209. //标准功图计算
  210. gt_data_cur_std.graphicfeatureinterpretation = predictresult_std;
  211. if (kit_stdgt.cal_std_sgt(gt_data_cur_std)) {
  212. std::cout << "标准功图计算成功!" << std::endl;
  213. } else
  214. std::cout << "标准功图计算失败!" << std::endl;
  215. std::cout << "----------------------------------------------" << std::endl;
  216. }
  217. cal_liqproc(gt_data_cur.recivetime);
  218. }
  219. int main() {
  220. /* dgns_gtcvrparam pa;
  221. std::string s= pa.allParamToJsonTranslate();
  222. std::cout<<s<<std::endl;
  223. return 0;*/
  224. /* if (0) {
  225. jsontest jt;
  226. jt.maintest(0, NULL);
  227. gtdgnsdao dd;
  228. dgns_gtbaseparam p;
  229. p.id = "3434f539-d1b5-4265-b32e-b31bffbfb05a";
  230. p.s_effect = 0.65;
  231. p.l_max = 9;
  232. if (dd.add_dgns_gtbaseparam_single(p)) {
  233. std::cout << "insert successfully!" << std::endl;
  234. }
  235. modbustest mb;
  236. mb.maintest();
  237. }*/
  238. /*gtdgnsdao d;
  239. std::vector<dgns_gtcvrparam> ds= d.getTableContent(0,"sd");
  240. dgns_gtcvrparam d3;
  241. d.alterTableContent( &d3);
  242. return 2;*/
  243. /*int cycle = 3;
  244. while (cycle > 0) {
  245. pthread_t pid;
  246. if (pthread_create((&pid), NULL, cal_main,NULL) < 0) {
  247. printf("creat thread faild\r\n");
  248. }*/
  249. while (1) {
  250. //cv::waitKey(1000);
  251. cal_main(NULL);
  252. //cal_liqproc();
  253. //TODO:报错后这里会导致无限重连,延迟一下?
  254. //目前多线程延迟或许不支持
  255. //主进程延迟,会导致上下文数据丢失,导致奇葩指针错误
  256. //_sleep(200);
  257. }
  258. // --cycle;
  259. // }
  260. return 0;
  261. }