websocket_handle.c 7.9 KB

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