Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mapper SDK #70

Merged
merged 7 commits into from
Jun 14, 2022
Merged

Add Mapper SDK #70

merged 7 commits into from
Jun 14, 2022

Conversation

QiQi-OvO
Copy link
Contributor

What type of PR is this?
/kind feature

What this PR does / why we need it:
mapper SDK helps developers reduce the workload of developing mapper.

Which issue(s) this PR fixes:
Fixes #69

Special notes for your reviewer:

  1. You can find more details in docs.
  2. You can find a demo named virtualdevice-sdk in mappers/virtualdevice-sdk

@kubeedge-bot kubeedge-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Apr 29, 2022
@kubeedge-bot
Copy link
Collaborator

Welcome @QiQi-OvO! It looks like this is your first PR to kubeedge/mappers-go 🎉

@kubeedge-bot kubeedge-bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Apr 29, 2022
@RyanZhaoXB
Copy link
Collaborator

hello, Qi @QiQi-OvO . your commit should be provided with sign-off information. you can try to re-commit with this command git commit -s -m "XXX" or git commit -s --amend and push your commit again.

QiQi-OvO and others added 2 commits May 5, 2022 12:55
Signed-off-by: QiQi-OvO <2763359332@qq.com>
Signed-off-by: caixindi <1435516315@qq.com>
@kubeedge-bot
Copy link
Collaborator

@RyanZhaoXB: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/test all

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/instancepool"
"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/mqttadapter"
"github.com/kubeedge/mappers-go/mapper-sdk-go/pkg/di"
"k8s.io/klog/v2"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golang的import引用有一定规范,一般是按包的稳定程度,先引用golang系统提供包,再引用第三方提供包,最后引用本项目自己的包,虽然不是必须,但建议遵守。如本文件中的引用顺序可修改为:

import (
	"context"
	"net/url"
	"sync"

	"k8s.io/klog/v2"

	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/common"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/configmap"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/controller"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/httpadapter/requests"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/instancepool"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/internal/mqttadapter"
	"github.com/kubeedge/mappers-go/mapper-sdk-go/pkg/di"
)

其他go代码可照此修改,不再单独指出

// Check if the device ID added by the user is duplicated
for k := range deviceInstances {
if k == instanceID {
klog.Error("The deviceInstance name used in the uploaded file already exists in mapperService")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance信息如name或id可以在log中体现一下,方便调试定位,下同

if _, ok := protocols[deviceInstance.ProtocolName]; !ok {
if addDeviceRequest.Protocol != nil {
i := 0
for i = 0; i < len(addDeviceRequest.Protocol); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这类判断元素是否存在的逻辑,都改成for range + isFind判断的方式会不会更简洁一点。另外如果有多个类似逻辑,不知道能否抽象成一个函数,如果不适合抽象就不用改了

driverName := common.DriverPrefix + deviceInstance.ID + visitorV.PropertyName
connectInfo[driverName] = &configmap.ConnectInfo{
ProtocolCommonConfig: deviceInstance.PProtocol.ProtocolCommonConfig,
VisitorConfig: tempVisitorV.VisitorConfig,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么用tempVisitorV呢


// AddDevice internal callback function
func AddDevice(addDeviceRequest requests.AddDeviceRequest, dic *di.Container) (kind common.ErrKind) {
instanceID := addDeviceRequest.DeviceInstance.ID
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改成instanceID := deviceInstance .ID,后面144、183、184行可直接引用instanceID,前后统一,降低代码阅读难度

cancelFunc()
modelNameDeleted := deviceInstances[instanceID].Model
protocolNameDeleted := deviceInstances[instanceID].ProtocolName
if _, ok := deviceInstances[instanceID]; ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里判断deviceInstance是否存在,应该放在198行之前做比较好吧

delete(stopFunctions, instanceID)
klog.V(1).Infof("Remove %s successful\n", instanceID)
} else {
klog.V(1).Infof("Remove %s failed,there is no such instanceId\n", instanceID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成Errorf,跟前面代码保持统一

func ReadDeviceData(deviceID string, propertyName string, dic *di.Container) (response string, kind common.ErrKind) {
deviceInstance := instancepool.DeviceInstancesNameFrom(dic.Get)
if _, ok := deviceInstance[deviceID]; !ok {
kind := common.KindEntityDoesNotExist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind已经作为显式命名的返回值,这里是不是可以不用重新声明一个kind,或者直接return common.KindEntityDoesNotExist

protocolDriver := instancepool.ProtocolDriverNameFrom(dic.Get)
response, err := controller.GetDeviceData(deviceID, deviceInstance[deviceID].Twins[index], protocolDriver, mapMutex[deviceID], dic)
if err != nil {
kind = common.KindServerError
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有error可以报个log,279行同

@kubeedge-bot
Copy link
Collaborator

@RyanZhaoXB: changing LGTM is restricted to collaborators

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Signed-off-by: QiQi-OvO <2763359332@qq.com>
Copy link
Collaborator

@RyanZhaoXB RyanZhaoXB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kubeedge-bot
Copy link
Collaborator

@RyanZhaoXB: changing LGTM is restricted to collaborators

In response to this:

lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@RyanZhaoXB
Copy link
Collaborator

@fisherxu please help to review this pr. thanks.

Signed-off-by: QiQi-OvO <2763359332@qq.com>
Signed-off-by: QiQi-OvO <2763359332@qq.com>
@RyanZhaoXB
Copy link
Collaborator

@chendave @fisherxu please help to review this pr. thanks.

README.md Outdated

### 2 mapper-go-sdk

SDK provide RESTful and you can connect your device fastly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's meaning of SDK provide RESTful? It's better to introduce how the the mapper SDK work here.

README.md Outdated
@@ -1,2 +1,11 @@
# mappers-go
KubeEdge Device Mappers written in go, it works for KubeEdge 1.4 version and later.

## Construction method
### 1 mappers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's meaning of Construction method here? Seems there's no content under mappers title.

It's better not to use the number like 1 in markdown

@@ -0,0 +1,78 @@
# Mapper-sdk-go Design
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file name should be mapper-sdk-design.md

@@ -0,0 +1,95 @@
# User Guide of Customized Mapper SDK in KubeEdge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -0,0 +1,209 @@
![Gitee Latest Dev Tag](https://img.shields.io/badge/latest--dev-v0.0.1-orange) ![Gitee go.mod Go version](https://img.shields.io/badge/Go-v1.17-brightgreen) [![Gitee License](https://camo.githubusercontent.com/3e671e69d5fad7978893d028dcdeb3af16edb20b61f23cd276f738a76f33f3cf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6b756265656467652f6b756265656467652e7376673f7374796c653d666c61742d737175617265)](https://gitee.com/ascend/mappers-go-sample/blob/mapper-go-sdk/LICENSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this line?

@@ -0,0 +1,13 @@
package main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete the two binaries above this file.

@@ -0,0 +1,6 @@
FROM alpine:3.14
RUN mkdir -p kubeedge
COPY ./bin/Template kubeedge/bin/
Copy link
Member

@fisherxu fisherxu Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should include make in all dockerfiles, users can make the image from code directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your valuable advice, I am trying to correct other mistakes : ).
For this Dockerfile,the corresponding operation has been given in README, make mapper Template package can build the code and copy the compiled file to the image. Mapper and mapper-sdk-go both follow this rule, so I think it is reasonable to follow the previous make rules and Dockerfile.

@QiQi-OvO QiQi-OvO force-pushed the mapper-sdk-go branch 2 times, most recently from bb97b36 to 2eac86e Compare June 14, 2022 07:18
Signed-off-by: QiQi-OvO <2763359332@qq.com>
get device data and device status through these interfaces.
Mapper-sdk-go supports connect customized devices to kubeedge fastly. If you want to access a device with a new protocol,
mapper-sdk-go is a good choice.
You can get more information and details in [mapper-sdk-go](./mapper-sdk-go/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the description, I'm not quite understand, users can use mapper-go-sdk diectly without any development?

From the name mapper-go-sdk, it's a lib, users can import it to implement the mapper.

Mapper-sdk-go is written based on the basic mapper, mapper-sdk-go is based on mapper? I think mapper-sdk-go should provide some basic libs or framework to build a new mapper. Users can build a new mapper easily with mapper-sdk-go, only need to implement the customized protocol driver.

Need to update the descriptions above

Signed-off-by: QiQi-OvO <2763359332@qq.com>
Copy link
Member

@fisherxu fisherxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@kubeedge-bot kubeedge-bot added the lgtm Indicates that a PR is ready to be merged. label Jun 14, 2022
@kubeedge-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fisherxu, RyanZhaoXB

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubeedge-bot kubeedge-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 14, 2022
@kubeedge-bot kubeedge-bot merged commit 34b73a4 into kubeedge:main Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

a proposal for mapper-sdk
5 participants