forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle.go
120 lines (100 loc) Β· 2.61 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
package cmd
import (
"fmt"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
)
// vehicleCmd represents the vehicle command
var vehicleCmd = &cobra.Command{
Use: "vehicle [name]",
Short: "Query configured vehicles",
Run: runVehicle,
}
func init() {
rootCmd.AddCommand(vehicleCmd)
vehicleCmd.PersistentFlags().StringP(flagName, "n", "", fmt.Sprintf(flagNameDescription, "vehicle"))
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)
vehicleCmd.Flags().BoolP(flagWakeup, "w", false, flagWakeupDescription)
//lint:ignore SA1019 as Title is safe on ascii
vehicleCmd.Flags().Bool(flagDiagnose, false, strings.Title(flagDiagnose))
}
func runVehicle(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
fatal(err)
}
// setup environment
if err := configureEnvironment(cmd, conf); err != nil {
fatal(err)
}
// select single vehicle
if err := selectByName(cmd, &conf.Vehicles); err != nil {
fatal(err)
}
if err := cp.configureVehicles(conf); err != nil {
fatal(err)
}
vehicles := cp.vehicles
if len(args) == 1 {
name := args[0]
vehicle, err := cp.Vehicle(name)
if err != nil {
log.FATAL.Fatal(err)
}
vehicles = map[string]api.Vehicle{name: vehicle}
}
// check single vehicle for error
if len(vehicles) == 1 {
if err, ok := maps.Values(vehicles)[0].(error); ok {
fatal(err)
}
}
var flagUsed bool
for _, v := range vehicles {
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.VehicleChargeController); ok {
if err := vv.StartCharge(); 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.VehicleChargeController); ok {
if err := vv.StopCharge(); 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 name, v := range vehicles {
d.DumpWithHeader(name, v)
if flag {
d.DumpDiagnosis(v)
}
}
}
// wait for shutdown
<-shutdownDoneC()
}