本帖最后由 keer_zu 于 2021-1-11 15:57 编辑
application Functions SDK
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 {
- message := fmt.Sprintf("SDK initialization failed: %v\n", err)
- if edgexSdk.LoggingClient != nil {
- edgexSdk.LoggingClient.Error(message)
- } else {
- fmt.Println(message)
- }
- os.Exit(-1)
- }
- // 3) Shows how to access the application's specific configuration settings.
- deviceNames, err := edgexSdk.GetAppSettingStrings("DeviceNames")
- if err != nil {
- edgexSdk.LoggingClient.Error(err.Error())
- os.Exit(-1)
- }
- // 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) Lastly, we'll go ahead and tell the SDK to "start" and begin listening for events to trigger the pipeline.
- err = edgexSdk.MakeItRun()
- if err != nil {
- edgexSdk.LoggingClient.Error("MakeItRun returned error: ", err.Error())
- os.Exit(-1)
- }
- // Do any required cleanup here
- os.Exit(0)
- }
上面的例子仅仅是为了演示应用程序的结构。注意,最后一个函数的输出在这个应用程序的任何地方都不可用。您必须提供一个函数来处理前一个函数的数据。让我们继续添加以下函数,将输出输出到控制台。
|