Quellcode durchsuchen

发邮件附件ok
导出电力数据ok

guohui vor 1 Jahr
Ursprung
Commit
dd1a1d92f6
5 geänderte Dateien mit 147 neuen und 145 gelöschten Zeilen
  1. 3 5
      pro/src/app.c
  2. 74 96
      pro/src/appweb_handle.c
  3. 32 15
      pro/src/mail.c
  4. 36 27
      pro/src/sqlite_handle.c
  5. 2 2
      pro/src/sys.c

+ 3 - 5
pro/src/app.c

@@ -1780,8 +1780,8 @@ 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)
     {
@@ -1796,7 +1796,6 @@ static void* ntp_thread(void *arg)
 
                 if (pc)
                 {
-                    printf("0000000\n");
                     if (getCurrentTimeFromNTP(pc, &utc_time) == 0)
                     {
                         time_t host_time = time(NULL);
@@ -1810,7 +1809,6 @@ static void* ntp_thread(void *arg)
                     {
                         log_e("getCurrentTimeFromNTP failed!");
                     }
-                    printf("2222222\n");
                     destroyNTPClient(pc);
                     pc = NULL;
                 }
@@ -1843,7 +1841,7 @@ int ntp_config(NTPManager *nm)
         system("/etc/init.d/S49ntp stop");
     }
 #else
-    system("/etc/init.d/S49ntp stop");
+    //system("/etc/init.d/S49ntp stop");
     if(nm->mode==1) {
         ntp_sync_flag = 1;
     }

+ 74 - 96
pro/src/appweb_handle.c

@@ -150,83 +150,80 @@ 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;
     }
-
-    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);
+    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, int days)
+static int get_range(time_t *from, time_t *to, char *start, char *end, int max_days)
 {
-    struct timeval tv;
-    struct timezone tz;
-    time_t cur=time(NULL);
-    struct tm *tm=localtime(&cur);
-    time_t start=mktime0(),secs=3600*24*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;
+    }
 
-    if((cur-start)<=secs) {
-        *from = start;
-        printf("____ 1111 %ld\n", *from);
+    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 {
-        *from = cur-secs-1;
-        printf("____ 2222 %ld\n", *from);
+        if(cur>pre) {
+            *from = pre;
+            *to = cur;
+        }
+        else {
+            *from = cur;
+            *to = pre;
+        }
     }
-    *to = cur;
+    
     return 0;
 }
-static int power_export(char *path)
+
+
+
+
+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, 7);
+    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);
@@ -237,7 +234,7 @@ static int power_export(char *path)
     return dev_power_xport(type, from, to, path);
 }
 
-static int file_io(void *conn, int ftype, int io)
+static int file_io(void *conn, int ftype, int io, char *buf)
 {
     int r,flag=0;
     char tmp[200],path[200];
@@ -300,36 +297,18 @@ 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) {
-#if 0
-                r = power_export(path);
+                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, path);
                     file_remove(path);
                 }
-#else
-                char *ftmp="channel.csv";
-                r = power_to_csv(ftmp, &wh->stt, ftype);
-                if(r==0) {
-                    download_file(conn, ftmp);
-                    file_remove(ftmp);
-                }
-#endif
-
             }
         }
         break;
@@ -696,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);
@@ -1300,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;     
     }
@@ -5629,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();
@@ -5660,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)

+ 32 - 15
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,15 +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 && strlen(attach)>0) {
-        mm = curl_mime_init(curl);
-        if(mm) {
-            curl_mimepart *part = curl_mime_addpart(mm);
-            curl_mime_name(part, "file");
-            curl_mime_filedata(part, attach);
-        }
-    }
-
     if(multi) {
         for(int i=0; i<MAIL_RECV_MAX; i++) {
             if(recv[i].use && strlen(recv[i].account)>0) {
@@ -151,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);

+ 36 - 27
pro/src/sqlite_handle.c

@@ -5154,38 +5154,44 @@ int dev_log_xport(int type, time_t from, time_t to, char *path)
     return 0;
 }
 
+typedef struct {
+    FILE *fp;
+    int  id;
+}pwr_arg_t;
 static int pwr_iterator_fn(handle_t l, node_t *node, node_t *xd, void *arg, int *act)
 {
-    FILE *fp=(FILE*)arg;
+    pwr_arg_t *parg=(pwr_arg_t*)arg;
     if(xd->tp==TAB_ID_POWER_INFO) {
         GlobalPowerInfo *tmp=(GlobalPowerInfo*)xd->buf;
 
-        fprintf(fp, "%d,", tmp->product_id);
-        fprintf(fp, "%d,", tmp->product_ch_id);
-        fprintf(fp, "%0.2f,", tmp->_power_info.voltage);
-        fprintf(fp, "%0.2f,", tmp->_power_info.current);
-        fprintf(fp, "%0.2f,", tmp->_power_info.power);
-        fprintf(fp, "%0.2f,", tmp->_power_info.freq);
-        fprintf(fp, "%0.2f,", tmp->_power_info.consumption);
-        fprintf(fp, "%0.2f,", tmp->_power_info.factor);
-        fprintf(fp, "%s,", tmp->samp_time);
-        fprintf(fp, "\n");
+        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);
+        fprintf(parg->fp, "%s,", tmp->samp_time);
+        fprintf(parg->fp, "\n");
+        parg->id++;
     }
     else {
         GlobalTreeACManager *tmp=(GlobalTreeACManager*)xd->buf;
-
-        
-        fprintf(fp, "%d,", tmp->product_id);
-        fprintf(fp, "%d,", tmp->product_ph_type);
-        fprintf(fp, "%d,", tmp->product_ch_id);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.voltage);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.current);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.power);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.freq);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.consumption);
-        fprintf(fp, "%0.2f,", tmp->_PowerInfo.factor);
-        //fprintf(fp, "%s,", tmp->_PowerInfo.samp_time);
-        fprintf(fp, "\n");
+        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);
+        fprintf(parg->fp, "%s,", tmp->samp_time);
+        fprintf(parg->fp, "\n");
+        parg->id++;
     }
     return 0;
 }
@@ -5199,6 +5205,7 @@ int dev_power_xport(int type, time_t from, time_t to, char *path)
     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);
@@ -5209,13 +5216,15 @@ int dev_power_xport(int type, time_t from, time_t to, char *path)
     fp = fopen(path, "wt");
     if(fp) {
         if(tab_id==TAB_ID_POWER_INFO) {
-            fprintf(fp, "product_id,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
+            fprintf(fp, "id,product_id,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
         }
         else {
-            fprintf(fp, "product_id,product_ph_type,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
+            fprintf(fp, "id,product_id,product_ph_type,product_ch_id,voltage,current,power,freq,consumption,factor,samp_time\n");
         }
 
-        xlist_iterator(l, NULL, pwr_iterator_fn, fp);
+        arg.fp = fp;
+        arg.id = 1;
+        xlist_iterator(l, NULL, pwr_iterator_fn, &arg);
         fclose(fp);
         r = 0;
     }

+ 2 - 2
pro/src/sys.c

@@ -1158,7 +1158,7 @@ static int get_range(struct tm *t, time_t *from, time_t *to, int days)
         x1 = start;
     }
     else {
-        x1 = x2-secs-1;
+        x1 = x2-secs+1;
     }
 
     *from = x1;
@@ -1194,7 +1194,7 @@ static void send_check(void)
                 }
 
                 if(file_exist(path)) {
-                    sprintf("%s %d power data", dm->_globalDevInfo.product.name, dm->_globalDevInfo.product.id);
+                    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);