From 4e3e4d99e9027a3ca1c8c6a3428e5f76856b50a0 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 26 Jun 2021 21:43:21 +0200 Subject: [PATCH] Add battery charge metric --- metrics.go | 6 ++++++ pkg/fronius/symo.go | 1 + pkg/fronius/symo_test.go | 4 +++- pkg/fronius/testdata/example_1.json | 3 ++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/metrics.go b/metrics.go index 098f204..6d10856 100644 --- a/metrics.go +++ b/metrics.go @@ -27,6 +27,11 @@ var ( Name: "inverter_power", Help: "Power flow of the inverter in Watt", }, []string{"inverter"}) + inverterBatteryChargeGaugeVec = promauto.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "inverter_soc", + Help: "State of charge of the battery attached to the inverter in percent", + }, []string{"inverter"}) sitePowerLoadGauge = promauto.NewGauge(prometheus.GaugeOpts{ Namespace: namespace, @@ -89,6 +94,7 @@ func parseMetrics(data *fronius.SymoData) { log.WithField("data", *data).Debug("Parsing data.") for key, inverter := range data.Inverters { inverterPowerGaugeVec.WithLabelValues(key).Set(inverter.Power) + inverterBatteryChargeGaugeVec.WithLabelValues(key).Set(inverter.BatterySoC / 100) } sitePowerAccuGauge.Set(data.Site.PowerAccu) sitePowerGridGauge.Set(data.Site.PowerGrid) diff --git a/pkg/fronius/symo.go b/pkg/fronius/symo.go index edd3b97..0c0932b 100644 --- a/pkg/fronius/symo.go +++ b/pkg/fronius/symo.go @@ -49,6 +49,7 @@ type ( Inverter struct { DT float64 `json:"DT"` Power float64 `json:"P"` + BatterySoC float64 `json:"SOC"` EnergyDay float64 `json:"E_Day"` EnergyYear float64 `json:"E_Year"` EnergyTotal float64 `json:"E_Total"` diff --git a/pkg/fronius/symo_test.go b/pkg/fronius/symo_test.go index 7dd924c..861b84f 100644 --- a/pkg/fronius/symo_test.go +++ b/pkg/fronius/symo_test.go @@ -14,7 +14,7 @@ func Test_Symo_GetPowerFlowData_GivenUrl_WhenRequestData_ThenParseStruct(t *test server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { payload, err := ioutil.ReadFile("testdata/example_1.json") require.NoError(t, err) - rw.Write(payload) + _, _ = rw.Write(payload) })) c, err := NewSymoClient(ClientOptions{ @@ -33,4 +33,6 @@ func Test_Symo_GetPowerFlowData_GivenUrl_WhenRequestData_ThenParseStruct(t *test assert.Equal(t, float64(22997), p.Site.EnergyDay) assert.Equal(t, float64(43059100), p.Site.EnergyTotal) assert.Equal(t, 3525577.75, p.Site.EnergyYear) + + assert.Equal(t, 34.5, p.Inverters["1"].BatterySoC) } diff --git a/pkg/fronius/testdata/example_1.json b/pkg/fronius/testdata/example_1.json index cc9a5ad..adc193c 100644 --- a/pkg/fronius/testdata/example_1.json +++ b/pkg/fronius/testdata/example_1.json @@ -7,7 +7,8 @@ "E_Day" : 22997, "E_Total" : 43059100, "E_Year" : 3525577.75, - "P" : 0 + "P" : 0, + "SOC": 34.5 } }, "Site" : {