diff --git a/datautil/datautil.go b/datautil/datautil.go index fbeb1db..e54701e 100644 --- a/datautil/datautil.go +++ b/datautil/datautil.go @@ -67,9 +67,7 @@ func FindValidPart(data []byte) (int, int) { // 从茫茫 data 中,寻找我所挂念的列表 y ,记录在列表 x 中。 // 所有的 add 我都难以忘记,所有的 del 我都不愿提起 func MakeChartPack(x *variable.ListChartT, add *variable.ListT, del *variable.ListT, y *variable.ListT, data []byte) { - var dataList variable.ListT - -OUTER1: + dataList := variable.ListT{} for i := 0; i < len(data)/20; i++ { // 变量的板子、心跳和地址 dataVar := variable.T{ @@ -79,58 +77,52 @@ OUTER1: Tick: variable.BytesToUint32(data[i*20+15 : i*20+19])} // 它是我要找的那个变量吗? - for _, v := range y.Variables { - if v.Addr == dataVar.Addr { // 是的,我还挂念着它 - dataVar.Name = v.Name - dataVar.Type = v.Type - switch v.Type { - case "uint8_t": - dataVar.Data = float64(variable.BytesToUint8(data[i*20+7 : i*20+15])) - case "uint16_t": - dataVar.Data = float64(variable.BytesToUint16(data[i*20+7 : i*20+15])) - case "uint32_t": - dataVar.Data = float64(variable.BytesToUint32(data[i*20+7 : i*20+15])) - case "uint64_t": - dataVar.Data = float64(variable.BytesToUint64(data[i*20+7 : i*20+15])) - case "int8_t": - dataVar.Data = float64(variable.BytesToInt8(data[i*20+7 : i*20+15])) - case "int16_t": - dataVar.Data = float64(variable.BytesToInt16(data[i*20+7 : i*20+15])) - case "int32_t", "int": - dataVar.Data = float64(variable.BytesToInt32(data[i*20+7 : i*20+15])) - case "int64_t": - dataVar.Data = float64(variable.BytesToInt64(data[i*20+7 : i*20+15])) - case "float": - dataVar.Data = float64(variable.BytesToFloat32(data[i*20+7 : i*20+15])) - case "double": - dataVar.Data = float64(variable.BytesToFloat64(data[i*20+7 : i*20+15])) - default: - dataVar.Data = 0 - } - dataList.Variables = append(dataList.Variables, dataVar) - - chartVar := variable.ToChartT{ - Board: dataVar.Board, - Name: dataVar.Name, - Data: dataVar.Data, - Tick: dataVar.Tick} - x.Variables = append(x.Variables, chartVar) - continue OUTER1 + if v, ok := (*y)[dataVar.Addr]; ok { // 是的,我还挂念着它 + dataVar.Name = v.Name + dataVar.Type = v.Type + switch v.Type { + case "uint8_t": + dataVar.Data = float64(variable.BytesToUint8(data[i*20+7 : i*20+15])) + case "uint16_t": + dataVar.Data = float64(variable.BytesToUint16(data[i*20+7 : i*20+15])) + case "uint32_t": + dataVar.Data = float64(variable.BytesToUint32(data[i*20+7 : i*20+15])) + case "uint64_t": + dataVar.Data = float64(variable.BytesToUint64(data[i*20+7 : i*20+15])) + case "int8_t": + dataVar.Data = float64(variable.BytesToInt8(data[i*20+7 : i*20+15])) + case "int16_t": + dataVar.Data = float64(variable.BytesToInt16(data[i*20+7 : i*20+15])) + case "int32_t", "int": + dataVar.Data = float64(variable.BytesToInt32(data[i*20+7 : i*20+15])) + case "int64_t": + dataVar.Data = float64(variable.BytesToInt64(data[i*20+7 : i*20+15])) + case "float": + dataVar.Data = float64(variable.BytesToFloat32(data[i*20+7 : i*20+15])) + case "double": + dataVar.Data = float64(variable.BytesToFloat64(data[i*20+7 : i*20+15])) + default: + dataVar.Data = 0 } + dataList[dataVar.Addr] = dataVar + + chartVar := variable.ToChartT{ + Board: dataVar.Board, + Name: dataVar.Name, + Data: dataVar.Data, + Tick: dataVar.Tick} + *x = append(*x, chartVar) + } else { + // 有些变量,我已不愿提起 + (*del)[dataVar.Addr] = dataVar } - // 有些变量,我已不愿提起 - del.Variables = append(del.Variables, dataVar) } // 我所挂念的,它们都还在吗 -OUTER2: - for _, v := range y.Variables { - for _, dv := range dataList.Variables { - if dv.Addr == v.Addr { - continue OUTER2 - } + for _, v := range *y { + if _, ok := dataList[v.Addr]; !ok { + // 我很想它,下次请别忘记 + (*add)[v.Addr] = v } - // 我很想它,下次请别忘记 - add.Variables = append(add.Variables, v) } } diff --git a/datautil/datautil_test.go b/datautil/datautil_test.go index e40f424..836d57a 100644 --- a/datautil/datautil_test.go +++ b/datautil/datautil_test.go @@ -195,14 +195,14 @@ func TestMakeChartPack(t *testing.T) { 0x0a, }, listV: variable.ListT{ - Variables: []variable.T{ - { + Variables: map[uint32]variable.T{ + 0x80123456: { Board: 1, Name: "a", Type: "float", Addr: 0x80123456, }, - { + 0x80654321: { Board: 1, Name: "b", Type: "int", @@ -221,8 +221,8 @@ func TestMakeChartPack(t *testing.T) { }, }, wantAdd: variable.ListT{ - Variables: []variable.T{ - { + Variables: map[uint32]variable.T{ + 0x80654321: { Board: 1, Name: "b", Type: "int", @@ -247,8 +247,8 @@ func TestMakeChartPack(t *testing.T) { 0x0a, }, listV: variable.ListT{ - Variables: []variable.T{ - { + Variables: map[uint32]variable.T{ + 0x80123456: { Board: 1, Name: "a", Type: "float", @@ -268,8 +268,8 @@ func TestMakeChartPack(t *testing.T) { }, wantAdd: variable.ListT{}, wantDel: variable.ListT{ - Variables: []variable.T{ - { + Variables: map[uint32]variable.T{ + 0x80654321: { Board: 1, Type: "uint32_t", Addr: 0x80654321, diff --git a/fromelf/fromelf.go b/fromelf/fromelf.go index 03790ad..5c725bc 100644 --- a/fromelf/fromelf.go +++ b/fromelf/fromelf.go @@ -44,7 +44,7 @@ func Check(f *os.File) (*elf.File, error) { func ReadVariable(x *variable.ListProjectT, f *elf.File) error { // 排除一切杂念,听你娓娓道来 - x.Variables = nil + x = &variable.ListProjectT{} // 故事很长,从那一天说起吧 dwarfData, err := f.DWARF() @@ -127,7 +127,7 @@ func ReadVariable(x *variable.ListProjectT, f *elf.File) error { } // 却总有些事,难以忘记 - x.Variables = append(x.Variables, y) + (*x)[y.Addr] = y } } } @@ -171,11 +171,12 @@ func dfsStruct(namePrefix []string, addrPrefix uint32, x *variable.ListProjectT, } // 道出心底的秘密 - x.Variables = append(x.Variables, variable.ToProjectT{ + addr := fmt.Sprintf("0x%08x", a) + (*x)[addr] = variable.ToProjectT{ Name: strings.Join(namePrefix, ".") + "." + v.Name, - Addr: fmt.Sprintf("0x%08x", a), + Addr: addr, Type: v.Type.String(), - }) + } } } } @@ -219,11 +220,12 @@ func dfsArray(namePrefix []string, addrPrefix uint32, x *variable.ListProjectT, if _, ok := variable.TypeLen[t]; !ok { continue } - x.Variables = append(x.Variables, variable.ToProjectT{ + addr := fmt.Sprintf("0x%08x", a) + (*x)[addr] = variable.ToProjectT{ Name: strings.Join(namePrefix, ".") + ".[" + strconv.FormatInt(i, 10) + "]", Addr: fmt.Sprintf("0x%08x", a), Type: t, - }) + } } addrPrefix = addrPrefix + uint32(t.Size()) } diff --git a/serial/serial.go b/serial/serial.go index 99e2175..3473401 100644 --- a/serial/serial.go +++ b/serial/serial.go @@ -128,7 +128,7 @@ func GrReceive() { buff := make([]byte, 200) for { <-chOp - for _, v := range variable.ToRead.Variables { + for _, v := range variable.ToRead { SendCmd(datautil.ActModeSubscribe, v) time.Sleep(10 * time.Millisecond) } @@ -163,7 +163,6 @@ func GrTransmit() { func GrRxPrase(c chan string) { var rxBuff []byte - var x variable.ListChartT for { rx := <-chRx // 收到你的来信 rxBuff = append(rxBuff, rx...) // 深藏我的心底 @@ -177,17 +176,17 @@ func GrRxPrase(c chan string) { buff := rxBuff[startIdx:endIdx] // 撷取甜蜜的片段 // 拼凑出完整的清单 - x.Variables = nil - var add variable.ListT // 有些变量,我难以忘记 - var del variable.ListT // 有些变量,我不愿提起 + x := variable.ListChartT{} + var add variable.ListT = variable.ListT{} // 有些变量,我难以忘记 + var del variable.ListT = variable.ListT{} // 有些变量,我不愿提起 datautil.MakeChartPack(&x, &add, &del, &variable.ToRead, buff) - if len(x.Variables) != 0 { + if len(x) != 0 { b, _ := json.Marshal(x) c <- string(b) } // 挂念的变量,还望顺问近祺 - for _, v := range add.Variables { + for _, v := range add { err := SendCmd(datautil.ActModeSubscribe, v) if err != nil { logger.Log.Println("SendCmd error:", err) @@ -196,7 +195,7 @@ func GrRxPrase(c chan string) { } // 无缘的变量,就请随风逝去 - for _, v := range del.Variables { + for _, v := range del { err := SendCmd(datautil.ActModeUnSubscribe, v) if err != nil { logger.Log.Println("SendCmd error:", err) diff --git a/server/variablectrl.go b/server/variablectrl.go index f22a31e..ab73831 100644 --- a/server/variablectrl.go +++ b/server/variablectrl.go @@ -41,14 +41,12 @@ func makeVariableCtrl(vList *variable.ListT, isVToRead bool) func(w http.Respons io.WriteString(w, errorJson("Address out of range")) return } - for _, v := range vList.Variables { - if v.Addr == newVariable.Addr { - w.WriteHeader(http.StatusBadRequest) - io.WriteString(w, errorJson("Address already used")) - return - } + if _, ok := (*vList)[newVariable.Addr]; ok { + w.WriteHeader(http.StatusBadRequest) + io.WriteString(w, errorJson("Address already used")) + return } - vList.Variables = append(vList.Variables, newVariable) + (*vList)[newVariable.Addr] = newVariable w.WriteHeader(http.StatusNoContent) io.WriteString(w, "") // 为变量赋值 @@ -90,17 +88,17 @@ func makeVariableCtrl(vList *variable.ListT, isVToRead bool) func(w http.Respons return } + // 我认为不必再检查是否存在这个变量 + // if _, ok := vList.Variables[oldVariable.Addr]; !ok { + // w.WriteHeader(http.StatusBadRequest) + // io.WriteString(w, errorJson("No such address")) + // } + // 从 vList.Variables 中删除地址为 oldVariable.Addr 的变量 - for i, v := range vList.Variables { - if v.Addr == oldVariable.Addr { - vList.Variables = append(vList.Variables[:i], vList.Variables[i+1:]...) - w.WriteHeader(http.StatusNoContent) - io.WriteString(w, "") - return - } - } - w.WriteHeader(http.StatusBadRequest) - io.WriteString(w, errorJson("No such address")) + delete(*vList, oldVariable.Addr) + w.WriteHeader(http.StatusNoContent) + io.WriteString(w, "") + return default: w.WriteHeader(http.StatusMethodNotAllowed) @@ -157,7 +155,7 @@ func variableToProjCtrl(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) io.WriteString(w, "") case http.MethodDelete: - variable.ToProj.Variables = nil + variable.ToProj = variable.ListProjectT{} w.WriteHeader(http.StatusNoContent) io.WriteString(w, "") diff --git a/src/api/variable.js b/src/api/variable.js index e058548..fde40b4 100644 --- a/src/api/variable.js +++ b/src/api/variable.js @@ -2,7 +2,7 @@ import { fetchApi, uploadFile } from "./internal"; export async function getVariable(mode) { const res = await fetchApi("/variable_" + mode); - return res.Variables; + return res; } export async function getVariableType() { diff --git a/src/components/ChartCard.vue b/src/components/ChartCard.vue index 277600e..b5fb046 100644 --- a/src/components/ChartCard.vue +++ b/src/components/ChartCard.vue @@ -16,7 +16,6 @@