Explorar el Código

修改文件导入导出接口

guohui hace 2 años
padre
commit
b505f488d3
Se han modificado 1 ficheros con 132 adiciones y 33 borrados
  1. 132 33
      pro/src/appweb_handle.c

+ 132 - 33
pro/src/appweb_handle.c

@@ -68,6 +68,44 @@ static web_handle_t webHandle;
 static int webSetCallback(void);
 
 ///////////////////////////////////////////////////
+static int upload_file(void *conn, char *path)
+{
+    int r=-1,flag=0;
+    char name[200];
+    struct mg_http_part part;
+    mg_stream_t *s=(mg_stream_t*)conn;
+    size_t pos = 0, total_bytes = 0;
+    struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
+    
+    memset(name, 0, sizeof(name));
+    while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
+        memcpy(name, part.filename.buf, part.filename.len);
+        printf("filename: %s length: %lu bytes\n", name, (unsigned long) part.body.len);
+
+        if(flag==0) {
+            file_remove(path); flag = 1;
+        }
+
+        r = file_append(path, part.body.buf, part.body.len);
+        if (r==0) {
+            total_bytes += part.body.len;
+        }
+    }
+    printf("___ upload file to %s %d\n", path, total_bytes);
+
+    return r;
+}
+static int download_file(void *conn, char *path, int remove)
+{
+    mg_stream_t *s=(mg_stream_t*)conn;
+    struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
+
+    struct mg_http_serve_opts opts = {.root_dir = NULL};
+    mg_http_serve_file(s->c, hm, path, &opts);
+    if(remove) file_remove(path);
+
+    return 0;
+}
 
 static int http_reply(void *conn, int status, char *ack)
 {
@@ -137,52 +175,94 @@ static int save_to_csv(char *path, status_data_t *data, int flag)
     fclose(fp);
     return 0;
 }
-static int export_handle(void *conn, int flag)
+static int export_power(void *conn, int flag)
 {
     int r;
-    char path[100];
+    char path[200];
     web_handle_t *wh=&webHandle;
-    mg_stream_t *s=(mg_stream_t*)conn;
-    struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
 
     sprintf(path, "%s.csv", (flag==0)?"overall":"channel");
     r = save_to_csv(path, &wh->stt, flag);
     if(r==0) {
-        struct mg_http_serve_opts opts = {.root_dir = NULL};
-        mg_http_serve_file(s->c, hm, path, &opts);
-        file_remove(path);
+        download_file(conn, path, 1);
     }
 
     return r;
 }
 
-static int upload_handle(void *conn, char *path)
+static int import_misc(void *conn, int flag)
 {
-    int r=-1,flag=0;
-    char name[200];
-    struct mg_http_part part;
+    char path[200];
+    web_handle_t *wh=&webHandle;
     mg_stream_t *s=(mg_stream_t*)conn;
-    size_t pos = 0, total_bytes = 0;
     struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
-    
-    memset(name, 0, sizeof(name));
-    while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
-        memcpy(name, part.filename.buf, part.filename.len);
-        printf("filename: %s length: %lu bytes\n", name, (unsigned long) part.body.len);
 
-        if(flag==0) {
-            file_remove(path); flag = 1;
+    switch(flag) {
+        case 0:     //sensor
+        {
+            //upload_file(path);
         }
+        break;
 
-        r = file_append(path, part.body.buf, part.body.len);
-        if (r==0) {
-            total_bytes += part.body.len;
+        case 1:     //alarm
+        {
+            //upload_file(path);
+        }
+        break;
+
+        case 2:     //service
+        {
+            //upload_file(path);
+        }
+        break;
+
+        case 3:     //config
+        {
+            //upload_file(path);
         }
+        break;
     }
-    printf("___ upload file to %s %d\n", path, total_bytes);
 
-    return r;
+    return 0;
 }
+
+static int export_misc(void *conn, int flag)
+{
+    char path[200];
+    web_handle_t *wh=&webHandle;
+    mg_stream_t *s=(mg_stream_t*)conn;
+    struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
+
+    switch(flag) {
+        case 0:     //sensor
+        {
+            //download_file(path);
+        }
+        break;
+
+        case 1:     //alarm
+        {
+            //download_file(path);
+        }
+        break;
+
+        case 2:     //service
+        {
+            //download_file(path);
+        }
+        break;
+
+        case 3:     //config
+        {
+            //download_file(path);
+        }
+        break;
+    }
+
+    return 0;
+}
+
+
 static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
 {
     int i,j,flag=0;
@@ -456,7 +536,7 @@ static void overallStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            export_handle(conn, 0);
+            export_power(conn, 0);
 
         }
         else {
@@ -1059,7 +1139,7 @@ static void channelStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            export_handle(conn, 1);
+            export_power(conn, 1);
         }
         return;     
     }
@@ -4419,7 +4499,7 @@ static void serviceManagementUpload(void *conn)
     
     if(strstr(hm->uri.buf, "type=importData")) {        //firmware upload
         sprintf(path, "%s/%s", DOWN_FOLDER, CERT_FILE);
-        r = upload_handle(conn, path);
+        r = upload_file(conn, path);
         if(r==0) {
             p = strstr(hm->uri.buf, tok);
             if(p) {
@@ -5248,7 +5328,7 @@ static void equipmentMintenanceUpload(void *conn)
     
     if(strstr(hm->uri.buf, "uploadType=gjsj")) {        //firmware upload
         sprintf(path, "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
-        r = upload_handle(conn, path);
+        r = upload_file(conn, path);
         if(r==0) {
             printf("_____ upg_start\n");
             upg_start();
@@ -5257,7 +5337,7 @@ static void equipmentMintenanceUpload(void *conn)
     else if(strstr(hm->uri.buf, "uploadType=shfw")) {   //logo upload
         sprintf(tmp, "%s/%s", IMAGE_PATH, LOGO_FILE);
         sys_get_path(path, tmp);
-        upload_handle(conn, path);
+        upload_file(conn, path);
     }
 
     ack_str = ctrl_sts_ack_to_json();
@@ -5274,11 +5354,30 @@ static void equipmentMintenanceExport(void *conn)
     char *rx_body_buf = (char*)hm->body.buf;
     char path[200];
     
-    if(strstr(hm->uri.buf, "export=2")) {
-        //export_handle(conn, 0);
+    if(strstr(hm->uri.buf, "export=0")) {
+        export_misc(conn, 0);
+    }
+    else if(strstr(hm->uri.buf, "export=1")) {
+        export_misc(conn, 1);
+    }
+    else if(strstr(hm->uri.buf, "export=2")) {
+        export_misc(conn, 2);
+    }
+    else if(strstr(hm->uri.buf, "export=3")) {
+        export_misc(conn, 3);
+    }
+    else if(strstr(hm->uri.buf, "uploadType=0")) {
+        import_misc(conn, 0);
+    }
+    else if(strstr(hm->uri.buf, "uploadType=1")) {
+        import_misc(conn, 1);
+    }
+    else if(strstr(hm->uri.buf, "uploadType=2")) {
+        import_misc(conn, 2);
+    }
+    else if(strstr(hm->uri.buf, "uploadType=3")) {
+        import_misc(conn, 3);
     }
-
-    //http_reply(conn, 200, "操作成功");
 }
 
 static int webSetCallback(void)