dtsd1352.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include "plt.h"
  2. #include "dtsd1352.h"
  3. int dtsd1352_nbr;
  4. struct dtsd1352_t dtsd1352[DTSD1352_NBR_MAX + 1];
  5. static int dtsd1352_dbcb_0(void *para,int ncolumn,char ** columnvalue,char *columnname[])
  6. {
  7. int i;
  8. struct dbcbparam_t* pcbparam = (struct dbcbparam_t*)para;
  9. struct dtsd1352_t* dev = NULL;
  10. pcbparam->nrow++;
  11. log_dbg("%s, ++,row:%d, col:%d",__func__,pcbparam->nrow,ncolumn);
  12. dev = &dtsd1352[pcbparam->nrow];
  13. for( i = 0; i < ncolumn; i++){
  14. if( strcmp("chan_idx",columnname[i]) == 0){
  15. dev->comm.chanidx = atoi(columnvalue[i]);
  16. }else if( strcmp("info",columnname[i]) == 0){
  17. strcpy(dev->szinfo, columnvalue[i]);
  18. }else if( strcmp("adr",columnname[i]) == 0){
  19. dev->comm.adr = atoi(columnvalue[i]);
  20. }
  21. }
  22. pcbparam->ret = 0;
  23. log_dbg("%s, --,ret:%d",__func__,pcbparam->ret);
  24. return 0;
  25. }
  26. void* dtsd1352_thrd_main(void *param)
  27. {
  28. struct dtsd1352_t* dev = NULL;
  29. int idx;
  30. log_dbg("%s, ++",__func__);
  31. for( idx = 1; idx <= dtsd1352_nbr; idx++){
  32. dev = &dtsd1352[idx];
  33. dev->PT = 1;
  34. dev->CT = 1;
  35. dtsd1352_sm_init(idx);
  36. dtsd1352_comm_init(idx);
  37. }
  38. while(1){
  39. for( idx = 1; idx <= dtsd1352_nbr; idx++ ){
  40. dtsd1352_comm_dac(idx);
  41. dtsd1352_sm( idx );
  42. usleep(300000); // 300ms
  43. }
  44. }
  45. log_dbg("%s, --, idx:%d",__func__,idx);
  46. return NULL;
  47. }
  48. int dtsd1352_get_cmd( int idx )
  49. {
  50. return dtsd1352[idx].cmd;
  51. }
  52. void dtsd1352_reset_cmd(int idx)
  53. {
  54. dtsd1352[idx].cmd = CMD_SM_DONE;
  55. }
  56. int dtsd1352_init()
  57. {
  58. pthread_t thrd;
  59. int result = 0;
  60. int ret = 0;
  61. int idx = 0;
  62. struct dtsd1352_t* dev = NULL ;
  63. char *errmsg = NULL;
  64. char sql[1024];
  65. struct dbcbparam_t cbparam;
  66. sqlite3* db = NULL;
  67. log_dbg("%s, ++",__func__);
  68. plt_lock_ctndb();
  69. db = plt_get_ctndb();
  70. sprintf(sql,"select * from dtsd1352");
  71. cbparam.nrow = 0;
  72. result = sqlite3_exec(db,sql, dtsd1352_dbcb_0,(void*)&cbparam,&errmsg);
  73. plt_unlock_ctndb();
  74. if( result != SQLITE_OK ){
  75. log_dbg("%s, result != SQLITE_OK, result:%d", __func__, result);
  76. ret = -1;
  77. }else if( cbparam.ret != 0){
  78. log_dbg("%s, cbparam.ret != 0, cbparam.ret:%d", __func__, cbparam.ret);
  79. ret = -2;
  80. }else{
  81. dtsd1352_nbr = cbparam.nrow ;
  82. if(pthread_create(&thrd,NULL, dtsd1352_thrd_main, NULL)!=0){
  83. log_dbg( "%s, create dtsd1352 thrd main fail", __func__);
  84. ret = -1;
  85. }
  86. }
  87. log_dbg("%s--, ret:%d",__func__,ret);
  88. return ret;
  89. }
  90. int dtsd1352_get_state( int idx)
  91. {
  92. return dtsd1352[idx].sm.state;
  93. }
  94. char* dtsd1352_get_state_string( int idx)
  95. {
  96. return dtsd1352[idx].sm.szState;
  97. }
  98. int dtsd1352_get_com_ap( int idx )
  99. {
  100. return dtsd1352[idx].com_active_p;
  101. }
  102. double dtsd1352_get_com_ae( int idx )
  103. {
  104. return dtsd1352[idx].com_active_e;
  105. }
  106. double dtsd1352_get_pos_ae( int idx )
  107. {
  108. return dtsd1352[idx].pos_active_e;
  109. }
  110. double dtsd1352_get_neg_ae( int idx )
  111. {
  112. return dtsd1352[idx].neg_active_e;
  113. }
  114. double dtsd1352_get_ua( int idx )
  115. {
  116. return dtsd1352[idx].ua;
  117. }
  118. double dtsd1352_get_ub( int idx )
  119. {
  120. return dtsd1352[idx].ub;
  121. }
  122. double dtsd1352_get_uc( int idx )
  123. {
  124. return dtsd1352[idx].uc;
  125. }
  126. double dtsd1352_get_ia( int idx )
  127. {
  128. return dtsd1352[idx].ia;
  129. }
  130. double dtsd1352_get_ib( int idx )
  131. {
  132. return dtsd1352[idx].ib;
  133. }
  134. double dtsd1352_get_ic( int idx )
  135. {
  136. return dtsd1352[idx].ic;
  137. }
  138. double dtsd1352_get_pwrfct( int idx )
  139. {
  140. return dtsd1352[idx].pwr_factor;
  141. }
  142. double dtsd1352_get_THDUa( int idx )
  143. {
  144. return dtsd1352[idx].THDUa;
  145. }
  146. double dtsd1352_get_THDUb( int idx )
  147. {
  148. return dtsd1352[idx].THDUb;
  149. }
  150. double dtsd1352_get_THDUc( int idx )
  151. {
  152. return dtsd1352[idx].THDUc;
  153. }
  154. double dtsd1352_get_THDIa( int idx )
  155. {
  156. return dtsd1352[idx].THDIa;
  157. }
  158. double dtsd1352_get_THDIb( int idx )
  159. {
  160. return dtsd1352[idx].THDIb;
  161. }
  162. double dtsd1352_get_THDIc( int idx )
  163. {
  164. return dtsd1352[idx].THDIc;
  165. }
  166. double dtsd1352_get_freq( int idx )
  167. {
  168. return dtsd1352[idx].freq;
  169. }
  170. double dtsd1352_get_total_fund_ap( int idx )
  171. {
  172. return dtsd1352[idx].total_fund_ap;
  173. }
  174. double dtsd1352_get_total_harm_ap( int idx )
  175. {
  176. return dtsd1352[idx].total_harm_ap;
  177. }
  178. double dtsd1352_get_total_fund_rp( int idx )
  179. {
  180. return dtsd1352[idx].total_fund_rp;
  181. }
  182. double dtsd1352_get_total_harm_rp( int idx )
  183. {
  184. return dtsd1352[idx].total_harm_rp;
  185. }
  186. char* dtsd1352_get_info_str(int idx)
  187. {
  188. return dtsd1352[idx].szinfo;
  189. }
  190. int dtsd1352_chk_state_all( int stat)
  191. {
  192. int idx;
  193. for( idx = 1; idx <= dtsd1352_nbr; idx++){
  194. if( dtsd1352_get_state(idx) != stat){
  195. return -1;
  196. }
  197. }
  198. return 0;
  199. }
  200. int dtsd1352_send_sm_cmd( int idx, int val )
  201. {
  202. int ret = 0;
  203. struct dtsd1352_t* dev = &dtsd1352[idx];
  204. dev->cmd = val;
  205. leave:
  206. log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, val, ret);
  207. return ret;
  208. }
  209. int dtsd1352_send_sm_cmd_all( int cmd )
  210. {
  211. int ret = 0;
  212. int idx;
  213. for( idx = 1; idx <= dtsd1352_nbr; idx++ ){
  214. dtsd1352_send_sm_cmd(idx, cmd);
  215. }
  216. log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, cmd, ret);
  217. return ret;
  218. }
  219. int dtsd1352_get_comm_st(int idx)
  220. {
  221. struct dtsd1352_t* dev = &dtsd1352[idx];
  222. struct comm_t* comm = &dev->comm;
  223. return comm_get_state(comm);
  224. }
  225. void dtsd1352_set_dac_param_en(int idx, int val)
  226. {
  227. struct dtsd1352_t* dev = &dtsd1352[idx];
  228. struct comm_t* comm = &dev->comm;
  229. comm_set_dac_param_en(comm, val);
  230. }
  231. int dtsd1352_get_sm_step(int idx)
  232. {
  233. struct statemachine_t *sm = &dtsd1352[idx].sm;
  234. return sm_get_step(sm);
  235. }
  236. char* dtsd1352_get_sm_err_str(int idx)
  237. {
  238. return dtsd1352[idx].sm.szerr;
  239. }
  240. int dtsd1352_get_chan_idx(int idx)
  241. {
  242. struct comm_t* comm = &dtsd1352[idx].comm;
  243. return comm_get_chan_idx(comm);
  244. }
  245. int dtsd1352_get_adr(int idx)
  246. {
  247. struct comm_t* comm = &dtsd1352[idx].comm;
  248. return comm_get_adr(comm);
  249. }
  250. int dtsd1352_get_tick(int idx)
  251. {
  252. return dtsd1352[idx].sm.tick;
  253. }
  254. char* dtsd1352_get_comm_state_str(int idx)
  255. {
  256. struct comm_t* comm = &dtsd1352[idx].comm;
  257. return comm_get_state_str(comm);
  258. }
  259. int dtsd1352_get_PT(int idx)
  260. {
  261. return dtsd1352[idx].PT;
  262. }
  263. int dtsd1352_get_CT(int idx)
  264. {
  265. return dtsd1352[idx].CT;
  266. }
  267. int dtsd1352_get_nbr()
  268. {
  269. return dtsd1352_nbr;
  270. }
  271. int dtsd1352_get_tool_data(int idx,char* buf)
  272. {
  273. struct dtsd1352_t* dev = &dtsd1352[idx];
  274. struct statemachine_t* sm = &dev->sm;
  275. struct comm_t* comm = &dev->comm;
  276. char temp_buf[1024 * 8];
  277. if(idx < 1 || idx > dtsd1352_nbr || buf == NULL)
  278. return -1;
  279. sm_get_summary(sm, temp_buf, sizeof(temp_buf));
  280. sprintf(buf," [%d] %s ", idx, temp_buf);
  281. comm_get_summary(comm, temp_buf, sizeof(temp_buf));
  282. strcat(buf,temp_buf);
  283. sprintf(temp_buf," pt:%d ct:%d com_ap:"L_GREEN"%.1f"NONE" com_ae:%.1f pos_ae:%.1f neg_ae:%.1f ua:%.1f ub:%.1f uc:%.1f ia:%.1f ib:%.1f ic:%.1f freq:%.2f pwr_fct:%.2f \
  284. THDUa:%.2f THDUb:%.2f THDUc:%.2f THDIa:%.2f THDIb:%.2f THDIc:%.2f \
  285. tot_fund_ap:%.3f tot_fund_rp:%.3f tot_harm_ap:%.3f tot_harm_rp:%.3f \n",
  286. dev->PT,dev->CT,
  287. dev->com_active_p, dev->com_active_e, dev->pos_active_e, dev->neg_active_e,
  288. dev->ua, dev->ub, dev->uc, dev->ia, dev->ib, dev->ic, dev->freq,
  289. dev->pwr_factor,
  290. dev->THDUa, dev->THDUb, dev->THDUc, dev->THDIa,dev->THDIb, dev->THDIc,
  291. dev->total_fund_ap, dev->total_fund_rp, dev->total_harm_ap, dev->total_harm_rp);
  292. strcat(buf,temp_buf);
  293. return 0;
  294. }
  295. int dtsd1352_get_tbmqtt_data(int idx,char* buf)
  296. {
  297. struct dtsd1352_t* dev = &dtsd1352[idx];
  298. struct statemachine_t* sm = &dev->sm;
  299. struct comm_t* comm = &dev->comm;
  300. char temp_buf[1024 * 8];
  301. sprintf(buf, "'id':'meter_%d', 'dt_state':'%s','dt_com_ae':%.1f,'dt_pos_ae':%.1f,'dt_neg_ae':%.1f,'dt_com_ap':%d,\
  302. 'dt_ua':%.1f,'dt_ub':%.1f,'dt_uc':%.1f,\
  303. 'dt_ia':%.1f,'dt_ib':%.1f,'dt_ic':%.1f,\
  304. 'dt_pwr_fct':%.2f,\
  305. 'dt_THDUa':%.2f,'dt_THDUb':%.2f,'dt_THDUc':%.2f,'dt_THDIa':%.2f,'dt_THDIb':%.2f,'dt_THDIc':%.2f,\
  306. 'dt_tot_fund_ap':%.3f,'dt_tot_fund_rp':%.3f,'dt_tot_harm_ap':%.3f,'dt_tot_harm_rp':%.3f",
  307. idx, dtsd1352_get_state_string( idx ),
  308. dtsd1352_get_com_ae( idx ),
  309. dtsd1352_get_pos_ae( idx ),
  310. dtsd1352_get_neg_ae( idx ),
  311. dtsd1352_get_com_ap( idx ),
  312. dtsd1352_get_ua( idx ),
  313. dtsd1352_get_ub( idx ),
  314. dtsd1352_get_uc( idx ),
  315. dtsd1352_get_ia( idx ),
  316. dtsd1352_get_ib( idx ),
  317. dtsd1352_get_ic( idx ),
  318. dtsd1352_get_pwrfct( idx ),
  319. dtsd1352_get_THDUa( idx ),
  320. dtsd1352_get_THDUb( idx ),
  321. dtsd1352_get_THDUc( idx ),
  322. dtsd1352_get_THDIa( idx ),
  323. dtsd1352_get_THDIb( idx ),
  324. dtsd1352_get_THDIc( idx ),
  325. dtsd1352_get_total_fund_ap( idx ),
  326. dtsd1352_get_total_fund_rp( idx ),
  327. dtsd1352_get_total_harm_ap( idx ),
  328. dtsd1352_get_total_harm_rp( idx ));
  329. }
  330. int dtsd1352_get_cloud_data(int ctn_idx,int idx,char* buf)
  331. {
  332. sprintf(buf, "'id':'meter_%d', 'com_ae':%.1f,\
  333. 'pos_ae':%.1f,\
  334. 'neg_ae':%.1f,\
  335. 'com_ap':%d,\
  336. 'ua':%.1f,\
  337. 'ub':%.1f,\
  338. 'uc':%.1f,\
  339. 'ia':%.1f,\
  340. 'ib':%.1f,\
  341. 'ic':%.1f,\
  342. 'gf':%.1f,\
  343. 'pf':%.1f,\
  344. 'state':%d",
  345. idx, dtsd1352_get_com_ae( idx ),
  346. dtsd1352_get_pos_ae( idx ),
  347. dtsd1352_get_neg_ae( idx ),
  348. dtsd1352_get_com_ap( idx ),
  349. dtsd1352_get_ua( idx ),
  350. dtsd1352_get_ub( idx ),
  351. dtsd1352_get_uc( idx ),
  352. dtsd1352_get_ia( idx ),
  353. dtsd1352_get_ib( idx ),
  354. dtsd1352_get_ic( idx ),
  355. dtsd1352_get_freq( idx ),
  356. dtsd1352_get_pwrfct( idx ),
  357. dtsd1352_get_state( idx ));
  358. }
  359. int dtsd1352_get_bkds_data(int idx,char* buf)
  360. {
  361. sprintf(buf,"\
  362. \"info\":\"%s\",\"model\":\"%s\",\"state\":\"%s\",\"stp\":%d,\"err\":\"%s\",\
  363. \"chan_idx\":%d,\"mbadr\":%d,\"tick\":%d,\"comm_state\":\"%s\",\"pt\":%d,\
  364. \"ct\":%d,\"com_ae\":%.1f,\"pos_ae\":%.1f,\"neg_ae\":%.1f,\"com_ap\":%d,\
  365. \"ua\":%.1f,\"ub\":%.1f,\"uc\":%.1f,\"ia\":%.1f,\"ib\":%.1f,\
  366. \"ic\":%.1f,\"freq\":%.1f,\"pwr_fct\":%.1f,\"thd_ua\":%.1f,\"thd_ub\":%f,\"thd_uc\":%f,\
  367. \"thd_ia\":%f,\"thd_ib\":%f,\"thd_ic\":%f,\"tot_fund_ap\":%f,\"tot_fund_rp\":%f,\
  368. \"tot_harm_ap\":%f,\"tot_harm_rp\":%f\
  369. ",
  370. dtsd1352_get_info_str( idx ),"dtsd1352",dtsd1352_get_state_string( idx ),dtsd1352_get_sm_step( idx ),dtsd1352_get_sm_err_str( idx ),
  371. dtsd1352_get_chan_idx( idx ),dtsd1352_get_adr( idx ),dtsd1352_get_tick( idx ),dtsd1352_get_comm_state_str( idx ),dtsd1352_get_PT( idx ),
  372. dtsd1352_get_CT( idx ),dtsd1352_get_com_ae( idx ),dtsd1352_get_pos_ae( idx ),dtsd1352_get_neg_ae( idx ),dtsd1352_get_com_ap( idx ),
  373. dtsd1352_get_ua( idx ),dtsd1352_get_ub( idx ),dtsd1352_get_uc( idx ),dtsd1352_get_ia( idx ),dtsd1352_get_ib( idx ),
  374. dtsd1352_get_ic( idx ),dtsd1352_get_freq( idx ),dtsd1352_get_pwrfct( idx ),dtsd1352_get_THDUa( idx ),dtsd1352_get_THDUb( idx ),dtsd1352_get_THDUc( idx ),
  375. dtsd1352_get_THDIa( idx ),dtsd1352_get_THDIb( idx ),dtsd1352_get_THDIc( idx ),dtsd1352_get_total_fund_ap( idx ),dtsd1352_get_total_fund_rp( idx ),
  376. dtsd1352_get_total_harm_ap( idx ),dtsd1352_get_total_harm_rp( idx ));
  377. }