forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.go
129 lines (108 loc) Β· 3.06 KB
/
vehicle.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
package cmd
import (
"strconv"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cobra"
)
// vehicleCmd represents the vehicle command
var vehicleCmd = &cobra.Command{
Use: "vehicle [name]",
Short: "Query configured vehicles",
Args: cobra.MaximumNArgs(1),
Run: runVehicle,
}
func init() {
rootCmd.AddCommand(vehicleCmd)
vehicleCmd.Flags().Int64P(flagCurrent, "i", 0, flagCurrentDescription)
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)
vehicleCmd.Flags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
vehicleCmd.Flags().Bool(flagDiagnose, false, flagDiagnoseDescription)
vehicleCmd.Flags().Bool(flagCloud, false, flagCloudDescription)
}
func runVehicle(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
fatal(err)
}
// setup environment
if err := configureEnvironment(cmd, &conf); err != nil {
fatal(err)
}
// use cloud
if cmd.Flags().Lookup(flagCloud).Changed {
for _, conf := range conf.Vehicles {
conf.Other["cloud"] = "true"
}
}
if err := configureVehicles(conf.Vehicles, args...); err != nil {
fatal(err)
}
vehicles := config.Vehicles().Devices()
var flagUsed bool
for _, v := range config.Instances(vehicles) {
if flag := cmd.Flags().Lookup(flagCurrent); flag.Changed {
flagUsed = true
f, err := strconv.ParseFloat(flag.Value.String(), 64)
if err != nil {
log.ERROR.Println("max current:", err)
} else {
if vv, ok := v.(api.ChargerEx); ok {
if err := vv.MaxCurrentMillis(f); err != nil {
log.ERROR.Println("max current:", err)
}
} else if vv, ok := v.(api.CurrentController); ok {
if err := vv.MaxCurrent(int64(f)); err != nil {
log.ERROR.Println("max current:", err)
}
} else {
log.ERROR.Println("max current: not implemented")
}
}
}
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 cmd.Flags().Lookup(flagStart).Changed {
flagUsed = true
if vv, ok := v.(api.ChargeController); ok {
if err := vv.ChargeEnable(true); err != nil {
log.ERROR.Println("start charge:", err)
}
} else {
log.ERROR.Println("start charge: not implemented")
}
}
if cmd.Flags().Lookup(flagStop).Changed {
flagUsed = true
if vv, ok := v.(api.ChargeController); ok {
if err := vv.ChargeEnable(false); err != nil {
log.ERROR.Println("stop charge:", err)
}
} else {
log.ERROR.Println("stop charge: not implemented")
}
}
}
if !flagUsed {
d := dumper{len: len(vehicles)}
flag := cmd.Flags().Lookup(flagDiagnose).Changed
for _, dev := range vehicles {
v := dev.Instance()
d.DumpWithHeader(dev.Config().Name, v)
if flag {
d.DumpDiagnosis(v)
}
}
}
// wait for shutdown
<-shutdownDoneC()
}