Browse Source

IP表,处理恢复出厂设置bug,处理电量数据导出显示不全。

liyi 11 months ago
parent
commit
7806504dbe
6 changed files with 712 additions and 10 deletions
  1. 307 7
      ip/src/appweb_handle.c
  2. 13 1
      ip/src/file.c
  3. 1 1
      ip/src/file.h
  4. 1 1
      ip/src/json_handle.c
  5. 367 0
      ip/src/sqlite_handle.c
  6. 23 0
      ip/src/sqlite_handle.h

+ 307 - 7
ip/src/appweb_handle.c

@@ -14,6 +14,33 @@
 #include "language_common.h"
 #include "websocket_handle.h"
 
+
+enum {
+    FILE_IMPORT=0,
+    FILE_EXPORT,
+};
+
+enum {
+    FILE_SYS_CFG=0,
+    FILE_SERV_CFG,
+    FILE_ALARM_CFG,
+    FILE_SENSOR_CFG,
+
+    FILE_POWER_ALL,
+    FILE_POWER_CHN,
+    
+    FILE_LOG_RUN,
+    FILE_LOG_OPERA,
+    FILE_LOG_ALARM,
+
+    FILE_LOGO,
+    FILE_HOST_FW,
+};
+
+////////////////////////////////////////////////
+
+
+
 static void get_time(char *ts)
 {
     struct timeval tv;
@@ -78,6 +105,19 @@ static int webSetCallback(void);
 
 ///////////////////////////////////////////////////
 
+
+static int download_file(void *conn, char *fpath)
+{
+    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_file2(s->c, hm, fpath, &opts);
+
+    return 0;
+}
+
+
 static int http_reply(void *conn, int status, char *ack)
 {
      int r=0;
@@ -146,6 +186,261 @@ static int save_to_csv(char *path, status_data_t *data, int flag)
     fclose(fp);
     return 0;
 }
+///////////////////////////////////////////////////////////////////////
+static time_t gettime(char *timestr)
+{
+    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;
+    }
+
+    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 power_export(char *path, char *start, char *end)
+{
+    int r;
+    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);
+
+    return dev_power_xport(from, to, path);
+}
+
+static int file_io(void *conn, int ftype, int io, char *buf)
+{
+    int r,flag=0;
+    char tmp[200],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(ftype) {
+#if 0
+        case FILE_SYS_CFG:
+        {
+            sprintf(tmp, "/tmp/config.ini");
+            sys_get_path(path, INI_FILE_NAME);
+            if(io==FILE_IMPORT) {
+                r = upload_file(conn, tmp);
+                if(r==0) {
+                    r = paras_check(tmp);
+                    if(r==0) {
+                        paras_copy(path, tmp);
+                        file_remove(tmp);
+
+                        //clear some database table
+                        sys_exit(); //restart smartPDU
+                    }
+                    file_remove(tmp);
+                }
+            }
+            else {
+                file_copy(tmp, path);
+                download_file(conn, tmp);
+                file_remove(tmp);
+            }
+        }
+        break;
+
+        case FILE_SERV_CFG:
+        {
+            sprintf(path, "/tmp/service.ini");
+            if(io==FILE_IMPORT) {
+                r = upload_file(conn, path);
+                if(r==0) {
+                    //thread_stop(THREAD_ID_POWER);
+                    r = paras_serv_xport(path, PARAS_IMPORT);
+                    file_remove(path);
+                    if(r==0) sys_exit();
+                    //thread_start2(THREAD_ID_POWER);
+                }
+            }
+            else {
+                r = paras_serv_xport(path, PARAS_EXPORT);
+                if(r==0) {
+                    r = download_file(conn, path);
+                }
+                file_remove(path);
+            }
+        }
+        break;
+
+        case FILE_ALARM_CFG:
+        {
+            sprintf(path, "/tmp/alarm.ini");
+            if(io==FILE_IMPORT) {
+                r = upload_file(conn, path);
+                if(r==0) {
+                    r = paras_alarm_xport(path, PARAS_IMPORT);
+                    file_remove(path);
+                    if(r==0) sys_exit();
+                }
+            }
+            else {
+                r = paras_alarm_xport(path, PARAS_EXPORT);
+                if(r==0) {
+                    r = download_file(conn, path);
+                    file_remove(path);
+                }
+            }
+        }
+        break;
+
+        case FILE_SENSOR_CFG:
+        {
+            sprintf(path, "/tmp/sensor.ini");
+            if(io==FILE_IMPORT) {
+                r = upload_file(conn, path);
+                if(r==0) {
+                    r = paras_sensor_xport(path, PARAS_IMPORT);
+                    file_remove(path);
+                    if(r==0) sys_exit();
+                }
+            }
+            else {
+                r = paras_sensor_xport(path, PARAS_EXPORT);
+                if(r==0) {
+                    r = download_file(conn, path);
+                    file_remove(path);
+                }
+            }
+        }
+        break;
+#endif
+        case FILE_POWER_ALL:
+        case FILE_POWER_CHN:
+        {
+            if(io==FILE_EXPORT) {
+                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);
+                }
+            }
+        }
+        break;
+#if 0
+        case FILE_LOG_RUN:
+        {
+            if(io==FILE_EXPORT) {
+                //download_file(path);
+            }
+        }
+        break;
+
+        case FILE_LOG_OPERA:
+        {
+            if(io==FILE_EXPORT) {
+                //download_file(path);
+            }
+        }
+        break;
+
+        case FILE_LOG_ALARM:
+        {
+            if(io==FILE_EXPORT) {
+                //download_file(path);
+            }
+        }
+        break;
+
+        case FILE_LOGO:
+        {
+            if(io==FILE_IMPORT) {
+                char tmp[100];
+                sprintf(tmp, "%s/%s", IMAGE_FOLDER, LOGO_FILE);
+                sys_get_path(path, tmp);
+                upload_file(conn, path);
+            }
+        }
+        break;
+
+        case FILE_HOST_FW:
+        {
+            if(io==FILE_IMPORT) {
+                sprintf(path, "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
+                r = upload_file(conn, path);
+                if(r==0) {
+                    printf("_____ upg_start\n");
+                    upg_host_send(UPGRADE_EVT_UPLOAD_FINISH);
+                }
+            }
+        }
+        break;
+#endif
+        default:
+        return -1;
+    }
+
+    return 0;
+}
+
+
 static int export_handle(void *conn, int flag)
 {
     int r;
@@ -156,6 +451,8 @@ static int export_handle(void *conn, int flag)
 
     sprintf(path, "%s.csv", (flag==0)?"overall":"channel");
     r = save_to_csv(path, &wh->stt, flag);
+    //dev_power_xport();
+    
     if(r==0) {
         struct mg_http_serve_opts opts = {.root_dir = NULL};
         mg_http_serve_file(s->c, hm, path, &opts);
@@ -201,9 +498,9 @@ static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
     if (ev == MG_EV_HTTP_MSG) {
         struct mg_http_message *hm = ev_data;
 
-        log_i("uri ask!!!!!!!!!!!!!!!!:%s",hm->uri.buf);   
+       // log_i("uri ask!!!!!!!!!!!!!!!!:%s",hm->uri.buf);   
         for(i=0; i<FN_MAX; i++) {
-            log_i("uri..................:%s",h->fn[i].uri);            
+        //    log_i("uri..................:%s",h->fn[i].uri);            
             if (h->fn[i].f && h->fn[i].uri && mg_match(hm->uri, mg_str(h->fn[i].uri), NULL)) {
                 h->fn[i].f(&s);
                 flag = 1;
@@ -467,7 +764,8 @@ static void overallStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            export_handle(conn, 0);
+           //export_handle (conn, 0);
+           file_io(conn, FILE_POWER_ALL, FILE_EXPORT, hm->uri.buf);
 
         }
         else {
@@ -1062,7 +1360,8 @@ static void channelStatusMonitoring(void* conn)
     if(cjson==NULL)
     {
         if (strstr(hm->uri.buf,"type=export")) {
-            export_handle(conn, 1);
+            //export_handle(conn, 1);
+            file_io(conn, FILE_POWER_CHN, FILE_EXPORT, hm->uri.buf);
         }
         return;     
     }
@@ -3513,8 +3812,8 @@ static void communicationManagement(void *conn)
     else if(strcmp(reqInfo.type,"RsSave")==0)//淇濆瓨modbus鏁版嵁
     {
         ModbusInfo_t* info = &devInfo->_gmodbus_info;
-        bool isStrMaster=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_master[__globalDeviceManage._globalDevInfo.product_language])==0;
-        bool isStrSlave=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_slave[__globalDeviceManage._globalDevInfo.product_language])==0;
+        bool isStrMaster=strcmp(reqInfo._rs_info.mode,"0")==0;
+        bool isStrSlave=strcmp(reqInfo._rs_info.mode,"1")==0;
         if (isStrMaster||isStrMaster)
         {
             if (isStrMaster)
@@ -3538,6 +3837,7 @@ static void communicationManagement(void *conn)
        // r = cascade_set_modbus(info);
         r = cascade_set_ip_modbus(info);
         if(r==0) {
+            __globalDeviceManage._globalDevInfo._gmodbus_info.product_modbus_addr = info->product_modbus_addr;
             dev_update_device_modus_info(__globalDeviceManage.db,devInfo);
         }
         char buff[256];
@@ -4804,7 +5104,7 @@ static void equipmentMintenance(void *conn)
         _over_back_sts_info.productAddr = getLocalIpAddress("eth0");
 
         _over_back_sts_info.product_mac = get_mac_address("eth0");
-
+        
         _over_back_sts_info.product_RS485Addr = __globalDeviceManage._globalDevInfo._gmodbus_info.product_modbus_addr;
 
         //_over_back_sts_info.product_status = __globalDeviceManage._globalDevInfo.product_status;

+ 13 - 1
ip/src/file.c

@@ -34,7 +34,7 @@ int file_save(char *path, void *buf, int len)
     int r = 0;
     FILE *fp;
 
-    int fd = open(path,O_RDWR|O_TRUNC,0777);
+    int fd = open(path,O_RDWR|O_TRUNC|O_CREAT,0777);
 
     write(fd,buf,len);
 
@@ -123,6 +123,18 @@ int file_remove(char *path)
     return remove(path);
 }
 
+int file_exist(char* path)
+{
+    int r;
+    struct stat st;
+
+    r = stat(path, &st);
+    if (r) {
+        return 0;
+    }
+
+    return 1;
+}
 
 
 

+ 1 - 1
ip/src/file.h

@@ -16,6 +16,6 @@ int file_save(char *path, void *buf, int len);
 int file_load(char* path, buf_t *buf);
 int file_copy(char *dst, char *src);
 int file_remove(char *path);
-
+int file_exist(char* path);
 
 #endif

+ 1 - 1
ip/src/json_handle.c

@@ -1380,7 +1380,7 @@ char* over_product_list_info_to_json(GlobalDeviceManager *dm, _OverBackStsInfo*
 
                         if (dl.slave[i].addr == 0)
                         {
-                            sprintf(temp, "%d", 0);
+                            sprintf(temp, "%d", _over_back_sts_info->product_RS485Addr);
                             cJSON_AddStringToObject(data_filed, "RSadress", temp);
                         }
                         else

+ 367 - 0
ip/src/sqlite_handle.c

@@ -8,6 +8,25 @@
 #include "smtp.h"
 #include "switch_ctrl.h"
 #include "pduMIB_trap.h"
+#include "xlist.h"
+#include "file.h"
+
+
+typedef struct {
+    int        prod_id;
+    int        channel_id;
+    int        phase_type;
+    int        product_status;
+    char       samp_time[32];
+    PowerInfo  power;
+}power_data_t;
+const char *tabNamePool[TAB_ID_MAX]={
+    "Table_PowerInfo",
+};
+
+
+
+
 
 #define	TOTAL_COUNT	60//统计总数
 
@@ -138,6 +157,247 @@ int sqlite_SQL_RUN(sqlite3 *db,const char *select_sql, char ***pResult, int *nro
     pthread_mutex_unlock(&__globalDeviceManage.dbThreadlock);
     return res;
 }
+
+static int db_exec(sqlite3 *db, int tab_id, int io_type, time_t from, time_t to, handle_t l)
+{
+    int r=-1,cnt=0;
+    char *p,temp[1024];
+    char time_s[64],time_e[64];
+    sqlite3_stmt *stmt=NULL;
+
+    get_time_str(time_s, sizeof(time_s), from);
+    get_time_str(time_e, sizeof(time_e), to);
+
+    switch(tab_id) {
+#if 0
+        case TAB_ID_POWER:
+        {
+            //
+
+        }
+        break;
+
+        case TAB_ID_POWER3:
+        {
+            //
+        }
+        break;
+#endif
+        case TAB_ID_POWER_INFO:
+        {
+            power_data_t tmp;
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE product_samp_time BETWEEN '%s' AND '%s' ORDER BY product_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 TAB_ID_POWER_INFO, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
+        
+            while (sqlite3_step(stmt)==SQLITE_ROW) {  //column start from 0
+                tmp.prod_id = sqlite3_column_int(stmt, 1);
+                tmp.channel_id =  sqlite3_column_int(stmt, 2);
+                tmp.product_status =  sqlite3_column_int(stmt, 3);
+                tmp.power.voltage = sqlite3_column_double(stmt, 4);
+                tmp.power.current = sqlite3_column_double(stmt, 5);
+                tmp.power.power = sqlite3_column_double(stmt, 6);
+                tmp.power.freq = sqlite3_column_double(stmt, 7);
+                tmp.power.consumption = sqlite3_column_double(stmt, 8);
+                tmp.power.factor = sqlite3_column_double(stmt, 9);
+                strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 0));
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+                cnt++;
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+
+#if 0
+        case TAB_ID_POWER3_INFO:
+        {
+
+            power_data_t tmp;
+
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (product_ph_samp_time BETWEEN '%s' AND '%s') ORDER BY product_ph_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 TAB_ID_POWER3_INFO, sqlite3_prepare_v2 failed\n");
+                return -1;
+            }
+
+            while (sqlite3_step(stmt)==SQLITE_ROW) {
+                tmp.prod_id = sqlite3_column_int(stmt, 1);
+                tmp.phase_type =  sqlite3_column_int(stmt, 2);
+                tmp.channel_id =  sqlite3_column_int(stmt, 3);
+                tmp.power.voltage = sqlite3_column_double(stmt, 4);
+                tmp.power.current = sqlite3_column_double(stmt, 5);
+                tmp.power.power = sqlite3_column_double(stmt, 6);
+                tmp.power.freq = sqlite3_column_double(stmt, 7);
+                tmp.power.consumption = sqlite3_column_double(stmt, 8);
+                tmp.power.factor = sqlite3_column_double(stmt, 9);
+                strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 10));
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+                cnt++;
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+
+        case TAB_ID_LOG_INFO:
+        {
+            
+        }
+        break;
+
+        case TAB_ID_ALARM_INFO:
+        {
+            char *p=NULL;
+            alarm_data_t tmp;
+            GlobalDeviceManager *dm=&__globalDeviceManage;
+
+            snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE ((alarmType=%d) OR (alarmType=%d)) AND (alarmDate BETWEEN '%s' AND '%s') ORDER BY alarmDate DESC;", 
+                                         tabNamePool[tab_id], ALARM_TYPE_OPRATION, ALARM_TYPE_RUNNING, time_s, time_e);
+            
+            r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
+            if (r != SQLITE_OK) {
+                log_e("___%s failed, %s\n", temp, sqlite3_errstr(r));
+                return -1;
+            }
+        
+            while (sqlite3_step(stmt) == SQLITE_ROW) {  //column start from 0
+                tmp.id = sqlite3_column_int(stmt, 0);
+                tmp.type =  sqlite3_column_int(stmt, 2);
+
+                p = (char*)sqlite3_column_text(stmt, 1);
+                if(p) strcpy(tmp.date, p);
+                else tmp.date[0] = 0;
+
+                p = (char*)sqlite3_column_text(stmt, 4);
+                if(p) {
+                    strcpy(tmp.content, p);
+                    replace_sequences(tmp.content, dm->alarmTab);
+                }
+                else tmp.content[0] = 0;
+
+                xlist_append(l, tab_id, &tmp, sizeof(tmp));
+                cnt++;
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+
+        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));
+                cnt++;
+            }
+            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));
+                cnt++;
+            }
+            sqlite3_finalize(stmt);
+        }
+        break;
+#endif
+        default:
+        return -1;
+    }
+
+    return 0;
+}
+
+
+
 /// @brief 查询设备信息表
 /// @param db 数据库接口          
 /// @param _globalDeviceInfo 返回数据接口
@@ -4314,6 +4574,113 @@ static int sw_update_all(sqlite3 *db, netswitch_info_t *sw)
 }
 
 
+////////////////////////////////////////////////////////////////////
+typedef struct {
+    FILE *fp;
+    int  id;
+    int  type;
+}arg_t;
+
+
+static void replace_chr(char *buf, char a, char b)
+{
+    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)
+{
+    arg_t *parg=(arg_t*)arg;
+    power_data_t *tmp=(power_data_t*)xd->buf;
+
+    if(xd->tp==TAB_ID_POWER_INFO) {
+        fprintf(parg->fp, "%d,", parg->id);
+        fprintf(parg->fp, "%d,", tmp->prod_id);
+        fprintf(parg->fp, "%d,", tmp->channel_id);
+        fprintf(parg->fp, "%d,", tmp->product_status);
+        fprintf(parg->fp, "%0.2f,", tmp->power.voltage);
+        fprintf(parg->fp, "%0.2f,", tmp->power.current);
+        fprintf(parg->fp, "%0.2f,", tmp->power.power);
+        fprintf(parg->fp, "%0.2f,", tmp->power.freq);
+        fprintf(parg->fp, "%0.2f,", tmp->power.consumption);
+        fprintf(parg->fp, "%0.2f,", tmp->power.factor);
+        
+        replace_chr(tmp->samp_time, '-', '.');
+        fprintf(parg->fp, "%s,", tmp->samp_time);
+        fprintf(parg->fp, "\n");
+        parg->id++;
+    }
+    else {
+        fprintf(parg->fp, "%d,", parg->id);
+        fprintf(parg->fp, "%d,", tmp->prod_id);
+        fprintf(parg->fp, "%d,", tmp->channel_id);
+        fprintf(parg->fp, "%d,", tmp->phase_type);
+        fprintf(parg->fp, "%d,", tmp->product_status);
+        fprintf(parg->fp, "%0.2f,", tmp->power.voltage);
+        fprintf(parg->fp, "%0.2f,", tmp->power.current);
+        fprintf(parg->fp, "%0.2f,", tmp->power.power);
+        fprintf(parg->fp, "%0.2f,", tmp->power.freq);
+        fprintf(parg->fp, "%0.2f,", tmp->power.consumption);
+        fprintf(parg->fp, "%0.2f,", tmp->power.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(time_t from, time_t to, char *path)
+{
+    int i,r=-1;
+    handle_t l;
+    FILE *fp;
+    arg_t arg;
+    list_cfg_t lc={0,0,-1};
+    GlobalDeviceManager *dm=&__globalDeviceManage;
+    int tab_id=TAB_ID_POWER_INFO;
+
+    if(file_exist(path)) {
+        return -1;
+    }
+
+    if(dm->_globalDevInfo.product_pwr_type != SmartPDU_AC) {
+        return -1;
+    }
+
+    l = xlist_init(&lc);
+
+    db_exec(dm->db, tab_id, IO_EXPORT, from, to, l);
+
+    fp = fopen(path, "wt");
+    if(!fp) {
+        log_e("__%s open failed\n", path);
+        xlist_free(l);
+        return -1;
+    }
+
+    if(tab_id==TAB_ID_POWER_INFO) {
+        fprintf(fp, "id,product_id,product_ch_id,status,voltage,current,power,freq,consumption,factor,samp_time\n");
+    }
+    else {
+        fprintf(fp, "id,product_id,product_ch_id,product_ph_type,status,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);
+    xlist_free(l);
+    
+    return 0;
+}
+
 int dev_switch_init(sqlite3 *db, netswitch_info_t *sw)
 {
     int r;

+ 23 - 0
ip/src/sqlite_handle.h

@@ -10,6 +10,21 @@
 #include "netswitch.h"
 
 
+enum {
+    IO_IMPORT=0,
+    IO_EXPORT,
+
+    IO_MAX,
+};
+
+enum {
+    TAB_ID_POWER_INFO,
+    TAB_ID_MAX
+};
+
+
+
+
 int sqlite_init(sqlite3** db,pthread_mutex_t* pDBMutex, const char* daba_name);
 void sqlite_deinit(sqlite3 *db,pthread_mutex_t* pDBMutex);
 
@@ -162,4 +177,12 @@ int dev_get_sensor_info (sqlite3 *db,int product_id,int sensor_id, GlobalSensorM
 int dev_get_sensor_info_new(sqlite3 *db,int product_id,int sensor_id, GlobalSensorManger* sensor_temp);
 int dev_update_consumer(sqlite3 *db,GlobalDeviceInfo* _globalDeviceInfo);
 int dev_mail_update_recv(sqlite3 *db, smtp_recv_t *recv);
+
+
+//////////////////////////////////////////////////////////////
+int dev_power_xport(time_t from, time_t to, char *path);
+
+
+
+
 #endif