websocket_handle.c 8.3 KB

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