2020-08-08 19:00:41 -05:00
|
|
|
package mijia
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.ocjtech.us/jeff/bluelisten/lib/bluetooth"
|
2020-08-09 07:23:47 -05:00
|
|
|
"github.com/go-ble/ble"
|
2020-08-08 19:00:41 -05:00
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go"
|
|
|
|
influxdb2_api "github.com/influxdata/influxdb-client-go/api"
|
2020-08-09 07:23:47 -05:00
|
|
|
"go.uber.org/zap"
|
2020-08-08 19:00:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// XiaomiMijiaHTV1UUID is the Bluetooth UUID for Xiaomi Mijia BLE sensor data
|
|
|
|
var XiaomiMijiaHTV1UUID ble.UUID = []byte{0x95, 0xfe}
|
|
|
|
|
|
|
|
const (
|
|
|
|
temperatureEvent = 0x1004
|
|
|
|
humidityEvent = 0x1006
|
|
|
|
illuminanceEvent = 0x1007
|
|
|
|
moistureEvent = 0x1008
|
|
|
|
conductivityEvent = 0x1009
|
|
|
|
batteryEvent = 0x100a
|
|
|
|
temperatureAndHumidityEvent = 0x100d
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseXiaomiMijiaSensorData parses
|
2020-08-09 07:23:47 -05:00
|
|
|
func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) {
|
|
|
|
|
2020-08-08 19:00:41 -05:00
|
|
|
var sensorData struct {
|
|
|
|
frameControl uint16
|
|
|
|
isFactoryNew bool
|
|
|
|
isConnected bool
|
|
|
|
isCentral bool
|
|
|
|
isEncrypted bool
|
|
|
|
hasMacAddress bool
|
|
|
|
hasCapabilities bool
|
|
|
|
hasEvent bool
|
|
|
|
hasCustomData bool
|
|
|
|
hasSubtitle bool
|
|
|
|
hasBinding bool
|
|
|
|
version uint16
|
|
|
|
productID uint16
|
|
|
|
frameCounter uint8
|
|
|
|
macAddress []byte
|
|
|
|
capabilities uint8
|
|
|
|
capabilityData struct {
|
|
|
|
connectable bool
|
|
|
|
central bool
|
|
|
|
secure bool
|
|
|
|
io uint8
|
|
|
|
}
|
|
|
|
eventData struct {
|
|
|
|
eventType uint16
|
|
|
|
eventLength uint8
|
|
|
|
temperature float64
|
|
|
|
humidity float64
|
|
|
|
battery uint8
|
|
|
|
illuminance uint
|
|
|
|
conductivity int16
|
|
|
|
moisture int8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sensorData.frameControl = binary.LittleEndian.Uint16(sd.Data[0:])
|
|
|
|
sensorData.isFactoryNew = sensorData.frameControl&(1<<0) != 0
|
|
|
|
sensorData.isConnected = sensorData.frameControl&(1<<1) != 0
|
|
|
|
sensorData.isCentral = sensorData.frameControl&(1<<2) != 0
|
|
|
|
sensorData.isEncrypted = sensorData.frameControl&(1<<3) != 0
|
|
|
|
sensorData.hasMacAddress = sensorData.frameControl&(1<<4) != 0
|
|
|
|
sensorData.hasCapabilities = sensorData.frameControl&(1<<5) != 0
|
|
|
|
sensorData.hasEvent = sensorData.frameControl&(1<<6) != 0
|
|
|
|
sensorData.hasCustomData = sensorData.frameControl&(1<<7) != 0
|
|
|
|
sensorData.hasSubtitle = sensorData.frameControl&(1<<8) != 0
|
|
|
|
sensorData.hasBinding = sensorData.frameControl&(1<<9) != 0
|
|
|
|
sensorData.version = (sensorData.frameControl >> 12) & 0x0f
|
|
|
|
sensorData.productID = binary.LittleEndian.Uint16(sd.Data[2:])
|
|
|
|
sensorData.frameCounter = uint8(sd.Data[4])
|
|
|
|
if sensorData.hasMacAddress {
|
|
|
|
sensorData.macAddress = sd.Data[5 : 5+6]
|
|
|
|
for i, j := 0, len(sensorData.macAddress)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
sensorData.macAddress[i], sensorData.macAddress[j] = sensorData.macAddress[j], sensorData.macAddress[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sensorData.hasCapabilities {
|
|
|
|
var capabilityOffset int = 11
|
|
|
|
if !sensorData.hasMacAddress {
|
|
|
|
capabilityOffset = 5
|
|
|
|
}
|
|
|
|
sensorData.capabilities = uint8(sd.Data[capabilityOffset])
|
|
|
|
sensorData.capabilityData.connectable = sensorData.capabilities&(1<<0) != 0
|
|
|
|
sensorData.capabilityData.central = sensorData.capabilities&(1<<1) != 0
|
|
|
|
sensorData.capabilityData.secure = sensorData.capabilities&(1<<2) != 0
|
|
|
|
sensorData.capabilityData.io = sensorData.capabilities & ((1 << 3) | (1 << 4)) // != 0
|
|
|
|
}
|
|
|
|
if sensorData.isEncrypted {
|
2020-08-09 07:23:47 -05:00
|
|
|
logger.Warn("packet is encrypted")
|
2020-08-08 19:00:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if sensorData.hasEvent {
|
|
|
|
var eventOffset int = 5
|
|
|
|
if sensorData.hasMacAddress {
|
|
|
|
eventOffset = 11
|
|
|
|
}
|
|
|
|
if sensorData.hasCapabilities {
|
|
|
|
eventOffset++
|
|
|
|
}
|
|
|
|
|
|
|
|
sensorData.eventData.eventType = binary.LittleEndian.Uint16(sd.Data[eventOffset:])
|
|
|
|
sensorData.eventData.eventLength = uint8(sd.Data[eventOffset+2])
|
|
|
|
|
|
|
|
switch sensorData.eventData.eventType {
|
|
|
|
case temperatureEvent:
|
|
|
|
sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
description := bluetooth.GetDescription(advertisement.Addr())
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("temperature_unit", "°C")
|
|
|
|
point.AddField("temperature", sensorData.eventData.temperature)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
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", sensorData.eventData.temperature))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case humidityEvent:
|
|
|
|
sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:])) / 10.0
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("humidity_unit", "%")
|
|
|
|
point.AddField("humidity", sensorData.eventData.humidity)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
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", sensorData.eventData.humidity))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case illuminanceEvent:
|
|
|
|
sensorData.eventData.illuminance = uint(sd.Data[eventOffset+3]) + uint(sd.Data[eventOffset+4])<<8 + uint(sd.Data[eventOffset+5])<<16
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("illuminance_unit", "lx")
|
|
|
|
point.AddField("illuminance", sensorData.eventData.illuminance)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "illuminance"),
|
|
|
|
zap.String("measurement_unit", "lx"),
|
|
|
|
zap.Uint("value", sensorData.eventData.illuminance))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case moistureEvent:
|
|
|
|
sensorData.eventData.moisture = int8(sd.Data[eventOffset+3])
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("moisture_unit", "%")
|
|
|
|
point.AddField("moisture", sensorData.eventData.moisture)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "moisture"),
|
|
|
|
zap.String("measurement_unit", "%"),
|
|
|
|
zap.Int8("value", sensorData.eventData.moisture))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case conductivityEvent:
|
|
|
|
sensorData.eventData.conductivity = int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("conductivity_unit", "µS/cm")
|
|
|
|
point.AddField("conductivity", sensorData.eventData.conductivity)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "RSSI"),
|
|
|
|
zap.String("measurement_unit", "µS/cm"),
|
|
|
|
zap.Int16("value", sensorData.eventData.conductivity))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case batteryEvent:
|
|
|
|
sensorData.eventData.battery = uint8(sd.Data[eventOffset+3])
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
2020-08-08 19:00:41 -05:00
|
|
|
}
|
|
|
|
point.AddTag("battery_unit", "%")
|
|
|
|
point.AddField("battery", sensorData.eventData.battery)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
logger.Debug("sending sensor reading",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.String("measurement", "battery"),
|
|
|
|
zap.String("measurement_unit", "%"),
|
|
|
|
zap.Uint8("value", sensorData.eventData.battery))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
case temperatureAndHumidityEvent:
|
|
|
|
sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0
|
2020-08-09 07:23:47 -05:00
|
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("temperature_unit", "°C")
|
|
|
|
point.AddField("temperature", sensorData.eventData.temperature)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
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", sensorData.eventData.temperature))
|
|
|
|
|
|
|
|
sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+5:])) / 10.0
|
|
|
|
point = influxdb2.NewPointWithMeasurement("sensor")
|
|
|
|
point.SetTime(timestamp)
|
|
|
|
if detector != "" {
|
|
|
|
point.AddTag("detector", detector)
|
|
|
|
}
|
|
|
|
point.AddTag("address", advertisement.Addr().String())
|
|
|
|
point.AddTag("product_id", fmt.Sprintf("%x", sensorData.productID))
|
|
|
|
if description != "" {
|
|
|
|
point.AddTag("description", description)
|
|
|
|
}
|
2020-08-08 19:00:41 -05:00
|
|
|
point.AddTag("humidity_unit", "%")
|
|
|
|
point.AddField("humidity", sensorData.eventData.humidity)
|
2020-08-09 07:23:47 -05:00
|
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
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", sensorData.eventData.humidity))
|
2020-08-08 19:00:41 -05:00
|
|
|
|
|
|
|
default:
|
2020-08-09 07:23:47 -05:00
|
|
|
logger.Warn("unknown event type",
|
|
|
|
zap.String("source", advertisement.Addr().String()),
|
|
|
|
zap.String("description", description),
|
|
|
|
zap.Uint16("event_type", sensorData.eventData.eventType))
|
2020-08-08 19:00:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|