From 1c05bdadc4756fd1cfacb755b7fe9987f3bf5b31 Mon Sep 17 00:00:00 2001 From: Pekka Vainio Date: Fri, 26 Apr 2024 08:56:48 +0300 Subject: [PATCH] Added (untested) temperature register for newer devices --- go.mod | 4 ++-- go.sum | 6 ++---- vallox.go | 45 +++++++++++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 23506dc..0fd6cbd 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/pvainio/vallox-rs485 -go 1.20 +go 1.22 require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 -require golang.org/x/sys v0.6.0 // indirect +require golang.org/x/sys v0.19.0 // indirect diff --git a/go.sum b/go.sum index d25a6e7..3bc5870 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= -golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac h1:oN6lz7iLW/YC7un8pq+9bOLyXrprv2+DKfkJY+2LJJw= -golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/vallox.go b/vallox.go index 39b22e1..0fe81dd 100644 --- a/vallox.go +++ b/vallox.go @@ -54,6 +54,12 @@ const ( TempOutgoingInside byte = 0x5a TempIncomingInside byte = 0x5b TempOutgoingOutside byte = 0x5c + + // Registers for newer protocol + TempIncomingOutsideNew byte = 0x32 + TempOutgoingInsideNew byte = 0x34 + TempIncomingInsideNew byte = 0x35 + TempOutgoingOutsideNew byte = 0x33 ) type Event struct { @@ -103,8 +109,8 @@ func Open(cfg Config) (*Vallox, error) { running: true, buffer: bufio.NewReadWriter(bufio.NewReader(buffer), bufio.NewWriter(buffer)), remoteClientId: cfg.RemoteClientId, - in: make(chan Event, 15), - out: make(chan valloxPackage, 15), + in: make(chan Event, 50), + out: make(chan valloxPackage, 50), writeAllowed: cfg.EnableWrite, logDebug: cfg.LogDebug, } @@ -182,6 +188,7 @@ func handleOutgoing(vallox *Vallox) { now := time.Now() if vallox.lastActivity.IsZero() || now.UnixMilli()-vallox.lastActivity.UnixMilli() < 50 { + updateLastActivity(vallox) vallox.logDebug.Printf("delay outgoing to %x %x = %x, lastActivity %v now %v, diff %d ms", pkg.Destination, pkg.Register, pkg.Value, vallox.lastActivity, now, now.UnixMilli()-vallox.lastActivity.UnixMilli()) time.Sleep(time.Millisecond * 50) @@ -255,28 +262,34 @@ func handleBuffer(vallox *Vallox) { } func handlePackage(pkg *valloxPackage, vallox *Vallox) { - vallox.in <- *event(pkg, vallox) + vallox.in <- *event(pkg) +} + +type mapFn func(byte) int8 + +var registerMap = map[byte]mapFn{ + FanSpeed: valueToSpeed, + TempIncomingInside: valueToTemp, + TempIncomingOutside: valueToTemp, + TempOutgoingInside: valueToTemp, + TempOutgoingOutside: valueToTemp, + TempIncomingInsideNew: valueToTemp, + TempIncomingOutsideNew: valueToTemp, + TempOutgoingInsideNew: valueToTemp, + TempOutgoingOutsideNew: valueToTemp, } -func event(pkg *valloxPackage, vallox *Vallox) *Event { +func event(pkg *valloxPackage) *Event { event := new(Event) event.Time = time.Now() event.Source = pkg.Source event.Destination = pkg.Destination event.Register = pkg.Register event.RawValue = pkg.Value - switch pkg.Register { - case FanSpeed: - event.Value = int16(valueToSpeed(pkg.Value)) - case TempIncomingInside: - event.Value = int16(valueToTemp(pkg.Value)) - case TempIncomingOutside: - event.Value = int16(valueToTemp(pkg.Value)) - case TempOutgoingInside: - event.Value = int16(valueToTemp(pkg.Value)) - case TempOutgoingOutside: - event.Value = int16(valueToTemp(pkg.Value)) - default: + mapFn, ok := registerMap[pkg.Register] + if ok { + event.Value = int16(mapFn(pkg.Value)) + } else { event.Value = int16(pkg.Value) } return event