-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add charger, meter and vehicle commands (#38)
- Loading branch information
Showing
6 changed files
with
268 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/andig/evcc/provider" | ||
"github.com/andig/evcc/server" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// chargerCmd represents the charger command | ||
var chargerCmd = &cobra.Command{ | ||
Use: "charger [name]", | ||
Short: "Query configured chargers", | ||
Run: runCharger, | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
rootCmd.AddCommand(chargerCmd) | ||
configureCommand(chargerCmd) | ||
} | ||
|
||
func runCharger(cmd *cobra.Command, args []string) { | ||
level, _ := cmd.PersistentFlags().GetString("log") | ||
configureLogging(level) | ||
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) | ||
|
||
// load config | ||
conf := loadConfigFile(cfgFile) | ||
|
||
// setup mqtt | ||
if viper.Get("mqtt") != nil { | ||
provider.MQTT = provider.NewMqttClient(conf.Mqtt.Broker, conf.Mqtt.User, conf.Mqtt.Password, clientID(), 1) | ||
} | ||
|
||
chargers := configureChargers(conf) | ||
|
||
for name, v := range chargers { | ||
if len(args) == 1 { | ||
if target := args[0]; name != target { | ||
if _, ok := chargers[target]; !ok { | ||
log.FATAL.Fatalf("charger not found: %s", target) | ||
} | ||
continue | ||
} | ||
} else if len(chargers) != 1 { | ||
fmt.Println(name) | ||
} | ||
|
||
if status, err := v.Status(); err != nil { | ||
fmt.Printf("Status: %v\n", err) | ||
} else { | ||
fmt.Printf("Status: %s\n", status) | ||
} | ||
|
||
if enabled, err := v.Enabled(); err != nil { | ||
fmt.Printf("Enabled: %v\n", err) | ||
} else { | ||
fmt.Printf("Enabled: %s\n", truefalse[enabled]) | ||
} | ||
|
||
dumpAPIs(v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/andig/evcc/api" | ||
) | ||
|
||
var truefalse = map[bool]string{false: "false", true: "true"} | ||
|
||
func dumpAPIs(v interface{}) { | ||
if v, ok := v.(api.Meter); ok { | ||
if power, err := v.CurrentPower(); err != nil { | ||
fmt.Printf("Power: %v\n", err) | ||
} else { | ||
fmt.Printf("Power: %.0fW\n", power) | ||
} | ||
} | ||
|
||
if v, ok := v.(api.MeterEnergy); ok { | ||
if energy, err := v.TotalEnergy(); err != nil { | ||
fmt.Printf("Energy: %v\n", err) | ||
} else { | ||
fmt.Printf("Energy: %.0fkWh\n", energy) | ||
} | ||
} | ||
|
||
if v, ok := v.(api.ChargeRater); ok { | ||
if energy, err := v.ChargedEnergy(); err != nil { | ||
fmt.Printf("Charged: %v\n", err) | ||
} else { | ||
fmt.Printf("Charged: %.0fkWh\n", energy) | ||
} | ||
} | ||
|
||
if v, ok := v.(api.ChargeTimer); ok { | ||
if duration, err := v.ChargingTime(); err != nil { | ||
fmt.Printf("Duration: %v\n", err) | ||
} else { | ||
fmt.Printf("Duration: %v\n", duration) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/andig/evcc/provider" | ||
"github.com/andig/evcc/server" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// meterCmd represents the meter command | ||
var meterCmd = &cobra.Command{ | ||
Use: "meter [name]", | ||
Short: "Query configured meters", | ||
Run: runMeter, | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
rootCmd.AddCommand(meterCmd) | ||
configureCommand(meterCmd) | ||
} | ||
|
||
func runMeter(cmd *cobra.Command, args []string) { | ||
level, _ := cmd.PersistentFlags().GetString("log") | ||
configureLogging(level) | ||
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) | ||
|
||
// load config | ||
conf := loadConfigFile(cfgFile) | ||
|
||
// setup mqtt | ||
if viper.Get("mqtt") != nil { | ||
provider.MQTT = provider.NewMqttClient(conf.Mqtt.Broker, conf.Mqtt.User, conf.Mqtt.Password, clientID(), 1) | ||
} | ||
|
||
meters := configureMeters(conf) | ||
|
||
for name, v := range meters { | ||
if len(args) == 1 { | ||
if target := args[0]; name != target { | ||
if _, ok := meters[target]; !ok { | ||
log.FATAL.Fatalf("meter not found: %s", target) | ||
} | ||
continue | ||
} | ||
} else if len(meters) != 1 { | ||
fmt.Println(name) | ||
} | ||
|
||
dumpAPIs(v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/andig/evcc/provider" | ||
"github.com/andig/evcc/server" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// vehicleCmd represents the vehicle command | ||
var vehicleCmd = &cobra.Command{ | ||
Use: "vehicle [name]", | ||
Short: "Query configured vehicles", | ||
Run: runVehicle, | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
rootCmd.AddCommand(vehicleCmd) | ||
configureCommand(vehicleCmd) | ||
} | ||
|
||
func runVehicle(cmd *cobra.Command, args []string) { | ||
level, _ := cmd.PersistentFlags().GetString("log") | ||
configureLogging(level) | ||
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit) | ||
|
||
// load config | ||
conf := loadConfigFile(cfgFile) | ||
|
||
// setup mqtt | ||
if viper.Get("mqtt") != nil { | ||
provider.MQTT = provider.NewMqttClient(conf.Mqtt.Broker, conf.Mqtt.User, conf.Mqtt.Password, clientID(), 1) | ||
} | ||
|
||
vehicles := configureVehicles(conf) | ||
|
||
for name, v := range vehicles { | ||
if len(args) == 1 { | ||
if target := args[0]; name != target { | ||
if _, ok := vehicles[target]; !ok { | ||
log.FATAL.Fatalf("charger not found: %s", target) | ||
} | ||
continue | ||
} | ||
} else if len(vehicles) != 1 { | ||
fmt.Println(name) | ||
} | ||
|
||
if soc, err := v.ChargeState(); err != nil { | ||
fmt.Printf("State: %v\n", err) | ||
} else { | ||
fmt.Printf("State: %.0f%%\n", soc) | ||
} | ||
|
||
dumpAPIs(v) | ||
} | ||
} |