cd1f3300.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "plt.h"
  2. int cd1f3300_nbr;
  3. struct cd1f3300_t cd1f3300[CD1F3300_NBR_MAX + 1];
  4. static int cd1f3300_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
  5. {
  6. int i;
  7. struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
  8. struct cd1f3300_t *dev = NULL;
  9. pcbparam->nrow++;
  10. log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
  11. dev = &cd1f3300[pcbparam->nrow];
  12. dev->threshold = INT_MIN;
  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. else if (strcmp("thres", columnname[i]) == 0)
  28. {
  29. dev->threshold = atoi(columnvalue[i]);
  30. }
  31. else if (strcmp("idx", columnname[i]) == 0)
  32. {
  33. dev->idx = atoi(columnvalue[i]);
  34. }
  35. else if (strcmp("devid", columnname[i]) == 0)
  36. {
  37. if (columnvalue[i] != NULL && strcmp(columnvalue[i], "") != 0)
  38. {
  39. strcpy(dev->szdev_id, columnvalue[i]);
  40. }
  41. else
  42. {
  43. log_dbg("%s, devid is null", __func__);
  44. }
  45. }
  46. }
  47. if (dev->threshold == INT_MIN)
  48. {
  49. dev->threshold = 40;
  50. }
  51. pcbparam->ret = 0;
  52. log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
  53. return 0;
  54. }
  55. int cd1f3300_get_addr(int idx)
  56. {
  57. return cd1f3300[idx].comm.adr;
  58. }
  59. int cd1f3300_get_co_density(int idx)
  60. {
  61. return cd1f3300[idx].co_density;
  62. }
  63. int cd1f3300_get_co_threshold(int idx)
  64. {
  65. return cd1f3300[idx].threshold;
  66. }
  67. static void aux(int idx)
  68. {
  69. static int cnt = 0;
  70. if (cnt < cd1f3300_nbr && mqtt_get_state() == SMST_READY)
  71. {
  72. cnt++;
  73. if (idx == cd1f3300_get_nbr())
  74. {
  75. cloud.co_init = 0;
  76. }
  77. char buf[4096] = {0};
  78. cd1f3300_get_init_data(ctn_get_idx_in_ess(), idx, buf);
  79. cloud_send_init(buf);
  80. }
  81. }
  82. void* cd1f3300_thrd_main(void *param)
  83. {
  84. struct cd1f3300_t *dev = NULL;
  85. int idx;
  86. log_dbg("%s, ++", __func__);
  87. for (idx = 1; idx <= cd1f3300_nbr; idx++)
  88. {
  89. cd1f3300_comm_init(idx);
  90. cd1f3300_sm_init(idx);
  91. }
  92. while (1)
  93. {
  94. for (idx = 1; idx <= cd1f3300_nbr; idx++)
  95. {
  96. cd1f3300_comm_dac(idx);
  97. cd1f3300_sm(idx);
  98. aux(idx);
  99. sleep(1); // 1s
  100. }
  101. }
  102. log_dbg("%s, --, idx:%d", __func__, idx);
  103. return NULL;
  104. }
  105. int cd1f3300_chk_stat_all(int stat)
  106. {
  107. int idx;
  108. for (idx = 1; idx <= cd1f3300_nbr; idx++)
  109. {
  110. if (cd1f3300_get_state(idx) != stat)
  111. {
  112. return -1;
  113. }
  114. }
  115. return 0;
  116. }
  117. int cd1f3300_send_sm_cmd_all(int cmd)
  118. {
  119. int ret = 0;
  120. int idx;
  121. for (idx = 1; idx <= cd1f3300_nbr; idx++)
  122. {
  123. cd1f3300_send_sm_cmd(idx, cmd);
  124. }
  125. log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret);
  126. return ret;
  127. }
  128. int cd1f3300_init()
  129. {
  130. pthread_t thrd;
  131. int result = 0;
  132. int ret = 0;
  133. int idx = 0;
  134. struct cd1f3300_t *dev = NULL;
  135. char *errmsg = NULL;
  136. char sql[1024];
  137. struct dbcbparam_t cbparam;
  138. sqlite3 *db = NULL;
  139. log_dbg("%s, ++", __func__);
  140. plt_lock_ctndb();
  141. db = plt_get_ctndb();
  142. sprintf(sql, "select * from cd1f3300");
  143. cbparam.nrow = 0;
  144. result = sqlite3_exec(db, sql, cd1f3300_dbcb_0, (void *)&cbparam, &errmsg);
  145. plt_unlock_ctndb();
  146. if (result != SQLITE_OK)
  147. {
  148. ret = -1;
  149. }
  150. else if (cbparam.ret != 0)
  151. {
  152. ret = -2;
  153. }
  154. else
  155. {
  156. cd1f3300_nbr = cbparam.nrow;
  157. cbparam.nrow = 0;
  158. db = plt_get_devdb();
  159. plt_lock_devdb();
  160. sprintf(sql, "select * from cd1f3300");
  161. result = sqlite3_exec(db, sql, cd1f3300_dbcb_0, (void *)&cbparam, &errmsg);
  162. plt_unlock_devdb();
  163. for (int i = 1; i <= cd1f3300_nbr; ++i)
  164. {
  165. if (strcmp(cd1f3300[i].szdev_id, "") == 0)
  166. {
  167. if (get_snow_id(cd1f3300[i].szdev_id) != 0)
  168. {
  169. ret = -4;
  170. }
  171. else
  172. {
  173. char sql[1024];
  174. char *errmsg = NULL;
  175. sprintf(sql, "update cd1f3300 set devid=\"%s\" where idx=%d", cd1f3300[i].szdev_id, cd1f3300[i].idx);
  176. plt_lock_devdb();
  177. int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
  178. plt_unlock_devdb();
  179. if (rc != SQLITE_OK)
  180. {
  181. log_dbg("%s, failed to update level device id:%s", __func__, errmsg);
  182. ret = -4;
  183. goto leave;
  184. }
  185. else
  186. {
  187. log_dbg("%s, update device id:%s", __func__, cd1f3300[i].szdev_id);
  188. }
  189. }
  190. }
  191. }
  192. if (pthread_create(&thrd, NULL, cd1f3300_thrd_main, NULL) != 0)
  193. {
  194. log_dbg("%s, create cd1f3300 thrd main fail", __func__);
  195. ret = -1;
  196. }
  197. }
  198. leave:
  199. log_dbg("%s--, ret:%d", __func__, ret);
  200. return ret;
  201. }
  202. int cd1f3300_get_state(int idx)
  203. {
  204. return cd1f3300[idx].sm.state;
  205. }
  206. char *cd1f3300_get_state_str(int idx)
  207. {
  208. return cd1f3300[idx].sm.szState;
  209. }
  210. int cd1f3300_get_cmd(int idx)
  211. {
  212. return cd1f3300[idx].cmd;
  213. }
  214. void cd1f3300_reset_cmd(int idx)
  215. {
  216. cd1f3300[idx].cmd = CMD_SM_DONE;
  217. }
  218. // int cd1f3300_get_humi( int idx )
  219. // {
  220. // return cd1f3300[idx].humi;
  221. // }
  222. int cd1f3300_send_sm_cmd(int idx, int val)
  223. {
  224. int ret = 0;
  225. struct cd1f3300_t *dev = &cd1f3300[idx];
  226. dev->cmd = val;
  227. leave:
  228. log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, val, ret);
  229. return ret;
  230. }
  231. int cd1f3300_get_comm_st(int idx)
  232. {
  233. struct cd1f3300_t *dev = &cd1f3300[idx];
  234. struct comm_t *comm = &dev->comm;
  235. return comm_get_state(comm);
  236. }
  237. int cd1f3300_get_chan_idx(int idx)
  238. {
  239. struct cd1f3300_t *dev = &cd1f3300[idx];
  240. struct comm_t *comm = &dev->comm;
  241. return comm_get_chan_idx(comm);
  242. }
  243. int cd1f3300_get_tick(int idx)
  244. {
  245. return cd1f3300[idx].sm.tick;
  246. }
  247. char *cd1f3300_get_comm_state_str(int idx)
  248. {
  249. struct cd1f3300_t *dev = &cd1f3300[idx];
  250. struct comm_t *comm = &dev->comm;
  251. return comm_get_state_str(comm);
  252. }
  253. int cd1f3300_get_nbr()
  254. {
  255. return cd1f3300_nbr;
  256. }
  257. struct cd1f3300_t *cd1f3300_get_all_data_ptr()
  258. {
  259. return cd1f3300;
  260. }
  261. int cd1f3300_get_tool_data(int idx, char *buf)
  262. {
  263. if (idx < 1 || idx > cd1f3300_nbr || buf == NULL)
  264. return -1;
  265. struct cd1f3300_t *dev = &cd1f3300[idx];
  266. struct statemachine_t *sm = &dev->sm;
  267. char buf_temp[1024];
  268. sm_get_summary(sm, buf_temp, sizeof(buf_temp));
  269. sprintf(buf, "dev id :%s %s CO Density:" L_GREEN "%d" NONE ", CO Warn Threshold: " L_GREEN "%d" NONE " state:%s\n", dev->szdev_id, buf_temp, dev->co_density, dev->threshold, dev->state_str);
  270. return 0;
  271. }
  272. double cd1f3300_get_co(int idx)
  273. {
  274. return cd1f3300[idx].co_density;
  275. }
  276. int cd1f3300_get_tbmqtt_data(int idx, char *buf)
  277. {
  278. sprintf(buf, "'env%d_state':'%s','env%d_co':%.1f",
  279. idx, cd1f3300_get_state_str(idx),
  280. idx, cd1f3300_get_co(idx));
  281. }
  282. int cd1f3300_get_cloud_data(int ctn_idx, int idx, char *buf)
  283. {
  284. sprintf(buf, "{\"device_id\":\"%s\",\"type\":8, \"co\":%.1f, \"state\":\"%d\"}",
  285. cd1f3300[idx].szdev_id, cd1f3300_get_co(idx), cd1f3300_get_state(idx));
  286. }
  287. int cd1f3300_get_init_data(int ctn_idx, int idx, char *buf)
  288. {
  289. sprintf(buf, "{\"device_id\":\"%s\", \"type\":8, \"idx\":%d, \"pid\":\"%s\", \"config\":{ \
  290. \"chan_idx\":%d, \"adr\":%d}}",
  291. cd1f3300[idx].szdev_id,
  292. idx,
  293. ctn_get_dev_id(),
  294. cd1f3300[idx].comm.chanidx,
  295. cd1f3300[idx].comm.adr);
  296. return 0;
  297. }
  298. int cd1f3300_get_modbus_data(int idx,unsigned short* data)
  299. {
  300. struct cd1f3300_t *dev = &cd1f3300[idx];
  301. data[0] = (unsigned short)DEVM_CD1F3300;
  302. data[1] = (unsigned short)1;
  303. data[2] = (unsigned short)dev->sm.state;
  304. data[3] = (unsigned short)dev->sm.step;
  305. data[4] = (unsigned short)dev->sm.err;
  306. data[5] = (unsigned short)dev->comm.adr;
  307. data[6] = (unsigned short)dev->comm.state;
  308. data[7] = (unsigned short)dev->co_density;
  309. data[8] = (unsigned short)dev->threshold;
  310. data[9] = (unsigned short)dev->state;
  311. return 0;
  312. }