forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
458 lines (389 loc) Β· 12.2 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package configure
import (
"bufio"
_ "embed"
"errors"
"fmt"
"os"
"slices"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/cloudfoundry/jibber_jabber"
"github.com/evcc-io/evcc/hems/semp"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/machine"
"github.com/evcc-io/evcc/util/templates"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/exp/maps"
"golang.org/x/text/language"
)
//go:embed localization/de.toml
var lang_de string
//go:embed localization/en.toml
var lang_en string
type CmdConfigure struct {
configuration Configure
localizer *i18n.Localizer
log *util.Logger
lang string
advancedMode, expandedMode bool
addedDeviceIndex int
errItemNotPresent, errDeviceNotValid error
capabilitySMAHems bool
}
// Run starts the interactive configuration
func (c *CmdConfigure) Run(log *util.Logger, flagLang string, advancedMode, expandedMode bool, category string) {
c.log = log
c.advancedMode = advancedMode
c.expandedMode = expandedMode
c.log.INFO.Printf("evcc %s", server.FormattedVersion())
bundle := i18n.NewBundle(language.German)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
if _, err := bundle.ParseMessageFileBytes([]byte(lang_de), "localization/de.toml"); err != nil {
panic(err)
}
if _, err := bundle.ParseMessageFileBytes([]byte(lang_en), "localization/en.toml"); err != nil {
panic(err)
}
c.lang = "de"
systemLanguage, err := jibber_jabber.DetectLanguage()
if err == nil {
c.lang = systemLanguage
}
if flagLang != "" {
c.lang = flagLang
}
c.localizer = i18n.NewLocalizer(bundle, c.lang)
c.setDefaultTexts()
// Assign random plant id. Don't use actual machine-id as file might
// be copied around to a different machine.
c.configuration.config.Plant = machine.RandomID()
if err = machine.CustomID(c.configuration.config.Plant); err != nil {
panic(err)
}
fmt.Println()
fmt.Println(c.localizedString("Intro"))
if !c.advancedMode {
// ask the user for his knowledge, so advanced mode can also be turned on this way
fmt.Println()
flowIndex, _ := c.askChoice(c.localizedString("Flow_Mode"), []string{
c.localizedString("Flow_Mode_Standard"),
c.localizedString("Flow_Mode_Advanced"),
})
if flowIndex == 1 {
c.advancedMode = true
}
}
if !c.advancedMode && category == "" {
c.flowNewConfigFile()
return
}
if category != "" {
for cat := range DeviceCategories {
if cat == DeviceCategory(category) {
c.flowSingleDevice(DeviceCategory(category))
return
}
}
log.FATAL.Fatalln("invalid category:", category, "have:", maps.Keys(DeviceCategories))
}
fmt.Println()
flowIndex, _ := c.askChoice(c.localizedString("Flow_Type"), []string{
c.localizedString("Flow_Type_NewConfiguration"),
c.localizedString("Flow_Type_SingleDevice"),
})
switch flowIndex {
case 0:
c.flowNewConfigFile()
case 1:
c.flowSingleDevice("")
}
}
// configureSingleDevice implements the flow for getting a single device configuration
func (c *CmdConfigure) flowSingleDevice(category DeviceCategory) {
fmt.Println()
fmt.Println(c.localizedString("Flow_SingleDevice_Setup"))
fmt.Println()
fmt.Println(c.localizedString("Flow_SingleDevice_Select"))
// only consider the device categories that are marked for this flow
categoryChoices := []string{
DeviceCategories[DeviceCategoryGridMeter].title,
DeviceCategories[DeviceCategoryPVMeter].title,
DeviceCategories[DeviceCategoryBatteryMeter].title,
DeviceCategories[DeviceCategoryChargeMeter].title,
DeviceCategories[DeviceCategoryCharger].title,
DeviceCategories[DeviceCategoryVehicle].title,
}
if category == "" {
fmt.Println()
_, categoryTitle := c.askChoice(c.localizedString("Flow_SingleDevice_Select"), categoryChoices)
for item, data := range DeviceCategories {
if data.title == categoryTitle {
category = item
break
}
}
}
devices := c.configureDevices(category, false, false)
for _, item := range devices {
fmt.Println()
fmt.Println(c.localizedString("Flow_SingleDevice_Config", localizeMap{}))
fmt.Println()
scanner := bufio.NewScanner(strings.NewReader(item.Yaml))
for scanner.Scan() {
fmt.Println(" " + scanner.Text())
}
}
fmt.Println()
}
// configureNewConfigFile implements the flow for creating a new configuration file
func (c *CmdConfigure) flowNewConfigFile() {
fmt.Println()
fmt.Println(c.localizedString("Flow_NewConfiguration_Setup"))
fmt.Println()
fmt.Println(c.localizedString("Flow_NewConfiguration_Select", localizeMap{"ItemNotPresent": c.localizedString("ItemNotPresent")}))
c.configureDeviceGuidedSetup()
_ = c.configureDevices(DeviceCategoryGridMeter, true, false)
_ = c.configureDevices(DeviceCategoryPVMeter, true, true)
_ = c.configureDevices(DeviceCategoryBatteryMeter, true, true)
_ = c.configureDevices(DeviceCategoryVehicle, true, true)
c.configureLoadpoints()
c.configureSite()
// check if SMA HEMS is available and ask the user if it should be added
if c.capabilitySMAHems {
c.configureSMAHems()
}
if c.advancedMode && c.configuration.config.SponsorToken == "" {
_ = c.askSponsortoken(false, false)
}
yaml, err := c.configuration.RenderConfiguration()
if err != nil {
c.log.FATAL.Fatal(err)
}
fmt.Println()
filename := DefaultConfigFilename
for {
file, err := os.OpenFile(filename, os.O_WRONLY, 0o666)
if errors.Is(err, os.ErrNotExist) {
break
}
file.Close()
// in case of permission error, we can't write to the file anyway
if os.IsPermission(err) {
fmt.Println(c.localizedString("File_Permissions", localizeMap{"FileName": filename}))
} else {
if c.askYesNo(c.localizedString("File_Exists", localizeMap{"FileName": filename})) {
break
}
}
filename = c.askValue(question{
label: c.localizedString("File_NewFilename"),
exampleValue: "evcc_neu.yaml",
required: true,
})
}
err = os.WriteFile(filename, yaml, 0o755)
if err != nil {
fmt.Printf("%s: ", c.localizedString("File_Error_SaveFailed", localizeMap{"FileName": filename}))
c.log.FATAL.Fatal(err)
}
fmt.Println(c.localizedString("File_SaveSuccess", localizeMap{"FileName": filename}))
}
// configureDevices asks device specific questions
func (c *CmdConfigure) configureDevices(deviceCategory DeviceCategory, askAdding, askMultiple bool) []device {
var devices []device
if deviceCategory == DeviceCategoryGridMeter && c.configuration.MetersOfCategory(deviceCategory) > 0 {
return nil
}
localizeMap := localizeMap{
"Article": DeviceCategories[deviceCategory].article,
"Additional": DeviceCategories[deviceCategory].additional,
"Category": DeviceCategories[deviceCategory].title,
}
if askAdding {
addDeviceText := c.localizedString("AddDeviceInCategory", localizeMap)
if c.configuration.MetersOfCategory(deviceCategory) > 0 {
addDeviceText = c.localizedString("AddAnotherDeviceInCategory", localizeMap)
}
fmt.Println()
if !c.askYesNo(addDeviceText) {
return nil
}
}
for {
device, capabilities, err := c.configureDeviceCategory(deviceCategory)
if err != nil {
break
}
devices = append(devices, device)
c.processDeviceCapabilities(capabilities)
if !askMultiple {
break
}
fmt.Println()
if !c.askYesNo(c.localizedString("AddAnotherDeviceInCategory", localizeMap)) {
break
}
}
return devices
}
// configureSMAHems asks the user if he wants to add the SMA HEMS
func (c *CmdConfigure) configureSMAHems() {
// check if the system provides a machine-id
if _, err := semp.UniqueDeviceID(); err != nil {
return
}
fmt.Println()
fmt.Println(c.localizedString("Flow_SMAHems_Setup"))
fmt.Println()
if !c.askYesNo(c.localizedString("Flow_SMAHems_Add")) {
return
}
// check if we need to setup a HEMS
c.configuration.config.Hems = "type: sma\nAllowControl: false\n"
}
// configureLoadpoints asks loadpoint specific questions
func (c *CmdConfigure) configureLoadpoints() {
fmt.Println()
fmt.Println(c.localizedString("Loadpoint_Setup"))
for {
loadpointTitle := c.askValue(question{
label: c.localizedString("Loadpoint_Title"),
defaultValue: c.localizedString("Loadpoint_DefaultTitle"),
required: true,
})
loadpoint := loadpoint{
Title: loadpointTitle,
Phases: 3,
MinCurrent: 6,
}
charger, capabilities, err := c.configureDeviceCategory(DeviceCategoryCharger)
if err != nil {
break
}
chargerHasMeter := charger.ChargerHasMeter
loadpoint.Charger = charger.Name
if !chargerHasMeter {
if c.askYesNo(c.localizedString("Loadpoint_WallboxWOMeter")) {
chargeMeter, _, err := c.configureDeviceCategory(DeviceCategoryChargeMeter)
if err == nil {
loadpoint.ChargeMeter = chargeMeter.Name
chargerHasMeter = true
}
}
}
vehicles := c.configuration.DevicesOfClass(templates.Vehicle)
if len(vehicles) > 0 {
fmt.Println()
if c.askYesNo(c.localizedString("Loadpoint_VehicleDisableAutoDetection")) {
if len(vehicles) == 1 {
loadpoint.Vehicle = vehicles[0].Name
} else {
fmt.Println()
var vehicleTitles []string
for _, vehicle := range vehicles {
vehicleTitles = append(vehicleTitles, vehicle.Title)
}
vehicleIndex, _ := c.askChoice(c.localizedString("Loadpoint_VehicleSelection"), vehicleTitles)
loadpoint.Vehicle = vehicles[vehicleIndex].Name
}
}
}
var minValue int = 6
if slices.Contains(capabilities, templates.CapabilityISO151182) {
minValue = 2
}
if c.advancedMode {
fmt.Println()
minAmperage := c.askValue(question{
label: c.localizedString("Loadpoint_WallboxMinAmperage"),
valueType: templates.TypeNumber,
minNumberValue: int64(minValue),
maxNumberValue: 32,
required: true,
})
loadpoint.MinCurrent, _ = strconv.Atoi(minAmperage)
maxAmperage := c.askValue(question{
label: c.localizedString("Loadpoint_WallboxMaxAmperage"),
valueType: templates.TypeNumber,
minNumberValue: 6,
maxNumberValue: 32,
required: true,
})
loadpoint.MaxCurrent, _ = strconv.Atoi(maxAmperage)
if !chargerHasMeter {
phaseChoices := []string{"1", "2", "3"}
fmt.Println()
phaseIndex, _ := c.askChoice(c.localizedString("Loadpoint_WallboxPhases"), phaseChoices)
loadpoint.Phases = phaseIndex + 1
}
} else {
powerChoices := []string{
c.localizedString("Loadpoint_WallboxPower36kW"),
c.localizedString("Loadpoint_WallboxPower11kW"),
c.localizedString("Loadpoint_WallboxPower22kW"),
c.localizedString("Loadpoint_WallboxPowerOther"),
}
fmt.Println()
powerIndex, _ := c.askChoice(c.localizedString("Loadpoint_WallboxMaxPower"), powerChoices)
loadpoint.MinCurrent = minValue
switch powerIndex {
case 0:
loadpoint.MaxCurrent = 16
if !chargerHasMeter {
loadpoint.Phases = 1
}
case 1:
loadpoint.MaxCurrent = 16
if !chargerHasMeter {
loadpoint.Phases = 3
}
case 2:
loadpoint.MaxCurrent = 32
if !chargerHasMeter {
loadpoint.Phases = 3
}
case 3:
amperage := c.askValue(question{
label: c.localizedString("Loadpoint_WallboxMaxAmperage"),
valueType: templates.TypeNumber,
minNumberValue: int64(minValue),
maxNumberValue: 32,
required: true,
})
loadpoint.MaxCurrent, _ = strconv.Atoi(amperage)
if !chargerHasMeter {
phaseChoices := []string{"1", "2", "3"}
fmt.Println()
phaseIndex, _ := c.askChoice(c.localizedString("Loadpoint_WallboxPhases"), phaseChoices)
loadpoint.Phases = phaseIndex + 1
}
}
}
fmt.Println()
loadpoint.Mode = c.askValue(question{valueType: templates.TypeChargeModes, excludeNone: true})
fmt.Println()
loadpoint.ResetOnDisconnect = c.askValue(question{
label: c.localizedString("Loadpoint_ResetOnDisconnect"),
valueType: templates.TypeBool,
})
c.configuration.AddLoadpoint(loadpoint)
fmt.Println()
if !c.askYesNo(c.localizedString("Loadpoint_AddAnother")) {
break
}
}
}
// configureSite asks site specific questions
func (c *CmdConfigure) configureSite() {
fmt.Println()
fmt.Println(c.localizedString("Site_Setup"))
siteTitle := c.askValue(question{
label: c.localizedString("Site_Title"),
defaultValue: c.localizedString("Site_DefaultTitle"),
required: true,
})
c.configuration.config.Site.Title = siteTitle
}