MQTT: publish the forecast as a single JSON message (#32710)
This commit is contained in:
parent
d2eef1190a
commit
93c182e486
3 changed files with 48 additions and 18 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
|
|
@ -13,6 +14,24 @@ import (
|
|||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// forecastDetails is the published forecast payload. It implements BytesMarshaler
|
||||
// so MQTT sends a single JSON message instead of decomposing every slot into an
|
||||
// individual topic (several thousand messages per update).
|
||||
type forecastDetails struct {
|
||||
Co2 [][]float64 `json:"co2,omitempty"`
|
||||
FeedIn [][]float64 `json:"feedin,omitempty"`
|
||||
Grid [][]float64 `json:"grid,omitempty"`
|
||||
Planner [][]float64 `json:"planner,omitempty"`
|
||||
Solar *solarDetails `json:"solar,omitempty"`
|
||||
Temperature [][]float64 `json:"temperature,omitempty"`
|
||||
}
|
||||
|
||||
var _ api.BytesMarshaler = (*forecastDetails)(nil)
|
||||
|
||||
func (fc forecastDetails) MarshalBytes() ([]byte, error) {
|
||||
return json.Marshal(fc)
|
||||
}
|
||||
|
||||
type solarDetails struct {
|
||||
Scale float64 `json:"scale"` // scale factor yield/forecasted today, 1 if unscaled
|
||||
Today dailyDetails `json:"today,omitempty"` // tomorrow
|
||||
|
|
@ -115,14 +134,7 @@ func (site *Site) publishTariffs(greenShareHome float64, greenShareLoadpoints fl
|
|||
site.publish(keys.TariffCo2Loadpoints, v)
|
||||
}
|
||||
|
||||
fc := struct {
|
||||
Co2 [][]float64 `json:"co2,omitempty"`
|
||||
FeedIn [][]float64 `json:"feedin,omitempty"`
|
||||
Grid [][]float64 `json:"grid,omitempty"`
|
||||
Planner [][]float64 `json:"planner,omitempty"`
|
||||
Solar *solarDetails `json:"solar,omitempty"`
|
||||
Temperature [][]float64 `json:"temperature,omitempty"`
|
||||
}{
|
||||
fc := forecastDetails{
|
||||
Co2: forecastRates(tariff.Rates(site.GetTariff(api.TariffUsageCo2))),
|
||||
FeedIn: forecastRates(tariff.Rates(site.GetTariff(api.TariffUsageFeedIn))),
|
||||
Planner: forecastRates(tariff.Rates(site.GetTariff(api.TariffUsagePlanner))),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue