forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
308 lines (248 loc) Β· 7.89 KB
/
root.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package cmd
import (
"errors"
"fmt"
"net/http"
_ "net/http/pprof" // pprof handler
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/evcc-io/evcc/core"
"github.com/evcc-io/evcc/push"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/server/modbus"
"github.com/evcc-io/evcc/server/updater"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/pipe"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/util/telemetry"
"github.com/fatih/structs"
"github.com/jeremywohl/flatten"
"golang.org/x/exp/maps"
_ "github.com/joho/godotenv/autoload"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const rebootDelay = 5 * time.Minute // delayed reboot on error
var (
log = util.NewLogger("main")
cfgFile string
ignoreEmpty = "" // ignore empty keys
ignoreErrors = []string{"warn", "error"} // ignore errors
ignoreMqtt = []string{"auth", "releaseNotes"} // excessive size may crash certain brokers
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "evcc",
Short: "EV Charge Controller",
Version: server.FormattedVersion(),
Run: runRoot,
}
func init() {
cobra.OnInitialize(initConfig)
// global options
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
// config file options
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
bindP(rootCmd, "log")
rootCmd.Flags().Bool("metrics", false, "Expose metrics")
bind(rootCmd, "metrics")
rootCmd.Flags().Bool("profile", false, "Expose pprof profiles")
bind(rootCmd, "profile")
}
// initConfig reads in config file and ENV variables if set
func initConfig() {
if cfgFile != "" {
// Use config file from the flag
viper.SetConfigFile(cfgFile)
} else {
// Search for config in home directory if available
if home, err := os.UserHomeDir(); err == nil {
viper.AddConfigPath(home)
}
// Search config in home directory with name "mbmd" (without extension).
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath("/etc") // path to look for the config file in
viper.SetConfigName("evcc")
}
viper.SetEnvPrefix("evcc")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv() // read in environment variables that match
// register all known config keys
flat, _ := flatten.Flatten(structs.Map(conf), "", flatten.DotStyle)
for _, v := range maps.Keys(flat) {
_ = viper.BindEnv(v)
}
// print version
util.LogLevel("info", nil)
log.INFO.Printf("evcc %s", server.FormattedVersion())
}
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runRoot(cmd *cobra.Command, args []string) {
// load config and re-configure logging after reading config file
var err error
if cfgErr := loadConfigFile(&conf); errors.As(cfgErr, &viper.ConfigFileNotFoundError{}) {
log.INFO.Println("missing config file - switching into demo mode")
if err := demoConfig(&conf); err != nil {
log.FATAL.Fatal(err)
}
} else {
err = cfgErr
}
// network config
if viper.GetString("uri") != "" {
log.WARN.Println("`uri` is deprecated and will be ignored. Use `network` instead.")
}
log.INFO.Printf("starting ui and api at :%d", conf.Network.Port)
// start broadcasting values
tee := new(util.Tee)
// value cache
cache := util.NewCache()
go cache.Run(pipe.NewDropper(ignoreErrors...).Pipe(tee.Attach()))
// create web server
socketHub := server.NewSocketHub()
httpd := server.NewHTTPd(fmt.Sprintf(":%d", conf.Network.Port), socketHub)
// metrics
if viper.GetBool("metrics") {
httpd.Router().Handle("/metrics", promhttp.Handler())
}
// pprof
if viper.GetBool("profile") {
httpd.Router().PathPrefix("/debug/").Handler(http.DefaultServeMux)
}
// publish to UI
go socketHub.Run(pipe.NewDropper(ignoreEmpty).Pipe(tee.Attach()), cache)
// setup values channel
valueChan := make(chan util.Param)
go tee.Run(valueChan)
// capture log messages for UI
util.CaptureLogs(valueChan)
// setup environment
if err == nil {
err = configureEnvironment(cmd, conf)
}
// setup telemetry
if err == nil {
telemetry.Create(conf.Plant)
if conf.Telemetry {
err = telemetry.Enable(true)
}
}
// setup modbus proxy
if err == nil {
for _, cfg := range conf.ModbusProxy {
if err = modbus.StartProxy(cfg.Port, cfg.Settings, cfg.ReadOnly); err != nil {
break
}
}
}
// setup site and loadpoints
var site *core.Site
if err == nil {
cp.TrackVisitors() // track duplicate usage
site, err = configureSiteAndLoadpoints(conf)
}
// setup database
if err == nil && conf.Influx.URL != "" {
configureInflux(conf.Influx, site, pipe.NewDropper(append(ignoreErrors, ignoreEmpty)...).Pipe(tee.Attach()))
}
// setup mqtt publisher
if err == nil && conf.Mqtt.Broker != "" {
publisher := server.NewMQTT(strings.Trim(conf.Mqtt.Topic, "/"))
go publisher.Run(site, pipe.NewDropper(append(ignoreMqtt, ignoreEmpty)...).Pipe(tee.Attach()))
}
// announce on mDNS
if err == nil && strings.HasSuffix(conf.Network.Host, ".local") {
err = configureMDNS(conf.Network)
}
// start HEMS server
if err == nil && conf.HEMS.Type != "" {
err = configureHEMS(conf.HEMS, site, httpd)
}
// setup messaging
var pushChan chan push.Event
if err == nil {
pushChan, err = configureMessengers(conf.Messaging, valueChan, cache)
}
// run shutdown functions on stop
var once sync.Once
stopC := make(chan struct{})
// catch signals
go func() {
signalC := make(chan os.Signal, 1)
signal.Notify(signalC, os.Interrupt, syscall.SIGTERM)
<-signalC // wait for signal
once.Do(func() { close(stopC) }) // signal loop to end
}()
// wait for shutdown
go func() {
<-stopC
select {
case <-shutdownDoneC(): // wait for shutdown
case <-time.After(conf.Interval):
}
if err != nil {
os.Exit(1)
}
os.Exit(0)
}()
// show main ui
if err == nil {
httpd.RegisterSiteHandlers(site, cache)
httpd.RegisterShutdownHandler(func() {
log.FATAL.Println("evcc was stopped by user. OS should restart the service. Or restart manually.")
once.Do(func() { close(stopC) }) // signal loop to end
})
// set channels
site.DumpConfig()
site.Prepare(valueChan, pushChan)
// show and check version, reduce api load during development
if server.Version != server.DevVersion {
valueChan <- util.Param{Key: "version", Val: server.FormattedVersion()}
go updater.Run(log, httpd, valueChan)
}
// expose sponsor to UI
if sponsor.Subject != "" {
valueChan <- util.Param{Key: "sponsor", Val: sponsor.Subject}
var validDuration time.Duration
if d := time.Until(sponsor.ExpiresAt); d > 0 && d < 30*24*time.Hour {
validDuration = d
}
valueChan <- util.Param{Key: "sponsorTokenExpires", Val: validDuration}
}
// allow web access for vehicles
cp.webControl(conf.Network, httpd.Router(), valueChan)
go func() {
site.Run(stopC, conf.Interval)
}()
} else {
httpd.RegisterShutdownHandler(func() {
log.FATAL.Println("evcc was stopped. OS should restart the service. Or restart manually.")
once.Do(func() { close(stopC) }) // signal loop to end
})
// improve error message
err = wrapErrors(err)
publishErrorInfo(valueChan, cfgFile, err)
log.FATAL.Println(err)
log.FATAL.Printf("will attempt restart in: %v", rebootDelay)
go func() {
<-time.After(rebootDelay)
once.Do(func() { close(stopC) }) // signal loop to end
}()
}
// uds health check listener
go server.HealthListener(site)
log.FATAL.Println(wrapErrors(httpd.ListenAndServe()))
}