diff --git a/core/loadpoint.go b/core/loadpoint.go index 3bb810444..e2889503f 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -532,6 +532,8 @@ func (lp *LoadPoint) Prepare(uiChan chan<- util.Param, pushChan chan<- push.Even lp.publish("activePhases", lp.activePhases()) lp.publishVehicles() + lp.setDefaultPhases(lp.DefaultPhases) + lp.Lock() lp.publish("mode", lp.Mode) lp.publish("targetSoC", lp.SoC.Target) @@ -1036,8 +1038,16 @@ func (lp *LoadPoint) scalePhasesIfAvailable(phases int) error { func (lp *LoadPoint) setDefaultPhases(phases int) { lp.Lock() defer lp.Unlock() + lp.DefaultPhases = phases lp.phaseTimer = time.Time{} + + // publish 1p3p capability and phase configuration + if _, ok := lp.charger.(api.ChargePhases); ok { + lp.publish("phases1p3p", lp.DefaultPhases) + } else { + lp.publish("phases1p3p", nil) + } } // setPhases sets the number of enabled phases without modifying the charger diff --git a/server/mqtt.go b/server/mqtt.go index 61da436ac..b230ecaf6 100644 --- a/server/mqtt.go +++ b/server/mqtt.go @@ -28,21 +28,26 @@ func NewMQTT(root string) *MQTT { } func (m *MQTT) encode(v interface{}) string { - var s string + // nil should erase the value + if v == nil { + return "" + } + switch val := v.(type) { + case string: + return val + case float64: + return fmt.Sprintf("%.5g", val) case time.Time: - s = strconv.FormatInt(val.Unix(), 10) + return strconv.FormatInt(val.Unix(), 10) case time.Duration: // must be before stringer to convert to seconds instead of string - s = fmt.Sprintf("%d", int64(val.Seconds())) - case fmt.Stringer, string: - s = fmt.Sprintf("%s", val) - case float64: - s = fmt.Sprintf("%.5g", val) + return fmt.Sprintf("%d", int64(val.Seconds())) + case fmt.Stringer: + return val.String() default: - s = fmt.Sprintf("%v", val) + return fmt.Sprintf("%v", val) } - return s } func (m *MQTT) publishSingleValue(topic string, retained bool, payload interface{}) { @@ -154,10 +159,6 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) { m.listenSetters(topic, lp) } - // alive indicator - updated := time.Now().Unix() - m.publish(fmt.Sprintf("%s/updated", m.root), true, updated) - // remove deprecated topics for id := range site.LoadPoints() { topic := fmt.Sprintf("%s/loadpoints/%d", m.root, id+1) @@ -166,6 +167,9 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) { } } + // alive indicator + var updated time.Time + // publish for p := range in { topic := fmt.Sprintf("%s/site", m.root) @@ -175,9 +179,9 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) { } // alive indicator - if now := time.Now().Unix(); now != updated { - updated = now - m.publish(fmt.Sprintf("%s/updated", m.root), true, updated) + if time.Since(updated) > time.Second { + updated = time.Now() + m.publish(fmt.Sprintf("%s/updated", m.root), true, updated.Unix()) } // value