Bladeren bron

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

liyi 1 jaar geleden
bovenliggende
commit
b2685d07fc

+ 15 - 4
output/push2/push.bat

@@ -27,9 +27,20 @@ SET FILES=pdutar logo.png smartPDU.ini
 CD %CURDIR%
 
 ::copy files
-MKDIR  %TMPDIR%
+MKDIR  %TMPDIR% 2 > NUL
 FOR %%x IN (%FILES%) DO (
-COPY /Y %%x  %TMPDIR%\
+    XCOPY /DQY %%x  %TMPDIR%\  > NUL
+)
+
+::test ip
+ECHO ping %IP% ...
+PING -n 3 -w 500 %IP%  >NUL
+IF %ERRORLEVEL% NEQ 0 (
+    IF %BAT% EQU 0 (
+    ECHO %IP% can't reached!
+        TIMEOUT /T 2 > NUL
+    )
+    EXIT /B 1
 )
 
 ::send files
@@ -37,14 +48,14 @@ ECHO send %TMPDIR% to device ...
 ECHO  y  | %PSCP% -r %TMPDIR% %USR%@%IP%:%DEVDIR%
 IF %ERRORLEVEL% NEQ 0 (
     ECHO send failed!
-    EXIT /B 1
+    EXIT /B 2
 )
 
 ECHO send finished, release files now ...
 ECHO  y  | %PLINK%  -ssh -t  -pw %PWD% %USR%@%IP% -m %FBAT%
 IF %ERRORLEVEL% NEQ 0 (
     ECHO upgrade failed!
-    EXIT /B 2
+    EXIT /B 3
 )
 
 IF %BAT% EQU 0 (

BIN
output/t113i/root/run/app/pdu_dat.db


BIN
output/t113i/root/run/app/pdu_dat_bak.db


+ 19 - 14
pro/src/app.c

@@ -519,8 +519,8 @@ static void* power_thread(void* arg)
                             {
                                 _pTreeACPowerMangerTemp->product_log_time = now;
                                 /* code */
-                                // 插入数据库
-                                if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3, _globalPowerInfo.samp_time) != 0)
+                                strcpy(_pTreeACPowerMangerTemp->samp_time, _globalPowerInfo.samp_time);
+                                if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3) != 0)
                                 {
                                     log_d("ph_ch%d insert data error.\n", _pTreeACPowerMangerTemp->product_ch_id);
                                 }
@@ -731,8 +731,8 @@ static void* power_thread(void* arg)
                         {
                             _pTreeACPowerMangerTemp->product_log_time = now;
                             /* code */
-                            // 插入数据库
-                            if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3,_globalPowerInfo.samp_time) != 0)
+                            strcpy(_pTreeACPowerMangerTemp->samp_time, _globalPowerInfo.samp_time);
+                            if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3) != 0)
                             {
                                 log_d("ph_ch%d insert data error.\n", _pTreeACPowerMangerTemp->product_ch_id);
                             }
@@ -1029,8 +1029,8 @@ static void* power_thread(void* arg)
                 {
                     _pTreeACPowerMangerTemp->product_log_time = now;
                     /* code */
-                    // 插入数据库
-                    if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3, _globalPowerInfo.samp_time) != 0)
+                    strcpy(_pTreeACPowerMangerTemp->samp_time, _globalPowerInfo.samp_time);
+                    if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3) != 0)
                     {
                         log_d("ph_ch%d insert data error.\n", _pTreeACPowerMangerTemp->product_ch_id);
                     }
@@ -1084,8 +1084,8 @@ static void* power_thread(void* arg)
                 {
                     _pTreeACPowerMangerTemp->product_log_time = now;
                     /* code */
-                    // 插入数据库
-                    if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3, _globalPowerInfo.samp_time) != 0)
+                    strcpy(_pTreeACPowerMangerTemp->samp_time, _globalPowerInfo.samp_time);
+                    if (dev_insert_t_ac_power_info(_globalDeviceManager->db, _pTreeACPowerMangerTemp, &lastIndex3) != 0)
                     {
                         log_d("ph_ch%d insert data error.\n", _pTreeACPowerMangerTemp->product_ch_id);
                     }
@@ -1780,16 +1780,20 @@ static void* ntp_thread(void *arg)
     thread_handle_t *h=(thread_handle_t*)arg;
     NTPManager *pntp=&_globalNTPManager;
 
-    #define NTP_TIMEOUT   5
-    #define NTP_INTERVAL_TIME  300
+    #define NTP_TIMEOUT         5
+    #define NTP_INTERVAL_TIME   300
     time1 = time(NULL);
     while (h->quit == 0)
     {
         if (pntp->mode > 0)
         {
-            if (ntp_sync_flag || ((time1 - time2) >= NTP_INTERVAL_TIME))
+            printf("___ntp_sync_flag: %d, interval: %d\n",  ntp_sync_flag, abs(time1-time2));
+            if (ntp_sync_flag || (abs(time1 - time2) >= NTP_INTERVAL_TIME))
             {
-                pc = createNTPClient(pntp->ntpaddress, pntp->port, NTP_TIMEOUT);
+                if(pc==NULL) {
+                    pc = createNTPClient(pntp->ntpaddress, pntp->port, NTP_TIMEOUT);
+                }
+
                 if (pc)
                 {
                     if (getCurrentTimeFromNTP(pc, &utc_time) == 0)
@@ -1837,6 +1841,7 @@ int ntp_config(NTPManager *nm)
         system("/etc/init.d/S49ntp stop");
     }
 #else
+    //system("/etc/init.d/S49ntp stop");
     if(nm->mode==1) {
         ntp_sync_flag = 1;
     }
@@ -1854,8 +1859,8 @@ static int ntp_init(void)
     r = dev_get_ntp_info(dm->db,  pntp);
     if (r) {
         pntp->mode = 1;
-        strcpy(pntp->ntpaddress, "119.28.183.184");
-        strcpy(pntp->ntpName, "百度NTP");
+        strcpy(pntp->ntpaddress, "cn.pool.ntp.org");
+        strcpy(pntp->ntpName, "cn-pool");
         pntp->port = 123;
         pntp->product_id = dm->_globalDevInfo.product.id;
         time_t t = time(NULL);

+ 100 - 76
pro/src/appweb_handle.c

@@ -150,57 +150,91 @@ static void httpAddCallback(char *uri, http_fn_t f)
         h->cnt++;
     }
 }
-static int power_to_csv(char *path, status_data_t *data, int ftype)
+
+static time_t gettime(char *timestr)
 {
-    int i=0;
-    FILE *fp=NULL;
-    _OverAllPwrAckInfo *info=((ftype==FILE_POWER_ALL)?(&data->all):(&data->chn));
-    
-    fp = fopen(path, "wt");
-    if(!fp) {
+    int r;
+    struct tm tm={0};
+    //2024-10-29 05:19:09
+    r = sscanf(timestr, "%d-%d-%d %d:%d:%d", &tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
+    if(r<6) {
+        return -1;
+    }
+    tm.tm_year -= 1900;
+    tm.tm_mon -= 1;
+    return mktime(&tm);
+}
+static int gettime2(char *buf, char *tok, char *timestr)
+{
+    char *p=strstr(buf, tok);
+    if(!p) {
         return -1;
     }
+    p += strlen(tok);
+    memcpy(timestr, p, 10); timestr[10] = ' '; 
+    memcpy(timestr+11, p+13, 8); timestr[19] = 0;
+    return 0;
+}
+static int get_range(time_t *from, time_t *to, char *start, char *end, int max_days)
+{
+    time_t cur=gettime(start);
+    time_t pre=gettime(end);
+    time_t sss=mktime0(),secs=3600*24*max_days;
+
+    if(cur<=sss) {
+        cur = sss;
+    }
 
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total voltage time,");
-        fprintf(fp, "%s,", info->_total_voltage.xAxis[i]);
-    } fprintf(fp, "\n");
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total voltage value,");
-        fprintf(fp, "%.8f,", info->_total_voltage.ydata[i]);
-    } fprintf(fp, "\n\n");
-
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total current time,");
-        fprintf(fp, "%s,", info->_total_current.xAxis[i]);
-    } fprintf(fp, "\n");
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total current value,");
-        fprintf(fp, "%.8f,", info->_total_current.ydata[i]);
-    } fprintf(fp, "\n\n");
-
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total power time,");
-        fprintf(fp, "%s,", info->_total_power.xAxis[i]);
-    } fprintf(fp, "\n");
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total power value,");
-        fprintf(fp, "%.8f,", info->_total_power.ydata[i]);
-    } fprintf(fp, "\n\n");
-
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total consum time,");
-        fprintf(fp, "%s,", info->_total_consumption.xAxis[i]);
-    } fprintf(fp, "\n");
-    for(i=0; i<TOTAL_COUNT; i++) {
-        if(i==0) fprintf(fp, "total consum value,");
-        fprintf(fp, "%.8f,", info->_total_consumption.ydata[i]);
-    } fprintf(fp, "\n\n");
-
-    fclose(fp);
+    if(pre<=sss) {
+        pre = sss;
+    }
+
+    if(abs(cur-pre)>=secs) {
+        if(cur>pre) {
+            *from = cur-secs;
+            *to = cur;
+        }
+        else {
+            *from = pre-secs;
+            *to = pre;
+        }
+    }
+    else {
+        if(cur>pre) {
+            *from = pre;
+            *to = cur;
+        }
+        else {
+            *from = cur;
+            *to = pre;
+        }
+    }
+    
     return 0;
 }
-static int file_io(void *conn, int ftype, int io)
+
+
+
+
+static int power_export(char *path, char *start, char *end)
+{
+    int r,type=0;
+    time_t from,to;
+    char tmp1[100],tmp2[100];
+    GlobalDeviceManager *dm=&__globalDeviceManage;
+
+    get_range(&from, &to, start, end, 7);
+    get_time_str2(tmp1,sizeof(tmp1),from);
+    get_time_str2(tmp2,sizeof(tmp2),to);
+    sprintf(path, "/tmp/power_%s_%s.csv", tmp1, tmp2);
+
+    if(dm->_globalDevInfo.product.pwr_type>=SmartPDU_Tree_AC_Tree) {
+        type = 1;
+    }
+    return dev_power_xport(type, from, to, path);
+}
+
+static int file_io(void *conn, int ftype, int io, char *buf)
 {
     int r,flag=0;
     char tmp[200],path[200];
@@ -208,11 +242,11 @@ static int file_io(void *conn, int ftype, int io)
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
 
-    sys_get_path(tmp, "config.ini");
-    sys_get_path(path, INI_FILE_NAME);
     switch(ftype) {
         case FILE_SYS_CFG:
         {
+            sys_get_path(tmp, "config.ini");
+            sys_get_path(path, INI_FILE_NAME);
             if(io==FILE_IMPORT) {
                 r = upload_file(conn, tmp);
                 if(r==0) {
@@ -263,26 +297,17 @@ static int file_io(void *conn, int ftype, int io)
         break;
 
         case FILE_POWER_ALL:
-        {
-            if(io==FILE_EXPORT) {
-                char *ftmp="overall.csv";
-                r = power_to_csv(ftmp, &wh->stt, ftype);
-                if(r==0) {
-                    download_file(conn, ftmp);
-                    file_remove(ftmp);
-                }
-            }
-        }
-        break;
-
         case FILE_POWER_CHN:
         {
             if(io==FILE_EXPORT) {
-                char *ftmp="channel.csv";
-                r = power_to_csv(ftmp, &wh->stt, ftype);
+                char start[100],end[100];
+
+                gettime2(buf, "startTime=", start);
+                gettime2(buf, "endTime=", end);
+                r = power_export(path, start, end);
                 if(r==0) {
-                    download_file(conn, ftmp);
-                    file_remove(ftmp);
+                    download_file(conn, path);
+                    file_remove(path);
                 }
             }
         }
@@ -650,7 +675,7 @@ static void overallStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            file_io(conn, FILE_POWER_ALL, FILE_EXPORT);
+            file_io(conn, FILE_POWER_ALL, FILE_EXPORT, hm->uri.buf);
         }
         else {
             http_reply(conn, 400, NULL);
@@ -1254,7 +1279,7 @@ static void channelStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            file_io(conn, FILE_POWER_CHN, FILE_EXPORT);
+            file_io(conn, FILE_POWER_CHN, FILE_EXPORT, hm->uri.buf);
         }
         return;     
     }
@@ -5583,22 +5608,22 @@ static void equipmentMintenanceUpload(void *conn)
     char path[200],tmp[60];
     
     if(strstr(hm->uri.buf, "uploadType=gjsj")) {        //firmware upload
-        file_io(conn, FILE_HOST_FW, FILE_IMPORT);
+        file_io(conn, FILE_HOST_FW, FILE_IMPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "uploadType=shfw")) {   //logo upload
-        file_io(conn, FILE_LOGO, FILE_IMPORT);
+        file_io(conn, FILE_LOGO, FILE_IMPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "uploadType=0")) {
-        file_io(conn, FILE_ALARM_CFG, FILE_IMPORT);
+        file_io(conn, FILE_ALARM_CFG, FILE_IMPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "uploadType=1")) {
-        file_io(conn, FILE_SERV_CFG, FILE_IMPORT);
+        file_io(conn, FILE_SERV_CFG, FILE_IMPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "uploadType=2")) {
-        file_io(conn, FILE_SENSOR_CFG, FILE_IMPORT);
+        file_io(conn, FILE_SENSOR_CFG, FILE_IMPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "uploadType=3")) {
-        file_io(conn, FILE_SYS_CFG, FILE_IMPORT);
+        file_io(conn, FILE_SYS_CFG, FILE_IMPORT, NULL);
     }
 
     ack_str = ctrl_sts_ack_to_json();
@@ -5614,18 +5639,17 @@ static void equipmentMintenanceExport(void *conn)
     struct mg_http_message *hm=s->ev_data;
     
     if(strstr(hm->uri.buf, "export=0")) {
-        file_io(conn, FILE_ALARM_CFG, FILE_EXPORT);
+        file_io(conn, FILE_ALARM_CFG, FILE_EXPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "export=1")) {
-        file_io(conn, FILE_SERV_CFG, FILE_EXPORT);
+        file_io(conn, FILE_SERV_CFG, FILE_EXPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "export=2")) {
-        file_io(conn, FILE_SENSOR_CFG, FILE_EXPORT);
+        file_io(conn, FILE_SENSOR_CFG, FILE_EXPORT, NULL);
     }
     else if(strstr(hm->uri.buf, "export=3")) {
-        file_io(conn, FILE_SYS_CFG, FILE_EXPORT);
+        file_io(conn, FILE_SYS_CFG, FILE_EXPORT, NULL);
     }
-    
 }
 
 static int webSetCallback(void)

+ 22 - 2
pro/src/common.c

@@ -338,6 +338,18 @@ time_t mktime2(struct tm *t, int zone)
 
     return res;
 }
+time_t mktime0(void)
+{
+    struct tm t;
+
+    t.tm_year = 70;
+    t.tm_mon = 0;
+    t.tm_mday = 1;
+    t.tm_hour = 0;
+    t.tm_min = 0;
+    t.tm_sec = 0;
+    return mktime(&t);
+}
 
 
 int set_time(struct tm *t, int zone)
@@ -450,12 +462,15 @@ int set_zone(int zone)
 
 void print_time(char *s, struct tm *tm)
 {
-    printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+    printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
 }
 
 void print_utc(char *s, time_t t)
 {
     struct tm *tm=gmtime(&t);
+
+    tm->tm_year += 1900;
+    tm->tm_mon += 1;
     print_time(s, tm);
 }
 
@@ -504,9 +519,14 @@ int set_utctime_zone(time_t t, int zone)
 int get_time_str(char *s, int len, time_t t)
 {
     struct tm *tm=localtime(&t);
-    return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d.000",tm->tm_year,tm->tm_mon,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
+    return snprintf(s, len, "%04d-%02d-%02d %02d:%02d:%02d.000",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
 }
 
+int get_time_str2(char *s, int len, time_t t)
+{
+    struct tm *tm=localtime(&t);
+    return snprintf(s, len, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
+}
 
 char* getUserNamebyToken(char* Token)
 {

+ 5 - 1
pro/src/common.h

@@ -471,8 +471,10 @@ typedef struct
     int product_ch_addr;
     int product_ph_outputType;//三相控制方式1,三项2单项
     int product_ph_outputStatus;//该相是否输出,1输出2不输出
+
+    char                samp_time[64];                           //采集时间
     PowerInfo           _PowerInfo;                              //存储最新的数据(兼容三项输入)
-    GlobalOverManager*  global_over_manager                        ;//超限管理
+    GlobalOverManager*  global_over_manager;                     //超限管理
     PowerWarningInfo    _PowerWarninginfo;                       //存储报警数据(兼容三项输入)  
     time_t              product_log_time;                           //存储最新时间   
     struct list_head    list_Tree_AC;                              //存储三项数据指针    
@@ -1082,8 +1084,10 @@ int set_utctime(time_t t);
 int set_time_zone(struct tm *t, int zone);
 int set_utctime_zone(time_t t, int zone);
 
+time_t mktime0(void);
 time_t mktime2(struct tm *t, int zone);
 int get_time_str(char *s, int len, time_t t);
+int get_time_str2(char *s, int len, time_t t);
 void print_time(char *s, struct tm *tm);
 void print_utc(char *s, time_t t);
 

+ 15 - 0
pro/src/file.c

@@ -28,6 +28,21 @@ int file_length(char* path)
     return st.st_size;
 }
 
+
+int file_exist(char* path)
+{
+    int r;
+    struct stat st;
+
+    r = stat(path, &st);
+    if (r) {
+        return 0;
+    }
+
+    return 1;
+}
+
+
 int file_save(char *path, void *buf, int len)
 {
     int r;

+ 1 - 0
pro/src/file.h

@@ -12,6 +12,7 @@ typedef struct {
 }buf_t;
 
 int file_length(char* path);
+int file_exist(char* path);
 int file_save(char *path, void *buf, int len);
 int file_load(char* path, buf_t *buf);
 char* file_load2(char* path);

+ 39 - 20
pro/src/mail.c

@@ -37,7 +37,7 @@ typedef struct {
 static mail_handle_t maHandle;
 
 
-static char send_buff[1024] = {0};
+static char send_buff[4096];
 struct upload_status 
 {
     size_t bytes_read;
@@ -64,14 +64,14 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
  
     return 0;
 }
-static void fill_send_buff(const char *to,const char *from,const char *subject,const char *body)
+static void fill_send_buff(char *dst_buf, const char *to,const char *from,const char *subject,const char *body)
 {
   time_t timer;//time_t就是long int 类型
   struct tm *tblock;
   timer = time(NULL);
   tblock = localtime(&timer);
-  memset(send_buff,0,1024);
-  sprintf(send_buff,"Subject: %s\r\n"
+  
+  sprintf(dst_buf,"Subject: %s\r\n"
                     "From: %s\r\n"
                     "To: %s\r\n"
                     "Date: %s\r\n"
@@ -90,7 +90,11 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     struct upload_status upload_ctx = { 0 };
     struct curl_slist *recipients = NULL;
     mail_recv_t *recv=(mail_recv_t*)addr;
-    curl_mime *mm=NULL;
+    curl_mime *mm=NULL,*alt=NULL;
+    curl_mimepart *part=NULL;
+    struct curl_slist *slist=NULL;
+    struct curl_slist *headers=NULL;
+    const char **cpp;
     
     lock_s_hold(LOCK_ID_MAIL);
 
@@ -126,14 +130,6 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
     r = curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
     r = curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
     
-    if(attach) {
-        //mm = curl_mime_init(curl);
-        if(mm) {
-            //curl_mime_addpart(mm);
-            //curl_mime_free(mm);
-        }
-    }
-
     if(multi) {
         for(int i=0; i<MAIL_RECV_MAX; i++) {
             if(recv[i].use && strlen(recv[i].account)>0) {
@@ -150,7 +146,29 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
         strcat(to_buff,sendto);
         strcat(to_buff,";");
     }
-    fill_send_buff(to_buff,send->account,sub,body);
+    fill_send_buff(send_buff,to_buff,send->account,sub,body);
+
+    if(attach && strlen(attach)>0) {
+        mm = curl_mime_init(curl);
+        alt = curl_mime_init(curl);
+        
+        headers = curl_slist_append(headers, send_buff);
+        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+
+        part = curl_mime_addpart(alt);
+        curl_mime_data(part, sub, CURL_ZERO_TERMINATED);
+
+        part = curl_mime_addpart(mm);
+        curl_mime_subparts(part, alt);
+        curl_mime_type(part, "multipart/alternative");
+        slist = curl_slist_append(NULL, "Content-Disposition: inline");
+        curl_mime_headers(part, slist, 1);
+
+        part = curl_mime_addpart(mm);
+        //curl_mime_name(part, "file");
+        curl_mime_filedata(part, attach);
+        curl_easy_setopt(curl, CURLOPT_MIMEPOST, mm);
+    }
 
     r = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
     r = curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
@@ -164,6 +182,7 @@ static int send_mail(mail_send_t *send, void *addr, char *sub, char *body, char
         LOGD("send mail ok!\n");
     }
     curl_slist_free_all(recipients);
+    curl_mime_free(mm);
 
 out:
     if(curl) curl_easy_cleanup(curl);
@@ -251,9 +270,9 @@ static int post_data(char *sub, char *body, char *attach)
     mail_pkt_t pkt;
     mail_handle_t *h=&maHandle;
 
-    snprintf(pkt.sub, sizeof(pkt.sub), "%s", sub);
-    snprintf(pkt.body, sizeof(pkt.body), "%s", body);
-    snprintf(pkt.body, sizeof(pkt.body), "%s", body);
+    snprintf(pkt.sub, sizeof(pkt.sub), "%s", sub?sub:"");
+    snprintf(pkt.body, sizeof(pkt.body), "%s", body?body:"");
+    snprintf(pkt.attach, sizeof(pkt.attach), "%s", attach?attach:"");
     r = xlist_append(h->list, 0, &pkt, sizeof(pkt));
 #endif
 
@@ -268,10 +287,10 @@ int mail_send(char *sub, char *body, char *attach)
 #ifdef USE_MAIL
     mail_handle_t *h=&maHandle;
 
-    if(!h->inited || !sub || !body || !h->info || !h->info->send.mode) {
+    if(!h->inited || !h->info || !h->info->send.mode) {
         return -1;
     }
-    r = send_mail(&h->info->send, h->info->recv, sub, body, attach, 1);
+    r = send_mail(&h->info->send, h->info->recv, sub?sub:"", body?body:"", attach?attach:"", 1);
 #endif
 
     return r;
@@ -284,7 +303,7 @@ int mail_post(char *sub, char *body, char *attach)
 #ifdef USE_MAIL
     mail_handle_t *h=&maHandle;
 
-    if(!h->inited || !sub || !body || !h->info || !h->info->send.mode) {
+    if(!h->inited || !h->info || !h->info->send.mode) {
         return -1;
     }
     r = post_data(sub, body, attach);

+ 28 - 16
pro/src/ntpclient.c

@@ -23,8 +23,8 @@ static int sendNTPRequest(NTPClient* client) {
 }  
   
 // 接收NTP响应  
-static int receiveNTPResponse(NTPClient* client) {  
-    socklen_t len = sizeof(client->server_addr);  
+static int receiveNTPResponse(NTPClient* client) {
+    socklen_t len = sizeof(client->server_addr);
     ssize_t receivedBytes = recvfrom(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,  
                               &client->server_addr, &len);  
     if (receivedBytes < 0) {  
@@ -64,14 +64,12 @@ static int get_addr(ipaddr_t *addr, struct addrinfo *info)
     }
     return 0;
 }
-
-
 static void *tmp_thread(void *arg)
 {
     ipaddr_t ipa;
     int r,fd=-1,err;
     struct addrinfo hints;
-    struct addrinfo *res=NULL,*rp;
+    struct addrinfo *rp;
     NTPClient *pc=(NTPClient*)arg;
     char tmp[32];
 
@@ -85,14 +83,14 @@ static void *tmp_thread(void *arg)
 
     sprintf(tmp, "%d", pc->port);
     printf("___getaddrinfo in\n");
-    err = getaddrinfo(pc->server, tmp, &hints, &res);
+    err = getaddrinfo(pc->server, tmp, &hints, &pc->res);
     if(err){
         printf("___getaddrinfo from %s failed, %s\n", pc->server, gai_strerror(err));
         pthread_exit(NULL);
     }
     printf("___getaddrinfo out\n");
 
-    for(rp=res; rp; rp=rp->ai_next){
+    for(rp=pc->res; rp; rp=rp->ai_next){
         get_addr(&ipa, rp);
         printf("socket to %s:%d ", ipa.ip, ipa.port);
 
@@ -105,8 +103,12 @@ static void *tmp_thread(void *arg)
         }
         printf("failed!\n");
     }
-    freeaddrinfo(res);
-    pc->flag = 1;
+    if(fd>=0) {
+        pc->flag = 1;
+    }
+    else {
+        pc->flag = -1;
+    }
 
     pthread_exit(NULL);
 }
@@ -120,22 +122,32 @@ NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec) {
     pthread_t tid;
     client->server = (char*)ntp_server;
     client->port = port;
+    client->res = NULL;
     client->flag = 0;
     pthread_create(&tid, NULL, tmp_thread, client);
     for(int i=0; i<timeout_sec; i++) {
-        if(client->flag==1) {
+        if(client->flag) {
             break;
         }
         sleep(1);
     }
-    if(client->flag==0) {
-        printf("___getaddrinfo timeout, cancel thread %lu\n", (unsigned long)tid);
-        pthread_cancel(tid);//SIGTERM
-        pthread_join(tid, NULL);
-        printf("___cancel ok!\n");
-
+    if(client->flag!=1) {
+        if(client->flag==0) {
+            printf("___getaddrinfo timeout, cancel thread %lu\n", (unsigned long)tid);
+            pthread_cancel(tid);//SIGTERM
+            pthread_join(tid, NULL);
+            printf("___cancel ok!\n");
+        }
+        if(client->res) freeaddrinfo(client->res);
         free(client); return NULL;
     }
+    if(client->res) freeaddrinfo(client->res);
+
+    struct timeval tv;
+    tv.tv_sec = timeout_sec;
+    tv.tv_usec = 0;
+    setsockopt(client->sockfd, SOL_SOCKET, SO_RCVTIMEO, (const void*)&tv, sizeof(tv));
+
     printf("___createNTPClient ok!\n");
   
     return client;  

+ 2 - 1
pro/src/ntpclient.h

@@ -17,7 +17,8 @@
 typedef struct NTPClient_s {  
     int sockfd;  
     struct sockaddr server_addr;  
-    char ntp_packet[NTP_PACKET_SIZE]; 
+    char ntp_packet[NTP_PACKET_SIZE];
+    struct addrinfo *res;
     int  flag; 
     char *server;
     int  port;

+ 296 - 94
pro/src/sqlite_handle.c

@@ -1725,7 +1725,7 @@ int dev_update_t_ac_power_manage_over_info(sqlite3 *db,GlobalTreeACManager* _glo
 /// @param product_id 
 /// @param _globalPowerManger 
 /// @return 
-int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnInfo,int* nIndex,char* pTimeData)
+int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnInfo,int* nIndex)
 {
     int res ;
     char select_sql[1024] = {0};
@@ -1753,7 +1753,7 @@ int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnI
                             pInfo->_PowerInfo.freq,
                             pInfo->_PowerInfo.consumption,
                             pInfo->_PowerInfo.factor,
-                            pTimeData,
+                            pInfo->samp_time,
                             table
                             );
     }
@@ -1770,7 +1770,7 @@ int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnI
                             pInfo->_PowerInfo.freq,
                             pInfo->_PowerInfo.consumption,
                             pInfo->_PowerInfo.factor,
-                            pTimeData
+                            pInfo->samp_time
                             );
     }
     // if(_globalPowerChnInfo->product_ch_id==0)
@@ -4645,7 +4645,7 @@ int dev_sql_set_overwrite(int flag)
 
 
 ///////////////////////////////////////////////////////////////////////////////
-
+#include "xlist.h"
 const char *tabNamePool[TAB_ID_MAX]={
     "Table_NTPManage",
     "Table_Userinfo",
@@ -4666,96 +4666,225 @@ const char *tabNamePool[TAB_ID_MAX]={
     "Table_AlarmManage",
     "Table_PowerInfo",
     "Table_PhasePowerInfo",
-
 };
-
-
-static int db_requery(sqlite3 *db, int tab_id, time_t from, time_t to, handle_t l)
+static int db_exec(sqlite3 *db, int tab_id, int io_type, time_t from, time_t to, handle_t l)
 {
     int r=-1;
-    
     char *p,temp[1024];
     char time_s[64],time_e[64];
     sqlite3_stmt *stmt=NULL;
-    GlobalPowerInfo pwr;
-    GlobalTreeACManager pwr3;
 
     get_time_str(time_s, sizeof(time_s), from);
     get_time_str(time_e, sizeof(time_e), to);
 
-    if(tab_id==TAB_ID_POWER_INFO) {
-        snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE product_samp_time BETWEEN '%s' AND '%s';", tabNamePool[tab_id], time_s, time_e);
+    switch(tab_id) {
+
+        case TAB_ID_POWER:
+        {
+            //
+
+        }
+        break;
+
+        case TAB_ID_POWER3:
+        {
+            //
+        }
+        break;
+
+        case TAB_ID_POWER_INFO:
+        {
+            GlobalPowerInfo tmp;
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE product_samp_time BETWEEN '%s' AND '%s';", tabNamePool[tab_id], time_s, time_e);
+            
+            r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
+            if (r != SQLITE_OK) {
+                log_e("___db_requery TAB_ID_POWER_INFO, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
         
-        r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
-        if (r != SQLITE_OK) {
-            log_e("___db_requery 1, sqlite3_prepare_v2 failed\n");
-            return -1;
+            while (sqlite3_step(stmt) == SQLITE_ROW) {  //column start from 0
+                tmp.product_id = sqlite3_column_int(stmt, 1);
+                tmp.product_ch_id =  sqlite3_column_int(stmt, 2);
+                tmp.product_status =  sqlite3_column_int(stmt, 3);
+                tmp._power_info.voltage = sqlite3_column_double(stmt, 4);
+                tmp._power_info.current = sqlite3_column_double(stmt, 5);
+                tmp._power_info.power = sqlite3_column_double(stmt, 6);
+                tmp._power_info.freq = sqlite3_column_double(stmt, 7);
+                tmp._power_info.consumption = sqlite3_column_double(stmt, 8);
+                tmp._power_info.factor = sqlite3_column_double(stmt, 9);
+                strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 0));
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+            }
+            sqlite3_finalize(stmt);
         }
-    
-        while (sqlite3_step(stmt) == SQLITE_ROW) {
-            pwr.product_id = sqlite3_column_int(stmt, 1);
-            pwr.product_ch_id =  sqlite3_column_int(stmt, 2);
-            pwr.product_status =  sqlite3_column_int(stmt, 3);
-            pwr._power_info.voltage = sqlite3_column_double(stmt, 4);
-            pwr._power_info.current = sqlite3_column_double(stmt, 5);
-            pwr._power_info.power = sqlite3_column_double(stmt, 6);
-            pwr._power_info.freq = sqlite3_column_double(stmt, 7);
-            pwr._power_info.consumption = sqlite3_column_double(stmt, 8);
-            pwr._power_info.factor = sqlite3_column_double(stmt, 9);
-
-            xlist_append(l, 0, &pwr, sizeof(pwr));
+        break;
+
+        case TAB_ID_POWER3_INFO:
+        {
+            GlobalTreeACManager tmp;
+
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (product_ph_samp_time BETWEEN '%s' AND '%s') ORDER BY product_index ASC;", tabNamePool[tab_id], time_s, time_e);
+            r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
+            if (r != SQLITE_OK) {
+                log_e("___db_requery TAB_ID_POWER3_INFO, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
+
+            while (sqlite3_step(stmt)==SQLITE_ROW) {
+                tmp.product_id = sqlite3_column_int(stmt, 1);
+                tmp.product_ph_type =  sqlite3_column_int(stmt, 2);
+                tmp.product_ch_id =  sqlite3_column_int(stmt, 3);
+                tmp._PowerInfo.voltage = sqlite3_column_double(stmt, 4);
+                tmp._PowerInfo.current = sqlite3_column_double(stmt, 5);
+                tmp._PowerInfo.power = sqlite3_column_double(stmt, 6);
+                tmp._PowerInfo.freq = sqlite3_column_double(stmt, 7);
+                tmp._PowerInfo.consumption = sqlite3_column_double(stmt, 8);
+                tmp._PowerInfo.factor = sqlite3_column_double(stmt, 9);
+                strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 10));
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+            }
+            sqlite3_finalize(stmt);
         }
-        sqlite3_finalize(stmt);
-    }
-    else if(tab_id==TAB_ID_POWER3_INFO) {
-        snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (product_ph_samp_time BETWEEN '%s' AND '%s') ORDER BY product_index DESC;", tabNamePool[tab_id], time_s, time_e);
+        break;
 
-        r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
-        if (r != SQLITE_OK) {
-            log_e("___db_requery 2, sqlite3_prepare_v2 failed\n");
-            return -1;
+        case TAB_ID_LOG_INFO:
+        {
+            //
         }
+        break;
 
-        while (sqlite3_step(stmt) == SQLITE_ROW) {
-            pwr3.product_id = sqlite3_column_int(stmt, 1);
-            pwr3.product_ph_type =  sqlite3_column_int(stmt, 2);
-            pwr3.product_ch_id =  sqlite3_column_int(stmt, 3);
-            pwr3._PowerInfo.voltage = sqlite3_column_double(stmt, 4);
-            pwr3._PowerInfo.current = sqlite3_column_double(stmt, 5);
-            pwr3._PowerInfo.power = sqlite3_column_double(stmt, 6);
-            pwr3._PowerInfo.freq = sqlite3_column_double(stmt, 7);
-            pwr3._PowerInfo.consumption = sqlite3_column_double(stmt, 8);
-            pwr3._PowerInfo.factor = sqlite3_column_double(stmt, 9);
-
-            xlist_append(l, 0, &pwr3, sizeof(pwr3));
+        case TAB_ID_ALARM_INFO:
+        {
+            //
         }
-        sqlite3_finalize(stmt);
-    }
-    else if(tab_id==TAB_ID_LOG_INFO) {
-        //
-    }
-    else if(tab_id==TAB_ID_ALARM_INFO) {
-        //
-    }
-    else if(tab_id==TAB_ID_SENSOR) {
-        //
-    }
-    else if(tab_id==TAB_ID_SENSOR_INFO) {
-        //sprintf(select_sql,"SELECT* FROM Table_SensorInfo WHERE (sensor_id=%d) AND (sensor_samp_time BETWEEN '%s' AND '%s') ORDER BY sensor_samp_time DESC LIMIT %d OFFSET %d;",
-        //snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (product_ph_samp_time BETWEEN '%s' AND '%s') ORDER BY product_index DESC;", tabNamePool[tab_id], time_s, time_e);
+        break;
 
-    }
-    else {
+        case TAB_ID_SENSOR:
+        {
+            GlobalSensorManger tmp;
+
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s;", tabNamePool[tab_id]);
+            r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
+            if (r != SQLITE_OK) {
+                log_e("___db_requery TAB_ID_SENSOR, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
+
+            while (sqlite3_step(stmt) == SQLITE_ROW) {
+                tmp.sensor_id = sqlite3_column_int(stmt, 1);
+                tmp.product_id = sqlite3_column_int(stmt, 2);
+                tmp.sensor_type = sqlite3_column_int(stmt, 3);
+                tmp.sensor_interface_type = sqlite3_column_int(stmt, 4);
+
+                p = (char*)sqlite3_column_text(stmt, 5);
+                tmp.sensor_name[0] = 0;
+                if(p) strcpy(tmp.sensor_name, p);
+
+                p = (char*)sqlite3_column_text(stmt, 6);
+                tmp.sensor_port_name[0] = 0;
+                if(p) strcpy(tmp.sensor_port_name, p);
+
+                tmp.sensor_baud = 0;
+                if(tmp.sensor_interface_type==1) {
+                    tmp.sensor_baud =  sqlite3_column_int(stmt, 7);
+                }
+                tmp.sensor_addr = sqlite3_column_int(stmt, 8);
+                
+                tmp.sensor_status = 1;
+                tmp.sensor_val_count = sqlite3_column_int(stmt, 10);
+
+                tmp.over_manager->sensor_val1_upper_enabled = sqlite3_column_int(stmt, 11);
+                tmp.over_manager->sensor_val1_upper_threshold = sqlite3_column_double(stmt, 12);
+                tmp.over_manager->sensor_val1_upper_threshold_ctrl = sqlite3_column_int(stmt, 13);
+
+                p = (char*)sqlite3_column_text(stmt, 14);
+                tmp.over_manager->sensor_val1_upper_threshold_ctrl_para[0] = 0;
+                if(p) strcpy(tmp.over_manager->sensor_val1_upper_threshold_ctrl_para, p);
+
+                tmp.over_manager->sensor_val1_lower_enabled = sqlite3_column_int(stmt, 15);
+                tmp.over_manager->sensor_val1_lower_threshold = sqlite3_column_double(stmt, 16);
+                tmp.over_manager->sensor_val1_lower_threshold_ctrl = sqlite3_column_int(stmt, 17);
+
+                p = (char*)sqlite3_column_text(stmt, 18);
+                tmp.over_manager->sensor_val1_lower_threshold_ctrl_para[0] = 0;
+                if(p) strcpy(tmp.over_manager->sensor_val1_lower_threshold_ctrl_para, p);
+
+                tmp.over_manager->sensor_val2_upper_enabled = sqlite3_column_int(stmt, 19);
+                tmp.over_manager->sensor_val2_upper_threshold = sqlite3_column_double(stmt, 20);
+                tmp.over_manager->sensor_val2_upper_threshold_ctrl = sqlite3_column_int(stmt, 21);
+
+                p = (char*)sqlite3_column_text(stmt, 22);
+                tmp.over_manager->sensor_val2_upper_threshold_ctrl_para[0] = 0;
+                if(p) strcpy(tmp.over_manager->sensor_val2_upper_threshold_ctrl_para, p);
+
+                tmp.over_manager->sensor_val2_lower_enabled = sqlite3_column_int(stmt, 23);
+                tmp.over_manager->sensor_val2_lower_threshold = sqlite3_column_double(stmt, 24);
+                tmp.over_manager->sensor_val2_lower_threshold_ctrl = sqlite3_column_int(stmt, 25);
+
+                p = (char*)sqlite3_column_text(stmt, 26);
+                tmp.over_manager->sensor_val2_lower_threshold_ctrl_para[0] = 0;
+                if(p) strcpy(tmp.over_manager->sensor_val2_lower_threshold_ctrl_para, p);
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+        
+        case TAB_ID_SENSOR_INFO:
+        {
+            GlobalSensorInfo tmp;
+
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (sensor_samp_time BETWEEN '%s' AND '%s') ORDER BY sensor_samp_time DESC;", tabNamePool[tab_id], time_s, time_e);
+            r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
+            if (r != SQLITE_OK) {
+                log_e("___db_requery 2, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
+
+            while (sqlite3_step(stmt) == SQLITE_ROW) {
+                p = (char*)sqlite3_column_text(stmt, 2);
+                tmp.samp_time[0] = 0;
+                if(p) strcpy(tmp.samp_time, p);
+
+                tmp.product_id = sqlite3_column_int(stmt, 3);
+                tmp.sensor_id = sqlite3_column_int(stmt, 4);
+                tmp.val1 = sqlite3_column_double(stmt, 5);
+                tmp.val2 = sqlite3_column_double(stmt, 6);
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+
+        default:
         return -1;
     }
 
     return 0;
 }
-static int db_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t to, void *data)
+static node_t* get_node2(handle_t l, int tp)
+{
+    int r;
+    node_t *nd=NULL;
+    list_node_t *ln=NULL;
+    for(int i=0;i<xlist_size(l); i++) {
+        r = xlist_get_node(l, &ln, i);
+        if(r==0 && ln->data.tp==tp) {
+            nd = &ln->data;
+        }
+    }
+    return nd;
+}
+int dev_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t to, void *data)
 {
     int i,r=-1;
 
-    if(tab_id<TAB_ID_NTP || tab_id>=TAB_ID_MAX || io_type<0 || io_type>=IO_MAX || (io_type==IO_IMPORT && !data)) {
+    if(tab_id<TAB_ID_NTP || tab_id>=TAB_ID_MAX || io_type<0 || io_type>=IO_MAX) {
         return -1;
     }
 
@@ -4853,11 +4982,12 @@ static int db_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t
         case TAB_ID_POWER:
         {
             if(io_type==IO_IMPORT) {
-                //r = dev_update_t_ac_power_manage_info(db, 0, (NTPManager*)data);
-                //dev_update_t_ac_power_manage_over_info(db, 0, (NTPManager*)data);
+                //r = dev_update_power_manage_genera_info(db, 0, (GlobalPowerManger*)data);
+                //r = dev_update_power_manage_genera_info_addr(db, 0, (GlobalPowerManger*)data);
+                //r = dev_update_power_manage_over_info(db, 0, (GlobalPowerManger*)data);
             }
             else {
-                //r = dev_get_power_manage_info(db, (NTPManager*)data);
+                //r = dev_get_power_manage_info(db, (GlobalPowerManger*)data);
             }
         }
         break;
@@ -4865,11 +4995,11 @@ static int db_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t
         case TAB_ID_POWER3:
         {
             if(io_type==IO_IMPORT) {
-                //r = dev_update_t_ac_power_manage_info(db, 0, (NTPManager*)data);
-                //dev_update_t_ac_power_manage_over_info(db, 0, (NTPManager*)data);
+                //r = dev_update_t_ac_power_manage_info(db, 0, (GlobalTreeACManager*)data);
+                //r = dev_update_t_ac_power_manage_over_info(db, 0, (GlobalTreeACManager*)data);
             }
             else {
-                //r = dev_get_t_ac_power_manage_info(db, (NTPManager*)data);
+                //r = dev_get_t_ac_power_manage_info(db, 0, (GlobalTreeACManager*)data);
             }
         }
         break;
@@ -4916,7 +5046,7 @@ static int db_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t
         {
             if(io_type==IO_EXPORT) {
                 handle_t l=(handle_t)data;
-                db_requery(db, tab_id, from, to, l);
+                db_exec(db, tab_id, io_type, from, to, l);
             }
         }
         break;
@@ -4925,12 +5055,13 @@ static int db_data_io(sqlite3 *db, int tab_id, int io_type, time_t from, time_t
     return 0;
 }
 //////////////////////////////////////////////////////////////////////////
-static int sensor_xport(sqlite3 *db, int io_type, char *path)
+int dev_sensor_xport(sqlite3 *db, int io_type, char *path)
 {
     int i,r;
     handle_t l;
     list_node_t *ln;
     list_cfg_t lc={0,0,-1};
+    GlobalDeviceManager *dm=&__globalDeviceManage;
 
     l = xlist_init(&lc);
     
@@ -4948,12 +5079,13 @@ static int sensor_xport(sqlite3 *db, int io_type, char *path)
 
     return 0;
 }
-static int alarm_xport(int io_type, char *path)
+int dev_alarm_xport(int io_type, char *path)
 {
     int i,r;
     handle_t l;
     list_node_t *ln;
     list_cfg_t lc={0,0,-1};
+    GlobalDeviceManager *dm=&__globalDeviceManage;
 
     l = xlist_init(&lc);
     
@@ -4972,12 +5104,13 @@ static int alarm_xport(int io_type, char *path)
     sprintf(path, "/tmp/alarm.ini");
     return 0;
 }
-static int serv_xport(int io_type, char *path)
+int dev_serv_xport(int io_type, char *path)
 {
     int i,r;
     handle_t l;
     list_node_t *ln;
     list_cfg_t lc={0,0,-1};
+    GlobalDeviceManager *dm=&__globalDeviceManage;
 
     l = xlist_init(&lc);
     
@@ -4991,18 +5124,17 @@ static int serv_xport(int io_type, char *path)
 
     }
 
-
-
     sprintf(path, "/tmp/serv.ini");
     return 0;
 }
 ////////////////////////////////////////////////////////////////////
-static int log_xport(int period, char *path)
+int dev_log_xport(int type, time_t from, time_t to, char *path)
 {
     int i,r;
     handle_t l;
     list_node_t *ln;
     list_cfg_t lc={0,0,-1};
+    GlobalDeviceManager *dm=&__globalDeviceManage;
 
     l = xlist_init(&lc);
     
@@ -5021,29 +5153,99 @@ static int log_xport(int period, char *path)
     sprintf(path, "/tmp/log.txt");
     return 0;
 }
-static int power_xport(int period, char *path)
+
+typedef struct {
+    FILE *fp;
+    int  id;
+}pwr_arg_t;
+static void replace_chr(char *buf, char a, char b)
 {
-    int i,r;
+    while(*buf) {
+        if((*buf)==a) {
+            *buf = b;
+        }
+        buf++;
+    }
+}
+static int pwr_iterator_fn(handle_t l, node_t *node, node_t *xd, void *arg, int *act)
+{
+    pwr_arg_t *parg=(pwr_arg_t*)arg;
+    if(xd->tp==TAB_ID_POWER_INFO) {
+        GlobalPowerInfo *tmp=(GlobalPowerInfo*)xd->buf;
+
+        fprintf(parg->fp, "%d,", parg->id);
+        fprintf(parg->fp, "%d,", tmp->product_id);
+        fprintf(parg->fp, "%d,", tmp->product_ch_id);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.voltage);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.current);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.power);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.freq);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.consumption);
+        fprintf(parg->fp, "%0.2f,", tmp->_power_info.factor);
+        
+        replace_chr(tmp->samp_time, '-', '.');
+        fprintf(parg->fp, "%s,", tmp->samp_time);
+        fprintf(parg->fp, "\n");
+        parg->id++;
+    }
+    else {
+        GlobalTreeACManager *tmp=(GlobalTreeACManager*)xd->buf;
+        fprintf(parg->fp, "%d,", parg->id);
+        fprintf(parg->fp, "%d,", tmp->product_id);
+        fprintf(parg->fp, "%d,", tmp->product_ph_type);
+        fprintf(parg->fp, "%d,", tmp->product_ch_id);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.voltage);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.current);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.power);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.freq);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.consumption);
+        fprintf(parg->fp, "%0.2f,", tmp->_PowerInfo.factor);
+
+        replace_chr(tmp->samp_time, '-', '.');
+        fprintf(parg->fp, "%s,", tmp->samp_time);
+        fprintf(parg->fp, "\n");
+        parg->id++;
+    }
+    return 0;
+}
+
+int dev_power_xport(int type, time_t from, time_t to, char *path)
+{
+    int i,r=-1;
     handle_t l;
+    FILE *fp;
+    node_t *nd;
     list_node_t *ln;
     list_cfg_t lc={0,0,-1};
+    GlobalPowerInfo *tmp;
+    pwr_arg_t arg;
+    GlobalTreeACManager *tmp3;
+    GlobalDeviceManager *dm=&__globalDeviceManage;
+    int tab_id=(type?TAB_ID_POWER3_INFO:TAB_ID_POWER_INFO);
 
     l = xlist_init(&lc);
-    
-    //
+    r = db_exec(dm->db, tab_id, IO_EXPORT, from, to, l);
 
-    for(i=0; i<xlist_size(l); i++) {
-        r = xlist_take_node(l, &ln, 0);
-        if(r==0) {
-            node_t *nd=&ln->data;
+    fp = fopen(path, "wt");
+    if(fp) {
+        if(tab_id==TAB_ID_POWER_INFO) {
+            fprintf(fp, "id,product_id,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
+        }
+        else {
+            fprintf(fp, "id,product_id,product_ph_type,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
         }
 
+        arg.fp = fp; arg.id = 1;
+        xlist_iterator(l, NULL, pwr_iterator_fn, &arg);
+        fclose(fp);
+        r = 0;
     }
-
-
-
-    sprintf(path, "/tmp/log.txt");
-    return 0;
+    else {
+        printf("__%s open failed\n", path);
+    }
+    xlist_free(l);
+    
+    return r;
 }
 
 

+ 6 - 5
pro/src/sqlite_handle.h

@@ -57,7 +57,7 @@ int dev_get_power_manage_info_by_ID(sqlite3 *db,int product_id,int product_addr,
 
 int dev_get_t_ac_power_manage_info(sqlite3 *db,int product_id,int product_addr,int product_ch_id,int product_ph_type,GlobalTreeACManager* _globalPowerChnManger);
 int dev_insert_t_ac_power_manage_info(sqlite3 *db,int product_id,int product_ch_id,GlobalTreeACManager* _globalPowerChnManger);
-int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnInfo,int* nIndex,char* pTimeData);
+int dev_insert_t_ac_power_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnInfo,int* nIndex);
 int dev_update_t_ac_power_manage_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnInfo);
 int dev_update_t_ac_power_manage_over_info(sqlite3 *db,GlobalTreeACManager* _globalPowerChnManger);
 int dev_delete_ac_power_info_all(sqlite3 *db);
@@ -196,11 +196,12 @@ enum {
     
     TAB_ID_MAX
 };
-#include "xlist.h"
-int dev_data_xport(sqlite3 *db, int tab_id, int io_type, uint32_t time_s, uint32_t time_e);
-
-
 
+int dev_sensor_xport(sqlite3 *db, int io_type, char *path);
+int dev_alarm_xport(int io_type, char *path);
+int dev_serv_xport(int io_type, char *path);
+int dev_log_xport(int type, time_t from, time_t to, char *path);
+int dev_power_xport(int type, time_t from, time_t to, char *path);
 
 
 

+ 58 - 8
pro/src/sys.c

@@ -1140,20 +1140,69 @@ static void mem_check(void)
     }
 }
 
-#define SEND_PERIOD       (24*3600)
-static void send_file(void)
+
+#include "mail.h"
+#define SEND_PERIOD 10
+static int get_range(struct tm *t, time_t *from, time_t *to, int days)
+{
+    time_t x1,x2;
+    struct tm t2=*t;
+    time_t start=mktime0(),secs=3600*24*days;
+   
+    t2.tm_hour = 0;
+    t2.tm_min = 0;
+    t2.tm_sec = 0;
+
+    x2 = mktime(&t2)-1;
+    if((x2-start)<=secs) {
+        x1 = start;
+    }
+    else {
+        x1 = x2-secs+1;
+    }
+
+    *from = x1;
+    *to = x2;
+    return 0;
+}
+static void send_check(void)
 {
-    int r,cnt;
+    int r,cnt,type=0;
+    char path[200],sub[100];
+    char tmp1[100],tmp2[100];
     static uint32_t send_cnt=0;
     GlobalDeviceManager *dm=&__globalDeviceManage;
+    static time_t sended_id=0;
 
     send_cnt++;
     if(send_cnt%SEND_PERIOD==0) {
+        time_t cur = time(NULL);
+        struct tm *tm = localtime(&cur);
+        if(tm->tm_hour==0) {
+            time_t from,to;
+            get_range(tm, &from, &to, 1);
+
+            if(sended_id != from) {
+                if(dm->_globalDevInfo.product.pwr_type>=SmartPDU_Tree_AC_Tree) {
+                    type = 1;
+                }
+                get_time_str2(tmp1,sizeof(tmp1),from);
+                get_time_str2(tmp2,sizeof(tmp2),to);
+                sprintf(path, "/tmp/power_%s_%s.csv", tmp1, tmp2);
+                if(!file_exist(path)) {
+                    dev_power_xport(type, from, to, path);
+                }
 
-        //r = dev_data_xport(dm->db, TAB_ID_POWER_INFO, IO_EXPORT, 0, 0, NULL, &cnt);
-        //r = dev_data_xport(dm->db, TAB_ID_POWER3_INFO, IO_EXPORT, 0, 0, NULL, &cnt);
-        //r = dev_data_xport(dm->db, TAB_ID_POWER_INFO, IO_EXPORT, 0, 0, NULL, &cnt);
-        //r = dev_data_xport(dm->db, TAB_ID_POWER_INFO, IO_EXPORT, 0, 0, NULL, &cnt);
+                if(file_exist(path)) {
+                    sprintf(sub, "%s %d power data", dm->_globalDevInfo.product.name, dm->_globalDevInfo.product.id);
+                    r = mail_send(sub, NULL, path);
+                    if(r==0) {
+                        file_remove(path);
+                        sended_id = from;
+                    }
+                }
+            }
+        }
     }
 }
 
@@ -1164,6 +1213,7 @@ static void* polling_thread(void *arg)
 
     while(h->quit==0) {
         mem_check();
+        send_check();
         sleep(1);
     }
     pthread_exit(NULL);
@@ -1177,7 +1227,7 @@ int sys_init(void)
     lock_s_init();
     paras_init();
     
-    nic_restart(DFLT_NIC);
+    //nic_restart(DFLT_NIC);
     thread_start(THREAD_ID_POLLING, polling_thread, NULL);
 
     return 0;