application SDK

[复制链接]
 楼主| keer_zu 发表于 2021-1-11 15:55 | 显示全部楼层 |阅读模式
本帖最后由 keer_zu 于 2021-1-11 15:57 编辑

application Functions  SDK
SDK是围绕“函数管道”的思想构建的。函数管道是按指定顺序处理数据的各种函数的集合。函数管道由配置中指定的触发器执行。toml。管道中的第一个函数被调用时带有触发管道的事件(例如events.Model)。管道中的每个后续调用都使用前一个函数的返回结果进行调用。让我们看一个简单的例子,它创建了一个管道来过滤特定的设备id,然后将数据转换为XML:
  1. package main

  2. import (
  3.     "fmt"
  4.     "github.com/edgexfoundry/app-functions-sdk-go/appsdk"
  5.     "github.com/edgexfoundry/app-functions-sdk-go/pkg/transforms"
  6.     "os"
  7. )

  8. func main() {

  9.     // 1) First thing to do is to create an instance of the EdgeX SDK, giving it a service key
  10.     edgexSdk := &appsdk.AppFunctionsSDK{
  11.         ServiceKey: "SimpleFilterXMLApp", // Key used by Registry (Aka Consul)
  12.     }

  13.     // 2) Next, we need to initialize the SDK
  14.     if err := edgexSdk.Initialize(); err != nil {
  15.         message := fmt.Sprintf("SDK initialization failed: %v\n", err)
  16.         if edgexSdk.LoggingClient != nil {
  17.             edgexSdk.LoggingClient.Error(message)
  18.         } else {
  19.             fmt.Println(message)
  20.         }
  21.         os.Exit(-1)
  22.     }

  23.     // 3) Shows how to access the application's specific configuration settings.
  24.     deviceNames, err := edgexSdk.GetAppSettingStrings("DeviceNames")
  25.     if err != nil {
  26.         edgexSdk.LoggingClient.Error(err.Error())
  27.         os.Exit(-1)
  28.     }   

  29.     // 4) This is our pipeline configuration, the collection of functions to
  30.     // execute every time an event is triggered.
  31.     if err = edgexSdk.SetFunctionsPipeline(
  32.             transforms.NewFilter(deviceNames).FilterByDeviceName,
  33.             transforms.NewConversion().TransformToXML,
  34.         ); err != nil {
  35.         edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK SetPipeline failed: %v\n", err))
  36.         os.Exit(-1)
  37.     }

  38.     // 5) Lastly, we'll go ahead and tell the SDK to "start" and begin listening for events to trigger the pipeline.
  39.     err = edgexSdk.MakeItRun()
  40.     if err != nil {
  41.         edgexSdk.LoggingClient.Error("MakeItRun returned error: ", err.Error())
  42.         os.Exit(-1)
  43.     }

  44.     // Do any required cleanup here

  45.     os.Exit(0)
  46. }
上面的例子仅仅是为了演示应用程序的结构。注意,最后一个函数的输出在这个应用程序的任何地方都不可用。您必须提供一个函数来处理前一个函数的数据。让我们继续添加以下函数,将输出输出到控制台。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

1481

主题

12924

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部