Skip to content

Commit

Permalink
能够删除和添加变量
Browse files Browse the repository at this point in the history
  • Loading branch information
BigeYoung committed Apr 26, 2022
1 parent 9a52de9 commit d95f7ed
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 152 deletions.
92 changes: 42 additions & 50 deletions datautil/datautil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
}
18 changes: 9 additions & 9 deletions datautil/datautil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions fromelf/fromelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -127,7 +127,7 @@ func ReadVariable(x *variable.ListProjectT, f *elf.File) error {
}

// 却总有些事,难以忘记
x.Variables = append(x.Variables, y)
(*x)[y.Addr] = y
}
}
}
Expand Down Expand Up @@ -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(),
})
}
}
}
}
Expand Down Expand Up @@ -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())
}
Expand Down
15 changes: 7 additions & 8 deletions serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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...) // 深藏我的心底
Expand All @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 16 additions & 18 deletions server/variablectrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
// 为变量赋值
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, "")
Expand Down
2 changes: 1 addition & 1 deletion src/api/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit d95f7ed

Please sign in to comment.