Parcourir la source

添加产品信息和升级逻辑

guohui il y a 1 an
Parent
commit
8120f21824

+ 11 - 0
output/prod/PDUInfo.txt

@@ -0,0 +1,11 @@
+"product_name": "SmartPDU"
+"product_sn": "XX1101010"
+"device_name": "YY110001"
+"device_date": "2024/01/01 11:10:10"
+"device_batch": "GC101X5502"
+"server_name": "购旺工业(赣州)有限公司"
+"server_net": "https://www.gowone.com"
+"server_mail": "steven@gowone.com"
+"server_address": "江西省赣州市赣州经济技术开发区香江科技园11栋"
+"server_telephone": "400-013-7168"
+

+ 57 - 0
output/prod/push.bat

@@ -0,0 +1,57 @@
+@ECHO OFF
+
+SET IP=192.168.1.132
+
+SET BAT=0
+IF "%~1" NEQ "" (
+    SET IP=%1
+    SET BAT=1
+)
+
+SET CURDIR=%~dp0
+SET CURDIR=%CURDIR:~0,-1%
+
+SET  TMPDIR=tmp
+SET  DEVDIR=/root
+
+SET PSCP=%CURDIR%\tools\pscp 
+SET PLINK=%CURDIR%\tools\plink
+
+SET USR=root
+SET FBAT=%CURDIR%\tools\script.sh
+SET PWD=123456
+
+SET FILE=PDUInfo.txt
+
+::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
+ECHO send %FILE% to device ...
+ECHO  y  | %PSCP%  %FILE% %USR%@%IP%:%DEVDIR%
+IF %ERRORLEVEL% NEQ 0 (
+    ECHO send failed!
+    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 3
+)
+
+IF %BAT% EQU 0 (
+    ECHO quit now ...
+    TIMEOUT /T 2 > NUL
+)
+
+EXIT /B 0

+ 17 - 0
output/prod/script.sh

@@ -0,0 +1,17 @@
+#!/bin/sh
+
+ftmp=PDUInfo.txt
+
+cp -f  $ftmp  /usr/share/
+rm -f  $ftmp
+
+exit 0
+
+
+
+
+
+
+
+
+

+ 25 - 7
pro/src/app.c

@@ -122,8 +122,8 @@ static int upgrade_proc(char *path, Modbus_Manger *mm)
         return -1;
     }
     
-    //broad upgrade file
-    upgrade_send(mm, 1, fbuf.buf, fbuf.dlen);
+    //broadcase upgrade file
+    upgrade_send(mm, 0, fbuf.buf, fbuf.dlen);
     for(i=1; i<=dm->brdMax; i++) {
         if(dm->pCtrlBoard[i].product_saddr==0) {
             continue;
@@ -135,28 +135,46 @@ static int upgrade_proc(char *path, Modbus_Manger *mm)
             upgrade_send(mm, i, fbuf.buf, fbuf.dlen);
         }
     }
-
     free(fbuf.buf);
 
     return 0;
 }
 
-
-int power_upgrade(char *path)
+typedef struct {
+    char path[200];
+    int  flag;
+}upgrade_arg_t;
+static upgrade_arg_t upgrade_arg;
+static void* upgrade_thread(void *arg)
 {
     int r;
+    upgrade_arg_t *parg=(upgrade_arg_t*)arg;
     GlobalDeviceManager *dm=&__globalDeviceManage;
     Modbus_Manger *mm=&dm->_globalRelaySampManger;
 
     thread_stop(THREAD_ID_SNMP);
     thread_stop(THREAD_ID_POWER);
     
-    r = upgrade_proc(path, mm);
+    r = upgrade_proc(parg->path, mm);
 
     thread_restart(THREAD_ID_SNMP);
     thread_restart(THREAD_ID_POWER);
     
-    return r;
+    //file_remove(path);
+    parg->flag = 0;
+
+    pthread_exit(NULL);
+}
+int board_upgrade(char *path)
+{
+    if(upgrade_arg.flag==1) {
+        return -1;
+    }
+    strcpy(upgrade_arg.path,path);
+    upgrade_arg.flag = 1;
+
+    thread_start_simp(upgrade_thread, &upgrade_arg, 4*MB);
+    return 0;
 }
 
 

+ 42 - 6
pro/src/appweb_handle.c

@@ -3726,7 +3726,7 @@ static void communicationManagement(void *conn)
         int ipv=(strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6);
         
         int mode = atoi(info2->mode);
-        if(ipv==IP_V4 || (ipv==IP_V6 && mode==IP_DHCP)) {
+        if(ipv==IP_V4 || (ipv==IP_V6 && mode==IP_STATIC)) {
             info->mode = mode;
             
             if(info2->ipaddr)   strcpy(info->ip_address, info2->ipaddr);
@@ -5652,6 +5652,43 @@ static void equipmentMintenanceExport(void *conn)
     }
 }
 
+static board_list_t brdList={0};
+static int get_list(char *s, board_list_t *l)
+{
+    int i=0,n=0;
+    char tmp[10];
+
+    char *p=strstr(s, "productId=");
+    if(!p) {
+        return -1;
+    }
+
+    int cnt=sizeof(l->id)/sizeof(l->id[0]);
+    while((*p)!='&') {
+        if((*p)==',') {
+            tmp[i] = 0;
+            if(n>=cnt) {
+                break;
+            }
+            else {
+                l->id[n] = atoi(tmp);
+                i = 0; n++;
+            }
+        }
+        else {
+            tmp[i++] = *p;
+        }
+    }
+    l->cnt = n;
+
+    return (n>0)?0:-1;
+}
+board_list_t *get_board_list(void)
+{
+    return &brdList;
+}
+
+
 static void controlBoardUpgrade(void *conn)
 {
     int r = -1 ;
@@ -5662,8 +5699,8 @@ static void controlBoardUpgrade(void *conn)
     if(strstr(hm->uri.buf, "listCbQuery")) {
         board_info_t info;
 
-        //get_board_info(&info);
-        ack_str = board_to_json(&info);
+        r = get_list(hm->uri.buf, &brdList);
+        
         http_reply(conn, 200, ack_str);
         cJSON_free(ack_str);
     }
@@ -5674,9 +5711,8 @@ static void controlBoardUpgrade(void *conn)
         r = upload_file(conn, path);
         if(r==0) {
             printf("_____ power upgrade now ...\n");
-            extern int power_upgrade(char *path);
-            power_upgrade(path);
-            file_remove(path);
+            extern int board_upgrade(char *path);
+            board_upgrade(path);
         }
     }
 }

+ 7 - 0
pro/src/common.h

@@ -46,6 +46,8 @@
 #define SENSOR_CFG_NAME     "sensor.ini"
 #define ALARM_CFG_NAME      "alarm.ini"
 
+#define INFO_FILE_PATH      "/usr/share/PDUInfo.txt"
+
 
 //高配表头串口定义-----------------------------------------------
 //控制板通信串口
@@ -1010,6 +1012,11 @@ typedef struct {
     int  status;
 }board_info_t;
 
+typedef struct {
+    int id[128];
+    int cnt;
+}board_list_t;
+
 typedef struct breaker_switch_sta{     
     uint16_t status;
     uint16_t gather;

+ 1 - 0
pro/src/json_handle.c

@@ -1541,6 +1541,7 @@ char* board_to_json(board_info_t *info)
 }
 
 
+
 char* vendor_info_to_json(GlobalDeviceInfo *dev)
 {
     char* str = NULL;

+ 98 - 32
pro/src/paras.c

@@ -122,6 +122,71 @@ static int str_replace(char *b, char *s1, char *s2)
     }
     return 0;
 }
+static char *str_get(char *buf, char *tok, char *dst, int dstlen)
+{
+    int n=0;
+    char *p=strstr(buf,tok);
+    if(!p) {
+        return NULL;
+    }
+    while((*p)!=':') {
+        if((*p)=='\r' || (*p)=='\n') {
+            return NULL;
+        }
+        p++;
+    }
+    p++;
+
+    while((*p)!='"') {
+        if((*p)=='\r' || (*p)=='\n') {
+            return NULL;
+        }
+        p++;
+    }
+    p++;
+
+    while((*p)!='"') {
+        if((*p)=='\r' || (*p)=='\n') {
+            return NULL;
+        }
+
+        *dst++ = *p++;
+        n++;
+        if(n>=dstlen-1) {
+            return NULL;
+        }
+    }
+    *dst = 0;
+
+    return dst;
+}
+static int get_prod(ProductInfo_t *info)
+{
+    int r;
+    char *p;
+    buf_t fbuf;
+    char temp[100];
+
+    r = file_load(INFO_FILE_PATH, &fbuf);
+    if(r) {
+        return -1;
+    }
+    p = get_str(fbuf.buf, "product_name",     temp, sizeof(temp));
+    p = get_str(fbuf.buf, "product_sn",       temp, sizeof(temp));
+    p = get_str(fbuf.buf, "device_name",      temp, sizeof(temp));
+    p = get_str(fbuf.buf, "device_date",      temp, sizeof(temp));
+    p = get_str(fbuf.buf, "device_batch",     temp, sizeof(temp));
+    p = get_str(fbuf.buf, "server_name",      temp, sizeof(temp));
+    p = get_str(fbuf.buf, "server_net",       temp, sizeof(temp));
+    p = get_str(fbuf.buf, "server_mail",      temp, sizeof(temp));
+    p = get_str(fbuf.buf, "server_address",   temp, sizeof(temp));
+    p = get_str(fbuf.buf, "server_telephone", temp, sizeof(temp));
+
+    free(fbuf.buf);
+    return 0;
+}
+
+
 static int ini_dump(char *path, dictionary *dic)
 {
     FILE *fp=fopen(path, "a+");
@@ -185,17 +250,19 @@ static int ini_read(dictionary *dic, char *key, void *val, int len, int type, in
     else if(type==TYPE_STR) {
         ((char*)val)[0] = 0;
         char *p = (char*)iniparser_getstring(dic, key, NULL);
-        if(p) {
-            int xlen=strlen(p);
-            if(str_chk && xlen==0) {
-                return -1;
-            }
-            if(len<=xlen) {
-                LOGE("___ %s val len %d longer than recv buf len %d\n", key, xlen, len);
-                return -1;
-            }
-            strcpy((char*)val, p);
+        if(!p) {
+            return -1;
+        }
+
+        int xlen=strlen(p);
+        if(str_chk && xlen==0) {
+            return -1;
+        }
+        if(len<=xlen) {
+            LOGE("___ %s val len %d longer than recv buf len %d\n", key, xlen, len);
+            return -1;
         }
+        strcpy((char*)val, p);
     }
     else if(type==TYPE_FLOAT) {
         double c=-10000.0;
@@ -1034,7 +1101,7 @@ static int proc_mqtt(char *path, int op, mqtt_info_t *mqtt)
                         r = -1;
                     }
 
-                    if(ser->id!=-1 || ser->id != i) {
+                    if(ser->id!=-1 && ser->id != i) {
                         LOGE("___ check mqtt[%d], id %d is mismatch the index\n", i, ser->id);
                         r = -1;
                     }
@@ -1064,7 +1131,6 @@ static int proc_mqtt(char *path, int op, mqtt_info_t *mqtt)
             for(i=0; i<MQTT_SER_MAX; i++) {
                 ser = &pmqtt->ser[i];
                 if(ser->id>=0) {
-                    fprintf(fp, "broker%d.plat      = \"%d\"\n", i+1,ser->plat);
                     fprintf(fp, "broker%d.plat      = \"%d\"\n", i+1,ser->plat);
                     fprintf(fp, "broker%d.mode      = \"%d\"\n", i+1,ser->mode);
                     //fprintf(fp, "broker%d.proto     = \"%s\"\n", i+1,ser->proto);
@@ -1095,7 +1161,7 @@ static int proc_mqtt(char *path, int op, mqtt_info_t *mqtt)
                 ser->id = -1;
 
                 sprintf(token, "mqtt:broker%d.plat", i+1);
-                r = ini_rw(dic, token, &ser->plat, sizeof(ser->plat), TYPE_INT, op, 1); if(r) goto quit;
+                r = ini_rw(dic, token, &ser->plat, sizeof(ser->plat), TYPE_INT, op, 1); if(r) break;
 
                 sprintf(token, "mqtt:broker%d.mode", i+1);
                 r = ini_rw(dic, token, &ser->mode, sizeof(ser->mode), TYPE_INT, op, 1); if(r) break;
@@ -1148,8 +1214,10 @@ static int proc_mqtt(char *path, int op, mqtt_info_t *mqtt)
             for(i=0; i<MQTT_SER_MAX; i++) {
                 ser = &pmqtt->ser[i];
 
+                if(ser->id==-1) break;
+
                 sprintf(token, "mqtt:broker%d.plat", i+1);
-                r = ini_rw(dic, token, &ser->plat, sizeof(ser->plat), TYPE_INT, op, 1); if(r) goto quit;
+                r = ini_rw(dic, token, &ser->plat, sizeof(ser->plat), TYPE_INT, op, 1); if(r) break;
 
                 sprintf(token, "mqtt:broker%d.mode", i+1);
                 r = ini_rw(dic, token, &ser->mode, sizeof(ser->mode), TYPE_INT, op, 1); if(r) break;
@@ -1331,7 +1399,6 @@ static int proc_mail(char *path, int op, mail_info_t *mail)
                 recv->use = 1;
             }
 
-            r = proc_mail(path, PARAS_CHECK, pmail);  if(r) goto quit;
             r = dev_clear_table(dm->db, TAB_ID_MAIL);  if(r) goto quit;
             r = dev_mail_update_send(dm->db, &pmail->send); if(r) goto quit;
             r = dev_mail_update_recv(dm->db, pmail->recv);  if(r) goto quit;
@@ -1346,7 +1413,6 @@ static int proc_mail(char *path, int op, mail_info_t *mail)
             send = &pmail->send;
             
             r = dev_mail_get(dm->db, pmail); if(r) return -1;
-            r = proc_mail(path, PARAS_CHECK, pmail); if(r) return -1;
             dic = dictionary_new(0); if(!dic) return -1;
 
             r = ini_rw(dic, "mail:send.mode",     &send->mode,    sizeof(send->mode),     TYPE_INT, op, 1); if(r) goto quit;
@@ -2594,29 +2660,29 @@ static int proc_power3(char *path, int op, GlobalTreeACManager *power)
                     ///////////voltage//////////
                     GlobalOverManager *pover=&tmp->global_over_manager;
                     //upper
-                    sprintf(token, "power:channel%d.threshold.voltate.up.en",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.up.en",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_upper_enable, sizeof(pover->product_vol_upper_enable), TYPE_INT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.up.val",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.up.val",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_upper_threshold, sizeof(pover->product_vol_upper_threshold), TYPE_FLOAT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.up.act",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.up.act",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_over_upper_threshold_ctrl, sizeof(pover->product_vol_over_upper_threshold_ctrl), TYPE_INT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.up.para",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.up.para",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_over_upper_threshold_ctrl_para, sizeof(pover->product_vol_over_upper_threshold_ctrl_para), TYPE_INT, op, 1); if(r) break;
 
                     //lower
-                    sprintf(token, "power:channel%d.threshold.voltate.lo.en",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.lo.en",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_lower_enable, sizeof(pover->product_vol_lower_enable), TYPE_INT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.lo.val",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.lo.val",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_lower_threshold, sizeof(pover->product_vol_lower_threshold), TYPE_FLOAT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.lo.act",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.lo.act",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_over_lower_threshold_ctrl, sizeof(pover->product_vol_over_lower_threshold_ctrl), TYPE_INT, op, 1); if(r) break;
 
-                    sprintf(token, "power:channel%d.threshold.voltate.lo.para",channelID);
+                    sprintf(token, "power:channel%d.threshold.voltage.lo.para",channelID);
                     r = ini_rw(dic, token, &pover->product_vol_over_lower_threshold_ctrl_para, sizeof(pover->product_vol_over_lower_threshold_ctrl_para), TYPE_INT, op, 1); if(r) break;
 
                     ///////////current//////////
@@ -2739,29 +2805,29 @@ static int proc_power3(char *path, int op, GlobalTreeACManager *power)
                 ///////////voltage//////////
                 GlobalOverManager *pover=&tmp->global_over_manager;
                 //upper
-                sprintf(token, "power:channel%d.threshold.voltate.up.en",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.up.en",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_upper_enable, sizeof(pover->product_vol_upper_enable), TYPE_INT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.up.val",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.up.val",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_upper_threshold, sizeof(pover->product_vol_upper_threshold), TYPE_FLOAT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.up.act",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.up.act",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_over_upper_threshold_ctrl, sizeof(pover->product_vol_over_upper_threshold_ctrl), TYPE_INT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.up.para",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.up.para",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_over_upper_threshold_ctrl_para, sizeof(pover->product_vol_over_upper_threshold_ctrl_para), TYPE_INT, op, 1); if(r) break;
 
                 //lower
-                sprintf(token, "power:channel%d.threshold.voltate.lo.en",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.lo.en",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_lower_enable, sizeof(pover->product_vol_lower_enable), TYPE_INT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.lo.val",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.lo.val",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_lower_threshold, sizeof(pover->product_vol_lower_threshold), TYPE_FLOAT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.lo.act",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.lo.act",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_over_lower_threshold_ctrl, sizeof(pover->product_vol_over_lower_threshold_ctrl), TYPE_INT, op, 1); if(r) break;
 
-                sprintf(token, "power:channel%d.threshold.voltate.lo.para",channelID);
+                sprintf(token, "power:channel%d.threshold.voltage.lo.para",channelID);
                 r = ini_rw(dic, token, &pover->product_vol_over_lower_threshold_ctrl_para, sizeof(pover->product_vol_over_lower_threshold_ctrl_para), TYPE_INT, op, 1); if(r) break;
 
                 ///////////current//////////

+ 8 - 1
pro/src/sqlite_handle.c

@@ -4669,6 +4669,13 @@ int dev_mail_update_recv(sqlite3 *db, mail_recv_t *recv)
 
 ////////////////mqtt/////////////////////////////////////////////
 #define MQTT_MANAGER_TABLE   "Table_MQTTManage"
+static void mq_init(mqtt_info_t *info)
+{
+    memset(info, 0, sizeof(mqtt_info_t));
+    for(int i=0;i<MQTT_SER_MAX;i++) {
+        info->ser[i].id = -1;
+    }
+}
 int dev_mqtt_get(sqlite3 *db, mqtt_info_t *info)
 {
     int i,r,idx=0;
@@ -4688,7 +4695,7 @@ int dev_mqtt_get(sqlite3 *db, mqtt_info_t *info)
         return -1;
     }
     
-    memset(info, 0, sizeof(mqtt_info_t));
+    mq_init(info);
     char *p=NULL;
     while (sqlite3_step(stmt) == SQLITE_ROW) {
         ser[idx].id = sqlite3_column_int(stmt, 0);

+ 35 - 0
pro/src/websocket_handle.c

@@ -31,6 +31,8 @@ typedef struct {
     mg_tls_t  tls;
 
     mg_conn_t *c;
+    mg_conn_t *upg;
+
     int       inited;
     int       ws_cnt;
     pwrall_info_t pwrall;
@@ -84,6 +86,28 @@ static int ws_send_data(ws_handle_t *wh, void *data, int len)
 #endif
 }
 
+static int get_board_info(board_info_t *info)
+{
+    return 0;
+}
+
+static int ws_upg_update(ws_handle_t *wh)
+{
+    if((!wh->upg)) {
+        return -1;
+    }
+    
+    board_info_t info;
+    int r = get_board_info(&info);
+    char* json = board_to_json(&info);
+
+    mg_ws_send2(wh->upg, json, strlen(json), WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
+
+    return 0;
+}
+
+
+
 
 static void web_update(ws_handle_t *wh)
 {
@@ -209,6 +233,9 @@ static void web_update(ws_handle_t *wh)
             ws_send_data(wh, json_str, strlen(json_str));
             cJSON_free(json_str);
         }
+
+        //upgrade refresh
+
     }
 }
 static void fn(mg_conn_t *c, int ev, void *ev_data) 
@@ -233,6 +260,10 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
         if(c==wh->c) {
             wh->c = NULL;
         }
+
+        if(c==wh->upg) {
+            wh->upg = NULL;
+        }
       }
       log_i("ws upgrade del connection ID:%d\n", c->id);
     }
@@ -260,6 +291,10 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
             wh->c = c;
             log_i("ws upgrade new connection ID:%d\n", c->id);
         }
+        else if (mg_match(hm->uri, mg_str("/websocket/controlBoardUpgrade"), NULL)) {
+            mg_ws_upgrade(c, hm, NULL);
+            wh->upg = c;
+        }
         else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
             mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
         }