Skip to content

Commit

Permalink
Merge pull request kubeedge#5217 from wbc6080/automated-cherry-pick-o…
Browse files Browse the repository at this point in the history
…f-#5140-upstream-release-1.15

Automated cherry pick of kubeedge#5140: Define Twin based on properties in mapper.
  • Loading branch information
kubeedge-bot authored Nov 28, 2023
2 parents bc7c4eb + 4985b35 commit 4ff43c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
30 changes: 26 additions & 4 deletions cloud/pkg/devicecontroller/controller/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
},
}
Expand Down

0 comments on commit 4ff43c0

Please sign in to comment.