-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
executable file
·145 lines (130 loc) · 6.06 KB
/
context.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright (c) 2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package appcontext
import (
syscontext "context"
"errors"
"fmt"
"time"
"github.com/edgexfoundry/go-mod-core-contracts/clients"
"github.com/edgexfoundry/go-mod-core-contracts/clients/command"
"github.com/edgexfoundry/go-mod-core-contracts/clients/coredata"
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/clients/notifications"
"github.com/edgexfoundry/go-mod-core-contracts/models"
"github.com/epcom-hdxt/app-functions-sdk-go/internal/common"
"github.com/epcom-hdxt/app-functions-sdk-go/internal/security"
"github.com/epcom-hdxt/app-functions-sdk-go/pkg/util"
"github.com/google/uuid"
)
// AppFunction is a type alias for func(edgexcontext *appcontext.Context, params ...interface{}) (bool, interface{})
type AppFunction = func(edgexcontext *Context, params ...interface{}) (bool, interface{})
// Context ...
type Context struct {
// ID of the EdgeX Event -- will be filled for a received JSON Event
EventID string
// Checksum of the EdgeX Event -- will be filled for a received CBOR Event
EventChecksum string
// This is the ID used to track the EdgeX event through entire EdgeX framework.
CorrelationID string
// OutputData is used for specifying the data that is to be outputted. Leverage the .Complete() function to set.
OutputData []byte
// This holds the configuration for your service. This is the preferred way to access your custom application settings that have been set in the configuration.
Configuration *common.ConfigurationStruct
// LoggingClient is exposed to allow logging following the preferred logging strategy within EdgeX.
LoggingClient logger.LoggingClient
// EventClient exposes Core Data's EventClient API
EventClient coredata.EventClient
// ValueDescriptorClient exposes Core Data's ValueDescriptor API
ValueDescriptorClient coredata.ValueDescriptorClient
// CommandClient exposes Core Commands's Command API
CommandClient command.CommandClient
// NotificationsClient exposes Support Notification's Notifications API
NotificationsClient notifications.NotificationsClient
// RetryData holds the data to be stored for later retry when the pipeline function returns an error
RetryData []byte
// SecretProvider exposes the support for getting and storing secrets
SecretProvider security.SecretProvider
// ResponseContentType is used for holding custom response type for HTTP trigger
ResponseContentType string
}
// Complete is optional and provides a way to return the specified data.
// In the case of an HTTP Trigger, the data will be returned as the http response.
// In the case of the message bus trigger, the data will be placed on the specifed
// message bus publish topic and host in the configuration.
func (context *Context) Complete(output []byte) {
context.OutputData = output
}
// MarkAsPushed will make a request to CoreData to mark the event that triggered the pipeline as pushed.
func (context *Context) MarkAsPushed() error {
context.LoggingClient.Debug("Marking event as pushed")
if context.EventClient == nil {
return fmt.Errorf("unable to Mark As Pushed: '%s' is missing from Clients configuration", common.CoreDataClientName)
}
if context.EventID != "" {
return context.EventClient.MarkPushed(syscontext.WithValue(syscontext.Background(), clients.CorrelationHeader, context.CorrelationID), context.EventID)
} else if context.EventChecksum != "" {
return context.EventClient.MarkPushedByChecksum(syscontext.WithValue(syscontext.Background(), clients.CorrelationHeader, context.CorrelationID), context.EventChecksum)
} else {
return errors.New("No EventID or EventChecksum Provided")
}
}
// SetRetryData sets the RetryData to the specified payload to be stored for later retry
// when the pipeline function returns an error.
func (context *Context) SetRetryData(payload []byte) {
context.RetryData = payload
}
// PushToCoreData pushes the provided value as an event to CoreData using the device name and reading name that have been set. If validation is turned on in
// CoreServices then your deviceName and readingName must exist in the CoreMetadata and be properly registered in EdgeX.
func (context *Context) PushToCoreData(deviceName string, readingName string, value interface{}) (*models.Event, error) {
context.LoggingClient.Debug("Pushing to CoreData")
if context.EventClient == nil {
return nil, fmt.Errorf("unable to Push To CoreData: '%s' is missing from Clients configuration", common.CoreDataClientName)
}
now := time.Now().UnixNano()
val, err := util.CoerceType(value)
if err != nil {
return nil, err
}
newReading := models.Reading{
Value: string(val),
Origin: now,
Device: deviceName,
Name: readingName,
}
readings := make([]models.Reading, 0, 1)
readings = append(readings, newReading)
newEdgeXEvent := &models.Event{
Device: deviceName,
Origin: now,
Readings: readings,
}
correlation := uuid.New().String()
ctx := syscontext.WithValue(syscontext.Background(), clients.CorrelationHeader, correlation)
result, err := context.EventClient.Add(ctx, newEdgeXEvent)
if err != nil {
return nil, err
}
newEdgeXEvent.ID = result
return newEdgeXEvent, nil
}
// GetSecrets retrieves secrets from a secret store.
// path specifies the type or location of the secrets to retrieve.
// keys specifies the secrets which to retrieve. If no keys are provided then all the keys associated with the
// specified path will be returned.
func (context *Context) GetSecrets(path string, keys ...string) (map[string]string, error) {
return context.SecretProvider.GetSecrets(path, keys...)
}