diff --git a/mappers/v1beta1-mapper/virtualdevice/device/device.go b/mappers/v1beta1-mapper/virtualdevice/device/device.go index 2008739e..76c70656 100644 --- a/mappers/v1beta1-mapper/virtualdevice/device/device.go +++ b/mappers/v1beta1-mapper/virtualdevice/device/device.go @@ -109,7 +109,6 @@ func dataHandler(ctx context.Context, dev *driver.CustomizedDev) { var visitorConfig driver.VisitorConfig err := json.Unmarshal(twin.Property.Visitors, &visitorConfig) - visitorConfig.VisitorConfigData.DataType = strings.ToLower(visitorConfig.VisitorConfigData.DataType) if err != nil { klog.Errorf("Unmarshal VisitorConfig error: %v", err) continue @@ -129,22 +128,15 @@ func dataHandler(ctx context.Context, dev *driver.CustomizedDev) { VisitorConfig: &visitorConfig, Topic: fmt.Sprintf(common.TopicTwinUpdate, dev.Instance.ID), CollectCycle: time.Duration(twin.Property.CollectCycle), + ReportToCloud: twin.Property.ReportToCloud, } go twinData.Run(ctx) // handle push method - testconfig := make(map[string]interface{}) - err = json.Unmarshal(twin.Property.PushMethod.MethodConfig, &testconfig) - if err == nil { - klog.V(1).Infof("twin.Property.PushMethod.MethodConfig = %v", testconfig) - } else { - klog.Error(err) - } if twin.Property.PushMethod.MethodConfig != nil && twin.Property.PushMethod.MethodName != "" { dataModel := common.NewDataModel(dev.Instance.Name, twin.Property.PropertyName, common.WithType(twin.ObservedDesired.Metadata.Type)) pushHandler(ctx, &twin, dev.CustomizedClient, &visitorConfig, dataModel) } // handle database - if twin.Property.PushMethod.DBMethod.DBMethodName != "" { dataModel := common.NewDataModel(dev.Instance.Name, twin.Property.PropertyName, common.WithType(twin.ObservedDesired.Metadata.Type)) dbHandler(ctx, &twin, dev.CustomizedClient, &visitorConfig, dataModel) @@ -156,18 +148,20 @@ func dataHandler(ctx context.Context, dev *driver.CustomizedDev) { func pushHandler(ctx context.Context, twin *common.Twin, client *driver.CustomizedClient, visitorConfig *driver.VisitorConfig, dataModel *common.DataModel) { var dataPanel global.DataPanel var err error + // initialization dataPanel switch twin.Property.PushMethod.MethodName { case "http": dataPanel, err = httpMethod.NewDataPanel(twin.Property.PushMethod.MethodConfig) case "mqtt": dataPanel, err = mqttMethod.NewDataPanel(twin.Property.PushMethod.MethodConfig) default: - err = errors.New("Custom protocols are not currently supported") + err = errors.New("custom protocols are not currently supported when push data") } if err != nil { klog.Errorf("new data panel error: %v", err) return } + // initialization PushMethod err = dataPanel.InitPushMethod() if err != nil { klog.Errorf("init publish method err: %v", err) @@ -205,6 +199,7 @@ func pushHandler(ctx context.Context, twin *common.Twin, client *driver.Customiz // dbHandler start db client to save data func dbHandler(ctx context.Context, twin *common.Twin, client *driver.CustomizedClient, visitorConfig *driver.VisitorConfig, dataModel *common.DataModel) { switch twin.Property.PushMethod.DBMethod.DBMethodName { + // TODO add more database case "influx": dbConfig, err := dbInflux.NewDataBaseClient(twin.Property.PushMethod.DBMethod.DBConfig.Influxdb2ClientConfig, twin.Property.PushMethod.DBMethod.DBConfig.Influxdb2DataConfig) if err != nil { @@ -255,16 +250,10 @@ func dbHandler(ctx context.Context, twin *common.Twin, client *driver.Customized // setVisitor check if visitor property is readonly, if not then set it. func setVisitor(visitorConfig *driver.VisitorConfig, twin *common.Twin, dev *driver.CustomizedDev) error { if twin.Property.PProperty.AccessMode == "ReadOnly" { - klog.V(1).Infof("%s twin readonly property: %s", dev.Instance.Name, twin.PropertyName) + klog.V(3).Infof("%s twin readonly property: %s", dev.Instance.Name, twin.PropertyName) return nil } - klog.V(2).Infof("Convert type: %s, value: %s ", twin.Property.PProperty.DataType, twin.ObservedDesired.Value) - value, err := common.Convert(twin.Property.PProperty.DataType, twin.ObservedDesired.Value) - if err != nil { - klog.Errorf("Failed to convert value as %s : %v", twin.Property.PProperty.DataType, err) - return err - } - err = dev.CustomizedClient.SetDeviceData(value, visitorConfig) + err := dev.CustomizedClient.SetDeviceData(twin, visitorConfig) if err != nil { return fmt.Errorf("%s set device data error: %v", twin.PropertyName, err) } diff --git a/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go b/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go index 821700cd..3d9667ac 100644 --- a/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go +++ b/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go @@ -26,11 +26,11 @@ type TwinData struct { Topic string Results interface{} CollectCycle time.Duration + ReportToCloud bool } func (td *TwinData) GetPayLoad() ([]byte, error) { var err error - td.VisitorConfig.VisitorConfigData.DataType = strings.ToLower(td.VisitorConfig.VisitorConfigData.DataType) td.Results, err = td.Client.GetDeviceData(td.VisitorConfig) if err != nil { return nil, fmt.Errorf("get device data failed: %v", err) @@ -87,6 +87,9 @@ func (td *TwinData) PushToEdgeCore() { } func (td *TwinData) Run(ctx context.Context) { + if !td.ReportToCloud { + return + } if td.CollectCycle == 0 { td.CollectCycle = 1 * time.Second } diff --git a/mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go b/mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go index 4facf2f8..19c0c38d 100644 --- a/mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go +++ b/mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go @@ -19,13 +19,13 @@ type CustomizedClient struct { ProtocolConfig } -type ProtocolConfig struct { //customizedprotocol字段 +type ProtocolConfig struct { ProtocolName string `json:"protocolName"` ConfigData `json:"configData"` } type ConfigData struct { - // TODO: add your config data according to configmap + // TODO: add your protocol config data DeviceID int `json:"deviceID,omitempty"` SerialPort string `json:"serialPort"` DataBits int `json:"dataBits"` @@ -41,6 +41,6 @@ type VisitorConfig struct { } type VisitorConfigData struct { - // TODO: add your Visitor ConfigData according to configmap + // TODO: add your visitor config data DataType string `json:"dataType"` } diff --git a/mappers/v1beta1-mapper/virtualdevice/driver/driver.go b/mappers/v1beta1-mapper/virtualdevice/driver/driver.go index a70b2966..c1412ee2 100644 --- a/mappers/v1beta1-mapper/virtualdevice/driver/driver.go +++ b/mappers/v1beta1-mapper/virtualdevice/driver/driver.go @@ -2,6 +2,7 @@ package driver import ( "fmt" + "github.com/kubeedge/virtualdevice/pkg/common" "k8s.io/klog/v2" "math/rand" "sync" @@ -18,15 +19,15 @@ func NewClient(protocol ProtocolConfig) (*CustomizedClient, error) { func (c *CustomizedClient) InitDevice() error { // TODO: add init operation - // you can use c.ProtocolConfig and c.ProtocolCommonConfig + // you can use c.ProtocolConfig klog.Infof("Init device%d successful, protocolID: %v", c.DeviceID, c.ProtocolID) klog.Infof("I can get Info: %v %v ", c.SerialPort, c.BaudRate) return nil } func (c *CustomizedClient) GetDeviceData(visitor *VisitorConfig) (interface{}, error) { - // TODO: get device's data - // you can use c.ProtocolConfig,c.ProtocolCommonConfig and visitor + // TODO: add the code to get device's data + // you can use c.ProtocolConfig and visitor if visitor.VisitorConfigData.DataType == "int" { if c.intMaxValue <= 0 { return nil, fmt.Errorf("max value is %d, should > 0", c.intMaxValue) @@ -37,14 +38,18 @@ func (c *CustomizedClient) GetDeviceData(visitor *VisitorConfig) (interface{}, e } else { return nil, fmt.Errorf("unrecognized data type: %s", visitor.DataType) } - return nil, nil } -func (c *CustomizedClient) SetDeviceData(data interface{}, visitor *VisitorConfig) error { +func (c *CustomizedClient) SetDeviceData(twin *common.Twin, visitor *VisitorConfig) error { // TODO: set device's data - // you can use c.ProtocolConfig,c.ProtocolCommonConfig and visitor + // you can use c.ProtocolConfig and visitor + value, err := common.Convert(twin.Property.PProperty.DataType, twin.Property.PProperty.Maximum) + if err != nil { + klog.Errorf("Failed to convert value as %s : %v", twin.Property.PProperty.DataType, err) + return err + } if visitor.DataType == "int" { - c.intMaxValue = int(data.(int64)) + c.intMaxValue = int(value.(int64)) } else { return fmt.Errorf("unrecognized data type: %s", visitor.DataType) } @@ -53,7 +58,7 @@ func (c *CustomizedClient) SetDeviceData(data interface{}, visitor *VisitorConfi func (c *CustomizedClient) StopDevice() error { // TODO: stop device - // you can use c.ProtocolConfig and c.ProtocolCommonConfig + // you can use c.ProtocolConfig klog.Infof("Stop device%d successful", c.DeviceID) return nil } diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml b/mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml index bd3a0073..132504e1 100644 --- a/mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml +++ b/mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml @@ -3,26 +3,19 @@ kind: Deployment metadata: name: mapper-test namespace: default - labels: - app: demo - version: stable spec: replicas: 1 selector: matchLabels: app: demo - version: stable template: metadata: - annotations: - sidecar.istio.io/inject: "false" labels: app: demo - version: stable spec: nodeName: edge-node containers: - - name: demo34 + - name: demo volumeMounts: - mountPath: /etc/kubeedge name: test-volume diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml b/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml index 041beb10..9c903d59 100644 --- a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml +++ b/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml @@ -23,7 +23,7 @@ spec: visitors: protocolName: virtualProtocol # in your mapper, this should be replaced by the protocol name your use configData: - dataType: 'INT' + dataType: int reportCycle: 10000000000 collectCycle: 10000000000 reportToCloud: true @@ -48,7 +48,7 @@ spec: visitors: protocolName: virtualProtocol configData: - dataType: 'FLOAT' + dataType: float reportCycle: 10000000000 collectCycle: 10000000000 reportToCloud: true diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml b/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml index a02bc20f..9bdbf024 100644 --- a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml +++ b/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml @@ -9,6 +9,7 @@ spec: - name: random-int description: random int type: INT + maximum: '100' accessMode: ReadWrite - name: random-float description: random float