-
Notifications
You must be signed in to change notification settings - Fork 125
/
service.go
292 lines (253 loc) · 10.7 KB
/
service.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2017-2018 Canonical Ltd
// Copyright (C) 2018-2022 IOTech Ltd
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
// This package provides a basic EdgeX Foundry device service implementation
// meant to be embedded in an application, similar in approach to the builtin
// net/http package.
package service
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"sync"
"github.com/edgexfoundry/device-sdk-go/v2/internal/clients"
sdkCommon "github.com/edgexfoundry/device-sdk-go/v2/internal/common"
"github.com/edgexfoundry/device-sdk-go/v2/internal/config"
"github.com/edgexfoundry/device-sdk-go/v2/internal/container"
restController "github.com/edgexfoundry/device-sdk-go/v2/internal/controller/http"
sdkModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
bootstrapConfig "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/config"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/flags"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces"
bootstrapTypes "github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/requests"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/models"
"github.com/edgexfoundry/go-mod-registry/v2/registry"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
var (
ds *DeviceService
)
// UpdatableConfig interface allows services to have custom configuration populated from configuration stored
// in the Configuration Provider (aka Consul). Services using custom configuration must implement this interface
// on their custom configuration, even if they do not use Configuration Provider. If they do not use the
// Configuration Provider they can have a dummy implementation of this interface.
// This wraps the actual interface from go-mod-bootstrap so device service code doesn't have to have the additional
// direct import of go-mod-bootstrap.
type UpdatableConfig interface {
interfaces.UpdatableConfig
}
type DeviceService struct {
ServiceName string
LoggingClient logger.LoggingClient
RegistryClient registry.Client
SecretProvider interfaces.SecretProvider
MetricsManager interfaces.MetricsManager
edgexClients clients.EdgeXClients
controller *restController.RestController
config *config.ConfigurationStruct
deviceService *models.DeviceService
driver sdkModels.ProtocolDriver
discovery sdkModels.ProtocolDiscovery
validator sdkModels.DeviceValidator
manager sdkModels.AutoEventManager
asyncCh chan *sdkModels.AsyncValues
deviceCh chan []sdkModels.DiscoveredDevice
initialized bool
dic *di.Container
flags flags.Common
configProcessor *bootstrapConfig.Processor
ctx context.Context
wg *sync.WaitGroup
}
func (s *DeviceService) Initialize(serviceName, serviceVersion string, proto interface{}) {
if serviceName == "" {
_, _ = fmt.Fprintf(os.Stderr, "Please specify device service name")
os.Exit(1)
}
s.ServiceName = serviceName
if serviceVersion == "" {
_, _ = fmt.Fprintf(os.Stderr, "Please specify device service version")
os.Exit(1)
}
sdkCommon.ServiceVersion = serviceVersion
if driver, ok := proto.(sdkModels.ProtocolDriver); ok {
s.driver = driver
} else {
_, _ = fmt.Fprintf(os.Stderr, "Please implement and specify the protocoldriver")
os.Exit(1)
}
if discovery, ok := proto.(sdkModels.ProtocolDiscovery); ok {
s.discovery = discovery
} else {
s.discovery = nil
}
if validator, ok := proto.(sdkModels.DeviceValidator); ok {
s.validator = validator
} else {
s.validator = nil
}
s.deviceService = &models.DeviceService{}
s.config = &config.ConfigurationStruct{}
}
func (s *DeviceService) UpdateFromContainer(r *mux.Router, dic *di.Container) {
s.LoggingClient = bootstrapContainer.LoggingClientFrom(dic.Get)
s.RegistryClient = bootstrapContainer.RegistryFrom(dic.Get)
s.SecretProvider = bootstrapContainer.SecretProviderFrom(dic.Get)
s.MetricsManager = bootstrapContainer.MetricsManagerFrom(dic.Get)
s.edgexClients.DeviceClient = bootstrapContainer.DeviceClientFrom(dic.Get)
s.edgexClients.DeviceServiceClient = bootstrapContainer.DeviceServiceClientFrom(dic.Get)
s.edgexClients.DeviceProfileClient = bootstrapContainer.DeviceProfileClientFrom(dic.Get)
s.edgexClients.ProvisionWatcherClient = bootstrapContainer.ProvisionWatcherClientFrom(dic.Get)
s.edgexClients.EventClient = bootstrapContainer.EventClientFrom(dic.Get)
s.config = container.ConfigurationFrom(dic.Get)
s.manager = container.ManagerFrom(dic.Get)
s.controller = restController.NewRestController(r, dic, s.ServiceName)
}
// Name returns the name of this Device Service
func (s *DeviceService) Name() string {
return s.ServiceName
}
// Version returns the version number of this Device Service
func (s *DeviceService) Version() string {
return sdkCommon.ServiceVersion
}
// GetSecretProvider returns the SecretProvider
func (s *DeviceService) GetSecretProvider() interfaces.SecretProvider {
return s.SecretProvider
}
// GetMetricsManager returns the Metrics Manager used to register counter, gauge, gaugeFloat64 or timer metric types from
// github.com/rcrowley/go-metrics
func (s *DeviceService) GetMetricsManager() interfaces.MetricsManager {
return s.MetricsManager
}
// GetLoggingClient returns the logger.LoggingClient
func (s *DeviceService) GetLoggingClient() logger.LoggingClient {
return s.LoggingClient
}
// AsyncReadings returns a bool value to indicate whether the asynchronous reading is enabled.
func (s *DeviceService) AsyncReadings() bool {
return s.config.Device.EnableAsyncReadings
}
func (s *DeviceService) DeviceDiscovery() bool {
return s.config.Device.Discovery.Enabled
}
// AddRoute allows leveraging the existing internal web server to add routes specific to Device Service.
func (s *DeviceService) AddRoute(route string, handler func(http.ResponseWriter, *http.Request), methods ...string) error {
return s.controller.AddRoute(route, handler, methods...)
}
// Stop shuts down the Service
func (s *DeviceService) Stop(force bool) {
if s.initialized {
err := s.driver.Stop(force)
if err != nil {
s.LoggingClient.Error(err.Error())
}
}
}
// LoadCustomConfig uses the Config Processor from go-mod-bootstrap to attempt to load service's
// custom configuration. It uses the same command line flags to process the custom config in the same manner
// as the standard configuration.
func (s *DeviceService) LoadCustomConfig(customConfig UpdatableConfig, sectionName string) error {
if s.configProcessor == nil {
s.configProcessor = bootstrapConfig.NewProcessorForCustomConfig(s.flags, s.ctx, s.wg, s.dic)
}
if err := s.configProcessor.LoadCustomConfigSection(customConfig, sectionName); err != nil {
return err
}
s.controller.SetCustomConfigInfo(customConfig)
return nil
}
// ListenForCustomConfigChanges uses the Config Processor from go-mod-bootstrap to attempt to listen for
// changes to the specified custom configuration section. LoadCustomConfig must be called previously so that
// the instance of svc.configProcessor has already been set.
func (s *DeviceService) ListenForCustomConfigChanges(
configToWatch interface{},
sectionName string,
changedCallback func(interface{})) error {
if s.configProcessor == nil {
return fmt.Errorf(
"custom configuration must be loaded before '%s' section can be watched for changes",
sectionName)
}
s.configProcessor.ListenForCustomConfigChanges(configToWatch, sectionName, changedCallback)
return nil
}
// selfAssign creates and assigns the deviceService model, so that it can later be accessed via the dic.
func (s *DeviceService) selfAssign() {
*s.deviceService = models.DeviceService{
Name: s.ServiceName,
Labels: s.config.Device.Labels,
BaseAddress: bootstrapTypes.DefaultHttpProtocol + "://" + s.config.Service.Host + ":" + strconv.FormatInt(int64(s.config.Service.Port), 10),
AdminState: models.Unlocked,
}
}
// selfRegister register device service itself onto metadata.
func (s *DeviceService) selfRegister() errors.EdgeX {
localDeviceService := *s.deviceService
s.LoggingClient.Debugf("trying to find device service %s", localDeviceService.Name)
ctx := context.WithValue(context.Background(), common.CorrelationHeader, uuid.NewString()) // nolint:staticcheck
res, err := s.edgexClients.DeviceServiceClient.DeviceServiceByName(ctx, localDeviceService.Name)
if err != nil {
if errors.Kind(err) == errors.KindEntityDoesNotExist {
s.LoggingClient.Infof("device service %s doesn't exist, creating a new one", localDeviceService.Name)
req := requests.NewAddDeviceServiceRequest(dtos.FromDeviceServiceModelToDTO(localDeviceService))
idRes, err := s.edgexClients.DeviceServiceClient.Add(ctx, []requests.AddDeviceServiceRequest{req})
if err != nil {
s.LoggingClient.Errorf("failed to add device service %s: %v", localDeviceService.Name, err)
return err
}
s.deviceService.Id = idRes[0].Id
s.LoggingClient.Debugf("new device service id: %s", localDeviceService.Id)
} else {
s.LoggingClient.Errorf("failed to find device service %s", localDeviceService.Name)
return err
}
} else {
s.LoggingClient.Infof("device service %s exists, updating it", s.ServiceName)
req := requests.NewUpdateDeviceServiceRequest(dtos.FromDeviceServiceModelToUpdateDTO(localDeviceService))
req.Service.Id = nil
_, err = s.edgexClients.DeviceServiceClient.Update(ctx, []requests.UpdateDeviceServiceRequest{req})
if err != nil {
s.LoggingClient.Errorf("failed to update device service %s with local config: %v", localDeviceService.Name, err)
oldDeviceService := dtos.ToDeviceServiceModel(res.Service)
*s.deviceService = oldDeviceService
}
}
return nil
}
// DriverConfigs retrieves the driver specific configuration
func (s *DeviceService) DriverConfigs() map[string]string {
return s.config.Driver
}
// SetDeviceOpState sets the operating state of device
func (s *DeviceService) SetDeviceOpState(name string, state models.OperatingState) error {
d, err := s.GetDeviceByName(name)
if err != nil {
return err
}
d.OperatingState = state
return s.UpdateDevice(d)
}
// RunningService returns the Service instance which is running
func RunningService() *DeviceService {
return ds
}
// DriverConfigs retrieves the driver specific configuration
// TODO remove this in EDGEX3.0
func DriverConfigs() map[string]string {
return ds.config.Driver
}