mirror of
https://github.com/jpeletier/koolnova2mqtt.git
synced 2026-04-20 23:50:53 +00:00
temperature averaging
This commit is contained in:
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module koolnova2mqtt
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/RobinUS2/golang-moving-average v1.0.0
|
||||
github.com/eclipse/paho.mqtt.golang v1.3.0
|
||||
github.com/epiclabs-io/diff3 v0.0.0-20181217103619-05282cece609 // indirect
|
||||
github.com/epiclabs-io/ut v0.0.0-20190416122157-8da7fe4b4947
|
||||
|
||||
2
go.sum
2
go.sum
@@ -1,3 +1,5 @@
|
||||
github.com/RobinUS2/golang-moving-average v1.0.0 h1:PD7DDZNt+UFb9XlsBbTIu/DtXqqaD/MD86DYnk3mwvA=
|
||||
github.com/RobinUS2/golang-moving-average v1.0.0/go.mod h1:MdzhY+KoEvi+OBygTPH0OSaKrOJzvILWN2SPQzaKVsY=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eclipse/paho.mqtt.golang v1.3.0 h1:MU79lqr3FKNKbSrGN7d7bNYqh8MwWW7Zcx0iG+VIw9I=
|
||||
|
||||
19
kn/bridge.go
19
kn/bridge.go
@@ -27,6 +27,7 @@ type Bridge struct {
|
||||
zw *watcher.Watcher
|
||||
sysw *watcher.Watcher
|
||||
refresh func()
|
||||
zones []*Zone
|
||||
}
|
||||
|
||||
func getActiveZones(w Watcher) ([]*Zone, error) {
|
||||
@@ -76,12 +77,13 @@ func (b *Bridge) Start() error {
|
||||
})
|
||||
|
||||
log.Printf("Starting bridge for %s\n", b.ModuleName)
|
||||
err := b.Tick()
|
||||
err := b.poll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
zones, err := getActiveZones(b.zw)
|
||||
b.zones = zones
|
||||
log.Printf("%d zones are present in %s\n", len(zones), b.ModuleName)
|
||||
b.zw.Resize(len(zones) * REG_PER_ZONE)
|
||||
|
||||
@@ -225,7 +227,7 @@ func (b *Bridge) Start() error {
|
||||
config := map[string]interface{}{
|
||||
"name": name,
|
||||
"current_temperature_topic": currentTempTopic,
|
||||
"precision": 0.5,
|
||||
"precision": 0.1,
|
||||
"temperature_state_topic": targetTempTopic,
|
||||
"temperature_command_topic": targetTempSetTopic,
|
||||
"temperature_unit": "C",
|
||||
@@ -284,7 +286,7 @@ func (b *Bridge) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bridge) Tick() error {
|
||||
func (b *Bridge) poll() error {
|
||||
err := b.zw.Poll()
|
||||
if err != nil {
|
||||
log.Printf("Timeout polling %s zone registers: %s\n", b.ModuleName, err)
|
||||
@@ -299,6 +301,17 @@ func (b *Bridge) Tick() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bridge) Tick() error {
|
||||
err := b.poll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, z := range b.zones {
|
||||
z.SampleTemperature()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bridge) getZoneTopic(zoneNum int, subtopic string) string {
|
||||
return fmt.Sprintf("%s/%s/zone%d/%s", b.TopicPrefix, b.ModuleName, zoneNum, subtopic)
|
||||
}
|
||||
|
||||
32
kn/zone.go
32
kn/zone.go
@@ -2,6 +2,9 @@ package kn
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
|
||||
average "github.com/RobinUS2/golang-moving-average"
|
||||
)
|
||||
|
||||
type Watcher interface {
|
||||
@@ -22,25 +25,20 @@ type Zone struct {
|
||||
OnTargetTempChange func(newTemp float32)
|
||||
OnFanModeChange func(newMode FanMode)
|
||||
OnKnModeChange func(newMode KnMode)
|
||||
lastTemp float32
|
||||
temp *average.MovingAverage
|
||||
}
|
||||
|
||||
func NewZone(config *ZoneConfig) *Zone {
|
||||
z := &Zone{
|
||||
ZoneConfig: *config,
|
||||
temp: average.New(300),
|
||||
}
|
||||
z.RegisterCallback(REG_ENABLED, func() {
|
||||
if z.OnEnabledChange != nil {
|
||||
z.OnEnabledChange()
|
||||
}
|
||||
})
|
||||
z.RegisterCallback(REG_CURRENT_TEMP, func() {
|
||||
if z.OnCurrentTempChange == nil {
|
||||
return
|
||||
}
|
||||
|
||||
temp := z.GetCurrentTemperature()
|
||||
z.OnCurrentTempChange(temp)
|
||||
})
|
||||
z.RegisterCallback(REG_TARGET_TEMP, func() {
|
||||
if z.OnTargetTempChange == nil {
|
||||
return
|
||||
@@ -97,11 +95,27 @@ func (z *Zone) IsPresent() bool {
|
||||
return r1&0x2 != 0
|
||||
}
|
||||
|
||||
func (z *Zone) GetCurrentTemperature() float32 {
|
||||
func (z *Zone) getCurrentTemperature() float32 {
|
||||
r4 := z.ReadRegister(REG_CURRENT_TEMP)
|
||||
return reg2temp(r4)
|
||||
}
|
||||
|
||||
func (z *Zone) GetCurrentTemperature() float32 {
|
||||
return float32(math.Round(z.temp.Avg()*10) / 10)
|
||||
}
|
||||
|
||||
func (z *Zone) SampleTemperature() {
|
||||
sample := z.getCurrentTemperature()
|
||||
z.temp.Add(float64(sample))
|
||||
if z.OnCurrentTempChange != nil {
|
||||
t := z.GetCurrentTemperature()
|
||||
if t != z.lastTemp {
|
||||
z.lastTemp = t
|
||||
z.OnCurrentTempChange(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (z *Zone) GetTargetTemperature() float32 {
|
||||
r3 := z.ReadRegister(REG_TARGET_TEMP)
|
||||
return reg2temp(r3)
|
||||
|
||||
Reference in New Issue
Block a user