51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package scale
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.ocjtech.us/jeff/bluelisten/lib/bluetooth"
|
|
"github.com/currantlabs/ble"
|
|
influxdb2 "github.com/influxdata/influxdb-client-go"
|
|
influxdb2_api "github.com/influxdata/influxdb-client-go/api"
|
|
)
|
|
|
|
// XiaomiScaleV1UUID is BLE UUID for Xiaomi Scale V1
|
|
var XiaomiScaleV1UUID ble.UUID = []byte{0x1d, 0x18}
|
|
|
|
// ParseXiaomiScaleV1 parses V1 scale service data
|
|
func ParseXiaomiScaleV1(mux *sync.Mutex, writeAPI *influxdb2_api.WriteAPI, advertisement ble.Advertisement, index int, sd ble.ServiceData) {
|
|
timestamp := time.Now()
|
|
point := influxdb2.NewPointWithMeasurement("sensor")
|
|
point.SetTime(timestamp)
|
|
detector, _ := os.Hostname()
|
|
if detector != "" {
|
|
point.AddTag("detector", detector)
|
|
}
|
|
point.AddTag("address", advertisement.Address().String())
|
|
description := bluetooth.GetDescription(advertisement.Address())
|
|
if description != "" {
|
|
point.AddTag("description", description)
|
|
}
|
|
point.AddField("rssi", advertisement.RSSI())
|
|
(*writeAPI).WritePoint(point)
|
|
|
|
mux.Lock()
|
|
fmt.Printf("*******************************************\n")
|
|
fmt.Printf("Xiaomi V1 Scale\n")
|
|
if len(advertisement.LocalName()) > 0 {
|
|
fmt.Printf(" Name: %s\n", advertisement.LocalName())
|
|
}
|
|
if len(advertisement.Services()) > 0 {
|
|
fmt.Printf(" Svcs: %v\n", advertisement.Services())
|
|
}
|
|
if len(advertisement.ManufacturerData()) > 0 {
|
|
fmt.Printf(" MD: %v\n", advertisement.ManufacturerData())
|
|
}
|
|
fmt.Printf(" %d %v %v\n", index, sd.UUID, sd.Data)
|
|
fmt.Printf("*******************************************\n")
|
|
mux.Unlock()
|
|
}
|