websocket_handle.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. #include "websocket_handle.h"
  2. #include "mongoose.h"
  3. #include "pthread.h"
  4. #include "elog.h"
  5. #include "common.h"
  6. #include "sys.h"
  7. #include "upg.h"
  8. #include "lock.h"
  9. #include "file.h"
  10. #include "thread.h"
  11. #include "sqlite_handle.h"
  12. #include "json_handle.h"
  13. #include "cascade.h"
  14. #include "cfg.h"
  15. #include "wifi.h"
  16. #include "Cellular.h"
  17. #define USE_WS2
  18. #if (CHIP_TYPE == CHIP_T113i)
  19. #define WS_SEND_TLEN_MAX (1024*1024*5)
  20. #else
  21. #define WS_SEND_TLEN_MAX (1024*1024*5)
  22. #endif
  23. #define WS_MAX 10
  24. #define USE_ONE_CONN
  25. typedef struct mg_mgr mg_mgr_t;
  26. typedef struct mg_connection mg_conn_t;
  27. typedef struct mg_ws_message mg_ws_msg_t;
  28. typedef struct mg_http_message mg_http_msg_t;
  29. typedef struct mg_tls_opts mg_tls_t;
  30. typedef struct {
  31. mg_mgr_t mgr;
  32. char wpath[200];
  33. char root[200];
  34. mg_tls_t tls;
  35. mg_conn_t *c;
  36. int inited;
  37. int ws_cnt;
  38. pwrall_info_t pwrall;
  39. }ws_handle_t;
  40. static ws_handle_t wsHandle;
  41. static int ws_is_online(ws_handle_t *wh)
  42. {
  43. #ifdef USE_ONE_CONN
  44. return (wh->c)?1:0;
  45. #else
  46. return (wh->ws_cnt>0)?1:0;
  47. #endif
  48. }
  49. static int ws_send(ws_handle_t *wh, void *data, int len, int isBinary)
  50. {
  51. if((!wh->c) || (!data) || (len<=0)) {
  52. return -1;
  53. }
  54. #ifdef USE_WS2
  55. mg_ws_send2(wh->c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
  56. #else
  57. mg_ws_send(wh->c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  58. #endif
  59. return 0;
  60. }
  61. static int ws_broadcast(ws_handle_t *wh, void *data, int len, int isBinary)
  62. {
  63. int r=-1;
  64. mg_conn_t *c;
  65. for (c=wh->mgr.conns; c!=NULL; c=c->next) {
  66. #ifdef USE_WS2
  67. mg_ws_send2(c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
  68. #else
  69. mg_ws_send(c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  70. #endif
  71. r = 0;
  72. }
  73. return r;
  74. }
  75. static int ws_send_data(ws_handle_t *wh, void *data, int len)
  76. {
  77. #ifdef USE_ONE_CONN
  78. return ws_send(wh, data, len, 0);
  79. #else
  80. return ws_broadcast(wh, data, len, 0);
  81. #endif
  82. }
  83. //////////////////////////////////////////////////////////////////////
  84. static int ws_all_update(ws_handle_t *h)
  85. {
  86. char *json;
  87. int r,r1,r2,r3;
  88. GlobalDeviceManager* gdm= &__globalDeviceManage;
  89. GlobalDeviceManager* gdm2=&__globalDeviceManage2;
  90. GlobalDeviceManager* pdm=NULL;
  91. pwrall_info_t *pall=&h->pwrall;
  92. if(gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_Tree ||
  93. gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One ||
  94. gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_Two ||
  95. gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One_B)
  96. {
  97. pall->ph3 = 1;
  98. if (cur_dev_addr == 0)
  99. {
  100. lock_s_hold(LOCK_ID_POWER_UPDATE);
  101. r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm);
  102. r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm);
  103. r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm);
  104. r = dev_search_latest_t_ac_power_statistic_info(0, 4, &pall->chall, gdm);
  105. lock_s_release(LOCK_ID_POWER_UPDATE);
  106. }
  107. else
  108. {
  109. cascade_lock();
  110. r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm2);
  111. r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm2);
  112. r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm2);
  113. r = dev_search_latest_t_ac_power_statistic_info(0, 4, &pall->chall, gdm2);
  114. cascade_unlock();
  115. }
  116. if (0 == r1 && 0 == r2 && 0 == r3 && 0 == r)
  117. {
  118. json = over_all_pwr_monitor_Tree_AC_to_json(&pall->ch[0], &pall->ch[1], &pall->ch[2], &pall->chall);
  119. ws_send_data(h, json, strlen(json));
  120. cJSON_free(json);
  121. }
  122. }
  123. else
  124. {
  125. pall->ph3 = 0;
  126. if(cur_dev_addr==0) {
  127. lock_s_hold(LOCK_ID_POWER_UPDATE);
  128. r = dev_search_latest_power_statistic_info(gdm, &pall->chall);
  129. lock_s_release(LOCK_ID_POWER_UPDATE);
  130. }
  131. else {
  132. cascade_lock();
  133. r = dev_search_latest_power_statistic_info(gdm2, &pall->chall);
  134. cascade_unlock();
  135. }
  136. if (0==r)
  137. {
  138. json = ws_over_status_ack_to_json(0, &pall->chall);
  139. ws_send_data(h, json, strlen(json));
  140. cJSON_free(json);
  141. }
  142. }
  143. return 0;
  144. }
  145. static int ws_chn_update(ws_handle_t *h)
  146. {
  147. int r;
  148. char *json;
  149. _OverChnPwrAckInfo chInfo;
  150. GlobalDeviceManager* gdm= &__globalDeviceManage;
  151. GlobalDeviceManager* gdm2=&__globalDeviceManage2;
  152. GlobalDeviceManager* pdm=NULL;
  153. INIT_LIST_HEAD(&chInfo.list);
  154. if(cur_dev_addr==0) {
  155. lock_s_hold(LOCK_ID_POWER_UPDATE);
  156. r = dev_search_latest_power_All_info(gdm,&chInfo, cur_dev_addr);
  157. lock_s_release(LOCK_ID_POWER_UPDATE);
  158. pdm = gdm;
  159. }
  160. else {
  161. cascade_lock();
  162. r = dev_search_latest_power_All_info(gdm2,&chInfo, cur_dev_addr);
  163. cascade_unlock();
  164. pdm = gdm2;
  165. }
  166. if (0 == r)
  167. {
  168. json = ws_chn_status_ack_to_json(1, &chInfo, pdm);
  169. ws_send_data(h, json, strlen(json));
  170. cJSON_free(json);
  171. }
  172. _OverChnPwrAckInfo *node, *next;
  173. list_for_each_entry_safe(node, next, &chInfo.list, list)
  174. {
  175. list_del(&node->list);
  176. free(node);
  177. }
  178. return 0;
  179. }
  180. static int ws_sensor_update(ws_handle_t *h)
  181. {
  182. char *json;
  183. GlobalDeviceManager* gdm= &__globalDeviceManage;
  184. //lock_s_hold(LOCK_ID_SENSOR);
  185. json = ws_sensor_status_ack_to_json(&gdm->_globalSensorManger);
  186. //lock_s_release(LOCK_ID_SENSOR);
  187. if(json) {
  188. ws_send_data(h, json, strlen(json));
  189. cJSON_free(json);
  190. }
  191. return 0;
  192. }
  193. static int ws_breaker_update(ws_handle_t *h)
  194. {
  195. char *json;
  196. GlobalDeviceManager* gdm= &__globalDeviceManage;
  197. GlobalDeviceManager* gdm2=&__globalDeviceManage2;
  198. if(cur_dev_addr==0) {
  199. json = breaker_status_ack_to_json(gdm,0);
  200. }
  201. else {
  202. cascade_lock();
  203. json = breaker_status_ack_to_json(gdm2,cur_dev_addr);
  204. cascade_unlock();
  205. }
  206. ws_send_data(h, json, strlen(json));
  207. cJSON_free(json);
  208. return 0;
  209. }
  210. static int ws_threshold_update(ws_handle_t *h)
  211. {
  212. char *json;
  213. GlobalDeviceManager* dm= &__globalDeviceManage;
  214. if(cur_dev_addr==0) {
  215. json = threshold_status_ack_to_json(dm);
  216. ws_send_data(h, json, strlen(json));
  217. cJSON_free(json);
  218. }
  219. return 0;
  220. }
  221. static int ws_upg_update(ws_handle_t *h)
  222. {
  223. handle_t l=upg_board_all_get();
  224. char* json = board_to_json(l, 1);
  225. ws_send_data(h, json, strlen(json));
  226. cJSON_free(json);
  227. return 0;
  228. }
  229. static int ws_Cellular_update(ws_handle_t *h)
  230. {
  231. char *json;
  232. if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
  233. {
  234. json = communication_4G_ack_to_json(&__globalDeviceManage._globalDevInfo.cell, 1);
  235. if (json)
  236. {
  237. ws_send_data(h, json, strlen(json));
  238. cJSON_free(json);
  239. }
  240. }
  241. return 0;
  242. }
  243. #if (CHIP_TYPE != CHIP_T113s)
  244. static int ws_WIFI_update(ws_handle_t *h)
  245. {
  246. char *json;
  247. if (__globalDeviceManage._globalDevInfo.wifi.WIFI_enable)
  248. {
  249. ap_stat_t stat;
  250. WIFIInfo_t *wm = &__globalDeviceManage._globalDevInfo.wifi;
  251. wifi_sta_stat(&stat);
  252. wm->Link_status = stat.link;
  253. strcpy(wm->WIFI_ip, stat.ipaddr);
  254. json = communication_WIFI_ack_to_json(wm, 1);
  255. if (json)
  256. {
  257. ws_send_data(h, json, strlen(json));
  258. cJSON_free(json);
  259. }
  260. }
  261. return 0;
  262. }
  263. #endif
  264. static int ws_DS_update(ws_handle_t *h)
  265. {
  266. char *json;
  267. lock_s_hold(LOCK_ID_POWER_UPDATE);
  268. json = service_ds_all_ack_to_json(DS_QueryDate,"","0",&__globalDeviceManage._globalPowerManger, 1);
  269. lock_s_release(LOCK_ID_POWER_UPDATE);
  270. if (json)
  271. {
  272. ws_send_data(h, json, strlen(json));
  273. cJSON_free(json);
  274. }
  275. return 0;
  276. }
  277. static int ws_MQTT_update(ws_handle_t *h)
  278. {
  279. char *json;
  280. json = service_mqtt_ack_to_json(&__globalDeviceManage.mqttInfo, 1);
  281. if (json)
  282. {
  283. ws_send_data(h, json, strlen(json));
  284. cJSON_free(json);
  285. }
  286. return 0;
  287. }
  288. /////////////////////////////////////////////////////////////
  289. static int ws_update(ws_handle_t *h)
  290. {
  291. int r;
  292. if(!ws_is_online(h)) {
  293. return -1;
  294. }
  295. //overall refresh
  296. r = ws_all_update(h);
  297. //channel refresh
  298. r = ws_chn_update(h);
  299. //sensor refresh
  300. r = ws_sensor_update(h);
  301. //breaker refresh
  302. r = ws_breaker_update(h);
  303. //threshold update
  304. //r = ws_threshold_update();
  305. //cellular refresh
  306. r = ws_Cellular_update(h);
  307. //wifi refresh
  308. #if (CHIP_TYPE != CHIP_T113s)
  309. r = ws_WIFI_update(h);
  310. #endif
  311. //DS refresh
  312. r = ws_DS_update(h);
  313. //MQTT refresh
  314. r = ws_MQTT_update(h);
  315. //upgrade refresh
  316. r = ws_upg_update(h);
  317. return r;
  318. }
  319. static void fn(mg_conn_t *c, int ev, void *ev_data)
  320. {
  321. ws_handle_t *wh=&wsHandle;
  322. switch(ev) {
  323. case MG_EV_OPEN:
  324. {
  325. }
  326. break;
  327. case MG_EV_CLOSE:
  328. {
  329. if (c->is_websocket )
  330. {
  331. if(wh->ws_cnt>0) {
  332. wh->ws_cnt--;
  333. }
  334. if(c==wh->c) {
  335. wh->c = NULL;
  336. }
  337. }
  338. log_i("ws upgrade del connection ID:%d\n", c->id);
  339. }
  340. break;
  341. case MG_EV_WS_OPEN:
  342. {
  343. wh->ws_cnt++;
  344. }
  345. break;
  346. case MG_EV_ACCEPT:
  347. {
  348. if (c->fn_data) {
  349. mg_tls_init(c, &wh->tls);
  350. }
  351. }
  352. break;
  353. case MG_EV_HTTP_MSG:
  354. {
  355. mg_http_msg_t *hm = (mg_http_msg_t *) ev_data;
  356. if (mg_match(hm->uri, mg_str("/websocket/PDU-WebSocket"), NULL)) {
  357. mg_ws_upgrade(c, hm, NULL);
  358. wh->c = c;
  359. log_i("ws upgrade new connection ID:%d\n", c->id);
  360. }
  361. else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
  362. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  363. }
  364. }
  365. break;
  366. case MG_EV_WS_MSG:
  367. {
  368. // struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  369. // mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  370. }
  371. break;
  372. }
  373. }
  374. static void timer_fn(void *arg)
  375. {
  376. ws_handle_t *h=(ws_handle_t*)arg;
  377. ws_update(h);
  378. }
  379. static void* ws_thread(void* arg)
  380. {
  381. int r=0;
  382. thread_handle_t *th=(thread_handle_t*)arg;
  383. ws_handle_t *h=(ws_handle_t*)th->arg;
  384. const char *ws_addr="ws://[::]:6785";
  385. const char *wss_addr="wss://[::]:6786";
  386. //mg_log_set(MG_LL_DEBUG);
  387. mg_mgr_init(&h->mgr); // Initialise event manager
  388. mg_http_listen(&h->mgr, ws_addr, fn, NULL); // Create HTTP listener
  389. mg_http_listen(&h->mgr, wss_addr, fn, (void*)1);
  390. #if (CHIP_TYPE == CHIP_T113s)
  391. h->tls.cert = mg_str(file_load2("/mnt/UDISK/app/cert/server.crt",0));
  392. h->tls.key = mg_str(file_load2("/mnt/UDISK/app/cert/server.key",0));
  393. #else
  394. h->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  395. h->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  396. #endif
  397. h->inited = 1;
  398. mg_timer_add(&h->mgr, 500, MG_TIMER_REPEAT, timer_fn, h);
  399. while(th->quit==0) {
  400. mg_mgr_poll(&h->mgr, 100); // Infinite event loop
  401. }
  402. h->inited = 0;
  403. mg_mgr_free(&h->mgr);
  404. pthread_exit(NULL);
  405. }
  406. int websocket_init(void)
  407. {
  408. ws_handle_t *wh=&wsHandle;
  409. memset(wh, 0, sizeof(ws_handle_t));
  410. thread_start(THREAD_ID_WS, ws_thread, wh);
  411. return 0;
  412. }
  413. pwrall_info_t *websocket_get_pwrall(void)
  414. {
  415. return &wsHandle.pwrall;
  416. }