-
Notifications
You must be signed in to change notification settings - Fork 125
/
main.go
137 lines (114 loc) · 4.05 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
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
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2020-2022 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package service
import (
"context"
"os"
"sync"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap"
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/handlers"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/gorilla/mux"
"github.com/edgexfoundry/device-sdk-go/v2/internal/autodiscovery"
"github.com/edgexfoundry/device-sdk-go/v2/internal/autoevent"
"github.com/edgexfoundry/device-sdk-go/v2/internal/container"
"github.com/edgexfoundry/device-sdk-go/v2/internal/controller/messaging"
)
const EnvInstanceName = "EDGEX_INSTANCE_NAME"
var instanceName string
func Main(serviceName string, serviceVersion string, proto interface{}, ctx context.Context, cancel context.CancelFunc, router *mux.Router) {
startupTimer := startup.NewStartUpTimer(serviceName)
additionalUsage :=
" -i, --instance Provides a service name suffix which allows unique instance to be created\n" +
" If the option is provided, service name will be replaced with \"<name>_<instance>\"\n"
sdkFlags := flags.NewWithUsage(additionalUsage)
sdkFlags.FlagSet.StringVar(&instanceName, "instance", "", "")
sdkFlags.FlagSet.StringVar(&instanceName, "i", "", "")
sdkFlags.Parse(os.Args[1:])
serviceName = setServiceName(serviceName)
ds = &DeviceService{}
ds.Initialize(serviceName, serviceVersion, proto)
ds.flags = sdkFlags
ds.dic = di.NewContainer(di.ServiceConstructorMap{
container.ConfigurationName: func(get di.Get) interface{} {
return ds.config
},
container.DeviceServiceName: func(get di.Get) interface{} {
return ds.deviceService
},
container.ProtocolDriverName: func(get di.Get) interface{} {
return ds.driver
},
container.ProtocolDiscoveryName: func(get di.Get) interface{} {
return ds.discovery
},
container.DeviceValidatorName: func(get di.Get) interface{} {
return ds.validator
},
})
httpServer := handlers.NewHttpServer(router, true)
bootstrap.Run(
ctx,
cancel,
sdkFlags,
ds.ServiceName,
common.ConfigStemDevice,
ds.config,
startupTimer,
ds.dic,
true,
[]interfaces.BootstrapHandler{
httpServer.BootstrapHandler,
messageBusBootstrapHandler,
handlers.NewServiceMetrics(ds.ServiceName).BootstrapHandler, // Must be after Messaging
clientBootstrapHandler,
autoevent.BootstrapHandler,
NewBootstrap(router).BootstrapHandler,
autodiscovery.BootstrapHandler,
handlers.NewStartMessage(serviceName, serviceVersion).BootstrapHandler,
})
ds.Stop(false)
}
func clientBootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer startup.Timer, dic *di.Container) bool {
configuration := container.ConfigurationFrom(dic.Get)
if configuration.Device.UseMessageBus {
delete(configuration.Clients, common.CoreDataServiceKey)
}
if !handlers.NewClientsBootstrap().BootstrapHandler(ctx, wg, startupTimer, dic) {
return false
}
return true
}
func messageBusBootstrapHandler(ctx context.Context, wg *sync.WaitGroup, startupTimer startup.Timer, dic *di.Container) bool {
configuration := container.ConfigurationFrom(dic.Get)
if configuration.Device.UseMessageBus {
if !handlers.MessagingBootstrapHandler(ctx, wg, startupTimer, dic) {
return false
}
err := messaging.SubscribeCommands(ctx, dic)
if err != nil {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
lc.Errorf("Failed to subscribe internal command request: %v", err)
return false
}
}
return true
}
func setServiceName(name string) string {
envValue := os.Getenv(EnvInstanceName)
if len(envValue) > 0 {
instanceName = envValue
}
if len(instanceName) > 0 {
name = name + "_" + instanceName
}
return name
}