Преглед изворни кода

js解析通道名字有误,改正
单次发送通道数目改为10,防止通道太多内存不足

guohui пре 1 година
родитељ
комит
94f2cd6def
4 измењених фајлова са 60 додато и 47 уклоњено
  1. 1 1
      f407/applications/power.c
  2. 27 33
      f407/applications/web.c
  3. 15 0
      f407/project/app/app.uvoptx
  4. 17 13
      f407/tools/web/data.js

+ 1 - 1
f407/applications/power.c

@@ -862,7 +862,7 @@ int power_scan(void)
                 return -1;
             }
             
-            times = (ch_idx+1)%nGroups?(ch_idx+1):nGroups;
+            times = (ch_idx%nGroups)?ch_idx:nGroups;
             for(j=0; j<pkey->chs; j++) {
                 pch[j].info.addr = i;
                 pch[j].info.sch  = j;                    //序号从0开始

+ 27 - 33
f407/applications/web.c

@@ -9,7 +9,8 @@
 #include "datadef.h"
 
 #define TIMER_INTERVAL     1000
-typedef void (*http_fn_t)(void *arg);
+#define ONCE_SEND_CH_CNT   10
+
 
 typedef struct mg_mgr mg_mgr_t;
 typedef struct mg_tls_opts mg_tls_t;
@@ -40,6 +41,9 @@ static int is_ws(mg_conn_t *c)
 }
 static int ws_send(mg_conn_t *c, void *data, int len, int isbin)
 {
+    if(!c || !data || len<=0) {
+        return -1;
+    }
     LOGD("____ws_send, len: %d\n", len);
     mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
     
@@ -68,47 +72,37 @@ static int ws_send_pkt(web_handle_t *h, mg_conn_t *c, uint8_t type, void *data,
 
 static void send_powerc(web_handle_t *h, mg_conn_t *c)
 {
-    int i,xlen,offset;
-    pkt_hdr_t *hdr=NULL;
-    power_data_t pd=power_data_get();
-    
-    for(i=0; i<pd.chs; i++) {
-        ws_send_pkt(h, c, PKT_TYPE_POWER_CH, &pd.pch[i], sizeof(power_ch_t), 1);
-    }
-}
-static void send_powerc_once(web_handle_t *h, mg_conn_t *c)
-{
-    int i,xlen,offset;
+    int i,j,xlen,offset,times,left;
     pkt_hdr_t *hdr=NULL;
     power_data_t pd=power_data_get();
     
-    pd.chs = 8;
-    if(pd.chs==0) {
-        return;
-    }
-    
-    xlen = pd.chs*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
-    if(!h->buf || h->buflen<xlen) {
-        if(h->buf) free(h->buf);
-        h->buf = malloc(xlen);
-        if(h->buf) {
-            h->buflen = xlen;
-        }
-    }
-
-    if(!h->buf) {
+    if(pd.chs==0 || h->buf==NULL ) {
         return;
     }
     hdr = (pkt_hdr_t*)h->buf;
-
+    
     hdr->magic = PKT_MAGIC;
     hdr->type  = PKT_TYPE_POWER_CH;
     hdr->nack  = 0;
-    hdr->dlen  = xlen-sizeof(pkt_hdr_t);
-    for(i=0; i<pd.chs; i++) {
+    
+    times = pd.chs/ONCE_SEND_CH_CNT;
+    for(i=0; i<times; i++) {
+        for(j=0; j<ONCE_SEND_CH_CNT; j++) {
+            offset = j*sizeof(power_ch_t);
+            memcpy(hdr->data+offset, &pd.pch[i*ONCE_SEND_CH_CNT+j], sizeof(power_ch_t));
+        }
+        xlen = ONCE_SEND_CH_CNT*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
+        hdr->dlen  = xlen-sizeof(pkt_hdr_t);
+        ws_send(c, h->buf, xlen, 1);
+    }
+    
+    left = pd.chs%ONCE_SEND_CH_CNT;
+    for(i=0; i<left; i++) {
         offset = i*sizeof(power_ch_t);
-        memcpy(hdr->data+offset, &pd.pch[i], sizeof(power_ch_t));
+        memcpy(hdr->data+offset, &pd.pch[pd.chs-left+i], sizeof(power_ch_t));
     }
+    xlen = (pd.chs%ONCE_SEND_CH_CNT)*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
+    hdr->dlen  = xlen-sizeof(pkt_hdr_t);
     ws_send(c, h->buf, xlen, 1);
 }
 
@@ -182,7 +176,7 @@ static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, mg_conn_t *c)
             }
             else {
                 LOGD("_____ send_powerc\n");
-                send_powerc_once(h, c);
+                send_powerc(h, c);
             }
         }
         break;
@@ -325,7 +319,7 @@ int web_init(void)
     memset(h, 0, sizeof(web_handle_t));
     h->list = list_init(&lc);
     
-    h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)+32;
+    h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)*ONCE_SEND_CH_CNT+8;
     h->buf = (char*)malloc(h->buflen);
     
     thread_start(THREAD_ID_WEB, web_thread, h);

+ 15 - 0
f407/project/app/app.uvoptx

@@ -189,6 +189,21 @@
           <WinNumber>1</WinNumber>
           <ItemText>fdata.width</ItemText>
         </Ww>
+        <Ww>
+          <count>8</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>pd.pch[0]</ItemText>
+        </Ww>
+        <Ww>
+          <count>9</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>pd.pch[1]</ItemText>
+        </Ww>
+        <Ww>
+          <count>10</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>((power_ch_t*)(((pkt_hdr_t*)h-&gt;buf)-&gt;data))[2]</ItemText>
+        </Ww>
       </WatchWindow1>
       <MemoryWindow1>
         <Mm>

+ 17 - 13
f407/tools/web/data.js

@@ -29,24 +29,28 @@ const SUB_TYPE=function(){
     }
 }();
 
-
-
+const PDU_TYPE=function(){
+    return {
+    PDU_AC_I1O1:0,
+    PDU_DC_I1O1:1,
+    PDU_AC_I3O3:3,
+    PDU_AC_I3O1:4,
+    PDU_AC_I3O1_H:5,
+    }
+}();
 ///////////////////////////////////////////////////////
 function array_to_string(data){
-    var s = "";
-    for(var i=0;i<data.length  && data[i] !=0;i++){
-        s +=String.fromCharCode(data[i]);
-    }
-    return s;
+    const decoder = new TextDecoder();
+    return decoder.decode(data);
 }
 
 function string_to_array(data,len){
-    var buffer = new ArrayBuffer(len);
-    for(var i=0;i<data.length;i++){
-        buffer[i] = data.charCodeAt(i);
-    }
-    buffer[len-1] = 0;
-    return buffer;
+    const encoder = new TextEncoder(); // ĬÈÏ'utf-8'±àÂë
+    const buffer = encoder.encode(data);
+    const fixedArray = new Uint8Array(len);
+    fixedArray.set(encoder.subarray(0, len), 0);
+    
+    return fixedArray.buffer
 }
 function array_cat(a1,a2){
     var tlen=a1.byteLength+a2.byteLength;