Publish 1p3p configuration (#4021)

This commit is contained in:
andig 2022-08-01 17:22:54 +02:00 • committed by GitHub
parent e2b1569ce3
commit 1fb4973a2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 16 deletions

View file

@ -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

View file

@ -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