瀏覽代碼

因加锁后导致长时间挂起,将web内容刷新移到websocket线程,防止并行操作

guohui 2 年之前
父節點
當前提交
a69c1967ad
共有 3 個文件被更改,包括 120 次插入 和 197 次删除
  1. 1 123
      pro/src/appweb_handle.c
  2. 118 69
      pro/src/websocket_handle.c
  3. 1 5
      pro/src/websocket_handle.h

+ 1 - 123
pro/src/appweb_handle.c

@@ -200,126 +200,6 @@ static void *web_thread(void *arg)
     pthread_exit(NULL);
 }
 #endif
-/// @brief 定时更新前端
-/// @param arg 
-/// @return 
-void* update_thread(void* arg)
-{
-    thread_handle_t *h=(thread_handle_t*)arg;
-    GlobalSensorManger* _globalSensorMangerTemp;
-    int ret = 0 ;
-    GlobalSensorInfo _globalSensorInfo;
-    struct tm* t ;
-    struct timeval tv;
-    struct timezone tz ;
-    char* json_str = NULL ;
-     GlobalPowerInfo _powerInfo;
-    GlobalPowerManger* _globalPowerMangerTemp = NULL ;
-    _OverAllPwrAckInfo allInfo; //设备电源信息
-    _OverChnPwrAckInfo chInfo;
-    GlobalDeviceManager* gdm= &__globalDeviceManage;
-    GlobalDeviceManager* gdm2=&__globalDeviceManage2;
-	
-    //采集线程
-    while (h->quit==0)
-    {
-        if(websocket_is_online())
-        {
-           // pthread_mutex_lock(&Websocketlock); 
-            //推送总体信息
-            //if(gdm->view_page==1)
-            { 
-                if(gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_Tree ||
-                    gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_One)
-                {
-                    //三相
-                    _OverAllPwrAckInfo l1,l2,l3;
-                    if (cur_dev_addr == 0)
-                    {
-                        pthread_mutex_lock(&__globalDeviceManage._power_update); 
-                        dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
-                        dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
-                        dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
-                        pthread_mutex_unlock(&__globalDeviceManage._power_update);
-                    }
-                    else
-                    {
-                        cascade_lock();
-                        dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
-                        dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
-                        dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
-                        cascade_unlock();
-                    }
-                    json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
-
-                    websocket_broadcast(json_str, strlen(json_str));
-                    cJSON_free((void*)json_str);
-                }
-                else
-                {
-                    if(cur_dev_addr==0) {
-                        pthread_mutex_lock(&__globalDeviceManage._power_update); 
-                        dev_search_latest_power_statistic_info(gdm, &allInfo);
-                        pthread_mutex_unlock(&__globalDeviceManage._power_update);
-                    }
-                    else {
-                        cascade_lock();
-                        dev_search_latest_power_statistic_info(gdm2, &allInfo);
-                        cascade_unlock();  
-                    }
-                    json_str = ws_over_status_ack_to_json(0, &allInfo);
-
-                    websocket_broadcast(json_str, strlen(json_str));
-                    cJSON_free(json_str);
-                }                
-            }
-            //推送通道信息
-            //else if(gdm->view_page==2)
-            {
-                INIT_LIST_HEAD(&chInfo.list);
-
-                if(cur_dev_addr==0) {
-                    pthread_mutex_lock(&__globalDeviceManage._power_update); 
-                    dev_search_latest_power_All_info(gdm,&chInfo);
-                    pthread_mutex_unlock(&__globalDeviceManage._power_update);
-                }
-                else {
-                    cascade_lock();
-                    dev_search_latest_power_All_info(gdm2,&chInfo);
-                    cascade_unlock();
-                }
-
-                json_str = ws_chn_status_ack_to_json(1,&chInfo);
-                websocket_broadcast(json_str, strlen(json_str));
-                 
-                //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-                cJSON_free((void*)json_str);
-                //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-                _OverChnPwrAckInfo *node,*next;
-                list_for_each_entry_safe(node, next, &chInfo.list, list)
-                {
-                    list_del(&node->list);
-                    free(node);
-                } 
-            }
-            //else if (gdm->view_page==3)
-            {
-                json_str = ws_sensor_status_ack_to_json(&__globalDeviceManage._globalSensorManger);
-                websocket_broadcast(json_str, strlen(json_str));
-                cJSON_free((void*)json_str);
-            }            
-            //usleep(400*1000);
-           // pthread_mutex_unlock(&Websocketlock); 
-            //log_i("ws upload.");
-        }
-
-        usleep(500*1000);
-    }
-
-    websocket_free() ;
-    pthread_exit(NULL);
-}
-
 
 
 /// @brief 系统信息
@@ -345,13 +225,12 @@ static void systemConfiguration(void* conn)
     if (g_token != NULL && !CheckLoginStatus(g_token))
     {
         ack_str = log_AthFailedinfo_to_json();
-        http_reply(conn, 401, ack_str);
     }
     else
     {
         ack_str = json_to_system_config_info();
-        http_reply(conn, 200, ack_str);
     }
+    http_reply(conn, 200, ack_str);
 
     if (g_token != NULL)
     {
@@ -3617,7 +3496,6 @@ static int webSetCallback(void)
 
 int appweb_init(void)
 {
-    thread_start(THREAD_ID_UPDATE, update_thread, &__globalDeviceManage, 20*MB, 0);
     thread_start(THREAD_ID_WEB, web_thread, &__globalDeviceManage, 16*MB, 0);
     
     return 0;

+ 118 - 69
pro/src/websocket_handle.c

@@ -5,6 +5,9 @@
 #include "common.h"
 #include "sys.h"
 #include "thread.h"
+#include "sqlite_handle.h"
+#include "json_handle.h"
+#include "cascade.h"
 
 #define WS_MAX    10
 
@@ -21,18 +24,127 @@ typedef struct {
 
     int       inited;
     int       ws_cnt;
-    pthread_mutex_t mutex;
 }ws_handle_t;
 static ws_handle_t wsHandle;
 
+static int ws_broadcast(ws_handle_t *wh, void *data, int len) 
+{
+    mg_conn_t *c;
+
+    for (c=wh->mgr.conns; c!=NULL; c=c->next) {
+        mg_ws_send(c, data, len, WEBSOCKET_OP_TEXT);
+    }
+
+    return 0;
+}
+
 
 static void read_ipaddr(ws_handle_t *h)
 { 
   sprintf(h->wpath,"ws://%s:6785", getLocalIpAddress("eth0"));
   printf("____ ws path: %s\n", h->wpath);
 }
-
-
+static void web_update(ws_handle_t *wh)
+{
+    GlobalSensorManger* _globalSensorMangerTemp;
+    int ret = 0 ;
+    GlobalSensorInfo _globalSensorInfo;
+    struct tm* t ;
+    struct timeval tv;
+    struct timezone tz ;
+    char* json_str = NULL ;
+     GlobalPowerInfo _powerInfo;
+    GlobalPowerManger* _globalPowerMangerTemp = NULL ;
+    _OverAllPwrAckInfo allInfo; //设备电源信息
+    _OverChnPwrAckInfo chInfo;
+    GlobalDeviceManager* gdm= &__globalDeviceManage;
+    GlobalDeviceManager* gdm2=&__globalDeviceManage2;
+	
+    if(wh->ws_cnt>0)
+    {
+        //if(gdm->view_page==1)
+        { 
+            if(gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_Tree ||
+                gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_One)
+            {
+                //三相
+                _OverAllPwrAckInfo l1,l2,l3;
+                if (cur_dev_addr == 0)
+                {
+                    pthread_mutex_lock(&__globalDeviceManage._power_update); 
+                    dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
+                    dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
+                    dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
+                    pthread_mutex_unlock(&__globalDeviceManage._power_update);
+                }
+                else
+                {
+                    cascade_lock();
+                    dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
+                    dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
+                    dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
+                    cascade_unlock();
+                }
+                json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
+
+                ws_broadcast(wh, json_str, strlen(json_str));
+                cJSON_free((void*)json_str);
+            }
+            else
+            {
+                if(cur_dev_addr==0) {
+                    pthread_mutex_lock(&__globalDeviceManage._power_update); 
+                    dev_search_latest_power_statistic_info(gdm, &allInfo);
+                    pthread_mutex_unlock(&__globalDeviceManage._power_update);
+                }
+                else {
+                    cascade_lock();
+                    dev_search_latest_power_statistic_info(gdm2, &allInfo);
+                    cascade_unlock();  
+                }
+                json_str = ws_over_status_ack_to_json(0, &allInfo);
+
+                ws_broadcast(wh, json_str, strlen(json_str));
+                cJSON_free(json_str);
+            }                
+        }
+        //推送通道信息
+        //else if(gdm->view_page==2)
+        {
+            INIT_LIST_HEAD(&chInfo.list);
+
+            if(cur_dev_addr==0) {
+                pthread_mutex_lock(&__globalDeviceManage._power_update); 
+                dev_search_latest_power_All_info(gdm,&chInfo);
+                pthread_mutex_unlock(&__globalDeviceManage._power_update);
+            }
+            else {
+                cascade_lock();
+                dev_search_latest_power_All_info(gdm2,&chInfo);
+                cascade_unlock();
+            }
+
+            json_str = ws_chn_status_ack_to_json(1,&chInfo);
+            ws_broadcast(wh, json_str, strlen(json_str));
+              
+            //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+            cJSON_free((void*)json_str);
+            //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+            _OverChnPwrAckInfo *node,*next;
+            list_for_each_entry_safe(node, next, &chInfo.list, list)
+            {
+                list_del(&node->list);
+                free(node);
+            } 
+        }
+        //else if (gdm->view_page==3)
+        {
+            json_str = ws_sensor_status_ack_to_json(&__globalDeviceManage._globalSensorManger);
+            ws_broadcast(wh, json_str, strlen(json_str));
+            cJSON_free(json_str);
+        }
+    }
+}
 static void fn(mg_conn_t *c, int ev, void *ev_data) 
 {
   ws_handle_t *wh=&wsHandle;
@@ -94,9 +206,9 @@ void* websocket_thread(void* arg)
     wh->inited = 1;
 
     while(h->quit==0) {
-        pthread_mutex_lock(&wh->mutex);
-        mg_mgr_poll(&wh->mgr, 200);                    // Infinite event loop
-        pthread_mutex_unlock(&wh->mutex);
+        mg_mgr_poll(&wh->mgr, 1000);                    // Infinite event loop
+
+        web_update(wh);
     }
     wh->inited = 0;
     mg_mgr_free(&wh->mgr);
@@ -109,11 +221,6 @@ int websocket_init(void)
 {
     ws_handle_t *wh=&wsHandle;
 
-    if (pthread_mutex_init(&wh->mutex, NULL) != 0) {  
-        log_e("websocket mutex init failed!\n");
-        return -1;  
-    }  
-    
     memset(wh, 0, sizeof(ws_handle_t));
     read_ipaddr(wh);
 
@@ -122,63 +229,5 @@ int websocket_init(void)
 }
 
 
-int websocket_send(void *conn, void *data, int len) 
-{
-    int i;
-    ws_handle_t *wh=&wsHandle;
-
-    if(!wh->inited || !conn) {
-        return -1;
-    }
-    pthread_mutex_lock(&wh->mutex);
-    mg_ws_send(conn, data, len, WEBSOCKET_OP_TEXT);
-    pthread_mutex_unlock(&wh->mutex);
-    return 0;
-}
-
-int websocket_broadcast(void *data, int len) 
-{
-    int i;
-    mg_conn_t *c;
-    ws_handle_t *wh=&wsHandle;
-
-    if(!wh->inited) {
-        return -1;
-    }
-
-    pthread_mutex_lock(&wh->mutex);
-    for (c=wh->mgr.conns; c!=NULL; c=c->next) {
-        mg_ws_send(c, data, len, WEBSOCKET_OP_TEXT);
-    }
-    pthread_mutex_unlock(&wh->mutex);
-
-    return 0;
-}
-
-int websocket_is_online(void) 
-{
-    ws_handle_t *wh=&wsHandle;
-    return (wh->ws_cnt>0)?1:0;
-}
-
-
-int websocket_free(void) 
-{
-    int r;
-    ws_handle_t *wh=&wsHandle;
-
-    if (!wh->inited) {
-      return -1;
-    }
-
-    thread_stop(THREAD_ID_WS);
-    pthread_mutex_destroy(&wh->mutex);
-    
-    return 0;
-}
-
-
-
-
 
   

+ 1 - 5
pro/src/websocket_handle.h

@@ -9,11 +9,7 @@ typedef struct {
     int     len;
 }ws_data_t;
 
-
 int websocket_init(void);
-int websocket_send(void *conn, void *data, int len);
-int websocket_broadcast(void *data, int len);
-int websocket_is_online(void);
-int websocket_free(void) ;
+
 #endif