clean up scale
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jeffrey C. Ollie 2020-09-04 20:03:59 -05:00
parent 778c513987
commit 7345fae9f2
Signed by: jeff
GPG key ID: 6F86035A6D97044E

View file

@ -3,7 +3,6 @@ package scale
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"log"
"time" "time"
"github.com/go-ble/ble" "github.com/go-ble/ble"
@ -17,7 +16,6 @@ var XiaomiScaleV2UUID ble.UUID = []byte{0x1b, 0x18}
// ParseXiaomiScaleV2 parses V2 scale service data // ParseXiaomiScaleV2 parses V2 scale service data
func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) { func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, timestamp time.Time, detector string, description string, advertisement ble.Advertisement, index int, sd ble.ServiceData) {
var scaleData struct { var scaleData struct {
RawUnit uint8 RawUnit uint8
ControlByte uint8 ControlByte uint8
@ -56,23 +54,8 @@ func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, ti
} }
if isStabilized { if isStabilized {
loc, err := time.LoadLocation("Local")
if err != nil {
log.Fatal(err)
}
scaleTimestamp := time.Date(
int(scaleData.Year),
time.Month(scaleData.Month),
int(scaleData.Day),
int(scaleData.Hour),
int(scaleData.Minute),
int(scaleData.Second),
0,
loc)
difference := timestamp.Sub(scaleTimestamp)
point := influxdb2.NewPointWithMeasurement("weight") point := influxdb2.NewPointWithMeasurement("weight")
point.SetTime(scaleTimestamp) point.SetTime(timestamp)
if detector != "" { if detector != "" {
point.AddTag("detector", detector) point.AddTag("detector", detector)
} }
@ -82,13 +65,25 @@ func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, ti
} }
point.AddTag("unit", "kg") point.AddTag("unit", "kg")
point.AddTag("original_unit", originalUnit) point.AddTag("original_unit", originalUnit)
point.AddField("time_difference", difference.Seconds())
point.AddField("value", weight) point.AddField("value", weight)
if hasImpedance {
point.AddField("impedance", scaleData.Impedance)
}
(*writeAPI).WritePoint(point) (*writeAPI).WritePoint(point)
if hasImpedance {
point := influxdb2.NewPointWithMeasurement("impedance")
point.SetTime(timestamp)
if detector != "" {
point.AddTag("detector", detector)
}
point.AddTag("address", advertisement.Addr().String())
if description != "" {
point.AddTag("description", description)
}
point.AddTag("unit", "kg")
point.AddField("impedance", scaleData.Impedance)
(*writeAPI).WritePoint(point)
}
logger.Debug("xiaomi v2 scale", logger.Debug("xiaomi v2 scale",
zap.String("name", advertisement.LocalName()), zap.String("name", advertisement.LocalName()),
zap.String("source", advertisement.Addr().String()), zap.String("source", advertisement.Addr().String()),
@ -96,7 +91,5 @@ func ParseXiaomiScaleV2(writeAPI *influxdb2_api.WriteAPI, logger *zap.Logger, ti
zap.Bool("is_stabilized", isStabilized), zap.Bool("is_stabilized", isStabilized),
zap.Bool("has_impedance", hasImpedance), zap.Bool("has_impedance", hasImpedance),
zap.Float64("weight", weight)) zap.Float64("weight", weight))
} }
} }