Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
Signed-off-by: cl2017 <[email protected]>
  • Loading branch information
cl2017 committed Sep 30, 2022
1 parent 68a145f commit 9e82f0a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
19 changes: 2 additions & 17 deletions mappers/modbus-dmi/device/twindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/kubeedge/mappers-go/pkg/common"
"github.com/kubeedge/mappers-go/pkg/driver/modbus"
"github.com/kubeedge/mappers-go/pkg/util/grpcclient"
"github.com/kubeedge/mappers-go/pkg/util/parse"
)

// TwinData is the timer structure for getting twin/data.
Expand Down Expand Up @@ -164,23 +165,7 @@ func (td *TwinData) Run() {
return
}

var twins []*dmiapi.Twin
for _, twin := range msg.Twin {
expectMetaValue, _ := json.Marshal(twin.Expected.Metadata)
reportedMetaValue, _ := json.Marshal(twin.Actual.Metadata)
twinData := &dmiapi.Twin{
PropertyName: td.Name,
Desired: &dmiapi.TwinProperty{
Value: *twin.Expected.Value,
Metadata: map[string]string{twin.Metadata.Type: string(expectMetaValue)},
},
Reported: &dmiapi.TwinProperty{
Value: *twin.Actual.Value,
Metadata: map[string]string{twin.Metadata.Type: string(reportedMetaValue)},
},
}
twins = append(twins, twinData)
}
twins := parse.ConvMsgTwinToGrpc(msg.Twin)

var rdsr = &dmiapi.ReportDeviceStatusRequest{
DeviceName: td.DeviceName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type TwinValue struct {

// ValueMetadata the meta of value.
type ValueMetadata struct {
Timestamp int64 `json:"timestamp,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}

// TypeMetadata the meta of value type.
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/parse/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,27 @@ func ConvGrpcToTwins(twins []*dmiapi.Twin, srcTwins []common.Twin) ([]common.Twi
}
return res, nil
}

func ConvMsgTwinToGrpc(msgTwin map[string]*common.MsgTwin) []*dmiapi.Twin {
var twins []*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{
"type": twin.Metadata.Type,
"timestamp": twin.Actual.Metadata.Timestamp,
}},
}
twins = append(twins, twinData)
}

return twins
}

0 comments on commit 9e82f0a

Please sign in to comment.