dehumi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include "dehumi.h"
  2. #include "plt.h"
  3. struct dehumi_t dehumi[DEHUMI_NBR_MAX + 1];
  4. static int dehumi_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 dehumi_t *dev = &dehumi[1];
  9. pcbparam->nrow++;
  10. log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
  11. for (i = 0; i < ncolumn; i++)
  12. {
  13. if (strcmp("model", columnname[i]) == 0)
  14. {
  15. strcpy(dev->szmodel, columnvalue[i]);
  16. dev->model = plt_devm_str2nbr(dev->szmodel);
  17. }
  18. }
  19. dev->model_nbr = 1;
  20. pcbparam->ret = 0;
  21. log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
  22. return 0;
  23. }
  24. int dehumi_get_model()
  25. {
  26. return dehumi[1].model;
  27. }
  28. int dehumi_get_init_data(int ctn_idx, char *buf)
  29. {
  30. int model = dehumi[1].model;
  31. if (model == DEVM_SDCS)
  32. {
  33. return sdcs_get_init_data(ctn_idx, 1, buf);
  34. }
  35. else
  36. {
  37. return NULL;
  38. }
  39. }
  40. int dehumi_init()
  41. {
  42. int result;
  43. char *errmsg = NULL;
  44. sqlite3 *db = NULL;
  45. char sql[1024];
  46. struct dbcbparam_t cbparam;
  47. int ret = 0;
  48. int model = 0;
  49. log_dbg("%s, ++", __func__);
  50. plt_lock_ctndb();
  51. db = plt_get_ctndb();
  52. sprintf(sql, "select * from dehumi");
  53. cbparam.nrow = 0;
  54. result = sqlite3_exec(db, sql, dehumi_dbcb_0, (void *)&cbparam, &errmsg);
  55. plt_unlock_ctndb();
  56. if (result != SQLITE_OK)
  57. {
  58. log_dbg("%s, result != SQLITE_OK : %d", __func__, result);
  59. ret = -1;
  60. }
  61. else if (cbparam.ret != 0)
  62. {
  63. log_dbg("%s, cbparam.ret != 0 : %d", __func__, cbparam.ret);
  64. ret = -2;
  65. }
  66. else if (cbparam.nrow != 1)
  67. {
  68. log_dbg("%s, cbparam.nrow != 1 : %d", __func__, cbparam.nrow);
  69. ret = -3;
  70. }
  71. else
  72. {
  73. model = dehumi_get_model();
  74. if (model == DEVM_SDCS)
  75. {
  76. sdcs_init();
  77. }
  78. else
  79. {
  80. log_dbg("%s, unknown model:%d", __func__, model);
  81. ret = -4;
  82. }
  83. }
  84. log_dbg("%s, --, ret:%d", __func__, ret);
  85. return ret;
  86. }
  87. // 0 : ok, all device in stat
  88. int dehumi_chk_state_all(int stat)
  89. {
  90. int model = dehumi[1].model;
  91. if (model == DEVM_SDCS)
  92. {
  93. return sdcs_chk_stat_all(stat);
  94. }
  95. else
  96. {
  97. return -1;
  98. }
  99. }
  100. int dehumi_get_comm_st(int idx)
  101. {
  102. int model = dehumi[1].model;
  103. if (model == DEVM_SDCS)
  104. {
  105. return sdcs_get_comm_st(idx);
  106. }
  107. else
  108. {
  109. return -1;
  110. }
  111. }
  112. int dehumi_send_sm_cmd_all(int cmd)
  113. {
  114. int model = dehumi[1].model;
  115. if (model == DEVM_SDCS)
  116. {
  117. return sdcs_send_sm_cmd_all(cmd);
  118. }
  119. else
  120. {
  121. return -1;
  122. }
  123. }
  124. int dehumi_send_sm_cmd(int idx, int cmd)
  125. {
  126. int model = dehumi[1].model;
  127. if (model == DEVM_SDCS)
  128. {
  129. return sdcs_send_sm_cmd(idx, cmd);
  130. }
  131. else
  132. {
  133. return -1;
  134. }
  135. }
  136. // char* dehumi_get_work_stat_str(int idx)
  137. // {
  138. // int model = dehumi[1].model;
  139. // if( model == DEVM_SDCS ){
  140. // return sdcs_get_workstat_str(idx );
  141. // }else{
  142. // return NULL;
  143. // }
  144. // }
  145. // int dehumi_get_work_stat(int idx)
  146. // {
  147. // int model = dehumi[1].model;
  148. // if( model == DEVM_SDCS ){
  149. // return sdcs_get_workstat(idx );
  150. // }else{
  151. // return -1;
  152. // }
  153. // }
  154. // char* dehumi_get_infan_stat_str(int idx)
  155. // {
  156. // int model = dehumi[1].model;
  157. // if( model == DEVM_SDCS ){
  158. // return sdcs_get_infan_stat_str(idx );
  159. // }else{
  160. // return NULL;
  161. // }
  162. // }
  163. // int dehumi_get_infan_stat(int idx)
  164. // {
  165. // int model = dehumi[1].model;
  166. // if( model == DEVM_SDCS ){
  167. // return sdcs_get_infan_stat(idx );
  168. // }else{
  169. // return -1;
  170. // }
  171. // }
  172. // char* dehumi_get_outfan_stat_str(int idx)
  173. // {
  174. // int model = dehumi[1].model;
  175. // if( model == DEVM_SDCS ){
  176. // return sdcs_get_outfan_stat_str(idx );
  177. // }else{
  178. // return NULL;
  179. // }
  180. // }
  181. // int dehumi_get_outfan_stat(int idx)
  182. // {
  183. // int model = dehumi[1].model;
  184. // if( model == DEVM_SDCS ){
  185. // return sdcs_get_outfan_stat(idx );
  186. // }else{
  187. // return -1;
  188. // }
  189. // }
  190. // char* dehumi_get_cmp_stat_str(int idx)
  191. // {
  192. // int model = dehumi[1].model;
  193. // if( model == DEVM_SDCS ){
  194. // return sdcs_get_cmp_stat_str(idx );
  195. // }else{
  196. // return NULL;
  197. // }
  198. // }
  199. // int dehumi_get_cmp_stat(int idx)
  200. // {
  201. // int model = dehumi[1].model;
  202. // if( model == DEVM_SDCS ){
  203. // return sdcs_get_cmp_stat(idx );
  204. // }else{
  205. // return -1;
  206. // }
  207. // }
  208. // int dehumi_get_rtnwind_temp(int idx)
  209. // {
  210. // int model = dehumi[1].model;
  211. // if( model == DEVM_SDCS ){
  212. // return sdcs_get_rtnwind_temp(idx );
  213. // }else{
  214. // return -100;
  215. // }
  216. // }
  217. // int dehumi_get_cond_temp(int idx)
  218. // {
  219. // int model = dehumi[1].model;
  220. // if( model == DEVM_SDCS ){
  221. // return sdcs_get_cond_temp(idx );
  222. // }else{
  223. // return -100;
  224. // }
  225. // }
  226. // int dehumi_get_eva_temp(int idx)
  227. // {
  228. // int model = dehumi[1].model;
  229. // if( model == DEVM_SDCS ){
  230. // return sdcs_get_eva_temp(idx );
  231. // }else{
  232. // return -100;
  233. // }
  234. // }
  235. // int dehumi_get_infan_spd(int idx)
  236. // {
  237. // int model = dehumi[1].model;
  238. // if( model == DEVM_SDCS ){
  239. // return sdcs_get_infan_spd(idx );
  240. // }else{
  241. // return -100;
  242. // }
  243. // }
  244. // int dehumi_get_outfan_spd(int idx)
  245. // {
  246. // int model = dehumi[1].model;
  247. // if( model == DEVM_SDCS ){
  248. // return sdcs_get_outfan_spd(idx );
  249. // }else{
  250. // return -100;
  251. // }
  252. // }
  253. // int dehumi_get_comm_st(int idx)
  254. // {
  255. // int model = dehumi[1].model;
  256. // if( model == DEVM_SDCS ){
  257. // return sdcs_get_comm_st(idx);
  258. // }else{
  259. // return -1;
  260. // }
  261. // }
  262. // int dehumi_get_nbr()
  263. // {
  264. // int model = dehumi[1].model;
  265. // if( model == DEVM_SDCS ){
  266. // return sdcs_get_nbr();
  267. // }else{
  268. // return -1;
  269. // }
  270. // }
  271. // int dehumi_get_state(int idx)
  272. // {
  273. // int model = dehumi[1].model;
  274. // if( model == DEVM_SDCS ){
  275. // return sdcs_get_state(idx);
  276. // }else{
  277. // return -1;
  278. // }
  279. // }
  280. char *dehumi_get_model_str(int idx)
  281. {
  282. return dehumi[idx].szmodel;
  283. }
  284. // int dehumi_get_chan_idx(int idx)
  285. // {
  286. // int model = dehumi[1].model;
  287. // if( model == DEVM_SDCS ){
  288. // return sdcs_get_chan_idx(idx);
  289. // }else{
  290. // return -1;
  291. // }
  292. // }
  293. // char* dehumi_get_comm_st_str(int idx)
  294. // {
  295. // int model = dehumi[1].model;
  296. // if( model == DEVM_SDCS ){
  297. // return sdcs_get_comm_st_str(idx);
  298. // }else{
  299. // return NULL;
  300. // }
  301. // }
  302. // int dehumi_get_tick(int idx)
  303. // {
  304. // int model = dehumi[1].model;
  305. // if( model == DEVM_SDCS ){
  306. // return sdcs_get_tick(idx);
  307. // }else{
  308. // return -1;
  309. // }
  310. // }
  311. // int dehumi_get_addr(int idx)
  312. // {
  313. // int model = dehumi[1].model;
  314. // if( model == DEVM_SDCS ){
  315. // return sdcs_get_addr(idx);
  316. // }else{
  317. // return -1;
  318. // }
  319. // }
  320. int dehumi_get_tool_data(char *buf)
  321. {
  322. int model = dehumi[1].model;
  323. int i = 0;
  324. char temp_buf[2048];
  325. if (model == DEVM_SDCS)
  326. {
  327. sprintf(buf, "" REVERSE "DEHUMI" NONE "model:%s info:%s\n", dehumi[1].szmodel, dehumi[1].szinfo);
  328. for (i = 1; i <= sdcs_get_nbr(); i++)
  329. {
  330. memset(temp_buf, 0, sizeof(temp_buf));
  331. sdcs_get_tool_data(i, temp_buf);
  332. strcat(buf, temp_buf);
  333. }
  334. return 0;
  335. }
  336. else
  337. {
  338. return -1;
  339. }
  340. }
  341. int dehumi_get_tbmqtt_data(char *buf)
  342. {
  343. for (int i = 1; i <= dehumi[1].model_nbr; i++)
  344. {
  345. if (dehumi[1].model == DEVM_SDCS)
  346. {
  347. for (int idx = 1; idx <= sdcs_get_nbr(); idx++)
  348. {
  349. if (sdcs_get_comm_st(idx) != COMMST_NORMAL)
  350. {
  351. if (i == dehumi[1].model_nbr && idx == sdcs_get_nbr() && strlen(buf) > 1)
  352. {
  353. buf[strlen(buf) - 1] = 0;
  354. }
  355. continue;
  356. }
  357. char temp_buf[1024];
  358. memset(temp_buf, 0, sizeof(temp_buf));
  359. sdcs_get_tbmqtt_data(idx, temp_buf);
  360. strcat(buf, temp_buf);
  361. if (i != dehumi[1].model_nbr || idx != sdcs_get_nbr())
  362. {
  363. strcat(buf, ",");
  364. }
  365. }
  366. }
  367. }
  368. }
  369. int dehumi_get_cloud_data(int ctn_idx, char *buf)
  370. {
  371. for (int i = 1; i <= dehumi[1].model_nbr; i++)
  372. {
  373. if (dehumi[1].model == DEVM_SDCS)
  374. {
  375. for (int idx = 1; idx <= sdcs_get_nbr(); idx++)
  376. {
  377. if (sdcs_get_comm_st(idx) != COMMST_NORMAL)
  378. {
  379. if (i == dehumi[1].model_nbr && idx == sdcs_get_nbr() && strlen(buf) > 1)
  380. {
  381. buf[strlen(buf) - 1] = 0;
  382. }
  383. continue;
  384. }
  385. char temp_buf[1024];
  386. memset(temp_buf, 0, sizeof(temp_buf));
  387. sdcs_get_cloud_data(ctn_idx, idx, temp_buf);
  388. strcat(buf, temp_buf);
  389. if (i != dehumi[1].model_nbr || idx != sdcs_get_nbr())
  390. {
  391. strcat(buf, ",");
  392. }
  393. }
  394. }
  395. }
  396. }
  397. int dehumi_get_modbus_data(int idx, unsigned short *data)
  398. {
  399. int model = dehumi[1].model;
  400. if (model == DEVM_SDCS)
  401. {
  402. sdcs_get_modbus_data(idx, data);
  403. }
  404. else
  405. {
  406. data[0] = (unsigned short)model;
  407. data[1] = (unsigned short)0;
  408. }
  409. return 0;
  410. }
  411. double dehumi_get_temp(int idx)
  412. {
  413. int model = dehumi[1].model;
  414. if (model == DEVM_SDCS)
  415. {
  416. return sdcs_get_temp(idx);
  417. }
  418. else
  419. {
  420. return -1;
  421. }
  422. }
  423. double dehumi_get_humi(int idx)
  424. {
  425. int model = dehumi[1].model;
  426. if (model == DEVM_SDCS)
  427. {
  428. return sdcs_get_humi(idx);
  429. }
  430. else
  431. {
  432. return -1;
  433. }
  434. }
  435. // int dehumi_get_bkds_data(char* buf)
  436. // {
  437. // int model = dehumi[1].model;
  438. // int i = 0;
  439. // char buf_temp[8000];
  440. // sprintf(buf,"\"dehumi\":[");
  441. // if( model == DEVM_SDCS ){
  442. // for(i = 1;i <= sdcs_get_nbr();i ++ ){
  443. // strcat(buf,"{");
  444. // sdcs_get_bkds_data(i,buf_temp);
  445. // strcat(buf,buf_temp);
  446. // strcat(buf,"}");
  447. // if(i != sdcs_get_nbr()){
  448. // strcat(buf,",");
  449. // }
  450. // }
  451. // }else{
  452. // return -1;
  453. // }
  454. // strcat(buf,"]");
  455. // }