本帖最后由 keer_zu 于 2020-12-15 16:36 编辑
SDK是围绕“函数管道”的思想构建的。函数管道是按指定顺序处理数据的各种函数的集合。函数管道由配置中的指定触发器执行。toml。管道中的第一个函数被触发管道的事件调用(例如events.Model)。管道中的每个连续调用都将调用前一个函数的返回结果。让我们看一个简单的示例,该示例创建一个管道来过滤特定的设备id,然后将数据转换为XML:
package main
import (
"fmt"
"github.com/edgexfoundry/app-functions-sdk-go/appsdk"
"github.com/edgexfoundry/app-functions-sdk-go/pkg/transforms"
"os"
)
func main() {
// 1) First thing to do is to create an instance of the EdgeX SDK, giving it a service key
edgexSdk := &appsdk.AppFunctionsSDK{
ServiceKey: "SimpleFilterXMLApp", // Key used by Registry (Aka Consul)
}
// 2) Next, we need to initialize the SDK
if err := edgexSdk.Initialize(); err != nil {
edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK initialization failed: %v\n", err))
os.Exit(-1)
}
// 3) Since our FilterByDeviceName Function requires the list of Device Names we would
// like to search for, we'll go ahead and define that now.
deviceNames := []string{"Random-Float-Device"}
// 4) This is our pipeline configuration, the collection of functions to
// execute every time an event is triggered.
if err := edgexSdk.SetFunctionsPipeline(
transforms.NewFilter(deviceNames).FilterByDeviceName,
transforms.NewConversion().TransformToXML,
); err != nil {
edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK SetPipeline failed: %v\n", err))
os.Exit(-1)
}
// 5) shows how to access the application's specific configuration settings.
appSettings := edgexSdk.ApplicationSettings()
if appSettings != nil {
appName, ok := appSettings["ApplicationName"]
if ok {
edgexSdk.LoggingClient.Info(fmt.Sprintf("%s now running...", appName))
} else {
edgexSdk.LoggingClient.Error("ApplicationName application setting not found")
os.Exit(-1)
}
} else {
edgexSdk.LoggingClient.Error("No application settings found")
os.Exit(-1)
}
// 6) Lastly, we'll go ahead and tell the SDK to "start" and begin listening for events to trigger the pipeline.
edgexSdk.MakeItRun()
}
上面的示例只是为了演示应用程序的结构。注意,最后一个函数的输出在这个应用程序的任何地方都不可用。您必须提供一个函数,以便使用前一个函数中的数据。让我们继续,并添加以下函数,将输出打印到控制台。
func printXMLToConsole(edgexcontext *appcontext.Context, params ...interface{}) (bool,interface{}) {
if len(params) < 1 {
// We didn't receive a result
return false, errors.New("No Data Received")
}
println(params[0].(string))
return true, nil
}
|