fe.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "fe.h"
  2. #include "plt.h"
  3. struct fe_t fe[FE_NBR_MAX + 1];
  4. static int fe_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
  5. {
  6. struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
  7. struct fe_t *dev = &fe[1];
  8. pcbparam->nrow++;
  9. log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
  10. for (int i = 0; i < ncolumn; i++)
  11. {
  12. if (strcmp("model", columnname[i]) == 0)
  13. {
  14. strcpy(dev->szmodel, columnvalue[i]);
  15. dev->model = plt_devm_str2nbr(dev->szmodel);
  16. }
  17. }
  18. dev->model_nbr = 1;
  19. pcbparam->ret = 0;
  20. log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
  21. return 0;
  22. }
  23. int fe_get_model()
  24. {
  25. return fe[1].model;
  26. }
  27. int fe_get_init_data(int ctn_idx, char *buf)
  28. {
  29. char temp[4096];
  30. int model = fe[1].model;
  31. if (model == DEVM_YF2825)
  32. {
  33. int r = yf2825_get_init_data(ctn_idx, 1, buf);
  34. strcat(buf, temp);
  35. return r;
  36. }
  37. else if (model == DEVM_RS311)
  38. {
  39. int r = rs311_get_init_data(ctn_idx, 1, buf);
  40. strcat(buf, temp);
  41. return r;
  42. }
  43. else
  44. {
  45. return NULL;
  46. }
  47. }
  48. int fe_set_start(int startIdx)
  49. {
  50. int model = fe[1].model;
  51. if (model == DEVM_YF2825)
  52. {
  53. return yf2825_set_dev_start(1, startIdx);
  54. }
  55. }
  56. int fe_init()
  57. {
  58. int result;
  59. char *errmsg = NULL;
  60. sqlite3 *db = NULL;
  61. char sql[1024];
  62. struct dbcbparam_t cbparam;
  63. int ret = 0;
  64. int model = 0;
  65. log_dbg("%s, ++", __func__);
  66. plt_lock_ctndb();
  67. db = plt_get_ctndb();
  68. sprintf(sql, "select * from fe");
  69. cbparam.nrow = 0;
  70. result = sqlite3_exec(db, sql, fe_dbcb_0, (void *)&cbparam, &errmsg);
  71. plt_unlock_ctndb();
  72. if (result != SQLITE_OK)
  73. {
  74. log_dbg("%s, result != SQLITE_OK : %d", __func__, result);
  75. ret = -1;
  76. }
  77. else if (cbparam.ret != 0)
  78. {
  79. log_dbg("%s, cbparam.ret != 0 : %d", __func__, cbparam.ret);
  80. ret = -2;
  81. }
  82. else if (cbparam.nrow != 1)
  83. {
  84. log_dbg("%s, cbparam.nrow != 1 : %d", __func__, cbparam.nrow);
  85. ret = -3;
  86. }
  87. else
  88. {
  89. model = fe_get_model();
  90. if (model == DEVM_YF2825)
  91. {
  92. yf2825_init();
  93. }
  94. else if (model == DEVM_RS311)
  95. {
  96. rs311_init();
  97. }
  98. else
  99. {
  100. log_dbg("%s, unknown model:%d", __func__, model);
  101. ret = -4;
  102. }
  103. }
  104. log_dbg("%s, --, ret:%d", __func__, ret);
  105. return ret;
  106. }
  107. // 0 : ok, all device in stat
  108. int fe_chk_state_all(int stat)
  109. {
  110. int model = fe[1].model;
  111. if (model == DEVM_YF2825)
  112. {
  113. return yf2825_chk_state_all(stat);
  114. }
  115. else if (model == DEVM_RS311)
  116. {
  117. return rs311_chk_state_all(stat);
  118. }
  119. else
  120. {
  121. return -1;
  122. }
  123. }
  124. int fe_send_sm_cmd_all(int cmd)
  125. {
  126. int model = fe[1].model;
  127. if (model == DEVM_YF2825)
  128. {
  129. return yf2825_send_sm_cmd_all(cmd);
  130. }
  131. else if (model == DEVM_RS311)
  132. {
  133. return rs311_send_sm_cmd_all(cmd);
  134. }
  135. else
  136. {
  137. return -1;
  138. }
  139. }
  140. int fe_send_sm_cmd(int idx, int cmd)
  141. {
  142. int model = fe[1].model;
  143. if (model == DEVM_YF2825)
  144. {
  145. return yf2825_send_sm_cmd(idx, cmd);
  146. }
  147. else if (model == DEVM_RS311)
  148. {
  149. return rs311_send_sm_cmd(idx, cmd);
  150. }
  151. else
  152. {
  153. return -1;
  154. }
  155. }
  156. char *fe_get_model_str(int idx)
  157. {
  158. return fe[idx].szmodel;
  159. }
  160. int fe_get_tool_data(char *buf)
  161. {
  162. int model = fe[1].model;
  163. int i = 0;
  164. char temp_buf[2048];
  165. if (model == DEVM_YF2825)
  166. {
  167. sprintf(buf, "" REVERSE " FE " NONE " model:%s info:%s\n", fe[1].szmodel, fe[1].szinfo);
  168. for (i = 1; i <= yf2825_get_nbr(); i++)
  169. {
  170. memset(temp_buf, 0, sizeof(temp_buf));
  171. yf2825_get_tool_data(i, temp_buf);
  172. strcat(buf, temp_buf);
  173. }
  174. return 0;
  175. }
  176. else if (model == DEVM_RS311)
  177. {
  178. sprintf(buf, "" REVERSE " FE " NONE " model:%s info:%s\n", fe[1].szmodel, fe[1].szinfo);
  179. for (i = 1; i <= rs311_get_nbr(); i++)
  180. {
  181. memset(temp_buf, 0, sizeof(temp_buf));
  182. rs311_get_tool_data(i, temp_buf);
  183. strcat(buf, temp_buf);
  184. }
  185. return 0;
  186. }
  187. else
  188. {
  189. return -1;
  190. }
  191. }
  192. int fe_get_tbmqtt_data(char *buf)
  193. {
  194. for (int i = 1; i <= fe[1].model_nbr; i++)
  195. {
  196. if (fe[1].model == DEVM_YF2825)
  197. {
  198. for (int idx = 1; idx <= yf2825_get_nbr(); idx++)
  199. {
  200. if (yf2825_get_comm_st(idx) != COMMST_NORMAL)
  201. {
  202. if (i == fe[1].model_nbr && idx == yf2825_get_nbr() && strlen(buf) > 1)
  203. {
  204. buf[strlen(buf) - 1] = 0;
  205. }
  206. continue;
  207. }
  208. char temp_buf[2048];
  209. memset(temp_buf, 0, sizeof(temp_buf));
  210. yf2825_get_tbmqtt_data(idx, temp_buf);
  211. strcat(buf, temp_buf);
  212. if (i != fe[1].model_nbr || idx != yf2825_get_nbr())
  213. {
  214. strcat(buf, ",");
  215. }
  216. }
  217. }
  218. else if (fe[1].model == DEVM_RS311)
  219. {
  220. for (int idx = 1; idx <= rs311_get_nbr(); idx++)
  221. {
  222. if (rs311_get_comm_st(idx) != COMMST_NORMAL)
  223. {
  224. if (i == fe[1].model_nbr && idx == rs311_get_nbr() && strlen(buf) > 1)
  225. {
  226. buf[strlen(buf) - 1] = 0;
  227. }
  228. continue;
  229. }
  230. char temp_buf[2048];
  231. memset(temp_buf, 0, sizeof(temp_buf));
  232. rs311_get_tbmqtt_data(idx, temp_buf);
  233. strcat(buf, temp_buf);
  234. if (i != fe[1].model_nbr || idx != rs311_get_nbr())
  235. {
  236. strcat(buf, ",");
  237. }
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. int fe_get_cloud_data(int ctn_idx, char *buf)
  244. {
  245. for (int i = 1; i <= fe[1].model_nbr; i++)
  246. {
  247. if (fe[1].model == DEVM_YF2825)
  248. {
  249. for (int idx = 1; idx <= yf2825_get_nbr(); idx++)
  250. {
  251. if (yf2825_get_comm_st(idx) != COMMST_NORMAL)
  252. {
  253. if (i == fe[1].model_nbr && idx == yf2825_get_nbr() && strlen(buf) > 1)
  254. {
  255. buf[strlen(buf) - 1] = 0;
  256. }
  257. continue;
  258. }
  259. char temp_buf[2048];
  260. memset(temp_buf, 0, sizeof(temp_buf));
  261. yf2825_get_cloud_data(ctn_idx, idx, temp_buf);
  262. strcat(buf, temp_buf);
  263. if (i != fe[1].model_nbr || idx != yf2825_get_nbr())
  264. {
  265. strcat(buf, ",");
  266. }
  267. }
  268. }
  269. else if (fe[1].model == DEVM_RS311)
  270. {
  271. for (int idx = 1; idx <= rs311_get_nbr(); idx++)
  272. {
  273. if (rs311_get_comm_st(idx) != COMMST_NORMAL)
  274. {
  275. if (i == fe[1].model_nbr && idx == rs311_get_nbr() && strlen(buf) > 1)
  276. {
  277. buf[strlen(buf) - 1] = 0;
  278. }
  279. continue;
  280. }
  281. char temp_buf[2048];
  282. memset(temp_buf, 0, sizeof(temp_buf));
  283. rs311_get_cloud_data(ctn_idx, idx, temp_buf);
  284. strcat(buf, temp_buf);
  285. if (i != fe[1].model_nbr || idx != rs311_get_nbr())
  286. {
  287. strcat(buf, ",");
  288. }
  289. }
  290. }
  291. }
  292. return 0;
  293. }
  294. int fe_get_modbus_data(int idx, unsigned short* data)
  295. {
  296. int model = fe[1].model;
  297. if (model == DEVM_YF2825)
  298. {
  299. yf2825_get_modbus_data(idx,data);
  300. }
  301. else
  302. {
  303. data[0] = (unsigned short)model;
  304. data[1] = (unsigned short)0;
  305. }
  306. return 0;
  307. }
  308. // int fe_get_bkds_data(char* buf)
  309. // {
  310. // int model = fe[1].model;
  311. // int i = 0;
  312. // char buf_temp[8000];
  313. // sprintf(buf,"\"fe\":[");
  314. // if( model == DEVM_YF2825 ){
  315. // for(i = 1;i <= yf2825_get_nbr();i ++ ){
  316. // strcat(buf,"{");
  317. // yf2825_get_bkds_data(i,buf_temp);
  318. // strcat(buf,buf_temp);
  319. // strcat(buf,"}");
  320. // if(i != yf2825_get_nbr()){
  321. // strcat(buf,",");
  322. // }
  323. // }
  324. // }else{
  325. // return -1;
  326. // }
  327. // strcat(buf,"]");
  328. // }