Files
MrAlders0n a7c36e8e76 feat(polling): decode sensor channels from LPP and serve them with node telemetry
Humidity, pressure, gas and power monitor channels are decoded when a reading is stored. Positions and altitude are never decoded. Fixes the humidity size, which is one byte on the wire.
2026-11-29 21:09:23 -05:00

231 lines
6.9 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package polling
import (
"math"
"sort"
)
// lppValueSize is the size the CayenneLPP library writes for each type. -1 means an unknown type.
// Humidity is one byte on the wire; the firmware's own skip table says two, which is wrong.
func lppValueSize(t byte) int {
switch t {
case 0, 1, 102, 104, 120, 142:
return 1
case 2, 3, 101, 103, 115, 116, 117, 121, 125, 128, 132:
return 2
case 135:
return 3
case 100, 118, 130, 131, 133:
return 4
case 113, 134:
return 6
case 136:
return 9
}
return -1
}
const (
lppChannelSelf = 1
lppVoltage = 116
lppTemperature = 103
lppTempMin = -600 // -60.0 C
lppTempMax = 1250 // 125.0 C
)
// DecodeLPP reads the repeater's own voltage and temperature (channel 1) from raw
// CayenneLPP. It stops at channel 0, an unknown type or a truncated record and
// keeps what it decoded. The first record of each kind wins, even if out of range.
func DecodeLPP(b []byte) (batteryMV *int, temperatureC *float64) {
sawV, sawT := false, false
for i := 0; i+2 <= len(b); {
ch, typ := b[i], b[i+1]
if ch == 0 {
break
}
sz := lppValueSize(typ)
if sz < 0 || i+2+sz > len(b) {
break
}
v := b[i+2 : i+2+sz]
switch {
case ch == lppChannelSelf && typ == lppVoltage && !sawV:
sawV = true
mv := int(uint16(v[0])<<8|uint16(v[1])) * 10
if mv <= 65535 {
batteryMV = &mv
}
case ch == lppChannelSelf && typ == lppTemperature && !sawT:
sawT = true
c10 := int(int16(uint16(v[0])<<8 | uint16(v[1])))
if c10 >= lppTempMin && c10 <= lppTempMax {
c := float64(c10) / 10
temperatureC = &c
}
}
i += 2 + sz
}
return batteryMV, temperatureC
}
// LPP types the sensor decoder reads. GPS (136) and altitude (121) are skipped on purpose: neither a position nor
// anything derived from one is kept.
const (
lppAnalogInput = 2
lppGeneric = 100
lppLuminosity = 101
lppHumidity = 104
lppPressure = 115
lppCurrent = 117
lppFrequency = 118
lppPercentage = 120
lppPower = 128
lppDistance = 130
// MaxSensorChannels caps what one reading stores.
MaxSensorChannels = 16
// An added sensor can be an infrared thermometer, which reads far outside what the board itself survives.
sensorTempMin, sensorTempMax = -273.0, 1000.0
)
// Sensor is what one LPP channel carried, in the JSON shape it is stored and served in. Channel 1 is the device
// itself, whose voltage and temperature are the reading's own battery and temperature and are not repeated here.
type Sensor struct {
Channel int `json:"channel"`
TemperatureC *float64 `json:"temperatureC,omitempty"`
HumidityPct *float64 `json:"humidityPct,omitempty"`
PressureHpa *float64 `json:"pressureHpa,omitempty"`
GasOhms *float64 `json:"gasOhms,omitempty"`
IAQ *float64 `json:"iaq,omitempty"`
IAQAccuracy *int `json:"iaqAccuracy,omitempty"`
VoltageV *float64 `json:"voltageV,omitempty"`
CurrentA *float64 `json:"currentA,omitempty"`
PowerW *float64 `json:"powerW,omitempty"`
DistanceM *float64 `json:"distanceM,omitempty"`
Percent *float64 `json:"percent,omitempty"`
LuminosityLux *float64 `json:"luminosityLux,omitempty"`
}
func (s Sensor) empty() bool {
return s.TemperatureC == nil && s.HumidityPct == nil && s.PressureHpa == nil && s.GasOhms == nil && s.IAQ == nil &&
s.VoltageV == nil && s.CurrentA == nil && s.PowerW == nil && s.DistanceM == nil && s.Percent == nil &&
s.LuminosityLux == nil
}
// DecodeSensors reads the sensor channels of raw CayenneLPP, ascending by channel. It stops where DecodeLPP stops
// and keeps what it decoded. The first record of each type on a channel wins; a value out of range is left out.
// Voltage and current are signed on the wire (a power monitor reads a reverse current), whatever the library's
// comments say.
func DecodeSensors(b []byte) []Sensor {
type raw struct {
s Sensor
generic, analog *float64
power *float64
frequency bool
sawT, sawV bool
}
chans := map[int]*raw{}
cut := false // the walk stopped on a record it could not read
for i := 0; i+2 <= len(b); {
ch, typ := int(b[i]), b[i+1]
if ch == 0 {
break
}
sz := lppValueSize(typ)
if sz < 0 || i+2+sz > len(b) {
cut = true
break
}
v := b[i+2 : i+2+sz]
i += 2 + sz
c := chans[ch]
if c == nil {
if len(chans) >= MaxSensorChannels {
continue
}
c = &raw{s: Sensor{Channel: ch}}
chans[ch] = c
}
var u uint32
for _, x := range v {
u = u<<8 | uint32(x)
}
set := func(dst **float64, val float64, lo, hi float64) {
if *dst == nil && val >= lo && val <= hi {
*dst = &val
}
}
switch typ {
case lppTemperature:
if ch != lppChannelSelf && !c.sawT {
c.sawT = true
set(&c.s.TemperatureC, float64(int16(u))/10, sensorTempMin, sensorTempMax)
}
case lppVoltage:
if ch != lppChannelSelf && !c.sawV {
c.sawV = true
set(&c.s.VoltageV, float64(int16(u))/100, -327.68, 327.67)
}
case lppHumidity:
set(&c.s.HumidityPct, float64(u)/2, 0, 100)
case lppPressure:
set(&c.s.PressureHpa, float64(u)/10, 300, 1100)
case lppCurrent:
set(&c.s.CurrentA, float64(int16(u))/1000, -32.768, 32.767)
case lppPower:
set(&c.power, float64(u), 0, 65535)
case lppDistance:
set(&c.s.DistanceM, float64(u)/1000, 0, 4294967.295)
case lppPercentage:
set(&c.s.Percent, float64(u), 0, 100)
case lppLuminosity:
set(&c.s.LuminosityLux, float64(u), 0, 65535)
case lppGeneric:
set(&c.generic, float64(u), 0, 4294967295)
case lppAnalogInput:
set(&c.analog, float64(int16(u))/100, -327.68, 327.67)
case lppFrequency:
c.frequency = true
}
}
out := make([]Sensor, 0, len(chans))
for _, c := range chans {
s := c.s
// The firmware's soil sensor reuses temperature and power on a calibration channel for other figures.
if c.frequency {
continue
}
// The computed product keeps the decimals the wire's whole watts lose.
switch {
case s.VoltageV != nil && s.CurrentA != nil:
w := math.Round(*s.VoltageV**s.CurrentA*1000) / 1000
s.PowerW = &w
case s.VoltageV != nil || s.CurrentA != nil:
s.PowerW = c.power
}
// A gas sensor has no LPP type of its own: beside humidity and pressure, the generic value is its reading.
// With an analog value next to it, it is an air quality index and the analog value is its accuracy (0 to 3).
if c.generic != nil && s.HumidityPct != nil && s.PressureHpa != nil {
if a := c.analog; a != nil && *a >= 0 && *a <= 3 && *a == math.Trunc(*a) && *c.generic <= 500 {
acc := int(*a)
s.IAQ, s.IAQAccuracy = c.generic, &acc
} else if a == nil && !cut {
// Cut short, the accuracy value may be what was lost, and the figure cannot be told apart.
s.GasOhms = c.generic
}
}
if !s.empty() {
out = append(out, s)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Channel < out[j].Channel })
if len(out) == 0 {
return nil
}
return out
}