websocket_handle.c 8.2 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. //#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(gdm,&chInfo, cur_dev_addr);
  151. pthread_mutex_unlock(&gdm->_power_update);
  152. pdm = gdm;
  153. }
  154. else {
  155. cascade_lock();
  156. nRetCh=dev_search_latest_power_All_info(gdm2,&chInfo, cur_dev_addr);
  157. cascade_unlock();
  158. pdm = gdm2;
  159. }
  160. if (0 == nRetCh)
  161. {
  162. json_str = ws_chn_status_ack_to_json(1, &chInfo, pdm);
  163. ws_send_data(wh, json_str, strlen(json_str));
  164. cJSON_free((void *)json_str);
  165. }
  166. _OverChnPwrAckInfo *node, *next;
  167. list_for_each_entry_safe(node, next, &chInfo.list, list)
  168. {
  169. list_del(&node->list);
  170. free(node);
  171. }
  172. }
  173. //sensor info
  174. {
  175. //pthread_mutex_lock(&__globalDeviceManage._sensor_update);
  176. json_str = ws_sensor_status_ack_to_json(&gdm->_globalSensorManger);
  177. // pthread_mutex_unlock(&__globalDeviceManage._sensor_update);
  178. ws_send_data(wh, json_str, strlen(json_str));
  179. cJSON_free(json_str);
  180. }
  181. #if 0
  182. // // breaker info
  183. {
  184. if(cur_dev_addr==0) {
  185. json_str = breaker_status_ack_to_json(gdm,0);
  186. }else
  187. {
  188. json_str = breaker_status_ack_to_json(gdm2,cur_dev_addr);
  189. }
  190. ws_send_data(wh, json_str, strlen(json_str));
  191. cJSON_free(json_str);
  192. }
  193. #endif
  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_mgr_init(&wh->mgr); // Initialise event manager
  251. mg_http_listen(&wh->mgr, listen_addr, fn, NULL); // Create HTTP listener
  252. wh->inited = 1;
  253. while(h->quit==0) {
  254. if (wh->c)
  255. {
  256. web_update(wh);
  257. }
  258. mg_mgr_poll(&wh->mgr, 1000); // Infinite event loop
  259. usleep(500000);
  260. }
  261. wh->inited = 0;
  262. mg_mgr_free(&wh->mgr);
  263. pthread_exit(NULL);
  264. }
  265. int websocket_init(void)
  266. {
  267. ws_handle_t *wh=&wsHandle;
  268. memset(wh, 0, sizeof(ws_handle_t));
  269. //read_ipaddr(wh);
  270. thread_start(THREAD_ID_WS, websocket_thread, wh, 30*MB, 0);
  271. return 0;
  272. }