forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtariff.go
79 lines (64 loc) · 1.5 KB
/
tariff.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
package cmd
import (
"fmt"
"os"
"text/tabwriter"
"github.com/evcc-io/evcc/tariff"
"github.com/evcc-io/evcc/util/config"
"github.com/spf13/cobra"
)
// tariffCmd represents the vehicle command
var tariffCmd = &cobra.Command{
Use: "tariff [name]",
Short: "Query configured tariff",
Args: cobra.MaximumNArgs(1),
Run: runTariff,
}
func init() {
rootCmd.AddCommand(tariffCmd)
}
func runTariff(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)
}
var name string
if len(args) == 1 {
name = args[0]
}
for key, cc := range map[string]config.Typed{
"grid": conf.Tariffs.Grid,
"feedin": conf.Tariffs.FeedIn,
"co2": conf.Tariffs.Co2,
"planner": conf.Tariffs.Planner,
} {
if cc.Type == "" || (name != "" && key != name) {
continue
}
if name == "" {
fmt.Println(key + ":")
}
tf, err := tariff.NewFromConfig(cc.Type, cc.Other)
if err != nil {
fatal(err)
}
rates, err := tf.Rates()
if err != nil {
fatal(err)
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintln(tw, "From\tTo\tPrice/Cost")
const format = "2006-01-02 15:04:05"
for _, r := range rates {
fmt.Fprintf(tw, "%s\t%s\t%.3f\n", r.Start.Local().Format(format), r.End.Local().Format(format), r.Price)
}
tw.Flush()
fmt.Println()
}
// wait for shutdown
<-shutdownDoneC()
}