The purpose of this document is to guide users to generate a custom mapper by mapper-sdk-go and run it. Now we have ModBus , Gige and device that communicates over a network mappers. For other protocol, you can generate your own mapper according to this guide to control the edge devices.
This document is suit for KubeEdge cloud/edge system. If you have a MQTT Broker and provide a configmap.json, you can use this mapper to connect your device easily.
We use CRD to define a new device. There're 2 device related CRDs. One is device model, it defines some common definitions
such as device properties, the property is like temperature, humidity, and enable-counter; another CRD is device instance,
it defines the detailed definition related to the specific device. More details could refer to the device crd design.
In order for mapper-sdk to recognize your protocol normally, protocol customizedProtocol protocolName
in instance.yaml must be filled in
spec:
deviceModelRef:
name: modelName
protocol:
customizedProtocol:
protocolName: protocolName # must be filled in
For this part, you could refer to the mapper-sdk-go design.
Use example to help build your first mapper.
As an example, the VirtualDevice mapper is a customized mapper. Its function is to generate random int and float numbers, and can send commands through the cloud or RESTful API to control the maximum value of int nums. You can find the model and instance definition of virtual device in build.
Mapper-sdk-go will parse the configmap you defined. The most important three parameters are
protocolCommonConfig
visitorConfig
ProtocolConfig
. These parameters will provide to you in []byte
.
So define your structure according to your own configmap. You can get an example of virtual Instance:virtual Instance Driver
Mapper-sdk-go will provide the following interfaces,and you can find them in protocoldriver.go
type ProtocolDriver interface {
// InitDevice do the job of initialization
// input:
// protocolCommon: protocolCommonConfig in configmap file
// return:
// err : error information
InitDevice(protocolCommon []byte) (err error)
// ReadDeviceData is an interface that reads data from a specific device,
// data is a type of string
// input:
// protocolCommon: protocolCommonConfig in configmap file
// visitor: visitorConfig in configmap file
// protocol: protocolConfig in configmap file
// return:
// err : error information
ReadDeviceData (protocolCommon, visitor, protocol []byte) (data interface{}, err error)
// WriteDeviceData is an interface that write data to a specific device,
// data's DataType is Consistent with configmap
// input:
// data: Data written to the device,type depends on the configuration file
// protocolCommon: protocolCommonConfig in configmap file
// visitor: visitorConfig in configmap file
// protocol: protocolConfig in configmap file
// return:
// err : error information
WriteDeviceData (data interface{}, protocolCommon, visitor, protocol []byte) (err error)
// GetDeviceStatus is an interface to get the device status true is OK ,
// false is DISCONNECTED
// input:
// protocolCommon: protocolCommonConfig in configmap file
// visitor: visitorConfig in configmap file
// protocol: protocolConfig in configmap file
// return:
// status : get devices's status true means normal
GetDeviceStatus(protocolCommon, visitor, protocol []byte) (status bool)
// StopDevice is an interface to stop all devices
// input:
// nil
// return:
// err : error information
StopDevice() (err error)
}
Run "make help" in mappers directory to get all availabe make methods. Try them and make your mapper.
Refer to README.md, Start your mapper service