dlt645.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "plt.h"
  2. int dlt645_nbr;
  3. struct dlt645_t dlt645[DLT645_NBR_MAX + 1];
  4. static int dlt645_dbcb_0(void *para,int ncolumn,char ** columnvalue,char *columnname[])
  5. {
  6. int i, idx;
  7. struct dbcbparam_t* pcbparam = (struct dbcbparam_t*)para;
  8. struct dlt645_t* dev;
  9. pcbparam->nrow++;
  10. log_dbg("%s, ++,row:%d, col:%d",__func__,pcbparam->nrow,ncolumn);
  11. idx = pcbparam->nrow;
  12. dev = &dlt645[idx];
  13. dev->low = -900000;
  14. dev->high = 900000;
  15. for( i = 0; i < ncolumn; i++){
  16. if( strcmp("chan_idx",columnname[i]) == 0){
  17. dev->comm.chanidx = atoi(columnvalue[i]);
  18. log_dbg("%s, idx:%d, chan_idx:%d, chan_idx:%s",__func__, idx, dev->comm.chanidx, columnvalue[i]);
  19. }else if( strcmp("info",columnname[i]) == 0){
  20. strcpy(dev->szinfo, columnvalue[i]);
  21. }else if( strcmp("address",columnname[i]) == 0){
  22. strcpy(dev->szaddr, columnvalue[i]);
  23. log_dbg("%s, idx:%d, addr:%s, address:%s",__func__, idx, dev->szaddr, columnvalue[i]);
  24. }else if( strcmp("pratio",columnname[i]) == 0){
  25. dev->pratio = atof(columnvalue[i]);
  26. }else if( strcmp("vratio",columnname[i]) == 0){
  27. dev->vratio = atof(columnvalue[i]);
  28. }else if( strcmp("cratio",columnname[i]) == 0){
  29. dev->cratio = atof(columnvalue[i]);
  30. }
  31. else if (strcmp("limit_low", columnname[i]) == 0)
  32. {
  33. dev->low = atoi(columnvalue[i]);
  34. }
  35. else if (strcmp("limit_high", columnname[i]) == 0)
  36. {
  37. dev->high = atoi(columnvalue[i]);
  38. }
  39. }
  40. pcbparam->ret = 0;
  41. log_dbg("%s, --,ret:%d",__func__,pcbparam->ret);
  42. return 0;
  43. }
  44. void dlt645_set_state( int idx, int state, int err)
  45. {
  46. sm_set_state(&dlt645[idx].sm, state, err );
  47. }
  48. int dlt645_get_com_ap( int idx )
  49. {
  50. return dlt645[idx].com_active_p;
  51. }
  52. double dlt645_get_com_ae( int idx )
  53. {
  54. return dlt645[idx].com_active_e;
  55. }
  56. double dlt645_get_pos_ae( int idx )
  57. {
  58. return dlt645[idx].pos_active_e;
  59. }
  60. double dlt645_get_neg_ae( int idx )
  61. {
  62. return dlt645[idx].neg_active_e;
  63. }
  64. double dlt645_get_ua( int idx )
  65. {
  66. return dlt645[idx].ua;
  67. }
  68. double dlt645_get_ub( int idx )
  69. {
  70. return dlt645[idx].ub;
  71. }
  72. double dlt645_get_uc( int idx )
  73. {
  74. return dlt645[idx].uc;
  75. }
  76. double dlt645_get_ia( int idx )
  77. {
  78. return dlt645[idx].ia;
  79. }
  80. double dlt645_get_ib( int idx )
  81. {
  82. return dlt645[idx].ib;
  83. }
  84. double dlt645_get_ic( int idx )
  85. {
  86. return dlt645[idx].ic;
  87. }
  88. double dlt645_get_freq( int idx )
  89. {
  90. return dlt645[idx].freq;
  91. }
  92. int dlt645_get_state( int idx)
  93. {
  94. return dlt645[idx].sm.state;
  95. }
  96. int dlt645_chk_low_hi(int idx)
  97. {
  98. if(dlt645[idx].com_active_p < dlt645[idx].low)
  99. {
  100. return -1;
  101. }
  102. if(dlt645[idx].com_active_p > dlt645[idx].high)
  103. {
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. char* dlt645_get_state_string( int idx)
  109. {
  110. return dlt645[idx].sm.szState;
  111. }
  112. char* dlt645_get_info_str(int idx)
  113. {
  114. return dlt645[idx].szinfo;
  115. }
  116. int dlt645_chk_state_all( int stat)
  117. {
  118. int idx;
  119. for( idx = 1; idx <= dlt645_nbr; idx++){
  120. if( dlt645_get_state(idx) != stat){
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. int dlt645_send_sm_cmd_all( int cmd )
  127. {
  128. int ret = 0;
  129. int idx;
  130. for( idx = 1; idx <= dlt645_nbr; idx++ ){
  131. dlt645_send_sm_cmd(idx, cmd);
  132. }
  133. log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, cmd, ret);
  134. return ret;
  135. }
  136. int dlt645_get_comm_st(int idx)
  137. {
  138. struct dlt645_t* dev = &dlt645[idx];
  139. struct comm_t* comm = &dev->comm;
  140. return comm_get_state(comm);
  141. }
  142. int dlt645_get_cmd( int idx )
  143. {
  144. return dlt645[idx].cmd;
  145. }
  146. void dlt645_reset_cmd(int idx)
  147. {
  148. dlt645[idx].cmd = CMD_SM_DONE;
  149. }
  150. static void* dlt645_thrd_main(void *param)
  151. {
  152. int idx;
  153. log_dbg("%s, ++",__func__);
  154. for( idx = 1; idx <= dlt645_nbr; idx++){
  155. dlt645_comm_init(idx);
  156. dlt645_sm_init(idx);
  157. }
  158. while(1){
  159. for( idx = 1; idx <= dlt645_nbr; idx++ ){
  160. dlt645_comm_dac(idx);
  161. dlt645_sm( idx );
  162. usleep(100000); // 100ms
  163. }
  164. }
  165. log_dbg("%s, --, idx:%d",__func__,idx);
  166. }
  167. int dlt645_init()
  168. {
  169. pthread_t thrd;
  170. int result = 0;
  171. int ret = 0;
  172. int idx = 0;
  173. struct dlt645_t* dev = NULL ;
  174. char *errmsg = NULL;
  175. char sql[1024];
  176. struct dbcbparam_t cbparam;
  177. sqlite3* db = NULL;
  178. log_dbg("%s, ++",__func__);
  179. plt_lock_ctndb();
  180. db = plt_get_ctndb();
  181. sprintf(sql,"select * from dlt645");
  182. cbparam.nrow = 0;
  183. result = sqlite3_exec(db,sql, dlt645_dbcb_0,(void*)&cbparam,&errmsg);
  184. plt_unlock_ctndb();
  185. if( result != SQLITE_OK ){
  186. log_dbg( "%s, result != SQLITE_OK %d", __func__, result);
  187. ret = -1;
  188. }else if( cbparam.ret != 0){
  189. log_dbg( "%s, cbparam.ret != 0 %d", __func__, cbparam.ret);
  190. ret = -2;
  191. }else{
  192. dlt645_nbr = cbparam.nrow ;
  193. if(pthread_create(&thrd,NULL, dlt645_thrd_main, NULL)!=0){
  194. ret = -3;
  195. log_dbg("%s, create dlt645 thrd main fail",__func__);
  196. }
  197. }
  198. log_dbg("%s--, ret:%d",__func__,ret);
  199. return ret;
  200. }
  201. int dlt645_send_sm_cmd( int idx, int val )
  202. {
  203. int ret = 0;
  204. struct dlt645_t* dev = &dlt645[idx];
  205. dev->cmd = val;
  206. leave:
  207. log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, val, ret);
  208. return ret;
  209. }
  210. void dlt645_set_dac_param_en(int idx, int val)
  211. {
  212. struct dlt645_t* dev = &dlt645[idx];
  213. struct comm_t* comm = &dev->comm;
  214. comm_set_dac_param_en(comm, val);
  215. }
  216. int dlt645_get_sm_step(int idx)
  217. {
  218. struct statemachine_t *sm = &dlt645[idx].sm;
  219. return sm_get_step(sm);
  220. }
  221. char* dlt645_get_sm_err_str(int idx)
  222. {
  223. return dlt645[idx].sm.szerr;
  224. }
  225. int dlt645_get_chan_idx(int idx)
  226. {
  227. struct comm_t* comm = &dlt645[idx].comm;
  228. return comm_get_chan_idx(comm);
  229. }
  230. int dlt645_get_adr(int idx)
  231. {
  232. struct comm_t* comm = &dlt645[idx].comm;
  233. return comm_get_adr(comm);
  234. }
  235. int dlt645_get_tick(int idx)
  236. {
  237. return dlt645[idx].sm.tick;
  238. }
  239. char* dlt645_get_comm_state_str(int idx)
  240. {
  241. struct comm_t* comm = &dlt645[idx].comm;
  242. return comm_get_state_str(comm);
  243. }
  244. // int dlt645_get_PT(int idx)
  245. // {
  246. // return dlt645[idx].PT;
  247. // }
  248. // int dlt645_get_CT(int idx)
  249. // {
  250. // return dlt645[idx].CT;
  251. // }
  252. double dlt645_get_pratio(int idx)
  253. {
  254. return dlt645[idx].pratio;
  255. }
  256. double dlt645_get_vratio(int idx)
  257. {
  258. return dlt645[idx].vratio;
  259. }
  260. double dlt645_get_cratio(int idx)
  261. {
  262. return dlt645[idx].cratio;
  263. }
  264. int dlt645_get_nbr()
  265. {
  266. return dlt645_nbr;
  267. }
  268. int dlt645_get_tool_data(int idx,char* buf)
  269. {
  270. struct dlt645_t* dev = &dlt645[idx];
  271. struct statemachine_t* sm = &dev->sm;
  272. struct comm_t* comm = &dev->comm;
  273. char temp_buf[1024*8];
  274. if(idx < 1 || idx > dlt645_nbr || buf == NULL)
  275. return -1;
  276. sm_get_summary(sm, temp_buf, sizeof(temp_buf));
  277. sprintf(buf,"[%d] %s ",idx,temp_buf);
  278. comm_get_summary(comm, temp_buf, sizeof(temp_buf));
  279. strcat(buf,temp_buf);
  280. sprintf(temp_buf,"pratio:%.1f vratio:%.1f cratio:%.1f 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\n",
  281. dev->pratio,dev->vratio, dev->cratio, dev->com_active_p, dev->com_active_e, dev->pos_active_e, dev->neg_active_e,
  282. dev->ua, dev->ub, dev->uc, dev->ia, dev->ib, dev->ic, dev->freq);
  283. strcat(buf,temp_buf);
  284. return 0;
  285. }
  286. int dlt645_get_tbmqtt_data(int idx,char* buf)
  287. {
  288. struct dlt645_t* dev = &dlt645[idx];
  289. struct statemachine_t* sm = &dev->sm;
  290. struct comm_t* comm = &dev->comm;
  291. sprintf(buf, "'device_id':'meter_%d', 'dlt_state':'%s','dlt_com_ae':%.1f,'dlt_pos_ae':%.1f,'dlt_neg_ae':%.1f,'dlt_com_ap':%d,\
  292. 'dlt_ua':%.1f,'dlt_ub':%.1f,'dlt_uc':%.1f,\
  293. 'dlt_ia':%.1f,'dlt_ib':%.1f,'dlt_ic':%.1f\
  294. ",
  295. idx, dlt645_get_state_string( idx ),
  296. dlt645_get_com_ae( idx ),
  297. dlt645_get_pos_ae( idx ),
  298. dlt645_get_neg_ae( idx ),
  299. dlt645_get_com_ap( idx ),
  300. dlt645_get_ua( idx ),
  301. dlt645_get_ub( idx ),
  302. dlt645_get_uc( idx ),
  303. dlt645_get_ia( idx ),
  304. dlt645_get_ib( idx ),
  305. dlt645_get_ic( idx ));
  306. return 0;
  307. }
  308. int dlt645_get_bkds_data(int idx,char* buf)
  309. {
  310. sprintf(buf,"\
  311. \"info\":\"%s\",\"model\":\"%s\",\"state\":\"%s\",\"stp\":%d,\"err\":\"%s\",\
  312. \"chan_idx\":%d,\"adr\":%d,\"tick\":%d,\"comm_state\":\"%s\",\"pratio\":%f,\
  313. \"vratio\":%f,\"cratio\":%f,\"com_ae\":%.1f,\"pos_ae\":%.1f,\"neg_ae\":%.1f,\
  314. \"com_ap\":%d,\"ua\":%.1f,\"ub\":%.1f,\"uc\":%.1f,\"ia\":%.1f,\
  315. \"ib\":%.1f,\"ic\":%.1f,\"freq\":%.1f,\"pwr_fct\":%.1f\
  316. ",
  317. dlt645_get_info_str( idx ),"dlt645",dlt645_get_state_string( idx ),dlt645_get_sm_step( idx ),dlt645_get_sm_err_str( idx ),
  318. dlt645_get_chan_idx( idx ),dlt645_get_adr( idx ),dlt645_get_tick( idx ),dlt645_get_comm_state_str( idx ),dlt645_get_pratio( idx ),
  319. dlt645_get_vratio( idx ),dlt645_get_cratio( idx ),dlt645_get_com_ae( idx ),dlt645_get_pos_ae( idx ),dlt645_get_neg_ae( idx ),
  320. dlt645_get_com_ap( idx ),dlt645_get_ua( idx ),dlt645_get_ub( idx ),dlt645_get_uc( idx ),dlt645_get_ia( idx ),
  321. dlt645_get_ib( idx ),dlt645_get_ic( idx ),dlt645_get_freq( idx ),1.0);
  322. return 0;
  323. }