-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
root.go
188 lines (153 loc) Β· 4.19 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
package cmd
import (
"fmt"
"os"
"time"
"github.com/andig/evcc/provider"
"github.com/andig/evcc/server"
"github.com/andig/evcc/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
latest "github.com/tcnksm/go-latest"
)
var (
log = util.NewLogger("main")
cfgFile string
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "evcc",
Short: "EV Charge Controller",
Version: fmt.Sprintf("%s (%s)", server.Version, server.Commit),
Run: run,
}
func bind(cmd *cobra.Command, flag string) {
if err := viper.BindPFlag(flag, cmd.PersistentFlags().Lookup(flag)); err != nil {
panic(err)
}
}
func configureCommand(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(
"log", "l",
"error",
"Log level (fatal, error, warn, info, debug, trace)",
)
bind(cmd, "log")
cmd.PersistentFlags().StringVarP(&cfgFile,
"config", "c",
"",
"Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")",
)
cmd.PersistentFlags().BoolP(
"help", "h",
false,
"Help for "+cmd.Name(),
)
}
func init() {
cobra.OnInitialize(initConfig)
configureCommand(rootCmd)
rootCmd.PersistentFlags().StringP(
"uri", "u",
"0.0.0.0:7070",
"Listen address",
)
bind(rootCmd, "uri")
rootCmd.PersistentFlags().DurationP(
"interval", "i",
10*time.Second,
"Update interval",
)
bind(rootCmd, "interval")
}
// 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.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in
if err := viper.ReadInConfig(); err == nil {
// using config file
cfgFile = viper.ConfigFileUsed()
} else if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// parsing failed - exit
fmt.Println(err)
os.Exit(1)
} else {
// not using config file
cfgFile = ""
}
}
// 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)
}
}
// checkVersion validates if updates are available
func checkVersion() {
githubTag := &latest.GithubTag{
Owner: "andig",
Repository: "evcc",
}
if res, err := latest.Check(githubTag, server.Version); err == nil {
if res.Outdated {
log.INFO.Printf("updates available - please upgrade to %s", res.Current)
}
}
}
func run(cmd *cobra.Command, args []string) {
util.LogLevel(viper.GetString("log"))
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)
// load config and re-configure logging after reading config file
conf := loadConfigFile(cfgFile)
util.LogLevel(viper.GetString("log"))
go checkVersion()
uri := viper.GetString("uri")
log.INFO.Println("listening at", uri)
// setup messaging
notificationChan := configureMessengers(conf.Messaging)
// setup mqtt
if viper.Get("mqtt") != nil {
provider.MQTT = provider.NewMqttClient(conf.Mqtt.Broker, conf.Mqtt.User, conf.Mqtt.Password, clientID(), 1)
}
// setup loadpoints
loadPoints := loadConfig(conf, notificationChan)
// start broadcasting values
tee := &Tee{}
cache := util.NewCache()
go cache.Run(tee.Attach())
// setup database
if conf.Influx.URL != "" {
configureDatabase(tee.Attach(), conf.Influx)
}
// create webserver
socketHub := server.NewSocketHub()
httpd := server.NewHTTPd(uri, conf.Menu, loadPoints[0], socketHub)
// publish to UI
go socketHub.Run(tee.Attach(), cache)
// setup values channel
valueChan := make(chan util.Param)
go tee.Run(valueChan)
// capture log messages for UI
util.CaptureLogs(valueChan)
// start all loadpoints
for _, lp := range loadPoints {
lp.Dump()
lp.Prepare(valueChan, notificationChan)
go lp.Run(conf.Interval)
}
log.FATAL.Println(httpd.ListenAndServe())
}