diff --git a/datautil/datautil.go b/datautil/datautil.go index fbeb1db..aff2c67 100644 --- a/datautil/datautil.go +++ b/datautil/datautil.go @@ -66,10 +66,11 @@ 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: +func MakeChartPack(y *variable.ListT, data []byte) (x variable.ListChartT, add variable.ListT, del variable.ListT) { + x = variable.ListChartT{} + add = variable.ListT{} // 有些变量,我难以忘记 + del = variable.ListT{} // 有些变量,我不愿提起 + dataList := variable.ListT{} for i := 0; i < len(data)/20; i++ { // 变量的板子、心跳和地址 dataVar := variable.T{ @@ -79,58 +80,53 @@ 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) } + return } diff --git a/datautil/datautil_test.go b/datautil/datautil_test.go index e40f424..09008cd 100644 --- a/datautil/datautil_test.go +++ b/datautil/datautil_test.go @@ -195,39 +195,33 @@ func TestMakeChartPack(t *testing.T) { 0x0a, }, listV: variable.ListT{ - Variables: []variable.T{ - { - Board: 1, - Name: "a", - Type: "float", - Addr: 0x80123456, - }, - { - Board: 1, - Name: "b", - Type: "int", - Addr: 0x80654321, - }, + 0x80123456: { + Board: 1, + Name: "a", + Type: "float", + Addr: 0x80123456, + }, + 0x80654321: { + Board: 1, + Name: "b", + Type: "int", + Addr: 0x80654321, }, }, wantChart: variable.ListChartT{ - Variables: []variable.ToChartT{ - { - Board: 1, - Name: "a", - Data: -8.25, - Tick: 1, - }, + { + Board: 1, + Name: "a", + Data: -8.25, + Tick: 1, }, }, wantAdd: variable.ListT{ - Variables: []variable.T{ - { - Board: 1, - Name: "b", - Type: "int", - Addr: 0x80654321, - }, + 0x80654321: { + Board: 1, + Name: "b", + Type: "int", + Addr: 0x80654321, }, }, wantDel: variable.ListT{}, @@ -247,33 +241,27 @@ func TestMakeChartPack(t *testing.T) { 0x0a, }, listV: variable.ListT{ - Variables: []variable.T{ - { - Board: 1, - Name: "a", - Type: "float", - Addr: 0x80123456, - }, + 0x80123456: { + Board: 1, + Name: "a", + Type: "float", + Addr: 0x80123456, }, }, wantChart: variable.ListChartT{ - Variables: []variable.ToChartT{ - { - Board: 1, - Name: "a", - Data: -8.25, - Tick: 1, - }, + { + Board: 1, + Name: "a", + Data: -8.25, + Tick: 1, }, }, wantAdd: variable.ListT{}, wantDel: variable.ListT{ - Variables: []variable.T{ - { - Board: 1, - Type: "uint32_t", - Addr: 0x80654321, - }, + 0x80654321: { + Board: 1, + Type: "uint32_t", + Addr: 0x80654321, }, }, }, @@ -286,17 +274,17 @@ func TestMakeChartPack(t *testing.T) { return sa == sb, "Chart Not Same: " + sa + ", " + sb } isSame := func(a variable.ListT, b variable.ListT) (bool, string) { - if len(a.Variables) != len(b.Variables) { + if len(a) != len(b) { return false, "" } - for i := range a.Variables { - if a.Variables[i].Board != b.Variables[i].Board { + for i := range a { + if a[i].Board != b[i].Board { return false, "" } - if a.Variables[i].Addr != b.Variables[i].Addr { + if a[i].Addr != b[i].Addr { return false, "" } - if variable.TypeLen[a.Variables[i].Type] != variable.TypeLen[b.Variables[i].Type] { + if variable.TypeLen[a[i].Type] != variable.TypeLen[b[i].Type] { return false, "" } } @@ -308,10 +296,7 @@ func TestMakeChartPack(t *testing.T) { return true, "List Not Same: " + sa + ", " + sb } for _, c := range cases { - var gotChart variable.ListChartT - var gotAdd variable.ListT - var gotDel variable.ListT - MakeChartPack(&gotChart, &gotAdd, &gotDel, &c.listV, c.in) + gotChart, gotAdd, gotDel := MakeChartPack(&c.listV, c.in) if ok, msg := isSameChart(gotChart, c.wantChart); !ok { t.Errorf("MakeChartPack, chart: " + msg) } 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..6d69d64 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,14 @@ func GrRxPrase(c chan string) { buff := rxBuff[startIdx:endIdx] // 撷取甜蜜的片段 // 拼凑出完整的清单 - x.Variables = nil - var add variable.ListT // 有些变量,我难以忘记 - var del variable.ListT // 有些变量,我不愿提起 - datautil.MakeChartPack(&x, &add, &del, &variable.ToRead, buff) - if len(x.Variables) != 0 { + x, add, del := datautil.MakeChartPack(&variable.ToRead, buff) + 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 +192,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/serial/testport.go b/serial/testport.go index b98643a..7d07ecf 100644 --- a/serial/testport.go +++ b/serial/testport.go @@ -17,10 +17,13 @@ type testPort struct { createdTime time.Time } +var BoardSysTime time.Time = time.Now() // 虚拟电路板的系统时间 + func newTestPort() serial.Port { + log.Println(BoardSysTime) return &testPort{ readingAddresses: []uint32{}, - createdTime: time.Now(), + createdTime: BoardSysTime, } } 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 @@