From 927cd391ba06d48cc7c37cf183224e6c25ab5858 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 5 Jan 2025 10:29:11 +0100 Subject: [PATCH] Plugins: add timeseries (#18049) --- provider/timeseries.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 provider/timeseries.go diff --git a/provider/timeseries.go b/provider/timeseries.go new file mode 100644 index 0000000000..bcae29b73c --- /dev/null +++ b/provider/timeseries.go @@ -0,0 +1,40 @@ +package provider + +import ( + "context" + "encoding/json" + "time" + + "github.com/evcc-io/evcc/api" + "github.com/jinzhu/now" +) + +type timeseriesProvider struct{} + +func init() { + registry.AddCtx("timeseries", TimeSeriesFromConfig) +} + +// TimeSeriesFromConfig creates timeseries provider +func TimeSeriesFromConfig(_ context.Context, _ map[string]interface{}) (Provider, error) { + return new(timeseriesProvider), nil +} + +var _ StringProvider = (*timeseriesProvider)(nil) + +func (p *timeseriesProvider) StringGetter() (func() (string, error), error) { + return func() (string, error) { + res := make(api.Rates, 48) + ts := now.BeginningOfHour() + for i := 0; i < 48; i++ { + res[i] = api.Rate{ + Start: ts, + End: ts.Add(time.Hour), + } + ts = ts.Add(time.Hour) + } + + b, err := json.Marshal(res) + return string(b), err + }, nil +}