websocket_handle.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "thread.h"
  8. #include "sqlite_handle.h"
  9. #include "json_handle.h"
  10. #include "cascade.h"
  11. #include "cfg.h"
  12. //#define USE_WS2
  13. #define WS_SEND_TLEN_MAX (1024*1024*10)
  14. #define WS_MAX 10
  15. #define USE_ONE_CONN
  16. typedef struct mg_mgr mg_mgr_t;
  17. typedef struct mg_connection mg_conn_t;
  18. typedef struct mg_ws_message mg_ws_msg_t;
  19. typedef struct mg_http_message mg_http_msg_t;
  20. typedef struct {
  21. mg_mgr_t mgr;
  22. char wpath[200];
  23. char root[200];
  24. mg_conn_t *c;
  25. int inited;
  26. int ws_cnt;
  27. }ws_handle_t;
  28. static ws_handle_t wsHandle;
  29. static int ws_is_online(ws_handle_t *wh)
  30. {
  31. #ifdef USE_ONE_CONN
  32. return (wh->c)?1:0;
  33. #else
  34. return (wh->ws_cnt>0)?1:0;
  35. #endif
  36. }
  37. static int ws_send(ws_handle_t *wh, void *data, int len, int isBinary)
  38. {
  39. if(!wh->c) {
  40. return -1;
  41. }
  42. #ifdef USE_WS2
  43. mg_ws_send2(wh->c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
  44. #else
  45. mg_ws_send(wh->c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  46. #endif
  47. return 0;
  48. }
  49. static int ws_broadcast(ws_handle_t *wh, void *data, int len, int isBinary)
  50. {
  51. int r=-1;
  52. mg_conn_t *c;
  53. for (c=wh->mgr.conns; c!=NULL; c=c->next) {
  54. #ifdef USE_WS2
  55. mg_ws_send2(c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
  56. #else
  57. mg_ws_send(c, data, len, isBinary?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  58. #endif
  59. r = 0;
  60. }
  61. return r;
  62. }
  63. static int ws_send_data(ws_handle_t *wh, void *data, int len)
  64. {
  65. #ifdef USE_ONE_CONN
  66. return ws_send(wh, data, len, 0);
  67. #else
  68. return ws_broadcast(wh, data, len, 0);
  69. #endif
  70. }
  71. static void read_ipaddr(ws_handle_t *h)
  72. {
  73. sprintf(h->wpath,"ws://%s:6785", getLocalIpAddress("eth0"));
  74. printf("____ ws path: %s\n", h->wpath);
  75. }
  76. static void web_update(ws_handle_t *wh)
  77. {
  78. GlobalSensorManger* _globalSensorMangerTemp;
  79. int ret = 0 ;
  80. GlobalSensorInfo _globalSensorInfo;
  81. struct tm* t ;
  82. struct timeval tv;
  83. struct timezone tz ;
  84. char* json_str = NULL ;
  85. GlobalPowerInfo _powerInfo;
  86. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  87. _OverAllPwrAckInfo allInfo; //设备电源信息
  88. _OverChnPwrAckInfo chInfo;
  89. GlobalDeviceManager* gdm= &__globalDeviceManage;
  90. GlobalDeviceManager* gdm2=&__globalDeviceManage2;
  91. GlobalDeviceManager* pdm=NULL;
  92. if(ws_is_online(wh))
  93. {
  94. //overall info
  95. if(gdm->_globalDevInfo.product_pwr_type==SmartPDU_Tree_AC_Tree ||
  96. gdm->_globalDevInfo.product_pwr_type==SmartPDU_Tree_AC_One ||
  97. gdm->_globalDevInfo.product_pwr_type==SmartPDU_Tree_AC_One_B)
  98. {
  99. int nRet_L1 = -1;
  100. int nRet_L2 = -1;
  101. int nRet_L3 = -1;
  102. _OverAllPwrAckInfo l1, l2, l3;
  103. if (cur_dev_addr == 0)
  104. {
  105. pthread_mutex_lock(&gdm->_power_update);
  106. nRet_L1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
  107. nRet_L2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
  108. nRet_L3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
  109. pthread_mutex_unlock(&gdm->_power_update);
  110. }
  111. else
  112. {
  113. cascade_lock();
  114. nRet_L1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
  115. nRet_L2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
  116. nRet_L3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
  117. cascade_unlock();
  118. }
  119. if (0 == nRet_L1 && 0 == nRet_L2 && 0 == nRet_L3)
  120. {
  121. json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
  122. ws_send_data(wh, json_str, strlen(json_str));
  123. cJSON_free((void *)json_str);
  124. }
  125. }
  126. else
  127. {
  128. int nRetTotal = -1;
  129. if(cur_dev_addr==0) {
  130. pthread_mutex_lock(&gdm->_power_update);
  131. nRetTotal=dev_search_latest_power_statistic_info(gdm, &allInfo);
  132. pthread_mutex_unlock(&gdm->_power_update);
  133. }
  134. else {
  135. cascade_lock();
  136. nRetTotal=dev_search_latest_power_statistic_info(gdm2, &allInfo);
  137. cascade_unlock();
  138. }
  139. if (0==nRetTotal)
  140. {
  141. json_str = ws_over_status_ack_to_json(0, &allInfo);
  142. ws_send_data(wh, json_str, strlen(json_str));
  143. cJSON_free(json_str);
  144. }
  145. }
  146. //channel info
  147. {
  148. INIT_LIST_HEAD(&chInfo.list);
  149. int nRetCh = -1;
  150. if(cur_dev_addr==0) {
  151. pthread_mutex_lock(&gdm->_power_update);
  152. nRetCh=dev_search_latest_power_All_info(gdm,&chInfo, cur_dev_addr);
  153. pthread_mutex_unlock(&gdm->_power_update);
  154. pdm = gdm;
  155. }
  156. else {
  157. cascade_lock();
  158. nRetCh=dev_search_latest_power_All_info(gdm2,&chInfo, cur_dev_addr);
  159. cascade_unlock();
  160. pdm = gdm2;
  161. }
  162. if (0 == nRetCh)
  163. {
  164. json_str = ws_chn_status_ack_to_json(1, &chInfo, pdm);
  165. ws_send_data(wh, json_str, strlen(json_str));
  166. cJSON_free((void *)json_str);
  167. }
  168. _OverChnPwrAckInfo *node, *next;
  169. list_for_each_entry_safe(node, next, &chInfo.list, list)
  170. {
  171. list_del(&node->list);
  172. free(node);
  173. }
  174. }
  175. //sensor info
  176. {
  177. //pthread_mutex_lock(&__globalDeviceManage._sensor_update);
  178. json_str = ws_sensor_status_ack_to_json(&gdm->_globalSensorManger);
  179. // pthread_mutex_unlock(&__globalDeviceManage._sensor_update);
  180. ws_send_data(wh, json_str, strlen(json_str));
  181. cJSON_free(json_str);
  182. }
  183. // // breaker info
  184. {
  185. if(cur_dev_addr==0) {
  186. json_str = breaker_status_ack_to_json(gdm,0);
  187. }else
  188. {
  189. json_str = breaker_status_ack_to_json(gdm2,cur_dev_addr);
  190. }
  191. ws_send_data(wh, json_str, strlen(json_str));
  192. cJSON_free(json_str);
  193. }
  194. }
  195. }
  196. static void fn(mg_conn_t *c, int ev, void *ev_data)
  197. {
  198. ws_handle_t *wh=&wsHandle;
  199. switch(ev) {
  200. case MG_EV_OPEN:
  201. {
  202. }
  203. break;
  204. case MG_EV_CLOSE:
  205. {
  206. if (c->is_websocket )
  207. {
  208. if(wh->ws_cnt>0) {
  209. wh->ws_cnt--;
  210. }
  211. if(c==wh->c) {
  212. wh->c = NULL;
  213. }
  214. }
  215. log_i("ws upgrade del connection ID:%d\n", c->id);
  216. }
  217. break;
  218. case MG_EV_WS_OPEN:
  219. {
  220. wh->ws_cnt++;
  221. }
  222. break;
  223. case MG_EV_HTTP_MSG:
  224. {
  225. mg_http_msg_t *hm = (mg_http_msg_t *) ev_data;
  226. if (mg_match(hm->uri, mg_str("/websocket/PDU-WebSocket"), NULL)) {
  227. mg_ws_upgrade(c, hm, NULL);
  228. wh->c = c;
  229. log_i("ws upgrade new connection ID:%d\n", c->id);
  230. }
  231. else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
  232. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  233. }
  234. }
  235. break;
  236. case MG_EV_WS_MSG:
  237. {
  238. // struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  239. // mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
  240. }
  241. break;
  242. }
  243. }
  244. void* websocket_thread(void* arg)
  245. {
  246. int r;
  247. thread_handle_t *h=(thread_handle_t*)arg;
  248. ws_handle_t *wh=(ws_handle_t*)h->arg;
  249. const char *listen_addr="ws://[::]:6785";
  250. mg_log_set(MG_LL_DEBUG);
  251. mg_mgr_init(&wh->mgr); // Initialise event manager
  252. mg_http_listen(&wh->mgr, listen_addr, fn, NULL); // Create HTTP listener
  253. wh->inited = 1;
  254. while(h->quit==0) {
  255. if (wh->c)
  256. {
  257. web_update(wh);
  258. }
  259. mg_mgr_poll(&wh->mgr, 1000); // Infinite event loop
  260. usleep(500000);
  261. }
  262. wh->inited = 0;
  263. mg_mgr_free(&wh->mgr);
  264. pthread_exit(NULL);
  265. }
  266. int websocket_init(void)
  267. {
  268. ws_handle_t *wh=&wsHandle;
  269. memset(wh, 0, sizeof(ws_handle_t));
  270. //read_ipaddr(wh);
  271. thread_start(THREAD_ID_WS, websocket_thread, wh, 30*MB, 0);
  272. return 0;
  273. }