Skip to content

Commit

Permalink
fix format error and lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
zongke committed Apr 20, 2021
1 parent 2e4579d commit 96d97e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
29 changes: 12 additions & 17 deletions pkg/modbus/common/data_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (
)

func SwitchRegister(value []byte) []byte {

for i := 0; i < len(value)/2; i = i+2 {
j := len(value)-i-2
for i := 0; i < len(value)/2; i = i + 2 {
j := len(value) - i - 2
value[i], value[j] = value[j], value[i]
value[i+1], value[j+1] = value[j+1], value[i+1]
}
Expand All @@ -36,17 +35,15 @@ func SwitchRegister(value []byte) []byte {
}

func SwitchByte(value []byte) []byte {

for i := 0; i < len(value); i = i+2 {
value[i], value[i + 1] = value[i + 1], value[i]
for i := 0; i < len(value); i = i + 2 {
value[i], value[i+1] = value[i+1], value[i]
}
return value
}

func TransferData(isRegisterSwap bool, isSwap bool,
dataType string, scale float64,
value []byte) (string, error) {

// accord IsSwap/IsRegisterSwap to transfer byte array
if isRegisterSwap {
SwitchRegister(value)
Expand Down Expand Up @@ -101,21 +98,19 @@ func TransferData(isRegisterSwap bool, isSwap bool,
case "double":
if len(value) != 8 {
return "", errors.New("BytesToDouble bytes length is invalid")
} else {
bits := binary.BigEndian.Uint64(value)
data := math.Float64frombits(bits) * scale
sData := strconv.FormatFloat(data,'f', 6, 64)
return sData, nil
}
bits := binary.BigEndian.Uint64(value)
data := math.Float64frombits(bits) * scale
sData := strconv.FormatFloat(data, 'f', 6, 64)
return sData, nil
case "float":
if len(value) != 4 {
return "", errors.New("BytesToFloat bytes length is invalid")
} else {
bits := binary.BigEndian.Uint32(value)
data := float64(math.Float32frombits(bits)) * scale
sData := strconv.FormatFloat(data,'f', 6, 64)
return sData, nil
}
bits := binary.BigEndian.Uint32(value)
data := float64(math.Float32frombits(bits)) * scale
sData := strconv.FormatFloat(data, 'f', 6, 64)
return sData, nil
case "boolean":
return strconv.FormatBool(value[0] == 1), nil
case "string":
Expand Down
4 changes: 2 additions & 2 deletions pkg/modbus/device/twindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"k8s.io/klog/v2"

"github.com/kubeedge/mappers-go/pkg/common"
modbuscommon "github.com/kubeedge/kubeedge/mappers/modbus-go/common"
modbuscommon "github.com/kubeedge/mappers-go/pkg/modbus/common"
"github.com/kubeedge/mappers-go/pkg/modbus/configmap"
"github.com/kubeedge/mappers-go/pkg/modbus/driver"
"github.com/kubeedge/mappers-go/pkg/modbus/globals"
"github.com/kubeedge/kubeedge/mappers/modbus-go/configmap"
)

// TwinData is the timer structure for getting twin/data.
Expand Down

0 comments on commit 96d97e6

Please sign in to comment.