-
Notifications
You must be signed in to change notification settings - Fork 125
/
init.go
89 lines (73 loc) · 2.42 KB
/
init.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
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2020-2022 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package service
import (
"context"
"sync"
"github.com/edgexfoundry/device-sdk-go/v2/internal/common"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/gorilla/mux"
"github.com/edgexfoundry/device-sdk-go/v2/internal/cache"
"github.com/edgexfoundry/device-sdk-go/v2/internal/provision"
"github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
)
// Bootstrap contains references to dependencies required by the BootstrapHandler.
type Bootstrap struct {
router *mux.Router
}
// NewBootstrap is a factory method that returns an initialized Bootstrap receiver struct.
func NewBootstrap(router *mux.Router) *Bootstrap {
return &Bootstrap{
router: router,
}
}
func (b *Bootstrap) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer startup.Timer, dic *di.Container) (success bool) {
ds.UpdateFromContainer(b.router, dic)
ds.selfAssign()
ds.ctx = ctx
ds.wg = wg
ds.controller.InitRestRoutes()
err := cache.InitCache(ds.ServiceName, dic)
if err != nil {
ds.LoggingClient.Errorf("Failed to init cache: %v", err)
return false
}
if ds.AsyncReadings() {
ds.asyncCh = make(chan *models.AsyncValues, ds.config.Device.AsyncBufferSize)
go ds.processAsyncResults(ctx, wg, dic)
}
if ds.DeviceDiscovery() {
ds.deviceCh = make(chan []models.DiscoveredDevice, 1)
go ds.processAsyncFilterAndAdd(ctx, wg)
}
e := ds.driver.Initialize(ds.LoggingClient, ds.asyncCh, ds.deviceCh)
if e != nil {
ds.LoggingClient.Errorf("Failed to init ProtocolDriver: %v", e)
return false
}
ds.initialized = true
err = ds.selfRegister()
if err != nil {
ds.LoggingClient.Errorf("Failed to register service on Metadata: %v", err)
return false
}
err = provision.LoadProfiles(ds.config.Device.ProfilesDir, dic)
if err != nil {
ds.LoggingClient.Errorf("Failed to create the pre-defined device profiles: %v", err)
return false
}
err = provision.LoadDevices(ds.config.Device.DevicesDir, dic)
if err != nil {
ds.LoggingClient.Errorf("Failed to create the pre-defined devices: %v", err)
return false
}
ds.manager.StartAutoEvents()
// Very important that this handler is called after the NewServiceMetrics handler so
// MetricsManager dependency has been created.
common.InitializeSentMetrics(ds.LoggingClient, dic)
return true
}