-
Notifications
You must be signed in to change notification settings - Fork 3
/
vallox.go
436 lines (373 loc) · 11.2 KB
/
vallox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Package valloxrs485 implements Vallox RS485 protocol
package valloxrs485
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"math"
"sync"
"time"
"github.com/tarm/serial"
)
// Config foo
type Config struct {
// Device file for rs485 device
Device string
// RemoteClientId is the id for this device in Vallox rs485 bus
RemoteClientId byte
// Enable writing to Vallox regisers, default false
EnableWrite bool
// Logge for debug, default no logging
LogDebug *log.Logger
}
type Vallox struct {
port *serial.Port
remoteClientId byte
running bool
//buffer *bufio.ReadWriter
buf *bytes.Buffer
in chan Event
out chan valloxPackage
lastActivity time.Time
writeAllowed bool
logDebug *log.Logger
mutex sync.Mutex
co2 twoByteValue
}
type twoByteValue struct {
high byteValue
low byteValue
}
func (tbv *twoByteValue) validValue(now time.Time) (int16, bool) {
limit := now.Add(-500 * time.Millisecond)
if tbv.high.at.Before(limit) {
return -1, false
}
if tbv.low.at.Before(limit) {
return -1, false
}
// Both values are within 500ms of the current time
res := int16(tbv.high.value)<<8 + int16(tbv.low.value)
if res <= 0 {
return -1, false
}
return res, true
}
type byteValue struct {
at time.Time
value byte
}
const (
DeviceMulticast = 0x10
DeviceMain = 0x11
RemoteClientMulticast = 0x20
)
// Some known registers
const (
// Reading and writing fan speed
FanSpeed byte = 0x29
// Registers Vallox is broadcasting temperatures
TempIncomingOutside byte = 0x58
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
RhHighest byte = 0x2a
Co2HighestHighByte byte = 0x2b
Co2HighestLowByte byte = 0x2c
Rh1 byte = 0x2f
Rh2 byte = 0x30
)
type Event struct {
Time time.Time `json:"time"`
Source byte `json:"source"`
Destination byte `json:"destination"`
Register byte `json:"register"`
RawValue byte `json:"raw"`
Value int16 `json:"value"`
}
type valloxPackage struct {
System byte
Source byte
Destination byte
Register byte
Value byte
Checksum byte
}
var writeAllowed = map[byte]bool{FanSpeed: true}
// Open opens the rs485 device specified in Config
func Open(cfg Config) (*Vallox, error) {
if cfg.LogDebug == nil {
cfg.LogDebug = log.New(io.Discard, "", 0)
}
if cfg.RemoteClientId == 0 {
cfg.RemoteClientId = 0x27
}
if cfg.RemoteClientId < 0x20 || cfg.RemoteClientId > 0x2f {
return nil, fmt.Errorf("invalid remoteClientId %x", cfg.RemoteClientId)
}
portCfg := &serial.Config{Name: cfg.Device, Baud: 9600, Size: 8, Parity: 'N', StopBits: 1}
port, err := serial.OpenPort(portCfg)
if err != nil {
return nil, err
}
buffer := new(bytes.Buffer)
vallox := &Vallox{
port: port,
running: true,
buf: buffer,
remoteClientId: cfg.RemoteClientId,
in: make(chan Event, 50),
out: make(chan valloxPackage, 50),
writeAllowed: cfg.EnableWrite,
logDebug: cfg.LogDebug,
}
sendInit(vallox)
go handleIncoming(vallox)
go handleOutgoing(vallox)
return vallox, nil
}
// Events returns channel for events from Vallox bus
func (vallox *Vallox) Events() chan Event {
return vallox.in
}
// ForMe returns true if event is addressed for this client
func (vallox *Vallox) ForMe(e Event) bool {
return e.Destination == RemoteClientMulticast || e.Destination == vallox.remoteClientId
}
// Query queries Vallox for register
func (vallox *Vallox) Query(register byte) {
pkg := createQuery(vallox, register)
vallox.out <- *pkg
}
// SetSpeed changes speed of ventilation fan
func (vallox *Vallox) SetSpeed(speed byte) {
if speed < 1 || speed > 8 {
vallox.logDebug.Printf("received invalid speed %x", speed)
return
}
value := speedToValue(int8(speed))
vallox.logDebug.Printf("received set speed %x", speed)
// Send value to the main vallox device
vallox.writeRegister(DeviceMain, FanSpeed, value)
// Also publish value to all the remotes
vallox.writeRegister(RemoteClientMulticast, FanSpeed, value)
}
func sendInit(vallox *Vallox) {
vallox.Query(FanSpeed)
}
func (vallox *Vallox) writeRegister(destination byte, register byte, value byte) {
pkg := createWrite(vallox, destination, register, value)
vallox.out <- *pkg
}
func createQuery(vallox *Vallox, register byte) *valloxPackage {
return createWrite(vallox, DeviceMain, 0, register)
}
func createWrite(vallox *Vallox, destination byte, register byte, value byte) *valloxPackage {
pkg := new(valloxPackage)
pkg.System = 1
pkg.Source = vallox.remoteClientId
pkg.Destination = destination
pkg.Register = register
pkg.Value = value
pkg.Checksum = calculateChecksum(pkg)
return pkg
}
func (vallox *Vallox) ifBusFreeProceed() bool {
//vallox.logDebug.Printf("if free proceed")
vallox.mutex.Lock()
defer vallox.mutex.Unlock()
if time.Since(vallox.lastActivity) < 100*time.Millisecond {
//vallox.logDebug.Printf("not free, no proceed")
return false
}
vallox.lastActivity = time.Now()
//vallox.logDebug.Printf("free proceed")
return true
}
func (vallox *Vallox) getLastActivity() time.Time {
//vallox.logDebug.Printf("get last activity")
vallox.mutex.Lock()
defer vallox.mutex.Unlock()
//vallox.logDebug.Printf("got last activity")
return vallox.lastActivity
}
func handleOutgoing(vallox *Vallox) {
for vallox.running {
pkg := <-vallox.out
if !isOutgoingAllowed(vallox, pkg.Register) {
vallox.logDebug.Printf("outgoing not allowed for %x = %x", pkg.Register, pkg.Value)
continue
}
if vallox.ifBusFreeProceed() {
binary.Write(vallox.port, binary.BigEndian, pkg)
vallox.logDebug.Printf("sent outgoing to %x %x = %x", pkg.Destination, pkg.Register, pkg.Value)
} else {
la := vallox.getLastActivity()
now := time.Now()
vallox.logDebug.Printf("delay outgoing to %x %x = %x, lastActivity %v now %v, diff %d ms",
pkg.Destination, pkg.Register, pkg.Value, la, now, time.Since(la).Milliseconds())
time.Sleep(time.Millisecond * 57)
vallox.out <- pkg
}
}
}
func isOutgoingAllowed(vallox *Vallox, register byte) bool {
if register == 0 {
// queries are allowed
return true
}
if !vallox.writeAllowed {
return false
}
return writeAllowed[register]
}
func handleIncoming(vallox *Vallox) {
vallox.running = true
buf := make([]byte, 128)
for vallox.running {
n, err := vallox.port.Read(buf)
if err != nil {
fatalError(err, vallox)
return
}
if n > 0 {
//vallox.logDebug.Printf("read %d bytes", n)
vallox.updateLastActivity()
vallox.buf.Write(buf[:n])
handleBuffer(vallox)
}
}
}
func (vallox *Vallox) updateLastActivity() {
//vallox.logDebug.Printf("updating last activity")
vallox.mutex.Lock()
defer vallox.mutex.Unlock()
vallox.lastActivity = time.Now()
//vallox.logDebug.Printf("updated last activity")
}
func fatalError(err error, vallox *Vallox) {
vallox.logDebug.Printf("fatal error %v", err)
vallox.running = false
}
func handleBuffer(vallox *Vallox) {
for vallox.buf.Len() >= 6 {
buf := vallox.buf.Bytes()
pkg := validPackage(buf)
if pkg != nil {
vallox.buf.Next(6)
handlePackage(pkg, vallox)
} else {
// discard byte, since no valid package starts here
vallox.buf.ReadByte()
//vallox.logDebug.Printf("invalid package, discarding byte %x, available %d", b, vallox.buf.Len())
}
}
}
func handlePackage(pkg *valloxPackage, vallox *Vallox) {
e := event(pkg, vallox)
if e != nil {
vallox.in <- *e
} else {
vallox.logDebug.Printf("discarding package from %x register %x value %x", pkg.Source, pkg.Register, pkg.Value)
}
}
type mapFn func(byte, *Vallox) (int16, bool)
var registerMap = map[byte]mapFn{
FanSpeed: valueToSpeed,
TempIncomingInside: valueToTemp,
TempIncomingOutside: valueToTemp,
TempOutgoingInside: valueToTemp,
TempOutgoingOutside: valueToTemp,
TempIncomingInsideNew: valueToTemp,
TempIncomingOutsideNew: valueToTemp,
TempOutgoingInsideNew: valueToTemp,
TempOutgoingOutsideNew: valueToTemp,
RhHighest: valueToRh,
Rh1: valueToRh,
Rh2: valueToRh,
Co2HighestHighByte: valueToCo2High,
Co2HighestLowByte: valueToCo2Low,
}
func valueToRh(val byte, vallox *Vallox) (int16, bool) {
if val < 0x33 {
return -1, false
}
return int16(math.Round(float64((float32(val) - 51.0) / 2.04))), true
}
func valueToCo2High(val byte, vallox *Vallox) (int16, bool) {
now := time.Now()
vallox.co2.high = byteValue{at: now, value: val}
return vallox.co2.validValue(now)
}
func valueToCo2Low(val byte, vallox *Vallox) (int16, bool) {
now := time.Now()
vallox.co2.low = byteValue{at: now, value: val}
return vallox.co2.validValue(now)
}
func event(pkg *valloxPackage, vallox *Vallox) *Event {
event := new(Event)
event.Time = time.Now()
event.Source = pkg.Source
event.Destination = pkg.Destination
event.Register = pkg.Register
event.RawValue = pkg.Value
mapFn, found := registerMap[pkg.Register]
if found {
val, ok := mapFn(pkg.Value, vallox)
if !ok {
return nil
}
event.Value = int16(val)
} else {
event.Value = int16(pkg.Value)
}
return event
}
func valueToSpeed(value byte, vallox *Vallox) (int16, bool) {
for i, v := range fanSpeedConversion {
if value == v {
return int16(i) + 1, true
}
}
return -1, false
}
func speedToValue(speed int8) byte {
return fanSpeedConversion[speed-1]
}
func valueToTemp(value byte, vallox *Vallox) (int16, bool) {
return tempConversion[value], true
}
func validPackage(buf []byte) (pkg *valloxPackage) {
pkg = &valloxPackage{buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]}
if validChecksum(pkg) && pkg.System == 1 {
return pkg
}
return nil
}
func validChecksum(pkg *valloxPackage) bool {
return pkg.Checksum == calculateChecksum(pkg)
}
func calculateChecksum(pkg *valloxPackage) byte {
return pkg.System + pkg.Source + pkg.Destination + pkg.Register + pkg.Value
}
var fanSpeedConversion = [8]byte{0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}
var tempConversion = [256]int16{
-74, -70, -66, -62, -59, -56, -54, -52, -50, -48, -47, -46, -44, -43, -42, -41,
-40, -39, -38, -37, -36, -35, -34, -33, -33, -32, -31, -30, -30, -29, -28, -28, -27, -27, -26, -25, -25,
-24, -24, -23, -23, -22, -22, -21, -21, -20, -20, -19, -19, -19, -18, -18, -17, -17, -16, -16, -16, -15,
-15, -14, -14, -14, -13, -13, -12, -12, -12, -11, -11, -11, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7,
-6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3,
3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13,
14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23,
24, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36,
36, 37, 37, 38, 38, 39, 40, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 48, 48, 49, 50, 51, 52, 53, 53, 54,
55, 56, 57, 59, 60, 61, 62, 63, 65, 66, 68, 69, 71, 73, 75, 77, 79, 81, 82, 86, 90, 93, 97, 100, 100, 100,
100, 100, 100, 100, 100, 100,
}