2020-08-13 00:13:15 -05:00
|
|
|
package cleargrass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-ble/ble"
|
2021-02-27 08:03:43 -06:00
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
|
|
influxdb2_api "github.com/influxdata/influxdb-client-go/v2/api"
|
2020-08-13 00:13:15 -05:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://github.com/alexvenom/XiaomiCleargrassInkDislpay/blob/master/XiaomiClearGrassInk.js
|
|
|
|
|
|
|
|
// CleargrassUUID is the Bluetooth UUID for Cleargrass BLE sensor data
|
|
|
|
var CleargrassUUID ble.UUID = []byte{0xcd, 0xfd}
|
|
|
|
|
|
|
|
// ParseCleargrassSensorData .
|
|
|
|
func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) {
|
|
|
|
logger.Debug("cleargrass", zap.String("event_data", hex.Dump(sd.Data)),
|
|
|
|
zap.Int("length", len(sd.Data)))
|
|
|
|
for eventOffset := 8; eventOffset < len(sd.Data)-2; {
|
|
|
|
eventType := sd.Data[eventOffset]
|
|
|
|
eventLength := int(sd.Data[eventOffset+1])
|
|
|
|
if eventOffset+eventLength > len(sd.Data) {
|
|
|
|
logger.Warn("event length exceeds data length")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
eventData := sd.Data[eventOffset+2 : eventOffset+2+eventLength]
|
|
|
|
eventOffset = eventOffset + 2 + eventLength
|
|
|
|
|
|
|
|
switch eventType {
|
|
|
|
case 0x01:
|
|
|
|
if eventLength != 4 {
|
|
|
|
logger.Warn("temperature/humidity event length should be 4",
|
|
|
|
zap.String("event_data", hex.Dump(eventData)))
|
|
|
|
continue
|
|
|
|
}
|
2020-08-13 00:46:28 -05:00
|
|
|
|
2020-08-13 00:13:15 -05:00
|
|
|
temperature := float64(binary.LittleEndian.Uint16(eventData[0:2])) / 10
|
2020-08-13 00:46:28 -05:00
|
|
|
|
|
|
|
point := influxdb2.NewPointWithMeasurement("temperature")
|
2020-08-13 00:13:15 -05:00
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-13 00:46:28 -05:00
|
|
|
point.AddTag("unit", "°C")
|
|
|
|
point.AddField("value", temperature)
|
2020-08-13 00:13:15 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
2020-08-13 00:46:28 -05:00
|
|
|
|
2020-08-13 00:13:15 -05:00
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "temperature"),
|
|
|
|
zap.String("measurement_unit", "°C"),
|
|
|
|
zap.Float64("value", temperature))
|
|
|
|
|
|
|
|
humidity := float64(binary.LittleEndian.Uint16(eventData[2:4])) / 10
|
2020-08-13 00:46:28 -05:00
|
|
|
|
|
|
|
point = influxdb2.NewPointWithMeasurement("humidity")
|
2020-08-13 00:13:15 -05:00
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-13 00:46:28 -05:00
|
|
|
point.AddTag("unit", "%")
|
|
|
|
point.AddField("value", humidity)
|
2020-08-13 00:13:15 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
2020-08-13 00:46:28 -05:00
|
|
|
|
2020-08-13 00:13:15 -05:00
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "humidity"),
|
|
|
|
zap.String("measurement_unit", "%"),
|
|
|
|
zap.Float64("value", humidity))
|
|
|
|
|
|
|
|
case 0x02:
|
|
|
|
if eventLength != 1 {
|
|
|
|
logger.Warn("battery event length should be 1")
|
|
|
|
continue
|
|
|
|
}
|
2020-08-13 00:46:28 -05:00
|
|
|
|
2021-02-27 08:18:53 -06:00
|
|
|
battery := uint8(eventData[0])
|
2020-08-13 00:46:28 -05:00
|
|
|
|
|
|
|
point := influxdb2.NewPointWithMeasurement("battery")
|
2020-08-13 00:13:15 -05:00
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-13 00:46:28 -05:00
|
|
|
point.AddTag("unit", "%")
|
2020-08-13 00:13:15 -05:00
|
|
|
point.AddField("value", battery)
|
|
|
|
(*writeAPI).WritePoint(point)
|
2020-08-13 00:46:28 -05:00
|
|
|
|
2020-08-13 00:13:15 -05:00
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "battery"),
|
|
|
|
zap.String("measurement_unit", "%"),
|
2021-02-27 08:18:53 -06:00
|
|
|
zap.Uint8("value", battery))
|
2020-08-13 00:13:15 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
logger.Warn("unknown event type",
|
|
|
|
zap.Uint8("event_type", eventType),
|
|
|
|
zap.String("event_data", hex.Dump(eventData)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|