abb_b23_4.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include "abb_b23_4.h"
  2. #include "plt.h"
  3. int abb_b23_4_nbr;
  4. struct abb_b23_4_t abb_b23_4[ABB_B23_4_NBR_MAX + 1];
  5. static int abb_b23_4_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 abb_b23_4_t *dev = NULL;
  10. pcbparam->nrow++;
  11. log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
  12. dev = &abb_b23_4[pcbparam->nrow];
  13. for (i = 0; i < ncolumn; i++)
  14. {
  15. if (strcmp("chan_idx", columnname[i]) == 0)
  16. {
  17. dev->comm.chanidx = atoi(columnvalue[i]);
  18. }
  19. else if (strcmp("info", columnname[i]) == 0)
  20. {
  21. strcpy(dev->szinfo, columnvalue[i]);
  22. }
  23. else if (strcmp("adr", columnname[i]) == 0)
  24. {
  25. dev->comm.adr = atoi(columnvalue[i]);
  26. }
  27. }
  28. pcbparam->ret = 0;
  29. log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
  30. return 0;
  31. }
  32. void* abb_b23_4_thrd_main(void *param)
  33. {
  34. struct abb_b23_4_t *dev = NULL;
  35. int idx;
  36. log_dbg("%s, ++", __func__);
  37. for (idx = 1; idx <= abb_b23_4_nbr; idx++)
  38. {
  39. dev = &abb_b23_4[idx];
  40. dev->PT = 1;
  41. dev->CT = 1;
  42. abb_b23_4_sm_init(idx);
  43. abb_b23_4_comm_init(idx);
  44. }
  45. while (1)
  46. {
  47. for (idx = 1; idx <= abb_b23_4_nbr; idx++)
  48. {
  49. abb_b23_4_comm_dac(idx);
  50. abb_b23_4_sm(idx);
  51. usleep(100000); // 100ms
  52. }
  53. }
  54. log_dbg("%s, --, idx:%d", __func__, idx);
  55. return NULL;
  56. }
  57. int abb_b23_4_get_cmd(int idx)
  58. {
  59. return abb_b23_4[idx].cmd;
  60. }
  61. void abb_b23_4_reset_cmd(int idx)
  62. {
  63. abb_b23_4[idx].cmd = CMD_SM_DONE;
  64. }
  65. int abb_b23_4_init()
  66. {
  67. pthread_t thrd;
  68. int result = 0;
  69. int ret = 0;
  70. int idx = 0;
  71. struct abb_b23_4_t *dev = NULL;
  72. char *errmsg = NULL;
  73. char sql[1024];
  74. struct dbcbparam_t cbparam;
  75. sqlite3 *db = NULL;
  76. log_dbg("%s, ++", __func__);
  77. plt_lock_ctndb();
  78. db = plt_get_ctndb();
  79. sprintf(sql, "select * from abb_b23_4");
  80. cbparam.nrow = 0;
  81. result = sqlite3_exec(db, sql, abb_b23_4_dbcb_0, (void *)&cbparam, &errmsg);
  82. plt_unlock_ctndb();
  83. if (result != SQLITE_OK)
  84. {
  85. log_dbg("%s, result != SQLITE_OK, result:%d", __func__, result);
  86. ret = -1;
  87. }
  88. else if (cbparam.ret != 0)
  89. {
  90. log_dbg("%s, cbparam.ret != 0, cbparam.ret:%d", __func__, cbparam.ret);
  91. ret = -2;
  92. }
  93. else
  94. {
  95. abb_b23_4_nbr = cbparam.nrow;
  96. if (pthread_create(&thrd, NULL, abb_b23_4_thrd_main, NULL) != 0)
  97. {
  98. log_dbg("%s, create abb_b23_4 thrd main fail", __func__);
  99. ret = -1;
  100. }
  101. }
  102. log_dbg("%s--, ret:%d", __func__, ret);
  103. return ret;
  104. }
  105. int abb_b23_4_get_state(int idx)
  106. {
  107. return abb_b23_4[idx].sm.state;
  108. }
  109. char *abb_b23_4_get_state_string(int idx)
  110. {
  111. return abb_b23_4[idx].sm.szState;
  112. }
  113. int abb_b23_4_get_com_ap(int idx)
  114. {
  115. return abb_b23_4[idx].com_ap;
  116. }
  117. double abb_b23_4_get_com_ae(int idx)
  118. {
  119. return abb_b23_4[idx].com_active_e;
  120. }
  121. double abb_b23_4_get_pos_ae(int idx)
  122. {
  123. return abb_b23_4[idx].pos_active_e;
  124. }
  125. double abb_b23_4_get_neg_ae(int idx)
  126. {
  127. return abb_b23_4[idx].neg_active_e;
  128. }
  129. double abb_b23_4_get_ua(int idx)
  130. {
  131. return abb_b23_4[idx].ua;
  132. }
  133. double abb_b23_4_get_ub(int idx)
  134. {
  135. return abb_b23_4[idx].ub;
  136. }
  137. double abb_b23_4_get_uc(int idx)
  138. {
  139. return abb_b23_4[idx].uc;
  140. }
  141. double abb_b23_4_get_ia(int idx)
  142. {
  143. return abb_b23_4[idx].ia;
  144. }
  145. double abb_b23_4_get_ib(int idx)
  146. {
  147. return abb_b23_4[idx].ib;
  148. }
  149. double abb_b23_4_get_ic(int idx)
  150. {
  151. return abb_b23_4[idx].ic;
  152. }
  153. double abb_b23_4_get_pwrfct(int idx)
  154. {
  155. return abb_b23_4[idx].pwr_factor;
  156. }
  157. double abb_b23_4_get_freq(int idx)
  158. {
  159. return abb_b23_4[idx].freq;
  160. }
  161. char *abb_b23_4_get_info_str(int idx)
  162. {
  163. return abb_b23_4[idx].szinfo;
  164. }
  165. int abb_b23_4_chk_state_all(int stat)
  166. {
  167. int idx;
  168. for (idx = 1; idx <= abb_b23_4_nbr; idx++)
  169. {
  170. if (abb_b23_4_get_state(idx) != stat)
  171. {
  172. return -1;
  173. }
  174. }
  175. return 0;
  176. }
  177. int abb_b23_4_send_sm_cmd(int idx, int val)
  178. {
  179. int ret = 0;
  180. struct abb_b23_4_t *dev = &abb_b23_4[idx];
  181. dev->cmd = val;
  182. leave:
  183. log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, val, ret);
  184. return ret;
  185. }
  186. int abb_b23_4_send_sm_cmd_all(int cmd)
  187. {
  188. int ret = 0;
  189. int idx;
  190. for (idx = 1; idx <= abb_b23_4_nbr; idx++)
  191. {
  192. abb_b23_4_send_sm_cmd(idx, cmd);
  193. }
  194. log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret);
  195. return ret;
  196. }
  197. int abb_b23_4_get_comm_st(int idx)
  198. {
  199. struct abb_b23_4_t *dev = &abb_b23_4[idx];
  200. struct comm_t *comm = &dev->comm;
  201. return comm_get_state(comm);
  202. }
  203. void abb_b23_4_set_dac_param_en(int idx, int val)
  204. {
  205. struct abb_b23_4_t *dev = &abb_b23_4[idx];
  206. struct comm_t *comm = &dev->comm;
  207. comm_set_dac_param_en(comm, val);
  208. }
  209. int abb_b23_4_get_sm_step(int idx)
  210. {
  211. struct statemachine_t *sm = &abb_b23_4[idx].sm;
  212. return sm_get_step(sm);
  213. }
  214. char *abb_b23_4_get_sm_err_str(int idx)
  215. {
  216. return abb_b23_4[idx].sm.szerr;
  217. }
  218. int abb_b23_4_get_chan_idx(int idx)
  219. {
  220. struct comm_t *comm = &abb_b23_4[idx].comm;
  221. return comm_get_chan_idx(comm);
  222. }
  223. int abb_b23_4_get_adr(int idx)
  224. {
  225. struct comm_t *comm = &abb_b23_4[idx].comm;
  226. return comm_get_adr(comm);
  227. }
  228. int abb_b23_4_get_tick(int idx)
  229. {
  230. return abb_b23_4[idx].sm.tick;
  231. }
  232. char *abb_b23_4_get_comm_state_str(int idx)
  233. {
  234. struct comm_t *comm = &abb_b23_4[idx].comm;
  235. return comm_get_state_str(comm);
  236. }
  237. int abb_b23_4_get_PT(int idx)
  238. {
  239. return abb_b23_4[idx].PT;
  240. }
  241. int abb_b23_4_get_CT(int idx)
  242. {
  243. return abb_b23_4[idx].CT;
  244. }
  245. int abb_b23_4_get_nbr()
  246. {
  247. return abb_b23_4_nbr;
  248. }
  249. int abb_b23_4_get_tool_data(int idx, char *buf)
  250. {
  251. struct abb_b23_4_t *dev = &abb_b23_4[idx];
  252. struct statemachine_t *sm = &dev->sm;
  253. struct comm_t *comm = &dev->comm;
  254. char temp_buf[1024 * 8];
  255. if (idx < 1 || idx > abb_b23_4_nbr || buf == NULL)
  256. return -1;
  257. sm_get_summary(sm, temp_buf, sizeof(temp_buf));
  258. sprintf(buf, " [%d] %s ", idx, temp_buf);
  259. comm_get_summary(comm, temp_buf, sizeof(temp_buf));
  260. strcat(buf, temp_buf);
  261. sprintf(temp_buf, " 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 \n",
  262. dev->com_ap, dev->com_active_e, dev->pos_active_e, dev->neg_active_e,
  263. dev->ua, dev->ub, dev->uc, dev->ia, dev->ib, dev->ic, dev->freq,
  264. dev->pwr_factor);
  265. strcat(buf, temp_buf);
  266. return 0;
  267. }
  268. int abb_b23_4_get_tbmqtt_data(int idx, char *buf)
  269. {
  270. struct abb_b23_4_t *dev = &abb_b23_4[idx];
  271. struct statemachine_t *sm = &dev->sm;
  272. struct comm_t *comm = &dev->comm;
  273. char temp_buf[1024 * 8];
  274. sprintf(buf, "'abb_m%d_state':'%s','abb_m%d_com_ae':%.1f,'abb_m%d_pos_ae':%.1f,'abb_m%d_neg_ae':%.1f,'abb_m%d_com_ap':%d,\
  275. 'abb_m%d_ua':%.1f,'abb_m%d_ub':%.1f,'abb_m%d_uc':%.1f,\
  276. 'abb_m%d_ia':%.1f,'abb_m%d_ib':%.1f,'abb_m%d_ic':%.1f,\
  277. 'abb_m%d_pwr_fct':%.2f",
  278. idx, abb_b23_4_get_state_string( idx ),
  279. idx, abb_b23_4_get_com_ae( idx ),
  280. idx, abb_b23_4_get_pos_ae( idx ),
  281. idx, abb_b23_4_get_neg_ae( idx ),
  282. idx, abb_b23_4_get_com_ap( idx ),
  283. idx, abb_b23_4_get_ua( idx ),
  284. idx, abb_b23_4_get_ub( idx ),
  285. idx, abb_b23_4_get_uc( idx ),
  286. idx, abb_b23_4_get_ia( idx ),
  287. idx, abb_b23_4_get_ib( idx ),
  288. idx, abb_b23_4_get_ic( idx ),
  289. idx, abb_b23_4_get_pwrfct( idx ));
  290. }
  291. int abb_b23_4_get_cloud_data(int ctn_idx, int idx, char *buf)
  292. {
  293. sprintf(buf, "'id':'meter_%d', 'com_ae':%.1f,\
  294. 'pos_ae':%.1f,\
  295. 'neg_ae':%.1f,\
  296. 'com_ap':%d,\
  297. 'ua':%.1f,\
  298. 'ub':%.1f,\
  299. 'uc':%.1f,\
  300. 'ia':%.1f,\
  301. 'ib':%.1f,\
  302. 'ic':%.1f,\
  303. 'gf':%.1f,\
  304. 'pf':%.1f,\
  305. 'state':%d",
  306. idx, abb_b23_4_get_com_ae(idx),
  307. abb_b23_4_get_pos_ae(idx),
  308. abb_b23_4_get_neg_ae(idx),
  309. abb_b23_4_get_com_ap(idx),
  310. abb_b23_4_get_ua(idx),
  311. abb_b23_4_get_ub(idx),
  312. abb_b23_4_get_uc(idx),
  313. abb_b23_4_get_ia(idx),
  314. abb_b23_4_get_ib(idx),
  315. abb_b23_4_get_ic(idx),
  316. abb_b23_4_get_freq(idx),
  317. abb_b23_4_get_pwrfct(idx),
  318. abb_b23_4_get_state(idx));
  319. }
  320. int abb_b23_4_get_bkds_data(int idx, char *buf)
  321. {
  322. sprintf(buf, "'id':'meter_%d', \
  323. \"info\":\"%s\",\"model\":\"%s\",\"state\":\"%s\",\"stp\":%d,\"err\":\"%s\",\
  324. \"chan_idx\":%d,\"mbadr\":%d,\"tick\":%d,\"comm_state\":\"%s\",\"pt\":%d,\
  325. \"ct\":%d,\"com_ae\":%.1f,\"pos_ae\":%.1f,\"neg_ae\":%.1f,\"com_ap\":%d,\
  326. \"ua\":%.1f,\"ub\":%.1f,\"uc\":%.1f,\"ia\":%.1f,\"ib\":%.1f,\
  327. \"ic\":%.1f,\"freq\":%.1f,\"pwr_fct\":%.1f\
  328. ",
  329. idx,
  330. abb_b23_4_get_info_str(idx), "abb_b23_4", abb_b23_4_get_state_string(idx), abb_b23_4_get_sm_step(idx), abb_b23_4_get_sm_err_str(idx),
  331. abb_b23_4_get_chan_idx(idx), abb_b23_4_get_adr(idx), abb_b23_4_get_tick(idx), abb_b23_4_get_comm_state_str(idx), abb_b23_4_get_PT(idx),
  332. abb_b23_4_get_CT(idx), abb_b23_4_get_com_ae(idx), abb_b23_4_get_pos_ae(idx), abb_b23_4_get_neg_ae(idx), abb_b23_4_get_com_ap(idx),
  333. abb_b23_4_get_ua(idx), abb_b23_4_get_ub(idx), abb_b23_4_get_uc(idx), abb_b23_4_get_ia(idx), abb_b23_4_get_ib(idx),
  334. abb_b23_4_get_ic(idx), abb_b23_4_get_freq(idx), abb_b23_4_get_pwrfct(idx));
  335. }