Edge-X 設(shè)備服務(wù)開(kāi)發(fā)
一,設(shè)備驅(qū)動(dòng)(device driver) 核心接口
protocoldriver.go
/ ProtocolDriver is a low-level device-specific interface used by
// by other components of an EdgeX Device Service to interact with
// a specific class of devices.
type ProtocolDriver interface {
// Initialize performs protocol-specific initialization for the device
// service. The given *AsyncValues channel can be used to push asynchronous
// events and readings to Core Data.
Initialize(lc logger.LoggingClient, asyncCh chan<- *AsyncValues) error
// HandleReadCommands passes a slice of CommandRequest struct each representing
// a ResourceOperation for a specific device resource.
HandleReadCommands(deviceName string, protocols map[string]contract.ProtocolProperties, reqs []CommandRequest) ([]*CommandValue, error)
// HandleWriteCommands passes a slice of CommandRequest struct each representing
// a ResourceOperation for a specific device resource.
// Since the commands are actuation commands, params provide parameters for the individual
// command.
HandleWriteCommands(deviceName string, protocols map[string]contract.ProtocolProperties, reqs []CommandRequest, params []*CommandValue) error
// Stop instructs the protocol-specific DS code to shutdown gracefully, or
// if the force parameter is 'true', immediately. The driver is responsible
// for closing any in-use channels, including the channel used to send async
// readings (if supported).
Stop(force bool) error
// AddDevice is a callback function that is invoked
// when a new Device associated with this Device Service is added
AddDevice(deviceName string, protocols map[string]contract.ProtocolProperties, adminState contract.AdminState) error
// UpdateDevice is a callback function that is invoked
// when a Device associated with this Device Service is updated
UpdateDevice(deviceName string, protocols map[string]contract.ProtocolProperties, adminState contract.AdminState) error
// RemoveDevice is a callback function that is invoked
// when a Device associated with this Device Service is removed
RemoveDevice(deviceName string, protocols map[string]contract.ProtocolProperties) error
}
1、初始化函數(shù):
// AsyncValues : 異步通道偎巢,用來(lái)做異步事件發(fā)送
Initialize(lc logger.LoggingClient, asyncCh chan<- *AsyncValues) error
2、寫(xiě)函數(shù)
HandleWriteCommands(deviceName string, protocols map[string]contract.ProtocolProperties, reqs []CommandRequest, params []*CommandValue) error
入?yún)ⅲ?/p>
deviceName string: 設(shè)備名
protocols map[string]contract.ProtocolProperties:設(shè)備的連接信息
reqs []CommandRequest:指令請(qǐng)求兼耀,可以是多條
params []*CommandValue:指令值压昼,可以是多個(gè)返回值,與reqs的數(shù)量通常是對(duì)應(yīng)的
在這里有一個(gè)設(shè)計(jì):在HandleWriteCommands時(shí)瘤运,reqs 與params 是分開(kāi)的窍霞,本意是分別代別請(qǐng)求的資源和參數(shù),其中的結(jié)構(gòu)體成員有重復(fù)拯坟;
在另外一個(gè)工程:github.com/edgexfoundry/go-mod-core-contracts/models/device.go
// ProtocolProperties contains the device connection information in key/value pair
type ProtocolProperties map[string]string
// CommandRequest is the struct for requesting a command to ProtocolDrivers
type CommandRequest struct {
// DeviceResourceName is the name of Device Resource for this command
// 例如一個(gè)設(shè)備的功能項(xiàng)(資源項(xiàng)名稱)但金,例如可以是屬性的名字
DeviceResourceName string
// Attributes is a key/value map to represent the attributes of the Device Resource
// 這個(gè)看起來(lái)是非必須的,資源名可以唯一約束時(shí)郁季,Attributes的補(bǔ)充看起來(lái)非必須
Attributes map[string]string
// Type is the data type of the Device Resource
Type ValueType
}
// CommandValue is the struct to represent the reading value of a Get command coming
// from ProtocolDrivers or the parameter of a Put command sending to ProtocolDrivers.
type CommandValue struct {
// DeviceResourceName is the name of Device Resource for this command
DeviceResourceName string
// Origin is an int64 value which indicates the time the reading
// contained in the CommandValue was read by the ProtocolDriver
// instance.
// 當(dāng)前值生成的時(shí)間
Origin int64
// Type is a ValueType value which indicates what type of
// value was returned from the ProtocolDriver instance in
// response to HandleCommand being called to handle a single
// ResourceOperation.
// 當(dāng)前返回值的類型冷溃,指示從后續(xù)的那個(gè)結(jié)構(gòu)體中去取值(NumericValue,stringValue梦裂,BinValue)
Type ValueType
// NumericValue is a byte slice with a maximum capacity of
// 64 bytes, used to hold a numeric value returned by a
// ProtocolDriver instance. The value can be converted to
// its native type by referring to the the value of ResType.
NumericValue []byte
// stringValue is a string value returned as a value by a ProtocolDriver instance.
stringValue string
// BinValue is a CBOR encoded binary value with a maximum
// capacity of 1MB, used to hold binary values returned
// by a ProtocolDriver instance. Its decoded value is externally accessed
// using BinaryValue() method
BinValue []byte
}
3似枕、讀函數(shù)
// HandleReadCommands passes a slice of CommandRequest struct each representing
// a ResourceOperation for a specific device resource.
HandleReadCommands(deviceName string, protocols map[string]contract.ProtocolProperties, reqs []CommandRequest) ([]*CommandValue, error)
4、停止函數(shù)
// Stop instructs the protocol-specific DS code to shutdown gracefully, or
// if the force parameter is 'true', immediately. The driver is responsible
// for closing any in-use channels, including the channel used to send async
// readings (if supported).
Stop(force bool) error
5年柠、添加設(shè)備回調(diào)函數(shù)
// AddDevice is a callback function that is invoked
// when a new Device associated with this Device Service is added
AddDevice(deviceName string, protocols map[string]contract.ProtocolProperties, adminState contract.AdminState) error
6凿歼、更新設(shè)備回調(diào)函數(shù)
// UpdateDevice is a callback function that is invoked
// when a Device associated with this Device Service is updated
UpdateDevice(deviceName string, protocols map[string]contract.ProtocolProperties, adminState contract.AdminState) error
7、移除設(shè)備回調(diào)函數(shù)
// RemoveDevice is a callback function that is invoked
// when a Device associated with this Device Service is removed
RemoveDevice(deviceName string, protocols map[string]contract.ProtocolProperties) error