fa.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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
  81. {
  82. log_dbg("%s, unknown model:%d", __func__, model);
  83. ret = -4;
  84. }
  85. }
  86. log_dbg("%s, --, ret:%d", __func__, ret);
  87. return ret;
  88. }
  89. // 0 : ok, all device in stat
  90. int fa_chk_state_all(int stat)
  91. {
  92. int model = fa[1].model;
  93. if (model == DEVM_CW1310)
  94. {
  95. return cw1310_chk_state_all(stat);
  96. }
  97. else
  98. {
  99. return -1;
  100. }
  101. }
  102. int fa_send_sm_cmd_all(int cmd)
  103. {
  104. int model = fa[1].model;
  105. if (model == DEVM_CW1310)
  106. {
  107. return cw1310_send_sm_cmd_all(cmd);
  108. }
  109. else
  110. {
  111. return -1;
  112. }
  113. }
  114. int fa_send_sm_cmd(int idx, int cmd)
  115. {
  116. int model = fa[1].model;
  117. if (model == DEVM_CW1310)
  118. {
  119. return cw1310_send_sm_cmd(idx, cmd);
  120. }
  121. else
  122. {
  123. return -1;
  124. }
  125. }
  126. int fa_get_warning_level(int idx)
  127. {
  128. int model = fa[1].model;
  129. if (model == DEVM_CW1310)
  130. {
  131. return cw1310_get_warning(idx);
  132. }
  133. else
  134. {
  135. return 0;
  136. }
  137. }
  138. int fa_get_tool_data(char *buf)
  139. {
  140. int model = fa[1].model;
  141. int i = 0;
  142. char temp_buf[2048];
  143. if (model == DEVM_CW1310)
  144. {
  145. sprintf(buf, "" REVERSE " FA " NONE " model:%s info:%s\n", fa[1].szmodel, fa[1].szinfo);
  146. for (i = 1; i <= cw1310_get_nbr(); i++)
  147. {
  148. memset(temp_buf, 0, sizeof(temp_buf));
  149. cw1310_get_tool_data(i, temp_buf);
  150. strcat(buf, temp_buf);
  151. }
  152. return 0;
  153. }
  154. else
  155. {
  156. return -1;
  157. }
  158. }
  159. int fa_get_tbmqtt_data(char *buf)
  160. {
  161. for (int i = 1; i <= fa[1].model_nbr; i++)
  162. {
  163. if (fa[1].model == DEVM_CW1310)
  164. {
  165. for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
  166. {
  167. if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
  168. {
  169. if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
  170. {
  171. buf[strlen(buf) - 1] = 0;
  172. }
  173. continue;
  174. }
  175. char temp_buf[2048];
  176. memset(temp_buf, 0, sizeof(temp_buf));
  177. cw1310_get_tbmqtt_data(idx, temp_buf);
  178. strcat(buf, temp_buf);
  179. if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
  180. {
  181. strcat(buf, ",");
  182. }
  183. }
  184. }
  185. }
  186. return 0;
  187. }
  188. int fa_get_cloud_data(int ctn_idx, char *buf)
  189. {
  190. for (int i = 1; i <= fa[1].model_nbr; i++)
  191. {
  192. if (fa[1].model == DEVM_CW1310)
  193. {
  194. for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
  195. {
  196. if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
  197. {
  198. if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
  199. {
  200. buf[strlen(buf) - 1] = 0;
  201. }
  202. continue;
  203. }
  204. char temp_buf[2048];
  205. memset(temp_buf, 0, sizeof(temp_buf));
  206. cw1310_get_cloud_data(ctn_idx, idx, temp_buf);
  207. strcat(buf, temp_buf);
  208. if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
  209. {
  210. strcat(buf, ",");
  211. }
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. int fa_get_modbus_data(int idx, unsigned short* data)
  218. {
  219. int model = fa[1].model;
  220. if (model == DEVM_CW1310)
  221. {
  222. cw1310_get_modbus_data(idx,data);
  223. }
  224. else
  225. {
  226. data[0] = (unsigned short)model;
  227. data[1] = (unsigned short)0;
  228. }
  229. return 0;
  230. }