Skip to content

Commit

Permalink
Adapt to the latest mapper-framework
Browse files Browse the repository at this point in the history
Signed-off-by: wbc6080 <[email protected]>
  • Loading branch information
wbc6080 committed Nov 2, 2023
1 parent 60ca411 commit e817332
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
25 changes: 7 additions & 18 deletions mappers/v1beta1-mapper/virtualdevice/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}
21 changes: 13 additions & 8 deletions mappers/v1beta1-mapper/virtualdevice/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"fmt"
"github.com/kubeedge/virtualdevice/pkg/common"
"k8s.io/klog/v2"
"math/rand"
"sync"
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +48,7 @@ spec:
visitors:
protocolName: virtualProtocol
configData:
dataType: 'FLOAT'
dataType: float
reportCycle: 10000000000
collectCycle: 10000000000
reportToCloud: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spec:
- name: random-int
description: random int
type: INT
maximum: '100'
accessMode: ReadWrite
- name: random-float
description: random float
Expand Down

0 comments on commit e817332

Please sign in to comment.