diff --git a/cloud/pkg/devicecontroller/controller/upstream.go b/cloud/pkg/devicecontroller/controller/upstream.go index 566a393d48c..d1bcc383687 100644 --- a/cloud/pkg/devicecontroller/controller/upstream.go +++ b/cloud/pkg/devicecontroller/controller/upstream.go @@ -135,8 +135,9 @@ func (uc *UpstreamController) updateDeviceStatus() { } deviceStatus := &DeviceStatus{Status: cacheDevice.Status} for twinName, twin := range msgTwin.Twin { - for i, cacheTwin := range deviceStatus.Status.Twins { - if twinName == cacheTwin.PropertyName && twin.Actual != nil && twin.Actual.Value != nil { + deviceTwin := findTwinByName(twinName, &deviceStatus.Status.Twins) + if deviceTwin != nil { + if twin.Actual != nil && twin.Actual.Value != nil { reported := v1beta1.TwinProperty{} reported.Value = *twin.Actual.Value reported.Metadata = make(map[string]string) @@ -146,8 +147,20 @@ func (uc *UpstreamController) updateDeviceStatus() { if twin.Metadata != nil { reported.Metadata["type"] = twin.Metadata.Type } - deviceStatus.Status.Twins[i].Reported = reported - break + deviceTwin.Reported = reported + } + + if twin.Expected != nil && twin.Expected.Value != nil { + observedDesired := v1beta1.TwinProperty{} + observedDesired.Value = *twin.Expected.Value + observedDesired.Metadata = make(map[string]string) + if twin.Expected.Metadata != nil { + observedDesired.Metadata["timestamp"] = strconv.FormatInt(twin.Expected.Metadata.Timestamp, 10) + } + if twin.Metadata != nil { + observedDesired.Metadata["type"] = twin.Metadata.Type + } + deviceTwin.ObservedDesired = observedDesired } } } @@ -212,3 +225,12 @@ func NewUpstreamController(dc *DownstreamController) (*UpstreamController, error } return uc, nil } + +func findTwinByName(twinName string, twins *[]v1beta1.Twin) *v1beta1.Twin { + for _, twin := range *twins { + if twinName == twin.PropertyName { + return &twin + } + } + return nil +} diff --git a/staging/src/github.com/kubeedge/mapper-framework/pkg/util/parse/grpc.go b/staging/src/github.com/kubeedge/mapper-framework/pkg/util/parse/grpc.go index 9fb9621e6f3..450e3730b6e 100644 --- a/staging/src/github.com/kubeedge/mapper-framework/pkg/util/parse/grpc.go +++ b/staging/src/github.com/kubeedge/mapper-framework/pkg/util/parse/grpc.go @@ -72,26 +72,18 @@ func BuildProtocolFromGrpc(device *dmiapi.Device) (common.ProtocolConfig, error) } func buildTwinsFromGrpc(device *dmiapi.Device) []common.Twin { - if len(device.Status.Twins) == 0 { + if len(device.Spec.Properties) == 0 { return nil } - res := make([]common.Twin, 0, len(device.Status.Twins)) - for _, twin := range device.Status.Twins { + res := make([]common.Twin, 0, len(device.Spec.Properties)) + for _, property := range device.Spec.Properties { cur := common.Twin{ - PropertyName: twin.PropertyName, - + PropertyName: property.Name, ObservedDesired: common.TwinProperty{ - Value: twin.ObservedDesired.Value, - Metadata: common.Metadata{ - Timestamp: twin.ObservedDesired.Metadata["timestamp"], - Type: twin.ObservedDesired.Metadata["type"], - }, - }, - Reported: common.TwinProperty{ - Value: twin.Reported.Value, + Value: property.Desired.Value, Metadata: common.Metadata{ - Timestamp: twin.ObservedDesired.Metadata["timestamp"], - Type: twin.ObservedDesired.Metadata["type"], + Timestamp: property.Desired.Metadata["timestamp"], + Type: property.Desired.Metadata["type"], }, }, }