Skip to content

Commit

Permalink
Merge pull request kubeedge#5913 from wbc6080/add-devicestatus-report…
Browse files Browse the repository at this point in the history
…cycle

Mapper support set device status reportcycle
  • Loading branch information
kubeedge-bot authored Oct 23, 2024
2 parents ea44ce5 + 72ec2ea commit 91bb084
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 156 deletions.
8 changes: 8 additions & 0 deletions build/crds/devices/devices_v1beta1_device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,14 @@ spec:
lastOnlineTime:
description: 'Optional: The last time the device was online.'
type: string
reportCycle:
description: 'Optional: Define how frequent mapper will report the
device status.'
format: int64
type: integer
reportToCloud:
description: 'Optional: whether be reported to the cloud'
type: boolean
state:
description: 'Optional: The state of the device.'
type: string
Expand Down
8 changes: 8 additions & 0 deletions manifests/charts/cloudcore/crds/devices_v1beta1_device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,14 @@ spec:
lastOnlineTime:
description: 'Optional: The last time the device was online.'
type: string
reportCycle:
description: 'Optional: Define how frequent mapper will report the
device status.'
format: int64
type: integer
reportToCloud:
description: 'Optional: whether be reported to the cloud'
type: boolean
state:
description: 'Optional: The state of the device.'
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ type DeviceStatus struct {
// Optional: The last time the device was online.
// +optional
LastOnlineTime string `json:"lastOnlineTime,omitempty"`
// Optional: whether be reported to the cloud
// +optional
ReportToCloud bool `json:"reportToCloud,omitempty"`
// Optional: Define how frequent mapper will report the device status.
// +optional
ReportCycle int64 `json:"reportCycle,omitempty"`
}

// Twin provides a logical representation of control properties (writable properties in the
Expand Down
309 changes: 166 additions & 143 deletions staging/src/github.com/kubeedge/api/apis/dmi/v1beta1/api.pb.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ message ReportDeviceStatesRequest {
message DeviceStatus {
// the device twins of the device.
repeated Twin twins = 1;
// whether be reported to the cloud
bool reportToCloud = 2;
// Define how frequent mapper will report the value.
int64 reportCycle = 3;

}

// Twin is the digital model of a device. It contains a series of properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func (d *DevPanel) start(ctx context.Context, dev *driver.CustomizedDev) {

// dataHandler initialize the timer to handle data plane and devicetwin.
func dataHandler(ctx context.Context, dev *driver.CustomizedDev) {
// handle device status report
getStates := &DeviceStates{
Client: dev.CustomizedClient,
DeviceName: dev.Instance.Name,
DeviceNamespace: dev.Instance.Namespace,
ReportToCloud: dev.Instance.Status.ReportToCloud,
ReportCycle: time.Millisecond * time.Duration(dev.Instance.Status.ReportCycle),
}
go getStates.Run(ctx)
// handle device twin report
for _, twin := range dev.Instance.Twins {
twin.Property.PProperty.DataType = strings.ToLower(twin.Property.PProperty.DataType)
var visitorConfig driver.VisitorConfig
Expand Down Expand Up @@ -150,11 +160,6 @@ func dataHandler(ctx context.Context, dev *driver.CustomizedDev) {
}
go twinData.Run(ctx)

//handle status
getStates := &DeviceStates{Client: dev.CustomizedClient, DeviceName: dev.Instance.Name,
DeviceNamespace: dev.Instance.Namespace}
go getStates.Run(ctx)

// handle push method
if twin.Property.PushMethod.MethodConfig != nil && twin.Property.PushMethod.MethodName != "" {
dataModel := common.NewDataModel(dev.Instance.Name, twin.Property.PropertyName, dev.Instance.Namespace, common.WithType(twin.ObservedDesired.Metadata.Type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package device

import (
"context"
"log"
"time"

"k8s.io/klog/v2"

"github.com/kubeedge/Template/driver"
dmiapi "github.com/kubeedge/api/apis/dmi/v1beta1"
"github.com/kubeedge/mapper-framework/pkg/common"
"github.com/kubeedge/mapper-framework/pkg/grpcclient"
)

Expand All @@ -33,13 +33,15 @@ type DeviceStates struct {
Client *driver.CustomizedClient
DeviceName string
DeviceNamespace string
ReportToCloud bool
ReportCycle time.Duration
}

// Run timer function.
func (deviceStates *DeviceStates) PushStatesToEdgeCore() {
states, error := deviceStates.Client.GetDeviceStates()
if error != nil {
klog.Errorf("GetDeviceStates failed: %v", error)
states, err := deviceStates.Client.GetDeviceStates()
if err != nil {
klog.Errorf("GetDeviceStates failed: %v", err)
return
}

Expand All @@ -49,15 +51,22 @@ func (deviceStates *DeviceStates) PushStatesToEdgeCore() {
DeviceNamespace: deviceStates.DeviceNamespace,
}

log.Printf("send statesRequest", statesRequest.DeviceName, statesRequest.State)
if err := grpcclient.ReportDeviceStates(statesRequest); err != nil {
klog.V(4).Infof("send device %s status %s request to cloud", statesRequest.DeviceName, statesRequest.State)
if err = grpcclient.ReportDeviceStates(statesRequest); err != nil {
klog.Errorf("fail to report device states of %s with err: %+v", deviceStates.DeviceName, err)
}
}

func (deviceStates *DeviceStates) Run(ctx context.Context) {
// TODO setting states reportCycle
ticker := time.NewTicker(2 * time.Second)
// No need to report device status to the cloud
if !deviceStates.ReportToCloud {
return
}
// Set device status report cycle
if deviceStates.ReportCycle == 0 {
deviceStates.ReportCycle = common.DefaultReportCycle
}
ticker := time.NewTicker(deviceStates.ReportCycle)
for {
select {
case <-ticker.C:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type DeviceInstance struct {
Twins []Twin `json:"twins,omitempty"`
Properties []DeviceProperty `json:"properties,omitempty"`
Methods []DeviceMethod `json:"methods,omitempty"`
Status DeviceStatus `json:"status,omitempty"`
}

// DeviceModel is structure to store detailed information about the devicemodel in the mapper.
Expand Down Expand Up @@ -74,6 +75,13 @@ type DeviceMethod struct {
PropertyNames []string `json:"propertyNames,omitempty"`
}

// DeviceStatus is structure to store parameters for device status reporting.
type DeviceStatus struct {
// whether be reported to the cloud
ReportToCloud bool `json:"reportToCloud,omitempty"`
ReportCycle int64 `json:"reportCycle,omitempty"`
}

// DeviceProperty is structure to store propertyVisitor in device.
type DeviceProperty struct {
Name string `json:"name,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func GetDeviceFromGrpc(device *dmiapi.Device, commonModel *common.DeviceModel) (
Twins: buildTwinsFromGrpc(device),
Properties: buildPropertiesFromGrpc(device),
Methods: buildMethodsFromGrpc(device),
Status: common.DeviceStatus{
ReportToCloud: device.Status.GetReportToCloud(),
ReportCycle: device.Status.GetReportCycle(),
},
}
// copy Properties to twin
propertiesMap := make(map[string]common.DeviceProperty)
Expand Down

0 comments on commit 91bb084

Please sign in to comment.