change influxdb2 schema
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jeffrey C. Ollie 2020-08-13 00:46:28 -05:00
parent 523a561015
commit 6fa0a57a20
Signed by: jeff
GPG key ID: 6F86035A6D97044E
5 changed files with 58 additions and 42 deletions

View file

@ -83,7 +83,7 @@ func advHandler(advertisement ble.Advertisement) {
timestamp := time.Now() timestamp := time.Now()
description := bluetooth.GetDescription(advertisement.Addr()) description := bluetooth.GetDescription(advertisement.Addr())
point := influxdb2.NewPointWithMeasurement("sensor") point := influxdb2.NewPointWithMeasurement("rssi")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -92,8 +92,7 @@ func advHandler(advertisement ble.Advertisement) {
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "rssi") point.AddTag("unit", "dBm")
point.AddTag("measurement_unit", "dBm")
point.AddField("value", advertisement.RSSI()) point.AddField("value", advertisement.RSSI())
writeAPI.WritePoint(point) writeAPI.WritePoint(point)

View file

@ -37,20 +37,22 @@ func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Log
zap.String("event_data", hex.Dump(eventData))) zap.String("event_data", hex.Dump(eventData)))
continue continue
} }
temperature := float64(binary.LittleEndian.Uint16(eventData[0:2])) / 10 temperature := float64(binary.LittleEndian.Uint16(eventData[0:2])) / 10
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("temperature")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
} }
point.AddTag("address", advertisement.Addr().String()) point.AddTag("address", advertisement.Addr().String())
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("temperature_unit", "°C") point.AddTag("unit", "°C")
point.AddField("temperature", temperature) point.AddField("value", temperature)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -59,7 +61,8 @@ func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Log
zap.Float64("value", temperature)) zap.Float64("value", temperature))
humidity := float64(binary.LittleEndian.Uint16(eventData[2:4])) / 10 humidity := float64(binary.LittleEndian.Uint16(eventData[2:4])) / 10
point = influxdb2.NewPointWithMeasurement("sensor")
point = influxdb2.NewPointWithMeasurement("humidity")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -68,9 +71,10 @@ func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Log
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement_unit", "%") point.AddTag("unit", "%")
point.AddField("humidity", humidity) point.AddField("value", humidity)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -83,8 +87,10 @@ func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Log
logger.Warn("battery event length should be 1") logger.Warn("battery event length should be 1")
continue continue
} }
battery := int(eventData[0]) battery := int(eventData[0])
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("battery")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -93,10 +99,10 @@ func ParseCleargrassSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Log
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "battery") point.AddTag("unit", "%")
point.AddTag("measurement_unit", "%")
point.AddField("value", battery) point.AddField("value", battery)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),

View file

@ -128,7 +128,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
switch sensorData.eventData.eventType { switch sensorData.eventData.eventType {
case temperatureEvent: case temperatureEvent:
sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0 sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("temperature")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -138,10 +139,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measaurement", "temperature") point.AddTag("unit", "°C")
point.AddTag("measurement_unit", "°C")
point.AddField("value", sensorData.eventData.temperature) point.AddField("value", sensorData.eventData.temperature)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -151,7 +152,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case humidityEvent: case humidityEvent:
sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:])) / 10.0 sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:])) / 10.0
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("humidity")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -161,10 +163,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measaurement", "humidity") point.AddTag("unit", "%")
point.AddTag("measaurement_unit", "%")
point.AddField("value", sensorData.eventData.humidity) point.AddField("value", sensorData.eventData.humidity)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -174,7 +176,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case illuminanceEvent: case illuminanceEvent:
sensorData.eventData.illuminance = uint(sd.Data[eventOffset+3]) + uint(sd.Data[eventOffset+4])<<8 + uint(sd.Data[eventOffset+5])<<16 sensorData.eventData.illuminance = uint(sd.Data[eventOffset+3]) + uint(sd.Data[eventOffset+4])<<8 + uint(sd.Data[eventOffset+5])<<16
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("illuminance")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -184,10 +187,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "illuminance") point.AddTag("unit", "lx")
point.AddTag("measurement_unit", "lx")
point.AddField("value", sensorData.eventData.illuminance) point.AddField("value", sensorData.eventData.illuminance)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -197,7 +200,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case moistureEvent: case moistureEvent:
sensorData.eventData.moisture = int8(sd.Data[eventOffset+3]) sensorData.eventData.moisture = int8(sd.Data[eventOffset+3])
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("moisture")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -207,10 +211,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "moisture") point.AddTag("unit", "%")
point.AddTag("measurement_unit", "%")
point.AddField("value", sensorData.eventData.moisture) point.AddField("value", sensorData.eventData.moisture)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -220,7 +224,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case conductivityEvent: case conductivityEvent:
sensorData.eventData.conductivity = int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:])) sensorData.eventData.conductivity = int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("conductivity")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -230,10 +235,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "conductivity") point.AddTag("unit", "µS/cm")
point.AddTag("measurement_unit", "µS/cm")
point.AddField("value", sensorData.eventData.conductivity) point.AddField("value", sensorData.eventData.conductivity)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -243,7 +248,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case batteryEvent: case batteryEvent:
sensorData.eventData.battery = uint8(sd.Data[eventOffset+3]) sensorData.eventData.battery = uint8(sd.Data[eventOffset+3])
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("battery")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -253,10 +259,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "battery") point.AddTag("unit", "%")
point.AddTag("measurement_unit", "%")
point.AddField("value", sensorData.eventData.battery) point.AddField("value", sensorData.eventData.battery)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -266,7 +272,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
case temperatureAndHumidityEvent: case temperatureAndHumidityEvent:
sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0 sensorData.eventData.temperature = float64(int16(binary.LittleEndian.Uint16(sd.Data[eventOffset+3:]))) / 10.0
point := influxdb2.NewPointWithMeasurement("sensor")
point := influxdb2.NewPointWithMeasurement("temperature")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -276,10 +283,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "temperature") point.AddTag("unit", "°C")
point.AddTag("measurement_unit", "°C")
point.AddField("value", sensorData.eventData.temperature) point.AddField("value", sensorData.eventData.temperature)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
@ -288,7 +295,8 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
zap.Float64("value", sensorData.eventData.temperature)) zap.Float64("value", sensorData.eventData.temperature))
sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+5:])) / 10.0 sensorData.eventData.humidity = float64(binary.LittleEndian.Uint16(sd.Data[eventOffset+5:])) / 10.0
point = influxdb2.NewPointWithMeasurement("sensor")
point = influxdb2.NewPointWithMeasurement("humidity")
point.SetTime(timestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
@ -298,10 +306,10 @@ func ParseXiaomiMijiaSensorData(writeAPI *influxdb2_api.WriteAPI, logger *zap.Lo
if description != "" { if description != "" {
point.AddTag("description", description) point.AddTag("description", description)
} }
point.AddTag("measurement", "humidity") point.AddTag("unit", "%")
point.AddTag("measurement_unit", "%")
point.AddField("value", sensorData.eventData.humidity) point.AddField("value", sensorData.eventData.humidity)
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
logger.Debug("sending sensor reading", logger.Debug("sending sensor reading",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),

View file

@ -13,7 +13,7 @@ var XiaomiScaleV1UUID ble.UUID = []byte{0x1d, 0x18}
// ParseXiaomiScaleV1 parses V1 scale service data // ParseXiaomiScaleV1 parses V1 scale service data
func ParseXiaomiScaleV1(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) { func ParseXiaomiScaleV1(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) {
logger.Debug("xiaomi v1 scale", logger.Info("xiaomi v1 scale",
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
zap.String("description", description), zap.String("description", description),
zap.String("name", advertisement.LocalName()), zap.String("name", advertisement.LocalName()),

View file

@ -71,16 +71,19 @@ func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, ti
loc) loc)
difference := timestamp.Sub(scaleTimestamp) difference := timestamp.Sub(scaleTimestamp)
point := influxdb2.NewPointWithMeasurement("sensor") point := influxdb2.NewPointWithMeasurement("weight")
point.SetTime(scaleTimestamp) point.SetTime(scaleTimestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
} }
point.AddTag("address", advertisement.Addr().String()) point.AddTag("address", advertisement.Addr().String())
point.AddTag("weight_unit", "kg") if description != "" {
point.AddTag("original_weight_unit", originalUnit) point.AddTag("description", description)
}
point.AddTag("unit", "kg")
point.AddTag("original_unit", originalUnit)
point.AddField("time_difference", difference.Seconds()) point.AddField("time_difference", difference.Seconds())
point.AddField("weight", weight) point.AddField("value", weight)
if hasImpedance { if hasImpedance {
point.AddField("impedance", scaleData.Impedance) point.AddField("impedance", scaleData.Impedance)
} }