forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charger.go
136 lines (112 loc) Β· 3.13 KB
/
charger.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
package cmd
import (
"strconv"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cobra"
)
// chargerCmd represents the charger command
var chargerCmd = &cobra.Command{
Use: "charger [name]",
Short: "Query configured chargers",
Args: cobra.MaximumNArgs(1),
Run: runCharger,
}
func init() {
rootCmd.AddCommand(chargerCmd)
chargerCmd.Flags().Float64P(flagCurrent, "i", 0, flagCurrentDescription)
//lint:ignore SA1019 as Title is safe on ascii
chargerCmd.Flags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))
//lint:ignore SA1019 as Title is safe on ascii
chargerCmd.Flags().BoolP(flagDisable, "d", false, strings.Title(flagDisable))
chargerCmd.Flags().Bool(flagDiagnose, false, flagDiagnoseDescription)
chargerCmd.Flags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
chargerCmd.Flags().IntP(flagPhases, "p", 0, flagPhasesDescription)
}
func runCharger(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}
// setup environment
if err := configureEnvironment(cmd, &conf); err != nil {
log.FATAL.Fatal(err)
}
if err := configureChargers(conf.Chargers, args...); err != nil {
log.FATAL.Fatal(err)
}
var phases int
if flag := cmd.Flags().Lookup(flagPhases); flag.Changed {
var err error
phases, err = strconv.Atoi(flag.Value.String())
if err != nil {
log.ERROR.Fatalln(err)
}
}
chargers := config.Chargers().Devices()
var flagUsed bool
for _, v := range config.Instances(chargers) {
if flag := cmd.Flags().Lookup(flagCurrent); flag.Changed {
flagUsed = true
current, err := strconv.ParseFloat(flag.Value.String(), 64)
if err != nil {
log.ERROR.Fatalln(err)
}
if vv, ok := v.(api.ChargerEx); ok {
if err := vv.MaxCurrentMillis(current); err != nil {
log.ERROR.Println("set current:", err)
}
} else {
if err := v.MaxCurrent(int64(current)); err != nil {
log.ERROR.Println("set current:", err)
}
}
}
if cmd.Flags().Lookup(flagEnable).Changed {
flagUsed = true
if err := v.Enable(true); err != nil {
log.ERROR.Println("enable:", err)
}
}
if cmd.Flags().Lookup(flagDisable).Changed {
flagUsed = true
if err := v.Enable(false); err != nil {
log.ERROR.Println("disable:", err)
}
}
if cmd.Flags().Lookup(flagWakeup).Changed {
flagUsed = true
if vv, ok := v.(api.Resurrector); ok {
if err := vv.WakeUp(); err != nil {
log.ERROR.Println("wakeup:", err)
}
} else {
log.ERROR.Println("wakeup: not implemented")
}
}
if phases > 0 {
flagUsed = true
if vv, ok := v.(api.PhaseSwitcher); ok {
if err := vv.Phases1p3p(phases); err != nil {
log.ERROR.Println("set phases:", err)
}
} else {
log.ERROR.Println("phases: not implemented")
}
}
}
if !flagUsed {
d := dumper{len: len(chargers)}
flag := cmd.Flags().Lookup(flagDiagnose).Changed
for _, dev := range chargers {
v := dev.Instance()
d.DumpWithHeader(dev.Config().Name, v)
if flag {
d.DumpDiagnosis(v)
}
}
}
// wait for shutdown
<-shutdownDoneC()
}