fa.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "fa.h"
  2. #include "plt.h"
  3. struct fa_t fa[FA_NBR_MAX + 1];
  4. static int fa_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
  5. {
  6. struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
  7. struct fa_t *dev = &fa[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 fa_get_model()
  24. {
  25. return fa[1].model;
  26. }
  27. int fa_get_init_data(int ctn_idx, char *buf)
  28. {
  29. char temp[1024];
  30. int model = fa[1].model;
  31. if (model == DEVM_CW1310)
  32. {
  33. int r = cw1310_get_init_data(ctn_idx, 1, buf);
  34. strcat(buf, temp);
  35. return r;
  36. }
  37. else
  38. {
  39. return NULL;
  40. }
  41. }
  42. int fa_init()
  43. {
  44. int result;
  45. char *errmsg = NULL;
  46. sqlite3 *db = NULL;
  47. char sql[1024];
  48. struct dbcbparam_t cbparam;
  49. int ret = 0;
  50. int model = 0;
  51. log_dbg("%s, ++", __func__);
  52. plt_lock_ctndb();
  53. db = plt_get_ctndb();
  54. sprintf(sql, "select * from fa");
  55. cbparam.nrow = 0;
  56. result = sqlite3_exec(db, sql, fa_dbcb_0, (void *)&cbparam, &errmsg);
  57. plt_unlock_ctndb();
  58. if (result != SQLITE_OK)
  59. {
  60. log_dbg("%s, result != SQLITE_OK : %d", __func__, result);
  61. ret = -1;
  62. }
  63. else if (cbparam.ret != 0)
  64. {
  65. log_dbg("%s, cbparam.ret != 0 : %d", __func__, cbparam.ret);
  66. ret = -2;
  67. }
  68. else if (cbparam.nrow != 1)
  69. {
  70. log_dbg("%s, cbparam.nrow != 1 : %d", __func__, cbparam.nrow);
  71. ret = -3;
  72. }
  73. else
  74. {
  75. model = fa_get_model();
  76. if (model == DEVM_CW1310)
  77. {
  78. cw1310_init();
  79. }
  80. else if(model == DEVM_EEP07C)
  81. {
  82. eep07c_init();
  83. }
  84. else
  85. {
  86. log_dbg("%s, unknown model:%d", __func__, model);
  87. ret = -4;
  88. }
  89. }
  90. log_dbg("%s, --, ret:%d", __func__, ret);
  91. return ret;
  92. }
  93. // 0 : ok, all device in stat
  94. int fa_chk_state_all(int stat)
  95. {
  96. int model = fa[1].model;
  97. if (model == DEVM_CW1310)
  98. {
  99. return cw1310_chk_state_all(stat);
  100. }
  101. else if (model == DEVM_EEP07C)
  102. {
  103. return eep07c_chk_state_all(stat);
  104. }
  105. else
  106. {
  107. return -1;
  108. }
  109. }
  110. int fa_send_sm_cmd_all(int cmd)
  111. {
  112. int model = fa[1].model;
  113. if (model == DEVM_CW1310)
  114. {
  115. return cw1310_send_sm_cmd_all(cmd);
  116. }
  117. else if (model == DEVM_CW1310)
  118. {
  119. return eep07c_send_sm_cmd_all(cmd);
  120. }
  121. else
  122. {
  123. return -1;
  124. }
  125. }
  126. int fa_send_sm_cmd(int idx, int cmd)
  127. {
  128. int model = fa[1].model;
  129. if (model == DEVM_CW1310)
  130. {
  131. return cw1310_send_sm_cmd(idx, cmd);
  132. }
  133. else if (model == DEVM_EEP07C)
  134. {
  135. return eep07c_send_sm_cmd(idx, cmd);
  136. }
  137. else
  138. {
  139. return -1;
  140. }
  141. }
  142. int fa_get_warning_level(int idx)
  143. {
  144. int model = fa[1].model;
  145. if (model == DEVM_CW1310)
  146. {
  147. return cw1310_get_warning(idx);
  148. }
  149. else if (model == DEVM_EEP07C)
  150. {
  151. return eep07c_get_warning(idx);
  152. }
  153. else
  154. {
  155. return 0;
  156. }
  157. }
  158. int fa_get_tool_data(char *buf)
  159. {
  160. int model = fa[1].model;
  161. int i = 0;
  162. char temp_buf[2048];
  163. if (model == DEVM_CW1310)
  164. {
  165. sprintf(buf, "" REVERSE " FA " NONE " model:%s info:%s\n", fa[1].szmodel, fa[1].szinfo);
  166. for (i = 1; i <= cw1310_get_nbr(); i++)
  167. {
  168. memset(temp_buf, 0, sizeof(temp_buf));
  169. cw1310_get_tool_data(i, temp_buf);
  170. strcat(buf, temp_buf);
  171. }
  172. return 0;
  173. }
  174. else if (model == DEVM_EEP07C)
  175. {
  176. sprintf(buf, "" REVERSE " FA " NONE " model:%s info:%s\n", fa[1].szmodel, fa[1].szinfo);
  177. for (i = 1; i <= eep07c_get_nbr(); i++)
  178. {
  179. memset(temp_buf, 0, sizeof(temp_buf));
  180. eep07c_get_tool_data(i, temp_buf);
  181. strcat(buf, temp_buf);
  182. }
  183. return 0;
  184. }
  185. else
  186. {
  187. return -1;
  188. }
  189. }
  190. int fa_get_tbmqtt_data(char *buf)
  191. {
  192. for (int i = 1; i <= fa[1].model_nbr; i++)
  193. {
  194. if (fa[1].model == DEVM_CW1310)
  195. {
  196. for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
  197. {
  198. if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
  199. {
  200. if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
  201. {
  202. buf[strlen(buf) - 1] = 0;
  203. }
  204. continue;
  205. }
  206. char temp_buf[2048];
  207. memset(temp_buf, 0, sizeof(temp_buf));
  208. cw1310_get_tbmqtt_data(idx, temp_buf);
  209. strcat(buf, temp_buf);
  210. if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
  211. {
  212. strcat(buf, ",");
  213. }
  214. }
  215. }
  216. else if(fa[1].model == DEVM_EEP07C)
  217. {
  218. for (int idx = 1; idx <= eep07c_get_nbr(); idx++)
  219. {
  220. if (eep07c_get_comm_st(idx) != COMMST_NORMAL)
  221. {
  222. if (i == fa[1].model_nbr && idx == eep07c_get_nbr() && strlen(buf) > 1)
  223. {
  224. buf[strlen(buf) - 1] = 0;
  225. }
  226. continue;
  227. }
  228. char temp_buf[2048];
  229. memset(temp_buf, 0, sizeof(temp_buf));
  230. eep07c_get_tbmqtt_data(idx, temp_buf);
  231. strcat(buf, temp_buf);
  232. if (i != fa[1].model_nbr || idx != eep07c_get_nbr())
  233. {
  234. strcat(buf, ",");
  235. }
  236. }
  237. }
  238. }
  239. return 0;
  240. }
  241. int fa_get_cloud_data(int ctn_idx, char *buf)
  242. {
  243. for (int i = 1; i <= fa[1].model_nbr; i++)
  244. {
  245. if (fa[1].model == DEVM_CW1310)
  246. {
  247. for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
  248. {
  249. if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
  250. {
  251. if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
  252. {
  253. buf[strlen(buf) - 1] = 0;
  254. }
  255. continue;
  256. }
  257. char temp_buf[2048];
  258. memset(temp_buf, 0, sizeof(temp_buf));
  259. cw1310_get_cloud_data(ctn_idx, idx, temp_buf);
  260. strcat(buf, temp_buf);
  261. if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
  262. {
  263. strcat(buf, ",");
  264. }
  265. }
  266. }
  267. else if (fa[1].model == DEVM_EEP07C)
  268. {
  269. for (int idx = 1; idx <= eep07c_get_nbr(); idx++)
  270. {
  271. if (eep07c_get_comm_st(idx) != COMMST_NORMAL)
  272. {
  273. if (i == fa[1].model_nbr && idx == eep07c_get_nbr() && strlen(buf) > 1)
  274. {
  275. buf[strlen(buf) - 1] = 0;
  276. }
  277. continue;
  278. }
  279. char temp_buf[2048];
  280. memset(temp_buf, 0, sizeof(temp_buf));
  281. eep07c_get_cloud_data(ctn_idx, idx, temp_buf);
  282. strcat(buf, temp_buf);
  283. if (i != fa[1].model_nbr || idx != eep07c_get_nbr())
  284. {
  285. strcat(buf, ",");
  286. }
  287. }
  288. }
  289. }
  290. return 0;
  291. }
  292. int fa_get_modbus_data(int idx, unsigned short* data)
  293. {
  294. int model = fa[1].model;
  295. if (model == DEVM_CW1310)
  296. {
  297. cw1310_get_modbus_data(idx,data);
  298. }
  299. else if (model == DEVM_EEP07C)
  300. {
  301. eep07c_get_modbus_data(idx,data);
  302. }
  303. else
  304. {
  305. data[0] = (unsigned short)model;
  306. data[1] = (unsigned short)0;
  307. }
  308. return 0;
  309. }