forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter.go
85 lines (70 loc) · 1.89 KB
/
meter.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
package cmd
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cobra"
)
// meterCmd represents the meter command
var meterCmd = &cobra.Command{
Use: "meter [name]",
Short: "Query configured meters",
Args: cobra.MaximumNArgs(1),
Run: runMeter,
}
func init() {
rootCmd.AddCommand(meterCmd)
meterCmd.Flags().StringP(flagBatteryMode, "b", "", flagBatteryModeDescription)
meterCmd.Flags().DurationP(flagBatteryModeWait, "w", 0, flagBatteryModeWaitDescription)
meterCmd.Flags().BoolP(flagRepeat, "r", false, flagRepeatDescription)
}
func runMeter(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 := configureMeters(conf.Meters, args...); err != nil {
log.FATAL.Fatal(err)
}
mode := api.BatteryUnknown
if val := cmd.Flags().Lookup(flagBatteryMode).Value.String(); val != "" {
var err error
mode, err = api.BatteryModeString(val)
if err != nil {
log.ERROR.Fatalln(err)
}
}
meters := config.Meters().Devices()
var flagUsed bool
if mode != api.BatteryUnknown {
flagUsed = true
for _, v := range config.Instances(meters) {
if b, ok := v.(api.BatteryController); ok {
if err := b.SetBatteryMode(mode); err != nil {
log.FATAL.Fatalln("set battery mode:", err)
}
}
if d, err := cmd.Flags().GetDuration(flagBatteryModeWait); d > 0 && err == nil {
log.INFO.Println("waiting for:", d)
time.Sleep(d)
}
}
}
if !flagUsed {
d := dumper{len: len(meters)}
REPEAT:
for _, dev := range meters {
v := dev.Instance()
d.DumpWithHeader(dev.Config().Name, v)
}
if ok, _ := cmd.Flags().GetBool(flagRepeat); ok {
goto REPEAT
}
}
// wait for shutdown
<-shutdownDoneC()
}