tb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. #include "plt.h"
  2. struct tb_t tb;
  3. /* in seconds */
  4. static long long tb_get_unixts()
  5. {
  6. return (long long)time(NULL);
  7. }
  8. /* in ms */
  9. static long long tb_get_ts()
  10. {
  11. return (tb_get_unixts() + tbmqtt_get_tz() * (long long)3600) * (long long)1000;
  12. // return tb_get_unixts()*1000;
  13. }
  14. static int tb_send_rpc_response(char *request_topic, char *response_payload)
  15. {
  16. char *p = NULL;
  17. char szreqid[256];
  18. char *pszreqid = szreqid;
  19. int reqid = 0;
  20. tbmqtt_ringbuffer_element_t e;
  21. p = strrchr(request_topic, '/');
  22. if (p != NULL)
  23. {
  24. p++;
  25. while (*p != 0)
  26. {
  27. *pszreqid++ = *p++;
  28. }
  29. *pszreqid = 0;
  30. reqid = atoi(szreqid);
  31. e.cmd = CMD_MQTT_SENDKV;
  32. e.sztopic[0] = 0;
  33. e.szpayload[0] = 0;
  34. strcpy(e.szpayload, response_payload);
  35. sprintf(e.sztopic, "v1/devices/me/rpc/response/%s", szreqid);
  36. tbmqtt_lock_txbuf();
  37. tbmqtt_queue_txbuf(e);
  38. tbmqtt_unlock_txbuf();
  39. }
  40. return 0;
  41. }
  42. static void tb_upload_param()
  43. {
  44. struct tb_t *dev = &tb;
  45. tbmqtt_ringbuffer_element_t e;
  46. char buf[128];
  47. char itm_buf[2048];
  48. int i;
  49. /* 5 seconds period */
  50. if (dev->param_en > 0)
  51. {
  52. dev->param_en = 0;
  53. e.cmd = CMD_MQTT_SENDKV;
  54. e.sztopic[0] = 0;
  55. e.szpayload[0] = 0;
  56. /* sw version */
  57. sprintf(itm_buf, "'sys_sw_version':'%s', 'norm_cap':%d,'norm_pow':%d",
  58. plt_get_version_string(), ctn_get_norm_cap(), ctn_get_norm_pow());
  59. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf); /* thingsboard */
  60. // sprintf(e.sztopic,"ems/data/rtEss/%s", sta_get_sn());
  61. strcpy(e.sztopic, "v1/devices/me/telemetry");
  62. tbmqtt_lock_txbuf();
  63. tbmqtt_queue_txbuf(e);
  64. tbmqtt_unlock_txbuf();
  65. }
  66. }
  67. static void tb_upload_sys()
  68. {
  69. struct tb_t *c = &tb;
  70. tbmqtt_ringbuffer_element_t e;
  71. char buf[128];
  72. char itm_buf[2048];
  73. int i;
  74. /* 5 seconds period */
  75. if (c->sys_intv[0] > 0)
  76. {
  77. if (c->sys_timer[0]++ >= c->sys_intv[0])
  78. {
  79. c->sys_timer[0] = 0;
  80. e.cmd = CMD_MQTT_SENDKV;
  81. e.sztopic[0] = 0;
  82. e.szpayload[0] = 0;
  83. // sys state and err
  84. itm_buf[0] = 0;
  85. sprintf(itm_buf, "'state':'%s','err':'%s','ap':%d,'soc':%.1f,'chgable':%d,'dhgable':%d,\
  86. 'con_com_ap':%d,'con_com_ae':%.1f,'con_pos_ae':%.1f,'con_neg_ae':%.1f,\
  87. 'load_com_ap':%d,'load_com_ae':%.1f,'load_pos_ae':%.1f,'load_neg_ae':%.1f,'ems_state':'%s'",
  88. ctn_get_state_str(), ctn_get_err_str(), ctn_get_ap(), ctn_get_soc(), ctn_get_chgable(), ctn_get_dhgable(),
  89. ems_get_transf_con_com_ap(), ems_get_transf_con_com_ae(), ems_get_transf_con_pos_ae(), ems_get_transf_con_neg_ae(),
  90. ems_get_transf_load_com_ap(), ems_get_transf_load_com_ae(), ems_get_transf_load_pos_ae(), ems_get_transf_load_neg_ae(), ems_get_state_str());
  91. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  92. strcpy(e.sztopic, "v1/devices/me/telemetry");
  93. tbmqtt_lock_txbuf();
  94. tbmqtt_queue_txbuf(e);
  95. tbmqtt_unlock_txbuf();
  96. }
  97. }
  98. /* 300 seconds period */
  99. if (c->sys_intv[1] > 0)
  100. {
  101. if (c->sys_timer[1]++ >= c->sys_intv[1])
  102. {
  103. c->sys_timer[1] = 0;
  104. e.cmd = CMD_MQTT_SENDKV;
  105. e.sztopic[0] = 0;
  106. e.szpayload[0] = 0;
  107. itm_buf[0] = 0;
  108. /* cpu mem disk */
  109. mac_get_tbmqtt_data(buf);
  110. strcat(itm_buf, buf);
  111. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  112. strcpy(e.sztopic, "v1/devices/me/telemetry");
  113. tbmqtt_lock_txbuf();
  114. tbmqtt_queue_txbuf(e);
  115. tbmqtt_unlock_txbuf();
  116. }
  117. }
  118. }
  119. static void tb_upload_pcs()
  120. {
  121. struct tb_t *dev = &tb;
  122. tbmqtt_ringbuffer_element_t e;
  123. char buf[128];
  124. char itm_buf[8192];
  125. int i;
  126. if (pcs_get_comm_st() != COMMST_NORMAL)
  127. {
  128. return;
  129. }
  130. // 5 seconds period
  131. if (dev->pcs_intv[0] > 0)
  132. {
  133. if (dev->pcs_timer[0]++ >= dev->pcs_intv[0])
  134. {
  135. dev->pcs_timer[0] = 0;
  136. e.cmd = CMD_MQTT_SENDKV;
  137. e.sztopic[0] = 0;
  138. e.szpayload[0] = 0;
  139. memset(itm_buf, 0, sizeof(itm_buf));
  140. // pcs_get_tbmqtt_main_data(itm_buf);
  141. pcs_get_tbmqtt_data_interval_30s(itm_buf);
  142. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  143. strcpy(e.sztopic, "v1/devices/me/telemetry");
  144. tbmqtt_lock_txbuf();
  145. tbmqtt_queue_txbuf(e);
  146. tbmqtt_unlock_txbuf();
  147. }
  148. }
  149. /* 60 seconds period */
  150. if (dev->pcs_intv[1] > 0)
  151. {
  152. if (dev->pcs_timer[1]++ >= dev->pcs_intv[1])
  153. {
  154. dev->pcs_timer[1] = 0;
  155. e.cmd = CMD_MQTT_SENDKV;
  156. e.sztopic[0] = 0;
  157. e.szpayload[0] = 0;
  158. memset(itm_buf, 0, sizeof(itm_buf));
  159. // pcs_get_tbmqtt_secondy_data(itm_buf);
  160. pcs_get_tbmqtt_data_interval_60s(itm_buf);
  161. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  162. strcpy(e.sztopic, "v1/devices/me/telemetry");
  163. tbmqtt_lock_txbuf();
  164. tbmqtt_queue_txbuf(e);
  165. tbmqtt_unlock_txbuf();
  166. }
  167. }
  168. }
  169. static void tb_upload_pack()
  170. {
  171. struct tb_t *c = &tb;
  172. tbmqtt_ringbuffer_element_t e;
  173. char itm_buf[10240];
  174. int i, j;
  175. int model = 0;
  176. model = pack_get_model();
  177. if (pack_get_comm_st() != COMMST_NORMAL)
  178. {
  179. return;
  180. }
  181. /* 5 seconds */
  182. if (c->pack_intv[0] > 0)
  183. {
  184. if (c->pack_timer[0]++ >= c->pack_intv[0])
  185. {
  186. c->pack_timer[0] = 0;
  187. e.cmd = CMD_MQTT_SENDKV;
  188. e.sztopic[0] = 0;
  189. e.szpayload[0] = 0;
  190. memset(itm_buf, 0, sizeof(itm_buf));
  191. // pack_get_tbmqtt_main_data(itm_buf);
  192. pack_get_tbmqtt_data_inteval_30s(itm_buf);
  193. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  194. // printf("%s,%s\n",__func__,e.szpayload);
  195. // fflush(stdout);
  196. strcpy(e.sztopic, "v1/devices/me/telemetry");
  197. tbmqtt_lock_txbuf();
  198. tbmqtt_queue_txbuf(e);
  199. tbmqtt_unlock_txbuf();
  200. }
  201. }
  202. // 60 seconds sub sys
  203. if (c->pack_intv[1] > 0)
  204. {
  205. if (c->pack_timer[1]++ >= c->pack_intv[1])
  206. {
  207. c->pack_timer[1] = 0;
  208. e.cmd = CMD_MQTT_SENDKV;
  209. e.sztopic[0] = 0;
  210. e.szpayload[0] = 0;
  211. /* bms sub voltage and current */
  212. memset(itm_buf, 0, sizeof(itm_buf));
  213. // pack_get_tbmqtt_submain_data(itm_buf);
  214. pack_get_tbmqtt_data_inteval_60s(itm_buf);
  215. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  216. // printf("%s,%s\n",__func__,e.szpayload);
  217. // fflush(stdout);
  218. strcpy(e.sztopic, "v1/devices/me/telemetry");
  219. tbmqtt_lock_txbuf();
  220. tbmqtt_queue_txbuf(e);
  221. tbmqtt_unlock_txbuf();
  222. }
  223. }
  224. /* cell voltage*/
  225. if (c->pack_intv[2] > 0)
  226. {
  227. if (c->pack_timer[2]++ >= c->pack_intv[2])
  228. {
  229. c->pack_timer[2] = 0;
  230. e.cmd = CMD_MQTT_SENDKV;
  231. e.sztopic[0] = 0;
  232. e.szpayload[0] = 0;
  233. /* module cell v */
  234. itm_buf[0] = 0;
  235. memset(itm_buf, 0, sizeof(itm_buf));
  236. // pack_get_tbmqtt_cellv_data(c->cellv_sub_idx,itm_buf);
  237. pack_get_tbmqtt_data_inteval_90s(itm_buf);
  238. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  239. // printf("%s,%s\n",__func__,e.szpayload);
  240. // fflush(stdout);
  241. strcpy(e.sztopic, "v1/devices/me/telemetry");
  242. tbmqtt_lock_txbuf();
  243. tbmqtt_queue_txbuf(e);
  244. tbmqtt_unlock_txbuf();
  245. c->cellv_sub_idx++;
  246. if (model == DEVM_CATL280A)
  247. {
  248. if (c->cellv_sub_idx > BMU_NBR)
  249. {
  250. c->cellv_sub_idx = 1;
  251. }
  252. }
  253. else if (model == DEVM_KL)
  254. {
  255. if (c->cellv_sub_idx > KL_CLUSTER_NBR)
  256. {
  257. c->cellv_sub_idx = 1;
  258. }
  259. }
  260. }
  261. }
  262. /* cell temp*/
  263. if (c->pack_intv[3] > 0)
  264. {
  265. if (c->pack_timer[3]++ >= c->pack_intv[3])
  266. {
  267. c->pack_timer[3] = 0;
  268. e.cmd = CMD_MQTT_SENDKV;
  269. e.sztopic[0] = 0;
  270. e.szpayload[0] = 0;
  271. /* module cell t */
  272. memset(itm_buf, 0, sizeof(itm_buf));
  273. // pack_get_tbmqtt_cellt_data(c->cellt_sub_idx,itm_buf);
  274. pack_get_tbmqtt_data_inteval_300s(itm_buf);
  275. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  276. // printf("%s,%s\n",__func__,e.szpayload);
  277. // fflush(stdout);
  278. strcpy(e.sztopic, "v1/devices/me/telemetry");
  279. tbmqtt_lock_txbuf();
  280. tbmqtt_queue_txbuf(e);
  281. tbmqtt_unlock_txbuf();
  282. c->cellt_sub_idx++;
  283. if (model == DEVM_CATL280A)
  284. {
  285. if (c->cellt_sub_idx > BMU_NBR)
  286. {
  287. c->cellt_sub_idx = 1;
  288. }
  289. }
  290. else if (model == DEVM_KL)
  291. {
  292. if (c->cellt_sub_idx > KL_CLUSTER_NBR)
  293. {
  294. c->cellt_sub_idx = 1;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. static void tb_upload_ac(void)
  301. {
  302. struct tb_t *dev = &tb;
  303. tbmqtt_ringbuffer_element_t e;
  304. char buf_temp[8000];
  305. /* 60 seconds */
  306. if (dev->ac_intv[0] > 0)
  307. {
  308. if (dev->ac_timer[0]++ >= dev->ac_intv[0])
  309. {
  310. dev->ac_timer[0] = 0;
  311. e.cmd = CMD_MQTT_SENDKV;
  312. e.sztopic[0] = 0;
  313. e.szpayload[0] = 0;
  314. memset(buf_temp, 0, sizeof(buf_temp));
  315. ac_get_tbmqtt_data(buf_temp);
  316. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), buf_temp);
  317. // printf("%s,%s\n",__func__,e.szpayload);
  318. strcpy(e.sztopic, "v1/devices/me/telemetry");
  319. tbmqtt_lock_txbuf();
  320. tbmqtt_queue_txbuf(e);
  321. tbmqtt_unlock_txbuf();
  322. }
  323. }
  324. }
  325. static void tb_upload_meter(void)
  326. {
  327. struct tb_t *dev = &tb;
  328. tbmqtt_ringbuffer_element_t e;
  329. char buf[8192];
  330. char itm_buf[8192];
  331. int i;
  332. /* 60 seconds */
  333. if (dev->meter_intv[0] > 0)
  334. {
  335. if (dev->meter_timer[0]++ >= dev->meter_intv[0])
  336. {
  337. dev->meter_timer[0] = 0;
  338. // dtsd1352
  339. e.cmd = CMD_MQTT_SENDKV;
  340. e.sztopic[0] = 0;
  341. e.szpayload[0] = 0;
  342. memset(itm_buf, 0, sizeof(itm_buf));
  343. meter_get_tbmqtt_data(itm_buf);
  344. if (strlen(itm_buf) > 0)
  345. {
  346. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  347. // printf("%s,%s\n",__func__,e.szpayload);
  348. strcpy(e.sztopic, "v1/devices/me/telemetry");
  349. tbmqtt_lock_txbuf();
  350. tbmqtt_queue_txbuf(e);
  351. tbmqtt_unlock_txbuf();
  352. }
  353. }
  354. }
  355. }
  356. static void tb_upload_env(void)
  357. {
  358. struct tb_t *dev = &tb;
  359. tbmqtt_ringbuffer_element_t e;
  360. char buf[8192];
  361. char itm_buf[8192];
  362. /* 60 seconds */
  363. if (dev->env_intv[0] > 0)
  364. {
  365. if (dev->env_timer[0]++ >= dev->env_intv[0])
  366. {
  367. dev->env_timer[0] = 0;
  368. e.cmd = CMD_MQTT_SENDKV;
  369. e.sztopic[0] = 0;
  370. e.szpayload[0] = 0;
  371. /* env */
  372. memset(itm_buf, 0, sizeof(itm_buf));
  373. env_get_tbmqtt_data(itm_buf);
  374. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  375. // printf("%s,%s\n",__func__,e.szpayload);
  376. strcpy(e.sztopic, "v1/devices/me/telemetry");
  377. tbmqtt_lock_txbuf();
  378. tbmqtt_queue_txbuf(e);
  379. tbmqtt_unlock_txbuf();
  380. }
  381. }
  382. }
  383. static void tb_upload_dehumi(void)
  384. {
  385. struct tb_t *dev = &tb;
  386. tbmqtt_ringbuffer_element_t e;
  387. char buf[8192];
  388. char itm_buf[8192];
  389. /* 60 seconds */
  390. if (dev->dehumi_intv[0] > 0)
  391. {
  392. if (dev->dehumi_timer[0]++ >= dev->dehumi_intv[0])
  393. {
  394. dev->dehumi_timer[0] = 0;
  395. e.cmd = CMD_MQTT_SENDKV;
  396. e.sztopic[0] = 0;
  397. e.szpayload[0] = 0;
  398. /* env */
  399. memset(itm_buf, 0, sizeof(itm_buf));
  400. dehumi_get_tbmqtt_data(itm_buf);
  401. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  402. // printf("%s,%s\n",__func__,e.szpayload);
  403. strcpy(e.sztopic, "v1/devices/me/telemetry");
  404. tbmqtt_lock_txbuf();
  405. tbmqtt_queue_txbuf(e);
  406. tbmqtt_unlock_txbuf();
  407. }
  408. }
  409. }
  410. static void tb_upload_fe(void)
  411. {
  412. struct tb_t *dev = &tb;
  413. tbmqtt_ringbuffer_element_t e;
  414. char buf[8192];
  415. char itm_buf[8192];
  416. /* 60 seconds */
  417. if (dev->fe_intv[0] > 0)
  418. {
  419. if (dev->fe_timer[0]++ >= dev->fe_intv[0])
  420. {
  421. dev->fe_timer[0] = 0;
  422. e.cmd = CMD_MQTT_SENDKV;
  423. e.sztopic[0] = 0;
  424. e.szpayload[0] = 0;
  425. /* env */
  426. memset(itm_buf, 0, sizeof(itm_buf));
  427. fe_get_tbmqtt_data(itm_buf);
  428. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  429. // printf("%s,%s\n",__func__,e.szpayload);
  430. strcpy(e.sztopic, "v1/devices/me/telemetry");
  431. tbmqtt_lock_txbuf();
  432. tbmqtt_queue_txbuf(e);
  433. tbmqtt_unlock_txbuf();
  434. }
  435. }
  436. }
  437. static void tb_upload_plc(void)
  438. {
  439. struct tb_t *dev = &tb;
  440. tbmqtt_ringbuffer_element_t e;
  441. char buf[8192];
  442. char itm_buf[8192];
  443. int i;
  444. /* 60 seconds */
  445. if (dev->plc_intv[0] > 0)
  446. {
  447. if (dev->plc_timer[0]++ >= dev->plc_intv[0])
  448. {
  449. dev->plc_timer[0] = 0;
  450. if (plc_get_comm_st() == COMMST_NORMAL)
  451. {
  452. e.cmd = CMD_MQTT_SENDKV;
  453. e.sztopic[0] = 0;
  454. e.szpayload[0] = 0;
  455. memset(itm_buf, 0, sizeof(itm_buf));
  456. plc_get_tbmqtt_data(itm_buf);
  457. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  458. // printf("%s,%s\n",__func__,e.szpayload);
  459. strcpy(e.sztopic, "v1/devices/me/telemetry");
  460. tbmqtt_lock_txbuf();
  461. tbmqtt_queue_txbuf(e);
  462. tbmqtt_unlock_txbuf();
  463. }
  464. }
  465. }
  466. }
  467. static void tb_upload_ups(void)
  468. {
  469. struct tb_t *c = &tb;
  470. tbmqtt_ringbuffer_element_t e;
  471. char buf[8192];
  472. char itm_buf[8192];
  473. int i;
  474. if (ups_get_comm_state() != COMMST_NORMAL)
  475. {
  476. return;
  477. }
  478. /* 60 seconds */
  479. if (c->ups_intv[0] > 0)
  480. {
  481. if (c->ups_timer[0]++ >= c->ups_intv[0])
  482. {
  483. c->ups_timer[0] = 0;
  484. e.cmd = CMD_MQTT_SENDKV;
  485. e.sztopic[0] = 0;
  486. e.szpayload[0] = 0;
  487. memset(itm_buf, 0, sizeof(itm_buf));
  488. ups_get_tbmqtt_data(itm_buf);
  489. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  490. // printf("%s,%s\n",__func__,e.szpayload);
  491. strcpy(e.sztopic, "v1/devices/me/telemetry");
  492. tbmqtt_lock_txbuf();
  493. tbmqtt_queue_txbuf(e);
  494. tbmqtt_unlock_txbuf();
  495. }
  496. }
  497. }
  498. static void tb_upload_ctn_cal(void)
  499. {
  500. struct tb_t *c = &tb;
  501. tbmqtt_ringbuffer_element_t e;
  502. char buf[8192];
  503. char itm_buf[8192];
  504. int i;
  505. if (ctn_cal_get_state() != SMST_RUN)
  506. {
  507. return;
  508. }
  509. /* 60 seconds */
  510. if (c->ctn_cal_intv[0] > 0)
  511. {
  512. if (c->ctn_cal_timer[0]++ >= c->ctn_cal_intv[0])
  513. {
  514. c->ctn_cal_timer[0] = 0;
  515. e.cmd = CMD_MQTT_SENDKV;
  516. e.sztopic[0] = 0;
  517. e.szpayload[0] = 0;
  518. memset(itm_buf, 0, sizeof(itm_buf));
  519. ctn_cal_get_tb_data(itm_buf);
  520. sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
  521. // printf("%s,%s\n",__func__,e.szpayload);
  522. strcpy(e.sztopic, "v1/devices/me/telemetry");
  523. tbmqtt_lock_txbuf();
  524. tbmqtt_queue_txbuf(e);
  525. tbmqtt_unlock_txbuf();
  526. }
  527. }
  528. }
  529. int tb_is_upload_enable()
  530. {
  531. struct tb_t *dev = &tb;
  532. return dev->upload_enable;
  533. }
  534. int tb_enable_upload()
  535. {
  536. struct tb_t *dev = &tb;
  537. dev->upload_enable = 1;
  538. return 0;
  539. }
  540. int tb_disable_upload()
  541. {
  542. struct tb_t *dev = &tb;
  543. dev->upload_enable = 0;
  544. return 0;
  545. }
  546. int tb_init(void)
  547. {
  548. struct tb_t *dev = &tb;
  549. int ret = 0;
  550. dev->upload_enable = 1;
  551. dev->param_en = 0;
  552. dev->sys_intv[0] = 5;
  553. dev->sys_intv[1] = 300;
  554. dev->pcs_intv[0] = 5;
  555. dev->pcs_intv[1] = 60;
  556. dev->cellv_sub_idx = 1;
  557. dev->cellt_sub_idx = 1;
  558. dev->pack_intv[0] = 5;
  559. dev->pack_intv[1] = 60;
  560. dev->pack_intv[2] = 90; /* cell v disabled */
  561. // tb_set_cellv_lowspeed();
  562. dev->pack_intv[3] = 300; /* cell t disabled 30*10=300s=5min for all 10 cabinet */
  563. dev->ac_intv[0] = 60;
  564. dev->meter_intv[0] = 60;
  565. dev->env_intv[0] = 60;
  566. dev->plc_intv[0] = 60;
  567. dev->ups_intv[0] = 60;
  568. dev->dehumi_intv[0] = 60;
  569. dev->fe_intv[0] = 60;
  570. dev->ctn_cal_intv[0] = 60;
  571. if (tbmqtt_init() != 0)
  572. {
  573. log_dbg("%s, tbmqtt init fail", __func__);
  574. }
  575. log_dbg("%s, ret:%d", __func__, ret);
  576. return ret;
  577. }
  578. int tb_set_cellv_highspeed()
  579. {
  580. struct tb_t *dev = &tb;
  581. dev->pack_intv[2] = 6; /* cell v */
  582. /* 6 = high: 6*10=60s=1min for all 10 cabinet */
  583. return 0;
  584. }
  585. int tb_set_cellv_lowspeed()
  586. {
  587. struct tb_t *dev = &tb;
  588. dev->pack_intv[2] = 60; /* cell v */
  589. /* 60 = low: 60*10=600s=10min for all 10 cabinet */
  590. }
  591. static int tmpaps_received = 0;
  592. static int tmpaps;
  593. static void tb_proc_recv()
  594. {
  595. cJSON *root = NULL;
  596. cJSON *item = NULL;
  597. int aps = 0;
  598. int rc;
  599. tbmqtt_ringbuffer_element_t e;
  600. tbmqtt_lock_rxbuf();
  601. rc = tbmqtt_dequeue_rxbuf(&e);
  602. tbmqtt_unlock_rxbuf();
  603. if (rc == 1)
  604. {
  605. root = cJSON_Parse(e.szpayload);
  606. if (!root)
  607. {
  608. log_dbg("%s, cJSON_Parse null", __func__);
  609. }
  610. else
  611. {
  612. item = cJSON_GetObjectItem(root, "method");
  613. if (!item)
  614. {
  615. log_dbg("%s, cJSON_GetObjectItem method fail", __func__);
  616. }
  617. else
  618. {
  619. log_dbg("%s, get method:%s", __func__, item->valuestring);
  620. if (strcmp(item->valuestring, "get_aps") == 0)
  621. {
  622. sprintf(e.szpayload, "%d", ess_get_aps());
  623. tb_send_rpc_response(e.sztopic, e.szpayload);
  624. }
  625. else if (strcmp(item->valuestring, "stdby") == 0)
  626. {
  627. ess_send_sm_cmd(CMD_SM_STDBY);
  628. }
  629. else if (strcmp(item->valuestring, "stop") == 0)
  630. {
  631. ess_send_sm_cmd(CMD_SM_STOP);
  632. }
  633. else if (strcmp(item->valuestring, "ready") == 0)
  634. {
  635. ess_send_sm_cmd(CMD_SM_READY);
  636. }
  637. else if (strcmp(item->valuestring, "offgrid") == 0)
  638. {
  639. ess_send_sm_cmd(CMD_SM_OFFGRID);
  640. }
  641. else if (strcmp(item->valuestring, "active_aps") == 0)
  642. {
  643. if (tmpaps_received == 1)
  644. {
  645. ess_set_aps(tmpaps);
  646. tmpaps_received = 0;
  647. }
  648. }
  649. else if (strcmp(item->valuestring, "set_aps") == 0)
  650. {
  651. item = cJSON_GetObjectItem(root, "params");
  652. if (!item)
  653. {
  654. log_dbg("%s, cJSON_GetObjectItem params fail", __func__);
  655. }
  656. else
  657. {
  658. // ess_set_aps(item->valueint);
  659. log_dbg("%s, aps:%d", __func__, item->valueint);
  660. tmpaps = item->valueint;
  661. tmpaps_received = 1;
  662. }
  663. }
  664. else if (strcmp(item->valuestring, "bms_ctrl_pwrup") == 0)
  665. {
  666. plc_bms_ctrl_pwrup();
  667. }
  668. else if (strcmp(item->valuestring, "pack_stop") == 0)
  669. {
  670. pack_send_sm_cmd(CMD_SM_STOP);
  671. }
  672. else if (strcmp(item->valuestring, "pack_ready") == 0)
  673. {
  674. pack_send_sm_cmd(CMD_SM_READY);
  675. }
  676. else if (strcmp(item->valuestring, "pcs_stop") == 0)
  677. {
  678. pcs_send_sm_cmd(CMD_SM_STOP);
  679. }
  680. else if (strcmp(item->valuestring, "pcs_ready") == 0)
  681. {
  682. pcs_send_sm_cmd(CMD_SM_READY);
  683. }
  684. else if (strcmp(item->valuestring, "bms_ctrl_pwrup_without_shielding_signal"))
  685. {
  686. plc_bms_ctrl_pwrup_without_shielding_signal();
  687. }
  688. else
  689. {
  690. log_dbg("%s, unknown cmd : %s", __func__, item->valuestring);
  691. }
  692. }
  693. cJSON_Delete(root);
  694. }
  695. }
  696. }
  697. void tb_exe(void)
  698. {
  699. tb_proc_recv();
  700. if (tb_is_upload_enable())
  701. {
  702. tb_upload_param();
  703. tb_upload_sys();
  704. tb_upload_pcs();
  705. tb_upload_pack();
  706. tb_upload_ac();
  707. tb_upload_meter();
  708. tb_upload_env();
  709. tb_upload_dehumi();
  710. tb_upload_fe();
  711. // tb_upload_plc();
  712. // tb_upload_ups();
  713. // tb_upload_ctn_cal();
  714. }
  715. }
  716. int tb_set_param_en(int val)
  717. {
  718. struct tb_t *dev = &tb;
  719. dev->param_en = val;
  720. }
  721. char *tb_get_state_str(void)
  722. {
  723. return tbmqtt_get_state_str();
  724. }
  725. int tb_get_stp(void)
  726. {
  727. return tbmqtt_get_stp();
  728. }
  729. char *tb_get_err_str(void)
  730. {
  731. return tbmqtt_get_err_str();
  732. }
  733. int tb_get_tick(void)
  734. {
  735. return tbmqtt_get_tick();
  736. }
  737. double tb_get_timing_ave(void)
  738. {
  739. return tbmqtt_get_timing_ave();
  740. }
  741. double tb_get_timing_cur(void)
  742. {
  743. return tbmqtt_get_timing_cur();
  744. }
  745. double tb_get_timing_max(void)
  746. {
  747. return tbmqtt_get_timing_max();
  748. }
  749. int tb_get_enable(void)
  750. {
  751. return tbmqtt_get_enable();
  752. }
  753. char *tb_get_servip_str(void)
  754. {
  755. return tbmqtt_get_servip_str();
  756. }
  757. int tb_get_servport(void)
  758. {
  759. return tbmqtt_get_servport();
  760. }
  761. char *tb_get_client_id(void)
  762. {
  763. return tbmqtt_get_client_id();
  764. }
  765. double tb_get_txbuf_usage(void)
  766. {
  767. return tbmqtt_get_txbuf_usage();
  768. }
  769. char *tb_get_access_token(void)
  770. {
  771. return tbmqtt_get_access_token();
  772. }
  773. int tb_get_tz(void)
  774. {
  775. return tbmqtt_get_tz();
  776. }
  777. int tb_get_tool_data(char *buf)
  778. {
  779. struct tb_t *t = &tb;
  780. char buf_temp[1024];
  781. sprintf(buf, "" REVERSE " TB " NONE " \n");
  782. tbmqtt_get_tool_data(buf_temp);
  783. strcat(buf, buf_temp);
  784. sprintf(buf_temp, " upload_en:%d param_en:%d\n", t->upload_enable, t->param_en);
  785. strcat(buf, buf_temp);
  786. return 0;
  787. }