-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (84 loc) · 3.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2020 IOTech Systems LTD
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"os"
"strings"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg"
sdkTransforms "github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/transforms"
influxTransforms "app-service-influx/pkg/transforms"
)
const serviceKey = "app-influx-export"
func main() {
// turn off secure mode for examples. Not recommended for production
_ = os.Setenv("EDGEX_SECURITY_SECRET_STORE", "false")
// 1) First thing to do is to create an new instance of an EdgeX Application Service.
service, ok := pkg.NewAppService(serviceKey)
if !ok {
os.Exit(-1)
}
// Leverage the built in logging service in EdgeX
lc := service.LoggingClient()
// 3) Load the MQTT custom configuration
config := &ServiceConfig{}
if err := service.LoadCustomConfig(config, "MqttSecretConfig"); err != nil {
lc.Errorf("LoadCustomConfig failed: %s", err.Error())
os.Exit(-1)
}
if err := config.Validate(); err != nil {
lc.Errorf("Config validation failed: %s", 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 := service.SetFunctionsPipeline(
influxTransforms.NewConversion().TransformToInflux,
sdkTransforms.NewMQTTSecretSender(config.MqttConfig, false).MQTTSend,
); err != nil {
lc.Errorf("SetFunctionsPipeline failed: %s", err.Error())
os.Exit(-1)
}
// 5) Lastly, we'll go ahead and tell the SDK to "start" and begin listening for events to trigger the pipeline.
if err := service.MakeItRun(); err != nil {
lc.Errorf("MakeItRun returned error: %s", err.Error())
os.Exit(-1)
}
// Do any required cleanup here
os.Exit(0)
}
// Service's custom configuration which is loaded from the configuration.toml
type ServiceConfig struct {
MqttConfig sdkTransforms.MQTTSecretConfig
}
// UpdateFromRaw updates the service's full configuration from raw data received from
// the Configuration Provider. Can just be a dummy 'return true' if never using the Configuration Provider
func (c *ServiceConfig) UpdateFromRaw(rawConfig interface{}) bool {
configuration, ok := rawConfig.(*ServiceConfig)
if !ok {
return false //errors.New("unable to cast raw config to type 'ServiceConfig'")
}
*c = *configuration
return true
}
func (c *ServiceConfig) Validate() error {
if len(strings.TrimSpace(c.MqttConfig.BrokerAddress)) == 0 {
return errors.New("configuration missing value for MqttSecretConfig.BrokerAddress")
}
if len(strings.TrimSpace(c.MqttConfig.ClientId)) == 0 {
return errors.New("configuration missing value for MqttSecretConfig.ClientId")
}
if len(strings.TrimSpace(c.MqttConfig.Topic)) == 0 {
return errors.New("configuration missing value for MqttSecretConfig.Topic")
}
if len(strings.TrimSpace(c.MqttConfig.AuthMode)) == 0 {
return errors.New("configuration missing value for MqttSecretConfig.AuthMode")
}
if len(strings.TrimSpace(c.MqttConfig.SecretPath)) == 0 {
return errors.New("configuration missing value for MqttSecretConfig.SecretPath")
}
return nil
}