Mqtt: fix forecast no longer published (#24483)

This commit is contained in:
andig 2025-10-16 17:51:40 +02:00 • committed by GitHub
parent b7e8b5670a
commit b642e14b05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 11 deletions

11
api/marshal.go Normal file
View file

@ -0,0 +1,11 @@
package api
// BytesMarshaler marshals into bytes/string representation
type BytesMarshaler interface {
MarshalBytes() ([]byte, error)
}
// BytesMarshaler marshals into a struct representation
type StructMarshaler interface {
MarshalStruct() (any, error)
}

View file

@ -47,7 +47,9 @@ func (rr Rates) At(ts time.Time) (Rate, error) {
return Rate{}, ErrNotAvailable
}
// MarshalMQTT implements server.MQTTMarshaler
func (r Rates) MarshalMQTT() ([]byte, error) {
var _ BytesMarshaler = Rates{}
// MarshalBytes implements server.BytesMarshaler
func (r Rates) MarshalBytes() ([]byte, error) {
return json.Marshal(r)
}

View file

@ -18,12 +18,6 @@ import (
"github.com/samber/lo"
)
// MQTTMarshaler is the interface implemented by types that
// can marshal themselves into valid an MQTT string representation.
type MQTTMarshaler interface {
MarshalMQTT() ([]byte, error)
}
// MQTT is the MQTT server. It uses the MQTT client for publishing.
type MQTT struct {
log *util.Logger
@ -91,15 +85,25 @@ func (m *MQTT) publishComplex(topic string, retained bool, payload interface{})
return
}
if mm, ok := payload.(MQTTMarshaler); ok {
if b, err := mm.MarshalMQTT(); err == nil {
if mm, ok := payload.(api.BytesMarshaler); ok {
if b, err := mm.MarshalBytes(); err == nil {
m.publishSingleValue(topic, retained, string(b))
} else {
m.log.ERROR.Printf("marshal mqtt: %v", err)
m.log.ERROR.Printf("marshal bytes: %v", err)
}
return
}
if mm, ok := payload.(api.StructMarshaler); ok {
if d, err := mm.MarshalStruct(); err != nil {
m.log.ERROR.Printf("marshal struct: %v", err)
return
} else {
payload = d
// fallthrough
}
}
switch typ := reflect.TypeOf(payload); typ.Kind() {
case reflect.Slice:
// publish count

View file

@ -7,6 +7,7 @@ import (
"strings"
"sync"
"github.com/evcc-io/evcc/api"
"github.com/fatih/structs"
)
@ -72,6 +73,12 @@ func (s *sharderImpl) Shards() []Shard {
return res
}
var _ api.StructMarshaler = (*sharderImpl)(nil)
func (s *sharderImpl) MarshalStruct() (any, error) {
return s.struc, nil
}
var _ Sharder = (*sharderImpl)(nil)
// NewSharder creates a Sharder that splits structs into sub-structs for space-efficient socket publishing