Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmi mapper bugfix #109

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/crd-samples/devices/modbus-device-model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ spec:
int:
accessMode: ReadWrite
defaultValue: 1

protocol: modbus
2 changes: 2 additions & 0 deletions build/crd-samples/devices/modbusrtu-device-instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ spec:
- test
propertyVisitors:
- propertyName: temperature
collectCycle: 1000
reportCycle: 1000
modbus:
register: CoilRegister
offset: 2
Expand Down
11 changes: 6 additions & 5 deletions build/crd-samples/devices/modbustcp-device-instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ spec:
status:
twins:
- propertyName: temperature-enable
reported:
desired:
metadata:
timestamp: '1550049403598'
type: integer
value: "0"
type: int
value: "1"
- propertyName: temperature
desired:
metadata:
timestamp: '1550049403598'
type: integer
value: "0"
type: int
value: "28"
16 changes: 8 additions & 8 deletions mappers/modbus-dmi/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ func main() {

grpcclient.Init(&c)

// start grpc server
grpcServer := grpcserver.NewServer(
grpcserver.Config{
SockPath: c.GrpcServer.SocketPath,
Protocol: common.ProtocolModbus,
},
)

panel := device.NewDevPanel()
err = panel.DevInit(&c)
if err != nil && !errors.Is(err, parse.ErrEmptyData) {
Expand All @@ -69,7 +61,15 @@ func main() {
}

panel.DevStart()
klog.Infoln("devices start finished")

// start grpc server
grpcServer := grpcserver.NewServer(
grpcserver.Config{
SockPath: c.GrpcServer.SocketPath,
Protocol: common.ProtocolModbus,
},
)
if err = grpcServer.Start(); err != nil {
klog.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions mappers/modbus-dmi/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func setVisitor(visitorConfig *modbus.ModbusVisitorConfig, twin *common.Twin, cl
}

klog.V(2).Infof("Convert type: %s, value: %s ", twin.PVisitor.PProperty.DataType, twin.Desired.Value)
value, err := common.Convert(twin.PVisitor.PProperty.DataType, twin.Desired.Value)
value, err := common.Convert(twin.Desired.Metadatas.Type, twin.Desired.Value)
if err != nil {
klog.Errorf("Convert error: %v", err)
return
Expand Down Expand Up @@ -127,7 +127,7 @@ func initTwin(ctx context.Context, dev *modbus.ModbusDev) {
VisitorConfig: &visitorConfig,
Topic: fmt.Sprintf(common.TopicTwinUpdate, dev.Instance.ID),
DeviceName: dev.Instance.Name}
collectCycle := time.Duration(dev.Instance.Twins[i].PVisitor.CollectCycle)
collectCycle := time.Duration(dev.Instance.Twins[i].PVisitor.CollectCycle) * time.Millisecond
// If the collect cycle is not set, set it to 1 second.
if collectCycle == 0 {
collectCycle = 1 * time.Second
Expand Down Expand Up @@ -172,7 +172,6 @@ func (d *DevPanel) start(ctx context.Context, dev *modbus.ModbusDev) {

go initTwin(ctx, dev)

<-ctx.Done()
d.wg.Done()
}

Expand Down
3 changes: 2 additions & 1 deletion mappers/modbus-dmi/device/twindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TransferData(isRegisterSwap bool, isSwap bool,
data := string(value)
return data, nil
default:
return "", errors.New("Data type is not support")
return "", errors.New("Data type is not support: " + dataType)
}
}

Expand Down Expand Up @@ -177,4 +177,5 @@ func (td *TwinData) Run() {
if err := grpcclient.ReportDeviceStatus(rdsr); err != nil {
klog.Errorf("fail to report device status of %s with err: %+v", rdsr.DeviceName, err)
}
klog.V(2).Infof("reported status: %s", td.DeviceName)
}
2 changes: 2 additions & 0 deletions pkg/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"encoding/json"
"regexp"
"strconv"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -124,6 +125,7 @@ func CreateMessageTwinUpdate(name string, valueType string, value string) (msg [
updateMsg.Twin = map[string]*MsgTwin{}
updateMsg.Twin[name] = &MsgTwin{}
updateMsg.Twin[name].Actual = &TwinValue{Value: &value}
updateMsg.Twin[name].Actual.Metadata.Timestamp = strconv.FormatInt(getTimestamp(), 10)
updateMsg.Twin[name].Metadata = &TypeMetadata{Type: valueType}

msg, err = json.Marshal(updateMsg)
Expand Down
6 changes: 0 additions & 6 deletions pkg/util/parse/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ func ConvMsgTwinToGrpc(msgTwin map[string]*common.MsgTwin) []*dmiapi.Twin {
for name, twin := range msgTwin {
twinData := &dmiapi.Twin{
PropertyName: name,
Desired: &dmiapi.TwinProperty{
Value: *twin.Expected.Value,
Metadata: map[string]string{
"type": twin.Metadata.Type,
"timestamp": twin.Expected.Metadata.Timestamp,
}},
Reported: &dmiapi.TwinProperty{
Value: *twin.Actual.Value,
Metadata: map[string]string{
Expand Down