generated from theriverman/go-cli-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.actions.go
472 lines (434 loc) · 15 KB
/
cli.actions.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package main
/*
The application's business logic shall be implemented here in cli.actions.go in a function similar to `actionGreet` and `actionVersion` below.
They must be added as a parameter in main.NewApplication where all possible application commands are defined.
*/
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"text/tabwriter"
"unicode"
vocdriver "github.com/theriverman/VolvoOnCall"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
func actionCars(c *cli.Context) error {
account, err := client.CustomerAccount.GetAccount()
if err != nil {
return err
}
if err = account.RetrieveHyperlinks(); err != nil {
return err
}
vehicles, err := account.GetVehicles()
if err != nil {
return err
}
fmt.Printf("Cars associated to Volvo Account(%s):\n", account.Username)
fmt.Println("-----------------------------------" + strings.Repeat("-", len(account.Username)))
for _, vehicle := range vehicles {
if err = vehicle.RetrieveHyperlinks(); err != nil {
return err
}
fmt.Printf(" * %s (%s)\n", vehicle.VehicleID, vehicle.Attributes.RegistrationNumber)
}
return nil
}
func actionRegister(c *cli.Context) error {
homeDirPath, err := os.UserHomeDir()
if err != nil {
return err
}
defaultConfPath := filepath.Join(homeDirPath, ".voc.conf")
return Config.WriteToFile(defaultConfPath)
}
func actionAttributes(c *cli.Context) error {
vehicle, err := client.Vehicles.GetVehicleByVIN(selectedVin)
if err != nil {
return err
}
if asJson {
s, err := json.MarshalIndent(vehicle.Attributes, "", "\t")
if err != nil {
return err
}
fmt.Println(string(s))
return nil
}
// for more advanced query options, see the Path Syntax at https://github.com/tidwall/gjson
if len(customAttributes.Value()) > 0 {
s, err := json.MarshalIndent(vehicle.Attributes, "", "\t")
if err != nil {
return err
}
for _, v := range customAttributes.Value() {
fmt.Printf("%s: %s\n", v, gjson.GetBytes(s, v).String())
}
return nil
}
// default mode - print all attributes
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
defer w.Flush()
fmt.Fprintf(w, "\n %s\t%s\t", "Car Attribute", "Attribute Value")
fmt.Fprintf(w, "\n %s\t%s\t", "----", "----")
fmt.Fprintf(w, "\n")
v := reflect.ValueOf(*vehicle.Attributes)
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
if unicode.IsLower(rune(typeOfS.Field(i).Name[0])) {
continue
}
fmt.Fprintf(w, "%s:\t %v\n", typeOfS.Field(i).Name, v.Field(i).Interface())
}
return nil
}
func actionPosition(c *cli.Context) error {
pos, err := client.Vehicles.GetVehiclePositionByVIN(selectedVin)
if err != nil {
return err
}
if asJson {
s, err := json.MarshalIndent(pos, "", "\t")
if err != nil {
return err
}
fmt.Println(string(s))
return nil
}
// POSITION
fmt.Println("Position")
fmt.Printf(" - Longitude:\t%.15f\n", pos.Position.Longitude)
fmt.Printf(" - Latitude:\t%.15f\n", pos.Position.Latitude)
fmt.Printf(" - Timestamp:\t%s\n", pos.Position.Timestamp)
// fmt.Printf(" - Speed:\t%s\n", (pos.Position.Speed)) // TODO: enable when actual type is known
// fmt.Printf(" - Heading:\t%s\n", pos.Position.Heading) // TODO: enable when actual type is known
fmt.Printf(" - Maps URL:\thttps://www.google.com/maps/place/%.15f,%.15f\n", pos.Position.Latitude, pos.Position.Longitude)
// CALCULATED POSITION
if pos.CalculatedPosition.Longitude > 1.0 {
fmt.Println("\nCalculated Position")
fmt.Printf(" - Longitude:\t%.15f\n", pos.CalculatedPosition.Longitude)
fmt.Printf(" - Latitude:\t%.15f\n", pos.CalculatedPosition.Latitude)
fmt.Printf(" - Timestamp:\t%s\n", pos.CalculatedPosition.Timestamp)
// fmt.Printf(" - Speed:\t%s\n", pos.CalculatedPosition.Speed) // TODO: enable when actual type is known
// fmt.Printf(" - Heading:\t%s\n", pos.CalculatedPosition.Heading) // TODO: enable when actual type is known
fmt.Printf(" - Maps URL:\thttps://www.google.com/maps/place/%.15f,%.15f\n", pos.CalculatedPosition.Latitude, pos.CalculatedPosition.Longitude)
} else {
fmt.Println("\nCalculated Position is not available")
}
return nil
}
func actionStatus(c *cli.Context) error {
vehicle, err := client.Vehicles.GetVehicleByVIN(selectedVin)
if err != nil {
return err
}
if asJson {
s, err := json.MarshalIndent(vehicle.Status, "", "\t")
if err != nil {
return err
}
fmt.Println(string(s))
return nil
}
// for more advanced query options, see the Path Syntax at https://github.com/tidwall/gjson
if len(customAttributes.Value()) > 0 {
s, err := json.MarshalIndent(vehicle.Status, "", "\t")
if err != nil {
return err
}
for _, v := range customAttributes.Value() {
fmt.Printf("%s: %s\n", v, gjson.GetBytes(s, v).String())
}
return nil
}
// default mode - print select attributes
fmt.Printf("Average Fuel Consumption:\t%.1f l/100 km\n", vehicle.Status.AverageFuelConsumption/10)
fmt.Printf("Average Speed:\t\t\t%d km/h\n", vehicle.Status.AverageSpeed)
fmt.Printf("Brake Fluid:\t\t\t%s\n", vehicle.Status.BrakeFluid)
if len(vehicle.Status.BulbFailures) > 0 {
fmt.Println("Bulb Failures:")
for _, failure := range vehicle.Status.BulbFailures {
fmt.Printf("\t %s\n", failure)
}
}
fmt.Printf("Car Locked:\t\t\t%t\n", vehicle.Status.CarLocked)
fmt.Printf("Distance to Empty:\t\t%d km\n", vehicle.Status.DistanceToEmpty)
doors := vehicle.Status.Doors
if doors.HoodOpen || doors.FrontLeftDoorOpen || doors.FrontRightDoorOpen || doors.RearLeftDoorOpen || doors.RearRightDoorOpen || doors.TailgateOpen {
fmt.Printf("Doors Open:\t\t\t%t\n", vehicle.Status.CarLocked)
fmt.Printf("\t Hood Open: %t\n", doors.HoodOpen)
fmt.Printf("\t Front Left Door Open: %t\n", doors.FrontLeftDoorOpen)
fmt.Printf("\t Front Right Door Open: %t\n", doors.FrontRightDoorOpen)
fmt.Printf("\t Rear Left Door Open: %t\n", doors.RearLeftDoorOpen)
fmt.Printf("\t Rear Right Door Open: %t\n", doors.RearRightDoorOpen)
fmt.Printf("\t Tailgate Open: %t\n", doors.TailgateOpen)
} else {
fmt.Println("Doors Open:\t\t\tNone")
}
fmt.Printf("Engine Running:\t\t\t%t\n", vehicle.Status.EngineRunning)
fmt.Printf("Fuel Amount [l]:\t\t%d l\n", vehicle.Status.FuelAmount)
fmt.Printf("Fuel Amount [%%]:\t\t%d%%\n", vehicle.Status.FuelAmountLevel)
return actionPosition(c)
}
func actionTrips(c *cli.Context) error {
trips, err := client.Vehicles.GetVehicleTripsByVIN(selectedVin)
if err != nil {
return err
}
if asJson {
s, err := json.MarshalIndent(trips.Trips, "", "\t")
if err != nil {
return err
}
fmt.Println(string(s))
return nil
}
// default mode
p := message.NewPrinter(language.English)
for i := len(trips.Trips) - 1; i >= 0; i-- { // ! reverse-loop !
trip := trips.Trips[i]
fmt.Printf("Trip %d\n", i+1)
fmt.Printf(" ID: %d\n", trip.ID)
fmt.Printf(" Name: %s\n", trip.Name)
fmt.Printf(" Category: %s\n", trip.Category)
fmt.Printf(" User Notes: %s\n", trip.UserNotes)
for ii, tripDetail := range trip.TripDetails {
fmt.Printf(" Trip Detail %d\n", ii+1)
fmt.Printf(" Start Time: %s\n", tripDetail.StartTime)
fmt.Printf(" End Time: %s\n", tripDetail.EndTime)
fmt.Printf(" Fuel Consumption: %.3f l\n", tripDetail.FuelConsumption/100)
fmt.Printf(" Electrical Consumption: %.3f kWh\n", float64(tripDetail.ElectricalConsumption)/1000)
fmt.Printf(" Electrical Regeneration: %.3f kWh\n", float64(tripDetail.ElectricalRegeneration)/1000)
fmt.Printf(" Distance: %.3f km\n", tripDetail.Distance/1000)
p.Printf(" Start Odometer: %d km\n", tripDetail.StartOdometer/1000)
p.Printf(" End Odometer: %d km\n", tripDetail.EndOdometer/1000)
fmt.Printf(" Start Position: %+v\n", tripDetail.StartPosition)
fmt.Printf(" End Position: %+v\n", tripDetail.EndPosition)
fmt.Println()
}
fmt.Println()
}
return nil
}
func actionLock(c *cli.Context) error {
status, err := client.Vehicles.LockVehicle(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionUnlock(c *cli.Context) error {
status, err := client.Vehicles.UnlockVehicle(selectedVin)
if err != nil {
return err
}
fmt.Println("Within 2 minutes press once gently on the rubberised pressure plate underneath the boot lid handle to unlock the car")
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStartHeater(c *cli.Context) error {
status, err := client.Vehicles.StartHeater(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStopHeater(c *cli.Context) error {
status, err := client.Vehicles.StopHeater(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStartEngine(c *cli.Context) error {
status, err := client.Vehicles.StartEngine(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStopEngine(c *cli.Context) error {
status, err := client.Vehicles.StopEngine(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStartPreclimatization(c *cli.Context) error {
status, err := client.Vehicles.StartPreclimatization(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionStopPreclimatization(c *cli.Context) error {
status, err := client.Vehicles.StopPreclimatization(selectedVin)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionBlink(c *cli.Context) error {
status, err := client.Vehicles.BlinkLights(selectedVin, nil)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionHonk(c *cli.Context) error {
status, err := client.Vehicles.HonkAndBlink(selectedVin, nil)
if err != nil {
return err
}
return client.Vehicles.EvaluateServiceStatusAuto(status)
}
func actionListChargingLocations(c *cli.Context) error {
chargingLocations, err := client.Vehicles.GetChargingLocations(selectedVin)
if err != nil {
return err
}
for _, cl := range chargingLocations.ChargingLocations {
fmt.Printf("Name: %s\n", cl.Name)
fmt.Printf(" ID: %s\n", path.Base(cl.ChargeLocation))
fmt.Printf(" Status: %s\n", cl.Status)
fmt.Printf(" Plug in reminder enabled: %t\n", cl.PlugInReminderEnabled)
fmt.Printf(" Vehicle is at charging location: %t\n", cl.VehicleAtChargingLocation)
fmt.Printf(" Position:\n")
fmt.Printf(" - Street Address:\t%s\n", cl.Position.StreetAddress)
fmt.Printf(" - City:\t\t%s\n", cl.Position.City)
fmt.Printf(" - Postal Code:\t%s\n", cl.Position.PostalCode)
fmt.Printf(" - Region:\t\t%s\n", cl.Position.Region)
fmt.Printf(" - Country Code:\t%s\n", cl.Position.ISO2CountryCode)
fmt.Printf(" - Longitude:\t%.15f\n", cl.Position.Longitude)
fmt.Printf(" - Latitude:\t\t%.15f\n", cl.Position.Latitude)
fmt.Printf(" - Maps URL:\t\thttps://www.google.com/maps/place/%.15f,%.15f\n", cl.Position.Latitude, cl.Position.Longitude)
fmt.Printf("---------------------------------------------------------------------------------------\n\n")
}
return nil
}
func actionGetChargingLocationById(c *cli.Context) error {
if c.Args().Len() == 0 {
return fmt.Errorf("you must provide a charging location id. see --help for more details")
}
cl, err := client.Vehicles.GetChargingLocation(selectedVin, c.Args().First())
if err != nil {
return err
}
fmt.Printf("Name: %s\n", cl.Name)
fmt.Printf(" ID: %s\n", path.Base(cl.ChargeLocation))
fmt.Printf(" Status: %s\n", cl.Status)
fmt.Printf(" Plug in reminder enabled: %t\n", cl.PlugInReminderEnabled)
fmt.Printf(" Vehicle is at charging location: %t\n", cl.VehicleAtChargingLocation)
fmt.Printf(" Position:\n")
fmt.Printf(" - Street Address:\t%s\n", cl.Position.StreetAddress)
fmt.Printf(" - City:\t\t%s\n", cl.Position.City)
fmt.Printf(" - Postal Code:\t%s\n", cl.Position.PostalCode)
fmt.Printf(" - Region:\t\t%s\n", cl.Position.Region)
fmt.Printf(" - Country Code:\t%s\n", cl.Position.ISO2CountryCode)
fmt.Printf(" - Longitude:\t%.15f\n", cl.Position.Longitude)
fmt.Printf(" - Latitude:\t\t%.15f\n", cl.Position.Latitude)
fmt.Printf(" - Maps URL:\t\thttps://www.google.com/maps/place/%.15f,%.15f\n", cl.Position.Latitude, cl.Position.Longitude)
fmt.Printf(" ---------------------------------------------------------------------------------------------\n\n")
return nil
}
func actionEnableDelayCharging(c *cli.Context) error {
var (
chargingId, startTime, stopTime string
)
dc := vocdriver.DelayCharging{
Enabled: true,
}
switch c.Args().Len() {
case 0:
return fmt.Errorf("you must provide a charging location id. see --help for more details")
case 1:
chargingId = c.Args().First()
cl, err := client.Vehicles.GetChargingLocation(selectedVin, chargingId)
if err != nil {
return err
}
dc.StartTime = cl.DelayCharging.StartTime
dc.StopTime = cl.DelayCharging.StopTime
case 2:
return fmt.Errorf("unexpected number of arguments were passed. minimum 1 or exactly 3 allowed")
case 3:
chargingId = c.Args().First()
startTime = c.Args().Get(1)
stopTime = c.Args().Get(2)
dc.StartTime = startTime
dc.StopTime = stopTime
default:
return fmt.Errorf("unexpected number of arguments were passed. minimum 1 or exactly 3 allowed")
}
vehicle, err := client.Vehicles.GetVehicleByVIN(selectedVin)
if err != nil {
return err
}
if _, err = vehicle.SetDelayCharging(chargingId, &dc); err != nil {
return err
}
return nil
}
func actionDisableDelayCharging(c *cli.Context) error {
if c.Args().Len() == 0 {
return fmt.Errorf("you must provide a charging location id. see --help for more details")
}
dc := vocdriver.DelayCharging{
Enabled: false,
}
vehicle, err := client.Vehicles.GetVehicleByVIN(selectedVin)
if err != nil {
return err
}
if _, err = vehicle.SetDelayCharging(c.Args().First(), &dc); err != nil {
return err
}
return nil
}
func actionUpdateDelayCharging(c *cli.Context) error {
var (
chargingId, startTime, stopTime string
)
dc := vocdriver.DelayCharging{}
switch c.Args().Len() {
case 3:
chargingId = c.Args().First()
startTime = c.Args().Get(1)
stopTime = c.Args().Get(2)
cl, err := client.Vehicles.GetChargingLocation(selectedVin, chargingId)
if err != nil {
return err
}
dc.Enabled = cl.DelayCharging.Enabled // keep current status
dc.StartTime = startTime
dc.StopTime = stopTime
default:
return fmt.Errorf("you must provide: charging location id + start time + stop time. see --help for more details")
}
vehicle, err := client.Vehicles.GetVehicleByVIN(selectedVin)
if err != nil {
return err
}
if _, err = vehicle.SetDelayCharging(chargingId, &dc); err != nil {
return err
}
return nil
}
func actionVersion(c *cli.Context) error {
fmt.Println(AppName + ":")
fmt.Printf(" Version: %s\n", AppSemVersion)
fmt.Printf(" Go version: %s\n", runtime.Version())
fmt.Printf(" Git commit: %s\n", GitCommit)
fmt.Printf(" Built: %s\n", AppBuildDate)
fmt.Printf(" OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf(" Build type: %s\n", AppBuildType)
return nil
}