From 7e650f88e5ca12d72a01f4a6ab4aed45ea46b402 Mon Sep 17 00:00:00 2001 From: wbc6080 Date: Tue, 30 Jan 2024 11:36:47 +0800 Subject: [PATCH] update mapper Signed-off-by: wbc6080 --- .../virtualdevice/Dockerfile | 0 .../virtualdevice/Makefile | 0 .../virtualdevice/cmd/main.go | 4 +- .../virtualdevice/config.yaml | 0 .../data/dbmethod/influxdb2/client.go | 1 + .../data/dbmethod/redis/client.go | 48 +++++ .../virtualdevice/data/publish/http/client.go | 12 +- .../virtualdevice/data/publish/mqtt/client.go | 4 +- .../virtualdevice/device/device.go | 11 +- .../virtualdevice/device/devicetwin.go | 3 +- .../virtualdevice/driver/devicetype.go | 0 .../virtualdevice/driver/driver.go | 14 +- .../virtualdevice/go.mod | 1 - .../virtualdevice/go.sum | 0 .../virtualdevice/hack/make-rules/mapper.sh | 0 .../virtualdevice/pkg/common/configmaptype.go | 13 +- .../virtualdevice/pkg/common/const.go | 0 .../virtualdevice/pkg/common/dataconverter.go | 0 .../virtualdevice/pkg/common/datamodel.go | 0 .../virtualdevice/pkg/common/event.go | 0 .../virtualdevice/pkg/common/eventtype.go | 0 .../virtualdevice/pkg/config/config.go | 0 .../virtualdevice/pkg/dmi-api/api.pb.go | 0 .../virtualdevice/pkg/dmi-api/api.proto | 0 .../virtualdevice/pkg/dmi-api/api_grpc.pb.go | 0 .../virtualdevice/pkg/global/global.go | 0 .../virtualdevice/pkg}/grpcclient/config.go | 0 .../virtualdevice/pkg}/grpcclient/register.go | 3 +- .../virtualdevice/pkg}/grpcclient/report.go | 0 .../virtualdevice/pkg/grpcserver/device.go | 8 +- .../virtualdevice/pkg/grpcserver/server.go | 4 +- .../virtualdevice/pkg/httpserver/callback.go | 0 .../pkg/httpserver/responsetype.go | 0 .../virtualdevice/pkg/httpserver/restapi.go | 0 .../virtualdevice/pkg/httpserver/router.go | 0 .../virtualdevice/pkg/httpserver/server.go | 6 +- .../virtualdevice/pkg/util/parse/grpc.go | 110 ++++++------ .../virtualdevice/pkg/util/parse/parse.go | 66 +++++++ .../virtualdevice/pkg/util/parse/type.go | 0 .../virtualdevice/resource/deployment.yaml | 0 .../virtualdevice/resource/secret.yaml | 0 .../resource/virtualdevice-instance.yaml | 0 .../resource/virtualdevice-model.yaml | 0 .../virtualdevice/pkg/util/parse/parse.go | 169 ------------------ 44 files changed, 210 insertions(+), 267 deletions(-) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/Dockerfile (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/Makefile (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/cmd/main.go (91%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/config.yaml (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/data/dbmethod/influxdb2/client.go (98%) create mode 100644 mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/redis/client.go rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/data/publish/http/client.go (80%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/data/publish/mqtt/client.go (94%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/device/device.go (95%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/device/devicetwin.go (94%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/driver/devicetype.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/driver/driver.go (78%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/go.mod (99%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/go.sum (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/hack/make-rules/mapper.sh (100%) mode change 100755 => 100644 rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/configmaptype.go (93%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/const.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/dataconverter.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/datamodel.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/event.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/common/eventtype.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/config/config.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/dmi-api/api.pb.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/dmi-api/api.proto (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/dmi-api/api_grpc.pb.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/global/global.go (100%) rename mappers/{v1beta1-mapper/virtualdevice/pkg/util => kubeedge-v1.15.0/virtualdevice/pkg}/grpcclient/config.go (100%) rename mappers/{v1beta1-mapper/virtualdevice/pkg/util => kubeedge-v1.15.0/virtualdevice/pkg}/grpcclient/register.go (90%) rename mappers/{v1beta1-mapper/virtualdevice/pkg/util => kubeedge-v1.15.0/virtualdevice/pkg}/grpcclient/report.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/grpcserver/device.go (96%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/grpcserver/server.go (94%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/httpserver/callback.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/httpserver/responsetype.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/httpserver/restapi.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/httpserver/router.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/httpserver/server.go (94%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/util/parse/grpc.go (73%) create mode 100644 mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/parse.go rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/pkg/util/parse/type.go (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/resource/deployment.yaml (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/resource/secret.yaml (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/resource/virtualdevice-instance.yaml (100%) rename mappers/{v1beta1-mapper => kubeedge-v1.15.0}/virtualdevice/resource/virtualdevice-model.yaml (100%) delete mode 100644 mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/parse.go diff --git a/mappers/v1beta1-mapper/virtualdevice/Dockerfile b/mappers/kubeedge-v1.15.0/virtualdevice/Dockerfile similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/Dockerfile rename to mappers/kubeedge-v1.15.0/virtualdevice/Dockerfile diff --git a/mappers/v1beta1-mapper/virtualdevice/Makefile b/mappers/kubeedge-v1.15.0/virtualdevice/Makefile similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/Makefile rename to mappers/kubeedge-v1.15.0/virtualdevice/Makefile diff --git a/mappers/v1beta1-mapper/virtualdevice/cmd/main.go b/mappers/kubeedge-v1.15.0/virtualdevice/cmd/main.go similarity index 91% rename from mappers/v1beta1-mapper/virtualdevice/cmd/main.go rename to mappers/kubeedge-v1.15.0/virtualdevice/cmd/main.go index 84bdac37..9957baf7 100644 --- a/mappers/v1beta1-mapper/virtualdevice/cmd/main.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/cmd/main.go @@ -9,9 +9,9 @@ import ( "github.com/kubeedge/virtualdevice/device" "github.com/kubeedge/virtualdevice/pkg/common" "github.com/kubeedge/virtualdevice/pkg/config" + "github.com/kubeedge/virtualdevice/pkg/grpcclient" "github.com/kubeedge/virtualdevice/pkg/grpcserver" "github.com/kubeedge/virtualdevice/pkg/httpserver" - "github.com/kubeedge/virtualdevice/pkg/util/grpcclient" "github.com/kubeedge/virtualdevice/pkg/util/parse" ) @@ -50,7 +50,7 @@ func main() { // if dev init mode is register, mapper's dev will init when registry to edgecore if c.DevInit.Mode != common.DevInitModeRegister { klog.Infoln("======dev init mode is not register, will register to edgecore") - if _, _, err = grpcclient.RegisterMapper(&c, false); err != nil { + if _, _, err = grpcclient.RegisterMapper(false); err != nil { klog.Fatal(err) } klog.Infoln("registerMapper finished") diff --git a/mappers/v1beta1-mapper/virtualdevice/config.yaml b/mappers/kubeedge-v1.15.0/virtualdevice/config.yaml similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/config.yaml rename to mappers/kubeedge-v1.15.0/virtualdevice/config.yaml diff --git a/mappers/v1beta1-mapper/virtualdevice/data/dbmethod/influxdb2/client.go b/mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/influxdb2/client.go similarity index 98% rename from mappers/v1beta1-mapper/virtualdevice/data/dbmethod/influxdb2/client.go rename to mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/influxdb2/client.go index 2bcffb9d..8620850d 100644 --- a/mappers/v1beta1-mapper/virtualdevice/data/dbmethod/influxdb2/client.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/influxdb2/client.go @@ -60,6 +60,7 @@ func (d *DataBaseConfig) CloseSession(client influxdb2.Client) { } func (d *DataBaseConfig) AddData(data *common.DataModel, client influxdb2.Client) error { + // write device data to influx database writeAPI := client.WriteAPIBlocking(d.Influxdb2ClientConfig.Org, d.Influxdb2ClientConfig.Bucket) p := influxdb2.NewPoint(d.Influxdb2DataConfig.Measurement, d.Influxdb2DataConfig.Tag, diff --git a/mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/redis/client.go b/mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/redis/client.go new file mode 100644 index 00000000..ca39c695 --- /dev/null +++ b/mappers/kubeedge-v1.15.0/virtualdevice/data/dbmethod/redis/client.go @@ -0,0 +1,48 @@ +package redis + +import ( + "github.com/kubeedge/virtualdevice/pkg/common" + "github.com/kubeedge/virtualdevice/pkg/global" +) + +type DataBaseConfig struct { +} + +func NewDataBaseClient() (global.DataBaseClient, error) { + return &DataBaseConfig{}, nil +} + +func (d *DataBaseConfig) InitDbClient() error { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) CloseSession() { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) AddData(data *common.DataModel) { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) GetDataByDeviceName(deviceName string) ([]*common.DataModel, error) { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) GetPropertyDataByDeviceName(deviceName string, propertyData string) ([]*common.DataModel, error) { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) GetDataByTimeRange(start int64, end int64) ([]*common.DataModel, error) { + //TODO implement me + panic("implement me") +} + +func (d *DataBaseConfig) DeleteDataByTimeRange(start int64, end int64) ([]*common.DataModel, error) { + //TODO implement me + panic("implement me") +} diff --git a/mappers/v1beta1-mapper/virtualdevice/data/publish/http/client.go b/mappers/kubeedge-v1.15.0/virtualdevice/data/publish/http/client.go similarity index 80% rename from mappers/v1beta1-mapper/virtualdevice/data/publish/http/client.go rename to mappers/kubeedge-v1.15.0/virtualdevice/data/publish/http/client.go index cfc41600..2ccdae82 100644 --- a/mappers/v1beta1-mapper/virtualdevice/data/publish/http/client.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/data/publish/http/client.go @@ -38,18 +38,21 @@ func NewDataPanel(config json.RawMessage) (global.DataPanel, error) { } func (pm *PushMethod) InitPushMethod() error { - fmt.Println("Init Http") + klog.V(1).Info("Init HTTP") return nil } func (pm *PushMethod) Push(data *common.DataModel) { + klog.V(2).Info("Publish device data by HTTP") targetUrl := pm.HTTP.HostName + ":" + strconv.Itoa(pm.HTTP.Port) + pm.HTTP.RequestPath - klog.V(1).Infof("targetUrl = %s", targetUrl) payload := data.PropertyName + "=" + data.Value formatTimeStr := time.Unix(data.TimeStamp/1e3, 0).Format("2006-01-02 15:04:05") currentTime := "&time" + "=" + formatTimeStr payload += currentTime + + klog.V(3).Infof("Publish %v to %s", payload, targetUrl) + resp, err := http.Post(targetUrl, "application/x-www-form-urlencoded", strings.NewReader(payload)) @@ -61,7 +64,10 @@ func (pm *PushMethod) Push(data *common.DataModel) { body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error + klog.Errorf("Publish device data by HTTP failed, err = %v", err) + return } - klog.V(1).Info(string(body)) + klog.V(1).Info("############### Message published. ###############") + klog.V(3).Infof("HTTP reviced %s", string(body)) } diff --git a/mappers/v1beta1-mapper/virtualdevice/data/publish/mqtt/client.go b/mappers/kubeedge-v1.15.0/virtualdevice/data/publish/mqtt/client.go similarity index 94% rename from mappers/v1beta1-mapper/virtualdevice/data/publish/mqtt/client.go rename to mappers/kubeedge-v1.15.0/virtualdevice/data/publish/mqtt/client.go index dcfb7aaa..c2b03301 100644 --- a/mappers/v1beta1-mapper/virtualdevice/data/publish/mqtt/client.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/data/publish/mqtt/client.go @@ -36,7 +36,7 @@ func NewDataPanel(config json.RawMessage) (global.DataPanel, error) { } func (pm *PushMethod) InitPushMethod() error { - fmt.Println("Init Mqtt") + klog.V(1).Info("Init MQTT") return nil } @@ -59,5 +59,5 @@ func (pm *PushMethod) Push(data *common.DataModel) { token.Wait() client.Disconnect(250) - klog.V(1).Info("############### Message published. ###############") + klog.V(2).Info("############### Message published. ###############") } diff --git a/mappers/v1beta1-mapper/virtualdevice/device/device.go b/mappers/kubeedge-v1.15.0/virtualdevice/device/device.go similarity index 95% rename from mappers/v1beta1-mapper/virtualdevice/device/device.go rename to mappers/kubeedge-v1.15.0/virtualdevice/device/device.go index 76c70656..5f9fba08 100644 --- a/mappers/v1beta1-mapper/virtualdevice/device/device.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/device/device.go @@ -109,6 +109,7 @@ 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 @@ -253,7 +254,13 @@ func setVisitor(visitorConfig *driver.VisitorConfig, twin *common.Twin, dev *dri klog.V(3).Infof("%s twin readonly property: %s", dev.Instance.Name, twin.PropertyName) return nil } - err := dev.CustomizedClient.SetDeviceData(twin, visitorConfig) + 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) if err != nil { return fmt.Errorf("%s set device data error: %v", twin.PropertyName, err) } @@ -270,7 +277,7 @@ func (d *DevPanel) DevInit(cfg *config.Config) error { // return err // } case common.DevInitModeRegister: - if err := parse.ParseByUsingRegister(cfg, devs, d.models, d.protocols); err != nil { + if err := parse.ParseByUsingRegister(devs, d.models, d.protocols); err != nil { return err } } diff --git a/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go b/mappers/kubeedge-v1.15.0/virtualdevice/device/devicetwin.go similarity index 94% rename from mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go rename to mappers/kubeedge-v1.15.0/virtualdevice/device/devicetwin.go index 3d9667ac..7ccdc606 100644 --- a/mappers/v1beta1-mapper/virtualdevice/device/devicetwin.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/device/devicetwin.go @@ -12,7 +12,7 @@ import ( "github.com/kubeedge/virtualdevice/driver" "github.com/kubeedge/virtualdevice/pkg/common" dmiapi "github.com/kubeedge/virtualdevice/pkg/dmi-api" - "github.com/kubeedge/virtualdevice/pkg/util/grpcclient" + "github.com/kubeedge/virtualdevice/pkg/grpcclient" "github.com/kubeedge/virtualdevice/pkg/util/parse" ) @@ -31,6 +31,7 @@ type TwinData struct { 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) diff --git a/mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go b/mappers/kubeedge-v1.15.0/virtualdevice/driver/devicetype.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/driver/devicetype.go rename to mappers/kubeedge-v1.15.0/virtualdevice/driver/devicetype.go diff --git a/mappers/v1beta1-mapper/virtualdevice/driver/driver.go b/mappers/kubeedge-v1.15.0/virtualdevice/driver/driver.go similarity index 78% rename from mappers/v1beta1-mapper/virtualdevice/driver/driver.go rename to mappers/kubeedge-v1.15.0/virtualdevice/driver/driver.go index c1412ee2..f3fce446 100644 --- a/mappers/v1beta1-mapper/virtualdevice/driver/driver.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/driver/driver.go @@ -2,10 +2,10 @@ package driver import ( "fmt" - "github.com/kubeedge/virtualdevice/pkg/common" - "k8s.io/klog/v2" "math/rand" "sync" + + "k8s.io/klog/v2" ) func NewClient(protocol ProtocolConfig) (*CustomizedClient, error) { @@ -40,16 +40,12 @@ func (c *CustomizedClient) GetDeviceData(visitor *VisitorConfig) (interface{}, e } } -func (c *CustomizedClient) SetDeviceData(twin *common.Twin, visitor *VisitorConfig) error { +func (c *CustomizedClient) SetDeviceData(data interface{}, visitor *VisitorConfig) error { // TODO: set device's data // 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(value.(int64)) + c.intMaxValue = int(data.(int64)) } else { return fmt.Errorf("unrecognized data type: %s", visitor.DataType) } diff --git a/mappers/v1beta1-mapper/virtualdevice/go.mod b/mappers/kubeedge-v1.15.0/virtualdevice/go.mod similarity index 99% rename from mappers/v1beta1-mapper/virtualdevice/go.mod rename to mappers/kubeedge-v1.15.0/virtualdevice/go.mod index adec2fac..20bb6cd9 100644 --- a/mappers/v1beta1-mapper/virtualdevice/go.mod +++ b/mappers/kubeedge-v1.15.0/virtualdevice/go.mod @@ -3,7 +3,6 @@ module github.com/kubeedge/virtualdevice go 1.17 require ( - github.com/eclipse/paho.mqtt.golang v1.4.3 github.com/golang/protobuf v1.5.2 github.com/gorilla/mux v1.8.0 diff --git a/mappers/v1beta1-mapper/virtualdevice/go.sum b/mappers/kubeedge-v1.15.0/virtualdevice/go.sum similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/go.sum rename to mappers/kubeedge-v1.15.0/virtualdevice/go.sum diff --git a/mappers/v1beta1-mapper/virtualdevice/hack/make-rules/mapper.sh b/mappers/kubeedge-v1.15.0/virtualdevice/hack/make-rules/mapper.sh old mode 100755 new mode 100644 similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/hack/make-rules/mapper.sh rename to mappers/kubeedge-v1.15.0/virtualdevice/hack/make-rules/mapper.sh diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/configmaptype.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/configmaptype.go similarity index 93% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/configmaptype.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/configmaptype.go index 0cc7dc2b..0e8e756e 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/common/configmaptype.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/configmaptype.go @@ -18,7 +18,7 @@ package common import "encoding/json" -// DeviceProfile is structure to store in configMap. +// DeviceProfile is structure to store in configMap. It will be removed later type DeviceProfile struct { DeviceInstances []DeviceInstance `json:"deviceInstances,omitempty"` DeviceModels []DeviceModel `json:"deviceModels,omitempty"` @@ -49,10 +49,9 @@ type ModelProperty struct { DataType string `json:"dataType,omitempty"` Description string `json:"description,omitempty"` AccessMode string `json:"accessMode,omitempty"` - //DefaultValue interface{} `json:"defaultValue,omitempty"` - Minimum string `json:"minimum,omitempty"` //todo todo why the type is int64 - Maximum string `json:"maximum,omitempty"` - Unit string `json:"unit,omitempty"` + Minimum string `json:"minimum,omitempty"` + Maximum string `json:"maximum,omitempty"` + Unit string `json:"unit,omitempty"` } // Protocol is structure to store protocol in deviceProfile.json in configmap. @@ -74,14 +73,12 @@ type DeviceProperty struct { ModelName string `json:"modelName,omitempty"` Protocol string `json:"protocol,omitempty"` Visitors json.RawMessage `json:"visitorConfig"` - // whether be reported to the cloud ReportToCloud bool `json:"reportToCloud,omitempty"` CollectCycle int64 `json:"collectCycle"` ReportCycle int64 `json:"reportCycle,omitempty"` PushMethod PushMethodConfig `json:"pushMethod,omitempty"` - - PProperty ModelProperty + PProperty ModelProperty } // PushMethodConfig is structure to store push config diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/const.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/const.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/const.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/const.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/dataconverter.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/dataconverter.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/dataconverter.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/dataconverter.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/datamodel.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/datamodel.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/datamodel.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/datamodel.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/event.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/event.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/event.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/event.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/common/eventtype.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/eventtype.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/common/eventtype.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/common/eventtype.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/config/config.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/config/config.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/config/config.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/config/config.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api.pb.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api.pb.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api.pb.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api.pb.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api.proto b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api.proto similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api.proto rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api.proto diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api_grpc.pb.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api_grpc.pb.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/dmi-api/api_grpc.pb.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/dmi-api/api_grpc.pb.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/global/global.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/global/global.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/global/global.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/global/global.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/config.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/config.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/config.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/config.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/register.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/register.go similarity index 90% rename from mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/register.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/register.go index c0114657..b34742b0 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/register.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/register.go @@ -9,12 +9,11 @@ import ( "google.golang.org/grpc" "github.com/kubeedge/virtualdevice/pkg/common" - "github.com/kubeedge/virtualdevice/pkg/config" dmiapi "github.com/kubeedge/virtualdevice/pkg/dmi-api" ) // RegisterMapper if withData is true, edgecore will send device and model list. -func RegisterMapper(cfg *config.Config, withData bool) ([]*dmiapi.Device, []*dmiapi.DeviceModel, error) { +func RegisterMapper(withData bool) ([]*dmiapi.Device, []*dmiapi.DeviceModel, error) { // connect grpc server conn, err := grpc.Dial(cfg.Common.EdgeCoreSock, grpc.WithInsecure(), diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/report.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/report.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/util/grpcclient/report.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcclient/report.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/device.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/device.go similarity index 96% rename from mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/device.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/device.go index ee6e655e..988e9aa7 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/device.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/device.go @@ -15,10 +15,8 @@ import ( ) func (s *Server) RegisterDevice(ctx context.Context, request *dmiapi.RegisterDeviceRequest) (*dmiapi.RegisterDeviceResponse, error) { - //klog.V(2).Info("RegisterDevice") + klog.V(3).Info("RegisterDevice") device := request.GetDevice() - //klog.V(1).Infof("In RegisterDevice, device = %v", device) - if device == nil { return nil, errors.New("device is nil") } @@ -67,7 +65,7 @@ func (s *Server) RemoveDevice(ctx context.Context, request *dmiapi.RemoveDeviceR } func (s *Server) UpdateDevice(ctx context.Context, request *dmiapi.UpdateDeviceRequest) (*dmiapi.UpdateDeviceResponse, error) { - klog.V(2).Info("UpdateDevice") + klog.V(3).Info("UpdateDevice") device := request.GetDevice() if device == nil { return nil, errors.New("device is nil") @@ -82,7 +80,7 @@ func (s *Server) UpdateDevice(ctx context.Context, request *dmiapi.UpdateDeviceR return nil, fmt.Errorf("parse device %s protocol failed, err: %s", device.Name, err) } - klog.Infof("model: %+v", model) + klog.V(3).Infof("model: %+v", model) deviceInstance, err := parse.ParseDeviceFromGrpc(device, &model) if err != nil { return nil, fmt.Errorf("parse device %s instance failed, err: %s", device.Name, err) diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/server.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/server.go similarity index 94% rename from mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/server.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/server.go index 41055eac..0303b557 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/grpcserver/server.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/grpcserver/server.go @@ -47,7 +47,7 @@ func (s *Server) Start() error { grpcServer := grpc.NewServer() dmiapi.RegisterDeviceMapperServiceServer(grpcServer, s) reflection.Register(grpcServer) - klog.Info("start grpc server") + klog.V(2).Info("start grpc server") return grpcServer.Serve(s.lis) } @@ -63,7 +63,7 @@ func (s *Server) Stop() { } func initSock(sockPath string) error { - klog.Infof("init uds socket: %s", sockPath) + klog.V(2).Infof("init uds socket: %s", sockPath) _, err := os.Stat(sockPath) if err == nil { err = os.Remove(sockPath) diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/callback.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/callback.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/callback.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/callback.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/responsetype.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/responsetype.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/responsetype.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/responsetype.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/restapi.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/restapi.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/restapi.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/restapi.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/router.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/router.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/router.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/router.go diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/server.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/server.go similarity index 94% rename from mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/server.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/server.go index 8dde5e2e..3ec6d4b0 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/httpserver/server.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/httpserver/server.go @@ -55,7 +55,7 @@ func (rs *RestServer) StartServer() { } if rs.CaCertFilePath == "" && (rs.KeyFilePath == "" || rs.CertFilePath == "") { // insecure - klog.Info("Insecure communication, skipping server verification") + klog.V(3).Info("Insecure communication, skipping server verification") err := rs.server.ListenAndServe() if err != nil { klog.Errorf("insecure http server error: %v", err) @@ -63,7 +63,7 @@ func (rs *RestServer) StartServer() { } } else if rs.CaCertFilePath == "" && rs.KeyFilePath != "" && rs.CertFilePath != "" { // tls - klog.Info("tls communication, https server start") + klog.V(3).Info("tls communication, https server start") err := rs.server.ListenAndServeTLS(rs.CertFilePath, rs.KeyFilePath) if err != nil { klog.Errorf("tls http server error: %v", err) @@ -71,7 +71,7 @@ func (rs *RestServer) StartServer() { } } else if rs.CaCertFilePath != "" && rs.KeyFilePath != "" && rs.CertFilePath != "" { // mtls - klog.Info("mtls communication, please provide client-key and client-cert to access service") + klog.V(3).Info("mtls communication, please provide client-key and client-cert to access service") // Configure the server to trust TLS client cert issued by your CA. certPool := x509.NewCertPool() if caCertPEM, err := ioutil.ReadFile(rs.CaCertFilePath); err != nil { diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/grpc.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/grpc.go similarity index 73% rename from mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/grpc.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/grpc.go index 74c2cd2a..ab33cb95 100644 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/grpc.go +++ b/mappers/kubeedge-v1.15.0/virtualdevice/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, + Value: property.Desired.Value, Metadata: common.Metadata{ - Timestamp: twin.ObservedDesired.Metadata["timestamp"], - Type: twin.ObservedDesired.Metadata["type"], - }, - }, - Reported: common.TwinProperty{ - Value: twin.Reported.Value, - Metadata: common.Metadata{ - Timestamp: twin.ObservedDesired.Metadata["timestamp"], - Type: twin.ObservedDesired.Metadata["type"], + Timestamp: property.Desired.Metadata["timestamp"], + Type: property.Desired.Metadata["type"], }, }, } @@ -109,7 +101,7 @@ func buildPropertiesFromGrpc(device *dmiapi.Device) []common.DeviceProperty { return nil } res := make([]common.DeviceProperty, 0, len(device.Spec.Properties)) - klog.V(1).Infof("In buildPropertiesFromGrpc, PropertyVisitors = %v", device.Spec.Properties) + klog.V(3).Infof("In buildPropertiesFromGrpc, PropertyVisitors = %v", device.Spec.Properties) for _, pptv := range device.Spec.Properties { // get visitorConfig filed by grpc device instance @@ -134,62 +126,65 @@ func buildPropertiesFromGrpc(device *dmiapi.Device) []common.DeviceProperty { // get dbMethod filed by grpc device instance var dbMethodName string var dbconfig common.DBConfig - if pptv.PushMethod.DBMethod != nil { - dbMethodName, err = getDBMethodFromGrpc(pptv) + var pushMethod []byte + var pushMethodName string + if pptv.PushMethod != nil { + if pptv.PushMethod.DBMethod != nil { + dbMethodName, err = getDBMethodFromGrpc(pptv) + if err != nil { + klog.Errorf("err: %+v", err) + return nil + } + switch dbMethodName { + case "influx": + clientconfig, err := json.Marshal(pptv.PushMethod.DBMethod.Influxdb2.Influxdb2ClientConfig) + if err != nil { + klog.Errorf("err: %+v", err) + return nil + } + dataconfig, err := json.Marshal(pptv.PushMethod.DBMethod.Influxdb2.Influxdb2DataConfig) + if err != nil { + klog.Errorf("err: %+v", err) + return nil + } + dbconfig = common.DBConfig{ + Influxdb2ClientConfig: clientconfig, + Influxdb2DataConfig: dataconfig, + } + } + } + // get pushMethod filed by grpc device instance + pushMethodName, err = getPushMethodFromGrpc(pptv) if err != nil { klog.Errorf("err: %+v", err) return nil } - switch dbMethodName { - case "influx": - clientconfig, err := json.Marshal(pptv.PushMethod.DBMethod.Influxdb2.Influxdb2ClientConfig) + switch pushMethodName { + case "http": + pushMethod, err = json.Marshal(pptv.PushMethod.Http) if err != nil { klog.Errorf("err: %+v", err) return nil } - dataconfig, err := json.Marshal(pptv.PushMethod.DBMethod.Influxdb2.Influxdb2DataConfig) + case "mqtt": + pushMethod, err = json.Marshal(pptv.PushMethod.Mqtt) if err != nil { klog.Errorf("err: %+v", err) return nil } - dbconfig = common.DBConfig{ - Influxdb2ClientConfig: clientconfig, - Influxdb2DataConfig: dataconfig, - } - } - } - - // get pushMethod filed by grpc device instance - pushMethodName, err := getPushMethodFromGrpc(pptv) - if err != nil { - klog.Errorf("err: %+v", err) - return nil - } - var pushMethod []byte - switch pushMethodName { - case "http": - pushMethod, err = json.Marshal(pptv.PushMethod.Http) - if err != nil { - klog.Errorf("err: %+v", err) - return nil - } - case "mqtt": - pushMethod, err = json.Marshal(pptv.PushMethod.Mqtt) - if err != nil { - klog.Errorf("err: %+v", err) - return nil } } // get the final Properties cur := common.DeviceProperty{ - Name: pptv.GetName(), - PropertyName: pptv.GetName(), - ModelName: device.Spec.DeviceModelReference, - CollectCycle: pptv.GetCollectCycle(), - ReportCycle: pptv.GetReportCycle(), - Protocol: protocolName, - Visitors: visitorConfig, + Name: pptv.GetName(), + PropertyName: pptv.GetName(), + ModelName: device.Spec.DeviceModelReference, + CollectCycle: pptv.GetCollectCycle(), + ReportCycle: pptv.GetReportCycle(), + ReportToCloud: pptv.GetReportToCloud(), + Protocol: protocolName, + Visitors: visitorConfig, PushMethod: common.PushMethodConfig{ MethodName: pushMethodName, MethodConfig: pushMethod, @@ -251,8 +246,7 @@ func ParseDeviceFromGrpc(device *dmiapi.Device, commonModel *common.DeviceModel) continue } - // ****这里是想把直接从model.yaml文件中解析出来的modelproperty解析到instance里,主要要看传入的commonModel是不是从yaml文件中 - //解析得到的model数据 + // parse the content of the modelproperty field into instance for _, property := range commonModel.Properties { if property.Name == instance.Properties[i].PropertyName { instance.Properties[i].PProperty = property @@ -266,6 +260,6 @@ func ParseDeviceFromGrpc(device *dmiapi.Device, commonModel *common.DeviceModel) instance.Twins[i].Property = &v } } - klog.V(1).Infof("final instance data from grpc = %v", instance) + klog.V(2).Infof("final instance data from grpc = %v", instance) return instance, nil } diff --git a/mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/parse.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/parse.go new file mode 100644 index 00000000..2c726541 --- /dev/null +++ b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/parse.go @@ -0,0 +1,66 @@ +/* +Copyright 2023 The KubeEdge Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package parse + +import ( + "errors" + + "k8s.io/klog/v2" + + "github.com/kubeedge/virtualdevice/pkg/common" + "github.com/kubeedge/virtualdevice/pkg/grpcclient" +) + +var ErrEmptyData error = errors.New("device or device model list is empty") + +func ParseByUsingRegister(devices map[string]*common.DeviceInstance, + dms map[string]common.DeviceModel, + protocols map[string]common.ProtocolConfig) error { + deviceList, deviceModelList, err := grpcclient.RegisterMapper(true) + if err != nil { + return err + } + + if len(deviceList) == 0 || len(deviceModelList) == 0 { + return ErrEmptyData + } + modelMap := make(map[string]common.DeviceModel) + for _, model := range deviceModelList { + cur := ParseDeviceModelFromGrpc(model) + modelMap[model.Name] = cur + } + + for _, device := range deviceList { + commonModel := modelMap[device.Spec.DeviceModelReference] + protocol, err := BuildProtocolFromGrpc(device) + if err != nil { + return err + } + instance, err := ParseDeviceFromGrpc(device, &commonModel) + if err != nil { + return err + } + instance.PProtocol = protocol + devices[instance.ID] = new(common.DeviceInstance) + devices[instance.ID] = instance + klog.V(4).Info("Instance: ", instance.ID) + dms[instance.Model] = modelMap[instance.Model] + protocols[instance.ProtocolName] = protocol + } + + return nil +} diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/type.go b/mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/type.go similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/type.go rename to mappers/kubeedge-v1.15.0/virtualdevice/pkg/util/parse/type.go diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml b/mappers/kubeedge-v1.15.0/virtualdevice/resource/deployment.yaml similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/resource/deployment.yaml rename to mappers/kubeedge-v1.15.0/virtualdevice/resource/deployment.yaml diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/secret.yaml b/mappers/kubeedge-v1.15.0/virtualdevice/resource/secret.yaml similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/resource/secret.yaml rename to mappers/kubeedge-v1.15.0/virtualdevice/resource/secret.yaml diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml b/mappers/kubeedge-v1.15.0/virtualdevice/resource/virtualdevice-instance.yaml similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-instance.yaml rename to mappers/kubeedge-v1.15.0/virtualdevice/resource/virtualdevice-instance.yaml diff --git a/mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml b/mappers/kubeedge-v1.15.0/virtualdevice/resource/virtualdevice-model.yaml similarity index 100% rename from mappers/v1beta1-mapper/virtualdevice/resource/virtualdevice-model.yaml rename to mappers/kubeedge-v1.15.0/virtualdevice/resource/virtualdevice-model.yaml diff --git a/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/parse.go b/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/parse.go deleted file mode 100644 index 1e9f136c..00000000 --- a/mappers/v1beta1-mapper/virtualdevice/pkg/util/parse/parse.go +++ /dev/null @@ -1,169 +0,0 @@ -/* -Copyright 2023 The KubeEdge Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package parse - -import ( - "errors" - - "k8s.io/klog/v2" - - "github.com/kubeedge/virtualdevice/pkg/common" - "github.com/kubeedge/virtualdevice/pkg/config" - "github.com/kubeedge/virtualdevice/pkg/util/grpcclient" -) - -var ErrEmptyData error = errors.New("device or device model list is empty") - -// Parse the configmap which will be removed -//func Parse(path string, -// devices map[string]*common.DeviceInstance, -// dms map[string]common.DeviceModel, -// protocols map[string]common.Protocol) error { -// var deviceProfile common.DeviceProfile -// jsonFile, err := ioutil.ReadFile(path) -// if err != nil { -// err = errors.New("failed to read " + path + " file") -// return err -// } -// //Parse the JSON file and convert it into the data structure of DeviceProfile -// if err = json.Unmarshal(jsonFile, &deviceProfile); err != nil { -// return err -// } -// // loop instIndex : judge whether the configmap definition is correct, and initialize the device instance -// for instIndex := 0; instIndex < len(deviceProfile.DeviceInstances); instIndex++ { -// instance := deviceProfile.DeviceInstances[instIndex] -// // loop protoIndex : judge whether the device's protocol is correct, and initialize the device protocol -// protoIndex := 0 -// for protoIndex = 0; protoIndex < len(deviceProfile.Protocols); protoIndex++ { -// if instance.ProtocolName == deviceProfile.Protocols[protoIndex].Name { -// // Verify that the protocols match -// protocolConfig := make(map[string]interface{}) -// err := json.Unmarshal(deviceProfile.Protocols[protoIndex].ProtocolConfigs, &protocolConfig) -// if err != nil { -// err = errors.New("failed to parse " + deviceProfile.Protocols[protoIndex].Name) -// return err -// } -// protocols[deviceProfile.Protocols[protoIndex].Name] = deviceProfile.Protocols[protoIndex] -// instance.PProtocol = deviceProfile.Protocols[protoIndex] -// } -// } -// // loop propertyIndex : find the device model's properties for each device instance's propertyVisitor -// for propertyIndex := 0; propertyIndex < len(instance.PropertyVisitors); propertyIndex++ { -// modelName := instance.PropertyVisitors[propertyIndex].ModelName -// propertyName := instance.PropertyVisitors[propertyIndex].PropertyName -// modelIndex := 0 -// // loop modelIndex : find a matching device model, and initialize the device model -// for modelIndex = 0; modelIndex < len(deviceProfile.DeviceModels); modelIndex++ { -// if modelName == deviceProfile.DeviceModels[modelIndex].Name { -// dms[deviceProfile.DeviceModels[modelIndex].Name] = deviceProfile.DeviceModels[modelIndex] -// m := 0 -// // loop m : find a matching device model's properties -// for m = 0; m < len(deviceProfile.DeviceModels[modelIndex].Properties); m++ { -// if propertyName == deviceProfile.DeviceModels[modelIndex].Properties[m].Name { -// instance.PropertyVisitors[propertyIndex].PProperty = deviceProfile.DeviceModels[modelIndex].Properties[m] -// break -// } -// } -// if m == len(deviceProfile.DeviceModels[modelIndex].Properties) { -// err = errors.New("property mismatch") -// return err -// } -// break -// } -// } -// if modelIndex == len(deviceProfile.DeviceModels) { -// err = errors.New("device model mismatch") -// return err -// } -// } -// // loop propertyIndex : find propertyVisitors for each instance's twin -// for propertyIndex := 0; propertyIndex < len(instance.Twins); propertyIndex++ { -// name := instance.Twins[propertyIndex].PropertyName -// l := 0 -// // loop l : find a matching propertyName -// for l = 0; l < len(instance.PropertyVisitors); l++ { -// if name == instance.PropertyVisitors[l].PropertyName { -// instance.Twins[propertyIndex].PVisitor = &instance.PropertyVisitors[l] -// break -// } -// } -// if l == len(instance.PropertyVisitors) { -// err = errors.New("propertyVisitor mismatch") -// return err -// } -// } -// // loop propertyIndex : find propertyVisitors for each instance's property -// for propertyIndex := 0; propertyIndex < len(instance.Datas.Properties); propertyIndex++ { -// name := instance.Datas.Properties[propertyIndex].PropertyName -// l := 0 -// // loop l : find a matching propertyName -// for l = 0; l < len(instance.PropertyVisitors); l++ { -// if name == instance.PropertyVisitors[l].PropertyName { -// instance.Datas.Properties[propertyIndex].PVisitor = &instance.PropertyVisitors[l] -// break -// } -// } -// if l == len(instance.PropertyVisitors) { -// err = errors.New("propertyVisitor mismatch") -// return err -// } -// } -// devices[instance.ID] = new(common.DeviceInstance) -// devices[instance.ID] = &instance -// klog.V(4).Infof("Instance:%s Successfully registered", instance.ID) -// } -// return nil -//} - -func ParseByUsingRegister(cfg *config.Config, - devices map[string]*common.DeviceInstance, - dms map[string]common.DeviceModel, - protocols map[string]common.ProtocolConfig) error { - deviceList, deviceModelList, err := grpcclient.RegisterMapper(cfg, true) - if err != nil { - return err - } - - if len(deviceList) == 0 || len(deviceModelList) == 0 { - return ErrEmptyData - } - modelMap := make(map[string]common.DeviceModel) - for _, model := range deviceModelList { - cur := ParseDeviceModelFromGrpc(model) - modelMap[model.Name] = cur - } - - for _, device := range deviceList { - commonModel := modelMap[device.Spec.DeviceModelReference] - protocol, err := BuildProtocolFromGrpc(device) - if err != nil { - return err - } - instance, err := ParseDeviceFromGrpc(device, &commonModel) - if err != nil { - return err - } - instance.PProtocol = protocol - devices[instance.ID] = new(common.DeviceInstance) - devices[instance.ID] = instance - klog.V(4).Info("Instance: ", instance.ID) - dms[instance.Model] = modelMap[instance.Model] - protocols[instance.ProtocolName] = protocol - } - - return nil -}