Edgex的application开发SDK

[复制链接]
 楼主| keer_zu 发表于 2020-12-15 15:50 | 显示全部楼层 |阅读模式
本帖最后由 keer_zu 于 2020-12-15 16:36 编辑

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.         edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK initialization failed: %v\n", err))
  16.         os.Exit(-1)
  17.     }

  18.     // 3) Since our FilterByDeviceName Function requires the list of Device Names we would
  19.     // like to search for, we'll go ahead and define that now.
  20.     deviceNames := []string{"Random-Float-Device"}

  21.     // 4) This is our pipeline configuration, the collection of functions to
  22.     // execute every time an event is triggered.
  23.     if err := edgexSdk.SetFunctionsPipeline(
  24.             transforms.NewFilter(deviceNames).FilterByDeviceName,
  25.             transforms.NewConversion().TransformToXML,
  26.         ); err != nil {
  27.             edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK SetPipeline failed: %v\n", err))
  28.             os.Exit(-1)
  29.         }

  30.     // 5) shows how to access the application's specific configuration settings.
  31.     appSettings := edgexSdk.ApplicationSettings()
  32.     if appSettings != nil {
  33.         appName, ok := appSettings["ApplicationName"]
  34.         if ok {
  35.             edgexSdk.LoggingClient.Info(fmt.Sprintf("%s now running...", appName))
  36.         } else {
  37.             edgexSdk.LoggingClient.Error("ApplicationName application setting not found")
  38.             os.Exit(-1)
  39.         }
  40.     } else {
  41.         edgexSdk.LoggingClient.Error("No application settings found")
  42.         os.Exit(-1)
  43.     }

  44.     // 6) Lastly, we'll go ahead and tell the SDK to "start" and begin listening for events to trigger the pipeline.
  45.     edgexSdk.MakeItRun()
  46. }

上面的示例只是为了演示应用程序的结构。注意,最后一个函数的输出在这个应用程序的任何地方都不可用。您必须提供一个函数,以便使用前一个函数中的数据。让我们继续,并添加以下函数,将输出打印到控制台。

  1. func printXMLToConsole(edgexcontext *appcontext.Context, params ...interface{}) (bool,interface{}) {
  2.   if len(params) < 1 {
  3.     // We didn't receive a result
  4.     return false, errors.New("No Data Received")
  5.   }
  6.   println(params[0].(string))
  7.   return true, nil
  8. }


 楼主| keer_zu 发表于 2020-12-15 16:49 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1479

主题

12921

帖子

55

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