Просмотр исходного кода

Merge branch 'master' of http://jiecheng2024.tpddns.cn:3000/liyuezong/SmartPDCode

liyi лет назад: 2
Родитель
Сommit
9bdb2b87d0
7 измененных файлов с 328 добавлено и 419 удалено
  1. 7 139
      pro/src/appweb_handle.c
  2. 1 1
      pro/src/dflt.c
  3. 8 8
      pro/src/paras.c
  4. 10 12
      pro/src/paras.h
  5. 183 185
      pro/src/sys.c
  6. 118 69
      pro/src/websocket_handle.c
  7. 1 5
      pro/src/websocket_handle.h

+ 7 - 139
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)
     {
@@ -2479,12 +2358,10 @@ static void communicationManagement(void *conn)
         NetworkInfo_t* info = &devInfo->_network_info;
         int ipv=strstr(reqInfo.type, "listWkIPV4Query")?IP_V4:IP_V6;
 
-        if(ipv==IP_V4) {
-            sys_get_net(info, ipv);
-            //print_net("get", info);
+        sys_get_net(info, ipv);
+        //print_net("get", info);
 
-            ack_str = communication_net_ack_to_json(info);
-        }
+        ack_str = communication_net_ack_to_json(info);
     }
     else if(strcmp(reqInfo.type,"WkIPV4Save")==0 || strcmp(reqInfo.type,"WkIPV6Save")==0 )  //淇濆瓨鏁版嵁
     {
@@ -2492,7 +2369,6 @@ static void communicationManagement(void *conn)
         _NET_CommunicationRequestInfo *info2=&reqInfo._net_info;
         int ipv=strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6;
         
-        
         info->mode = atoi(info2->mode);
         if(info->mode==1) { //static
             info->mode = info->mode;
@@ -2505,16 +2381,9 @@ static void communicationManagement(void *conn)
         }
         
         ////////////////////////////////////////////////////////////////////////////////
-        if(info->mode==1 && ipv==IP_V4) {    //only proc static ipaddress
-            r = sys_set_net(info, ipv);
-            //print_net("set", info);
-            if(r==0) {
-                //paras_save();
-                ack_str = ctrl_sts_ack_to_json(); 
-            }
-        }
-        //////////////////////////////////////////////////////////////////////////////    
-                    
+        r = sys_set_net(info, ipv);
+        //print_net("set", info);
+        ack_str = ctrl_sts_ack_to_json(); 
     }
     //查询设置RS485参数
     else if(strcmp(reqInfo.type,"listRsQuery")==0)//modbus鍙傛暟鏌ヨ
@@ -3627,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;

+ 1 - 1
pro/src/dflt.c

@@ -2,7 +2,7 @@
 #include "cfg.h"
 
 
-AllParasInfo DFLT_PARAS={
+all_paras_t DFLT_PARAS={
 
     .dev = {
         .product_id         = PROD_ID,

+ 8 - 8
pro/src/paras.c

@@ -8,7 +8,7 @@
 #define LOG printf
 
 
-AllParasInfo allParas;
+all_paras_t allParas;
 ////////////////////////////////////////////////////////////
 static int file_length(char* path)
 {
@@ -72,7 +72,7 @@ static int file_remove(char *path)
     return remove(path);
 }
 
-static int do_check(AllParasInfo *paras)
+static int do_check(all_paras_t *paras)
 {
     int i,j;
     
@@ -119,7 +119,7 @@ int paras_save(void)
 {
     int r,flen;
     char path[PATH_MAX];
-    AllParasInfo *p;
+    all_paras_t *p;
     
     r = do_check(&allParas);
     if(r) {
@@ -150,7 +150,7 @@ int paras_reset(void)
 }
 
 
-AllParasInfo* paras_get(void)
+all_paras_t* paras_get(void)
 {
     return &allParas;
 }
@@ -159,9 +159,9 @@ AllParasInfo* paras_get(void)
 int paras_power_set(int saddr, GlobalPowerManger *pm)
 {
     int i,j;
-    RelayPowerInfo *rp=&allParas.relay.ch[saddr];
+    relay_t *rp=&allParas.relay.ch[saddr];
 
-    if(pm) rp->pwr[(int)pm->product_ch_id] = *pm;
+    //if(pm) rp->pwr[(int)pm->product_ch_id] = *pm;
     rp->inited = 1;
 
     return 0;
@@ -170,12 +170,12 @@ int paras_power_set(int saddr, GlobalPowerManger *pm)
 int paras_power_get(int addr, int saddr, GlobalPowerManger *pm)
 {
     int i,j;
-    RelayPowerInfo *rp=&allParas.relay.ch[saddr];
+    relay_t *rp=&allParas.relay.ch[saddr];
 
     if(rp->inited==0) {
         return -1;
     }
-    if(pm) rp->pwr[saddr] = *pm;
+    //if(pm) rp->pwr[saddr] = *pm;
 
     return 0;
 }

+ 10 - 12
pro/src/paras.h

@@ -9,34 +9,32 @@ extern "C" {
 
 typedef struct {
     char              inited;
-    GlobalPowerManger pwr[MAX_RELAY_CHN];
-}RelayPowerInfo;
+    //GlobalPowerManger pwr[MAX_RELAY_CHN];
+}relay_t;
 typedef struct {
-    RelayPowerInfo    ch[MAX_RELAY_CHN];
-}RelayParas;
+    relay_t           ch[MAX_RELAY_CHN];
+}relay_para_t;
 typedef struct {
     GlobalDeviceInfo  dev;
-    RelayParas        relay;
-}AllParasInfo;
+    relay_para_t      relay;
+}all_paras_t;
 typedef struct {
     char              bldtime[32];
     char              version[32];
-}AppInfomation;
+}app_info_t;
 
 
-extern AllParasInfo allParas;
-extern AllParasInfo DFLT_PARAS;
+extern all_paras_t allParas;
+extern all_paras_t DFLT_PARAS;
 
 
 int paras_load(void);
 int paras_save(void);
 int paras_reset(void);
-AllParasInfo* paras_get(void);
+all_paras_t* paras_get(void);
 int paras_power_set(int saddr, GlobalPowerManger *pm);
 int paras_power_get(int addr, int saddr, GlobalPowerManger *pm);
 
-
-
 #ifdef __cplusplus
 }
 #endif

+ 183 - 185
pro/src/sys.c

@@ -29,16 +29,22 @@
 //#define USE_API
 
 #define DFLT_NIC            "eth0"
-//#define FILE_INTERFACE    "/root/interfaces"
-#define FILE_INTERFACE      "/etc/network/interfaces" 
+//#define FILE_NET          "/root/interfaces"
+#define FILE_NET            "/etc/network/interfaces"
+#define FILE_DNS            "/etc/dnsmasq.conf"
 
 
+typedef struct {
+    char *buf;
+    int  len;
+}file_data_t;
+
 typedef struct {
 	char *pbuf;
     int  dlen;
     int  blen;
 }node_t;
-static int set_net(NetworkInfo_t *para);
+static int set_net(NetworkInfo_t *para, int ipv);
 
 
 static int node_free(node_t *nd)
@@ -99,6 +105,7 @@ static int exe_cmd(char *cmd, node_t *nd)
     
 	return 0;
 }
+
 ////////////////////////////////////////////////////////////////////
 int sys_reboot(void)
 {
@@ -133,7 +140,7 @@ int sys_set_factory(void)
     LOGD("___ %s\n", tmp);
     r = system(tmp);
     if(r==0) {
-        r = set_net(&DFLT_PARAS.dev._network_info);
+        r = set_net(&DFLT_PARAS.dev._network_info, IP_V4);
         if(r==0) {
             exit(0);
         }
@@ -423,126 +430,27 @@ finish:
 }
 static int set_dhcp(int on)
 {
-    return 0;
-}
-
-static int set_dns(int family, const char* iface, char* dns1, char *dns2)
-{
-    int fd=-1;
-    struct ifreq ifr;
-    
-#if 0 
-    fd = socket(family, SOCK_DGRAM, 0);
-    if (fd < 0) {
-        log_e("socket SOCK_DGRAM failed\n");
-        return -1;
+    if(on) {
+        system("udhcpc –i eth0 –s /usr/share/udhcpc/default.script");
     }
- 
-    memset(&ifr, 0, sizeof(ifr));
-    strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
- 
-    struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
-    addr->sin_family = family;
-    inet_pton(family, dns1, &addr->sin_addr);
-
-
-    if (ioctl(fd, SIOCSIFDNS, &ifr) < 0) {
-        log_e("socket SIOCSIFDNS failed\n");
-        close(fd);
-        return -1;
+    else {
+        system("killall udhcpc");
     }
-#endif
-    close(fd);
-    
     return 0;
 }
-static int get_dns(int family, const char* iface, char* dns1, char *dns2)
-{
-    return 0;
-}
-
-
 
 static int ip_check(char* addr)
 {
     int flag = IP_ERR;
-    int len = 0;
-    int num = 0;
-    int Count = 0;
-    int first = 0;
-    int i = 0,j=0;
- 
-    len = strlen(addr);
-    if (strchr(addr,'.') != NULL) {// 含 .的情况,判断是否是IPv4
-        for(i = 0;i <= len-1; i++) {
-            if(addr[i] >= '0' && addr[i] <= '9') {
-                num = num * 10 + addr[i] - '0';
-            }
-            else if(addr[i] == '.') {
-                if((i == 0)||(i == len-1)) {
-                    break;
-                }
- 
-                if(addr[i-1] < '0' || addr[i-1] > '9') {
-                    break;
-                }
- 
-                if(addr[i] == '.') {
-                    Count++;
-                    if(Count > 3) {
-                        break;
-                    }
-                }
- 
-                if(num > 255) {
-                    break;
-                }
- 
-                if(num != 0 && addr[first] == '0') {
-                    break;
-                }
-                first = i+1;
-                num = 0;
-            }
-            else {
-                break;
-            }
-        }
- 
-        if(Count == 3) {
-            flag = IP_V4;
-        }
-    }
-    else if (strchr(addr,':') != NULL) {// 含 :的情况,判断是否是IPv6
-        for(i = 0;i <= len-1; i++) {
-            if((addr[i] >= '0' && addr[i] <= '9')||(tolower(addr[i]) >= 'a' && tolower(addr[i]) <= 'f')) {
-                j++;
-                if (j > 4) {
-                    break;
-                }
-            }
-            else if(addr[i] == ':') {
-                if((i == 0)||(i == len-1)) {
-                    break;
-                }
-                if (j == 0) {
-                    break;
-                }
-                Count++;
-                if(Count > 7) {
-                    break;
-                }
-                j = 0;
-            }
-            else {
-                break;
-            }
-        }
- 
-        if((Count == 7)&&(j <= 4))
-        {
-            flag = IP_V6;
-        }
+    struct in_addr addr4;
+    struct in6_addr addr6;
+
+    if (inet_pton(AF_INET, addr, &addr4) == 1) {
+        flag = IP_V4;
+    } else if (inet_pton(AF_INET6, addr, &addr6) == 1) {
+        flag = IP_V6;
+    } else {
+        LOGE("____ wrong ip addr: %s\n", addr);
     }
     
     return flag;
@@ -586,23 +494,36 @@ static int file_save(char *path, void *buf, int len)
     return r;
 }
 
-static int file_load(char* path, void* buf, int len)
+static int file_load(char* path, file_data_t *fdat)
 {
-    int r;
+    int r,flen;
     FILE* fp;
 
+    flen = file_length(path);
+    if(flen<=0) {
+        LOGE("____ file_load, %s length is wrong\n", path);
+        return -1;
+    }
+
+    fdat->buf = (char*)malloc(flen+1);
+    if(!fdat->buf) {
+        LOGE("____ file_load, %s malloc %d failed\n", path, flen);
+        return -1;
+    }
+    fdat->len = flen;
+
     fp = fopen(path, "r");
     if (!fp) {
-        LOGE("file_load, fopen %s failed\n", path);
+        LOGE("file_load, fopen %s failed\n", path); free(fdat->buf);
         return -1;
     }
-    r = fread(buf, 1, len, fp);
-    if (r != len) {
-        LOGE("file_load %s failed, %d, %d\n", path, r, len);
+    r = fread(fdat->buf, 1, flen, fp);
+    if (r != flen) {
+        LOGE("file_load %s failed, %d, %d\n", path, r, flen); free(fdat->buf);
         r = -1;
     }
     else {
-        r = 0;
+        fdat->buf[flen] = 0; r = 0;
     }
     fclose(fp);
 
@@ -610,11 +531,10 @@ static int file_load(char* path, void* buf, int len)
 }
 
 typedef struct {
-    char lo[500];
-    char e0[500];
-    char e1[500];
-}net_t;
-static net_t sNet;
+    char lo[1000];
+    char e0[1000];
+    char e1[1000];
+}interface_t;
 typedef uint32_t u32;
 #define XLEN(a,b)  ((u32)(a)-(u32)(b))
 typedef struct {
@@ -677,12 +597,24 @@ static int get_nic(char *nbuf, int nlen, char *nic, char *fbuf, int flen)
 
     return -1;
 }
-static int get_ip(char *nbuf, char *token, char *ipaddr)
+static int get_ip(char *nbuf, char *token, char *ipaddr, int ipv)
 {
     int cnt=0;
     char *p,*p1,*p2;
+    char *pbuf=NULL;
+
+    if(ipv==IP_V4) {
+        pbuf = strstr(nbuf, "inet");
+    }
+    else {
+        pbuf = strstr(nbuf, "inet6");
+    }
+    if(pbuf==NULL) {
+        LOGE("___get_ip, not found inet or inet6\n");
+        return -1;
+    }
 
-    p = strstr(nbuf, token);
+    p = strstr(pbuf, token);
     if(p) {
         while(!isdigit(*p)) p++;
         p1 = p;
@@ -700,110 +632,179 @@ static int get_ip(char *nbuf, char *token, char *ipaddr)
     }
     return -1;
 }
+static int get_dns(char *nbuf, char *dns1, char *dns2)
+{
+    int cnt;
+    char *p1,*p2;
+    char *token="server=";
+
+    p1 = strstr(nbuf, token);
+    if(p1==NULL) {
+        dns1[0] = 0;
+        return -1;
+    }
 
-static int get_net(NetworkInfo_t *para)
+    p1 += strlen(token); cnt=0;
+    while(!isspace(*p1)) {
+        dns1[cnt++] = *p1;
+        p1++;
+    }
+    dns1[cnt] = 0;
+
+    p2 = strstr(p1, token);
+    if(p2==NULL) {
+        dns2[0] = 0;
+        return -1;
+    }
+
+    p2 += strlen(token); cnt=0;
+    while(!isspace(*p2)) {
+        dns2[cnt++] = *p2;
+        p2++;
+    }
+    dns2[cnt] = 0;
+
+    return 0;
+}
+static int set_dns(char *dns1, char *dns2)
 {
-    char *fpath=FILE_INTERFACE;
-    int xlen=10*1000;
-    net_t *pNet=&sNet;
+    int r;
+    char buf[4000];
+    
+    memset(buf, 0, sizeof(buf));
+    snprintf(buf, sizeof(buf), "interface=eth0\nserver=%s\nserver=%s\n", dns1, dns2);
+    r = file_save(FILE_DNS, buf, strlen(buf))+1;
+    if(r==0) {
+        r = system("/etc/init.d/dnsmasq restart");
+    }
 
-    int flen=file_length(fpath);
-    char *fbuf=malloc(xlen);
-    if(!fbuf) {
-        LOGE("___ malloc %d failed\n", flen);
+    return r;
+}
+static int get_net(NetworkInfo_t *para, int ipv)
+{
+    int r;
+    char *p=NULL;
+    interface_t intf;
+    file_data_t fdat;
+
+    r = file_load(FILE_NET, &fdat);
+    if(r) {
         return -1;
     }
-    file_load(fpath, fbuf, flen);
-    fbuf[flen] = 0;
     
-    get_nic(pNet->lo, sizeof(pNet->lo), "lo",   fbuf, flen);
-    get_nic(pNet->e0, sizeof(pNet->e0), "eth0", fbuf, flen);
-    get_nic(pNet->e1, sizeof(pNet->e1), "eth1", fbuf, flen);
+    memset(&intf, 0, sizeof(intf));
+    get_nic(intf.lo, sizeof(intf.lo), "lo",   fdat.buf, fdat.len);
+    get_nic(intf.e0, sizeof(intf.e0), "eth0", fdat.buf, fdat.len);
+    get_nic(intf.e1, sizeof(intf.e1), "eth1", fdat.buf, fdat.len);
 
-    if(strstr(pNet->e0, "dhcp")) {
+    p = strstr(intf.e0, (ipv==IP_V4)?"inet":"inet6");
+    if(p==NULL) {
+        LOGE("___get_net, inet/inet6 not found!\n");
+        free(fdat.buf); return -1;
+    }
+    
+    if(strstr(p, "dhcp")) {
         para->mode = 0;
     }
-    else if(strstr(pNet->e0, "static")) {
+    else if(strstr(p, "static")) {
         para->mode = 1;
+    } else {
+        LOGE("___get_net, dhcp/static not found!\n");
+        free(fdat.buf); return -1;
     }
-    
-    get_ip(pNet->e0, "address", para->ip_address);
-    get_ip(pNet->e0, "netmask", para->mask);
-    get_ip(pNet->e0, "gateway", para->gateway);
-    
-    free(fbuf);
+
+    get_ip(p, "address", para->ip_address, ipv);
+    get_ip(p, "netmask", para->mask, ipv);
+    get_ip(p, "gateway", para->gateway, ipv);
+    free(fdat.buf);
+
+    r = file_load(FILE_DNS, &fdat);
+    if(r) {
+        return -1;
+    }
+    get_dns(fdat.buf, para->dns1, para->dns2);
 
     return 0;
 }
 
-static int set_net(NetworkInfo_t *para)
+static int set_net(NetworkInfo_t *para, int ipv)
 {
-    int r;
+    int r,ipv2;
     char *p1,*p2,*p3,*p4;
-    char *fpath=FILE_INTERFACE;
-    int xlen=10*1000;
-    NetworkInfo_t para2;
-    net_t *pNet=&sNet;
+    int flen=100*1000;
+    NetworkInfo_t info4,info6,*info;
+
+    get_net(&info4, IP_V4);
+    get_net(&info6, IP_V6);
 
-    memset(&para2, 0, sizeof(para2));
-    get_net(&para2);
+    info = ((ipv==IP_V4)?&info4:&info6);
     
-    if(para->mode<2 && para->mode!=para2.mode) {
-        para2.mode = para->mode;
+    if(para->mode<2 && para->mode!=info->mode) {
+        info->mode = para->mode;
     }
     
-    if(ip_check(para->ip_address) && strcmp(para->ip_address, para2.ip_address)) {
-        strcpy(para2.ip_address, para->ip_address);
+    ipv2 = ip_check(para->ip_address);
+    if((ipv==ipv2) && strcmp(para->ip_address, info->ip_address)) {
+        strcpy(info->ip_address, para->ip_address);
     }
     
-    if(ip_check(para->mask) && strcmp(para->mask, para2.mask)) {
-        strcpy(para2.mask, para->mask);
+    ipv2 = ip_check(para->mask);
+    if((ipv==ipv2) && strcmp(para->mask, info->mask)) {
+        strcpy(info->mask, para->mask);
     }
 
-    if(ip_check(para->gateway) && strcmp(para->gateway, para2.gateway)) {
-        strcpy(para2.gateway, para->gateway);
+    ipv2 = ip_check(para->gateway);
+    if((ipv==ipv2) && strcmp(para->gateway, info->gateway)) {
+        strcpy(info->gateway, para->gateway);
     }
  
-    char *fbuf=malloc(xlen);
+    char *fbuf=malloc(flen);
     if(!fbuf) {
-        LOGE("___ malloc %d failed\n", xlen);
+        LOGE("___ malloc %d failed\n", flen);
         return -1;
     }
 
 #if 0
-    file_load(fpath, fbuf, flen);
-    fbuf[flen] = 0;
+    file_load(FILE_NET, fbuf);
 
     get_nic(pNet->lo, sizeof(pNet->lo), "lo",   fbuf, flen);
     get_nic(pNet->e1, sizeof(pNet->e1), "eth1", fbuf, flen);
 
     snprintf(pNet->e0, sizeof(pNet->e0), "auto eth0\niface eth0 inet %s\n  address %s\n  netmask %s\n  gateway %s\n\n", 
-                              para2.mode?"static":"dhcp", para2.ip_address, para2.mask, para2.gateway);
+                              info4.mode?"static":"dhcp", info4.ip_address, info4.mask, info4.gateway);
     strcpy(fbuf, pNet->lo);
     strcat(fbuf, pNet->e0);
     strcat(fbuf, pNet->e1);  
 #else
     char *lo="auto lo\niface lo inet loopback\n\n";
     char *eth1="auto eth1\niface eth1 inet dhcp\npre-up /etc/network/nfs_check\nwait-delay 5\nhostname $(hostname)\n";
-    char eth0[500];
+    char eth0[1000];
 
-    snprintf(eth0, sizeof(eth0), "auto eth0\niface eth0 inet %s\n  address %s\n  netmask %s\n  gateway %s\n\n", 
-             para2.mode?"static":"dhcp", para2.ip_address, para2.mask, para2.gateway);
+    snprintf(eth0, sizeof(eth0), "auto eth0\n"
+                                 "iface eth0 inet %s\n  address %s\n  netmask %s\n  gateway %s\n\n"
+                                 "iface eth0 inet6 %s\n  address %s\n  netmask %s\n  gateway %s\n\n",
+                                 (info4.mode==IP_STATIC)?"static":"dhcp", info4.ip_address, info4.mask, info4.gateway,
+                                 (info6.mode==IP_STATIC)?"static":"dhcp", info6.ip_address, info6.mask, info6.gateway);
 
     sprintf(fbuf, "%s%s%s", lo, eth0, eth1);
 #endif
     //LOGD("___interfaces___:\n%s", fbuf);
-    r = file_save(fpath, fbuf, strlen(fbuf));
-    free(fbuf);
+
+    r = file_save(FILE_NET, fbuf, strlen(fbuf));
     if(r==0) {
-        paras_get()->dev._network_info = para2;
+        system("ifdown eth0 && ifup eth0");
+    }
+    free(fbuf);
+    
+    r = set_dns(para->dns1, para->dns2);
+    if(r) {
+        LOGE("___ set_dns failed\n");
     }
 
     return r;
 }
 
 //////////////////////////////////////////////////////////////////////////////
-
 int sys_set_net(NetworkInfo_t *para, int ipVersion)
 {
     int r,ipv;
@@ -841,13 +842,13 @@ int sys_set_net(NetworkInfo_t *para, int ipVersion)
 
 #elif defined USE_FILE
     NetworkInfo_t para2;
-    r = get_net(&para2);
+    r = get_net(&para2, ipVersion);
     if(r==0) {
         if(memcmp(para, &para2, sizeof(NetworkInfo_t))==0) {
             LOGW("restart %s failed\n", DFLT_NIC);
             return -1;
         }
-        r = set_net(para);
+        r = set_net(para, ipVersion);
     }
     else {
         LOGE("get_net failed\n");
@@ -880,9 +881,6 @@ int sys_set_net(NetworkInfo_t *para, int ipVersion)
         }
     }
 #endif
-
-    system("ifdown eth0 && ifup eth0");
-    //system("重启dns服务器"); //目前dns服务未启动
     
     return r;
 }
@@ -895,7 +893,7 @@ int sys_get_net(NetworkInfo_t *para, int ipVersion)
     
 
 #elif defined USE_FILE
-    r = get_net(para);
+    r = get_net(para, ipVersion);
 #elif defined USE_API  
     r = get_ipaddr(family, DFLT_NIC, para->ip_address, para->mask);
     if(r) {

+ 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