Compare commits
10
Commits
f4479a07ba
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dc17baefb | ||
|
|
1e288f8229 | ||
|
|
2bced50e29 | ||
|
|
93fe1358e8 | ||
|
|
012c35a556 | ||
|
|
60c0e0f260 | ||
|
|
358c16a3a5 | ||
|
|
a626e6e376 | ||
|
|
b548fdc402 | ||
|
|
9bda319f12 |
No files matched your search
@@ -0,0 +1,28 @@
|
|||||||
|
name: Go
|
||||||
|
on: [push]
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Set up Go 1.13
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: 1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v1
|
||||||
|
|
||||||
|
- name: Get dependencies
|
||||||
|
run: |
|
||||||
|
go get -v -t -d ./...
|
||||||
|
if [ -f Gopkg.toml ]; then
|
||||||
|
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
|
||||||
|
dep ensure
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: go build -v .
|
||||||
@@ -1,2 +1,19 @@
|
|||||||
# blegw
|
# blegw
|
||||||
Gateway Bluetooth LE->MQTT (now support only xiaomi scale and didn't support mqtt :-D).
|
This app will retrieve Xiaomi Smart Scale data and save it to weights.json.
|
||||||
|
Project based on openScale (https://github.com/oliexdev/openScale/) code.
|
||||||
|
|
||||||
|
# License
|
||||||
|
Project is licensed under the GPL v3, see LICENSE file for full notice.
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"blegw/workers"
|
|
||||||
|
|
||||||
"github.com/currantlabs/gatt"
|
|
||||||
"github.com/currantlabs/gatt/examples/option"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BleService struct {
|
|
||||||
workers []*workers.Worker
|
|
||||||
done chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddWorker - add new worker
|
|
||||||
func (s *BleService) AddWorker(worker workers.Worker) {
|
|
||||||
if s.workers == nil {
|
|
||||||
fmt.Println("Init workers.")
|
|
||||||
s.workers = make([]*workers.Worker, 0, 0)
|
|
||||||
}
|
|
||||||
s.workers = append(s.workers, &worker)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BleService) Init() error {
|
|
||||||
s.done = make(chan struct{})
|
|
||||||
d, err := gatt.NewDevice(option.DefaultClientOptions...)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to open device, err: %s\n", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register handlers.
|
|
||||||
d.Handle(
|
|
||||||
gatt.PeripheralDiscovered(s.onPeriphDiscovered),
|
|
||||||
gatt.PeripheralConnected(s.onPeriphConnected),
|
|
||||||
gatt.PeripheralDisconnected(s.onPeriphDisconnected),
|
|
||||||
)
|
|
||||||
|
|
||||||
d.Init(s.onStateChanged)
|
|
||||||
<-s.done
|
|
||||||
fmt.Println("Done")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BleService) onStateChanged(d gatt.Device, state gatt.State) {
|
|
||||||
fmt.Println("State:", state)
|
|
||||||
switch state {
|
|
||||||
case gatt.StatePoweredOn:
|
|
||||||
fmt.Println("scanning...")
|
|
||||||
d.Scan([]gatt.UUID{}, false)
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
d.StopScanning()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BleService) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
|
||||||
isConnect := false
|
|
||||||
for _, worker := range s.workers {
|
|
||||||
ret := (*worker).OnDiscovery(p, a, rssi)
|
|
||||||
if ret && !isConnect {
|
|
||||||
isConnect = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if isConnect {
|
|
||||||
p.Device().Connect(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BleService) onPeriphConnected(p gatt.Peripheral, err error) {
|
|
||||||
defer p.Device().CancelConnection(p)
|
|
||||||
|
|
||||||
if err := p.SetMTU(500); err != nil {
|
|
||||||
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("Set mtu done.\n")
|
|
||||||
|
|
||||||
for _, worker := range s.workers {
|
|
||||||
(*worker).OnConnect(p, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BleService) onPeriphDisconnected(p gatt.Peripheral, err error) {
|
|
||||||
fmt.Println("Disconnected")
|
|
||||||
close(s.done)
|
|
||||||
}
|
|
||||||
@@ -1,15 +1,201 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "blegw/workers"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/currantlabs/gatt"
|
||||||
|
"github.com/currantlabs/gatt/examples/option"
|
||||||
|
)
|
||||||
|
|
||||||
|
var done = make(chan struct{})
|
||||||
|
var weights []*Weight
|
||||||
|
var isdone bool
|
||||||
|
|
||||||
|
func onStateChanged(d gatt.Device, s gatt.State) {
|
||||||
|
fmt.Println("State:", s)
|
||||||
|
switch s {
|
||||||
|
case gatt.StatePoweredOn:
|
||||||
|
fmt.Println("scanning...")
|
||||||
|
d.Scan([]gatt.UUID{}, false)
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
d.StopScanning()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||||
|
ID := p.ID()
|
||||||
|
//xiaomi scale
|
||||||
|
if !strings.HasPrefix(ID, "88:0F:10") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", ID, p.Name())
|
||||||
|
|
||||||
|
fmt.Printf("%v+", a)
|
||||||
|
fmt.Println(" Local Name =", a.LocalName)
|
||||||
|
fmt.Println(" TX Power Level =", a.TxPowerLevel)
|
||||||
|
fmt.Println(" Manufacturer Data =", a.ManufacturerData)
|
||||||
|
fmt.Println(" Service Data =", a.ServiceData)
|
||||||
|
fmt.Println(" RSSI =", rssi)
|
||||||
|
|
||||||
|
p.Device().StopScanning()
|
||||||
|
var mu sync.Mutex
|
||||||
|
count := 0
|
||||||
|
mu.Lock()
|
||||||
|
go func(count int) {
|
||||||
|
time.Sleep(time.Duration(count) * 5 * time.Second)
|
||||||
|
fmt.Println("Connecting...")
|
||||||
|
p.Device().Connect(p)
|
||||||
|
}(count)
|
||||||
|
count++
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func onPeriphConnected(p gatt.Peripheral, err error) {
|
||||||
|
var WEIGHT_MEASUREMENT_SERVICE = gatt.MustParseUUID("181d")
|
||||||
|
var WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC = gatt.MustParseUUID("00002a2f-0000-3512-2118-0009af100700")
|
||||||
|
// var WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC = gatt.MustParseUUID("2a2b")
|
||||||
|
var WEIGHT_MEASUREMENT_CONFIG = gatt.MustParseUUID("2902")
|
||||||
|
|
||||||
|
fmt.Println("Connected")
|
||||||
|
defer p.Device().CancelConnection(p)
|
||||||
|
if err := p.SetMTU(500); err != nil {
|
||||||
|
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Set mtu done.\n")
|
||||||
|
|
||||||
|
// Discovery services
|
||||||
|
services, err := p.DiscoverServices(nil)
|
||||||
|
fmt.Printf("Services: %v+", services)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to discover services, err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
service := getServiceByUUID(WEIGHT_MEASUREMENT_SERVICE, services)
|
||||||
|
fmt.Printf("Service Found %s\n", service.Name())
|
||||||
|
chars, _ := p.DiscoverCharacteristics(nil, service)
|
||||||
|
|
||||||
|
// char := getCharByUUID(WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC, chars)
|
||||||
|
// b, err := p.ReadCharacteristic(char)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// parseData(b)
|
||||||
|
// return
|
||||||
|
// fmt.Printf(" value %x | %q\n", b, b)
|
||||||
|
|
||||||
|
histChar := getCharByUUID(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, chars)
|
||||||
|
descriptors, _ := p.DiscoverDescriptors(nil, histChar)
|
||||||
|
descriptor := getDescriptorByUUID(WEIGHT_MEASUREMENT_CONFIG, descriptors)
|
||||||
|
p.WriteDescriptor(descriptor, []byte{0x02, 0x00})
|
||||||
|
|
||||||
|
isdone = false
|
||||||
|
p.SetNotifyValue(histChar, func(c *gatt.Characteristic, b []byte, e error) {
|
||||||
|
log.Printf("Got back from %s: %#x\n", c.UUID(), b)
|
||||||
|
//weight :=
|
||||||
|
if len(b) == 20 {
|
||||||
|
weight := parseData(b[0:10])
|
||||||
|
if weight != nil {
|
||||||
|
weights = append(weights, weight)
|
||||||
|
}
|
||||||
|
weight = parseData(b[10:20])
|
||||||
|
if weight != nil {
|
||||||
|
weights = append(weights, weight)
|
||||||
|
}
|
||||||
|
} else if len(b) == 10 {
|
||||||
|
weight := parseData(b)
|
||||||
|
if weight != nil {
|
||||||
|
weights = append(weights, weight)
|
||||||
|
}
|
||||||
|
} else if (len(b) == 1) && b[0] == 0x3 {
|
||||||
|
isdone = true
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Invalid data: %v", b)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
p.WriteCharacteristic(histChar, []byte{0x02}, true)
|
||||||
|
|
||||||
|
fmt.Printf("Waiting for 120 seconds to get some notifiations, if any.\n")
|
||||||
|
for i := 0; i < 120; i++ {
|
||||||
|
if isdone {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDescriptorByUUID(uuid gatt.UUID, descriptors []*gatt.Descriptor) *gatt.Descriptor {
|
||||||
|
for _, descriptor := range descriptors {
|
||||||
|
if !descriptor.UUID().Equal(uuid) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return descriptor
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getServiceByUUID(uuid gatt.UUID, services []*gatt.Service) *gatt.Service {
|
||||||
|
for _, service := range services {
|
||||||
|
if !service.UUID().Equal(uuid) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return service
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCharByUUID(uid gatt.UUID, chars []*gatt.Characteristic) *gatt.Characteristic {
|
||||||
|
for _, c := range chars {
|
||||||
|
if !c.UUID().Equal(uid) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||||
|
fmt.Println("Disconnected")
|
||||||
|
close(done)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
service := BleService{}
|
d, err := gatt.NewDevice(option.DefaultClientOptions...)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open device, err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log := workers.Log{}
|
// Register handlers.
|
||||||
service.AddWorker(log)
|
d.Handle(
|
||||||
|
gatt.PeripheralDiscovered(onPeriphDiscovered),
|
||||||
|
gatt.PeripheralConnected(onPeriphConnected),
|
||||||
|
gatt.PeripheralDisconnected(onPeriphDisconnected),
|
||||||
|
)
|
||||||
|
|
||||||
scale := workers.Scale{}
|
d.Init(onStateChanged)
|
||||||
service.AddWorker(scale)
|
<-done
|
||||||
|
if isdone {
|
||||||
|
writeToFile()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
service.Init()
|
func writeToFile() {
|
||||||
|
jsonbytes, err := json.Marshal(weights)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var out bytes.Buffer
|
||||||
|
err = json.Indent(&out, jsonbytes, "", "\t")
|
||||||
|
ioutil.WriteFile("weights.json", out.Bytes(), 0644)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseData(weightBytes []byte) *Weight {
|
||||||
|
ctrlByte := weightBytes[0]
|
||||||
|
|
||||||
|
isWeightRemoved := isBitSet(ctrlByte, 7)
|
||||||
|
isStabilized := isBitSet(ctrlByte, 5)
|
||||||
|
isLBSUnit := isBitSet(ctrlByte, 0)
|
||||||
|
isCattyUnit := isBitSet(ctrlByte, 4)
|
||||||
|
|
||||||
|
// fmt.Printf("IsWeightRemoved: %v\n", isBitSet(ctrlByte, 7))
|
||||||
|
// fmt.Printf("6 LSB Unknown: %v\n", isBitSet(ctrlByte, 6))
|
||||||
|
// fmt.Printf("IsStabilized: %v\n", isBitSet(ctrlByte, 5))
|
||||||
|
// fmt.Printf("IsCattyOrKg: %v\n", isBitSet(ctrlByte, 4))
|
||||||
|
// fmt.Printf("BMI and Height present: %v\n", isBitSet(ctrlByte, 3))
|
||||||
|
// fmt.Printf("User ID present: %v\n", isBitSet(ctrlByte, 2))
|
||||||
|
// fmt.Printf("Time stamp present: %v\n", isBitSet(ctrlByte, 1))
|
||||||
|
// fmt.Printf("IsLBS: %v\n", isBitSet(ctrlByte, 0))
|
||||||
|
|
||||||
|
// // Only if the value is stabilized and the weight is *not* removed, the date is valid
|
||||||
|
if isStabilized && !isWeightRemoved {
|
||||||
|
wb4 := int(weightBytes[4])
|
||||||
|
wb3 := int(weightBytes[3])
|
||||||
|
year := (wb4<<8 | wb3)
|
||||||
|
month := int(weightBytes[5])
|
||||||
|
day := int(weightBytes[6])
|
||||||
|
hours := int(weightBytes[7])
|
||||||
|
min := int(weightBytes[8])
|
||||||
|
sec := int(weightBytes[9])
|
||||||
|
|
||||||
|
// Is the year plausible? Check if the year is in the range of 20 years...
|
||||||
|
now := time.Now()
|
||||||
|
if math.Abs(float64(now.Year()-year)) > 20 || year > now.Year() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
wb1 := int(weightBytes[1])
|
||||||
|
wb2 := int(weightBytes[2])
|
||||||
|
weightVal := 0.0
|
||||||
|
if isLBSUnit || isCattyUnit {
|
||||||
|
weightVal = float64(((wb2)<<8)|(wb1)) / 100.0
|
||||||
|
} else {
|
||||||
|
weightVal = float64(((wb2)<<8)|(wb1)) / 200.0
|
||||||
|
}
|
||||||
|
|
||||||
|
t := time.Date(year, time.Month(month), day, hours, min, sec, 0, time.Local)
|
||||||
|
weight := &Weight{Weight: weightVal, Time: t}
|
||||||
|
|
||||||
|
// fmt.Printf("Payload is: %#x\n", weightBytes)
|
||||||
|
fmt.Printf("[+] Year: %v, Month: %v, day: %v, hr: %v, min: %v, sec: %v = %v \n", year, month, day, hours, min, sec, weightVal)
|
||||||
|
return weight
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isBitSet(value byte, bit uint) bool {
|
||||||
|
return (value & (1 << bit)) != 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Weight struct {
|
||||||
|
Time time.Time `json:"datetime"`
|
||||||
|
Weight float64 `json:"weight"`
|
||||||
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package workers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/currantlabs/gatt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Log struct {
|
|
||||||
Worker
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Log) OnDiscovery(p gatt.Peripheral, a *gatt.Advertisement, rssi int) bool {
|
|
||||||
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
|
|
||||||
fmt.Println(" Local Name =", a.LocalName)
|
|
||||||
fmt.Println(" Manufacturer Data =", a.ManufacturerData)
|
|
||||||
fmt.Println(" Service Data =", a.ServiceData)
|
|
||||||
fmt.Println(" RSSI =", rssi)
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Log) OnConnect(p gatt.Peripheral, err error) error {
|
|
||||||
ID := p.ID()
|
|
||||||
fmt.Println("Connected to " + ID)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
package workers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"math"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/currantlabs/gatt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Weight struct {
|
|
||||||
Time time.Time
|
|
||||||
Weight float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type Scale struct {
|
|
||||||
Worker
|
|
||||||
}
|
|
||||||
|
|
||||||
var WEIGHT_MEASUREMENT_SERVICE = gatt.MustParseUUID("181d")
|
|
||||||
var WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC = gatt.MustParseUUID("00002a2f-0000-3512-2118-0009af100700")
|
|
||||||
var WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC = gatt.MustParseUUID("2a2b")
|
|
||||||
var WEIGHT_MEASUREMENT_CONFIG = gatt.MustParseUUID("2902")
|
|
||||||
|
|
||||||
func (s Scale) OnDiscovery(p gatt.Peripheral, a *gatt.Advertisement, rssi int) bool {
|
|
||||||
ID := p.ID()
|
|
||||||
if !strings.HasPrefix(ID, "88:0F:10") {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Scale) OnConnect(p gatt.Peripheral, err error) error {
|
|
||||||
ID := p.ID()
|
|
||||||
//not our device
|
|
||||||
if !strings.HasPrefix(ID, "88:0F:10") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discovery services
|
|
||||||
services, err := p.DiscoverServices(nil)
|
|
||||||
fmt.Printf("Services: %v+", services)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to discover services, err: %s\n", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
service := getServiceByUUID(WEIGHT_MEASUREMENT_SERVICE, services)
|
|
||||||
fmt.Printf("Service Found %s\n", service.Name())
|
|
||||||
chars, _ := p.DiscoverCharacteristics(nil, service)
|
|
||||||
|
|
||||||
char := getCharByUUID(WEIGHT_MEASUREMENT_TIME_CHARACTERISTIC, chars)
|
|
||||||
b, err := p.ReadCharacteristic(char)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
fmt.Printf(" value %x | %q\n", b, b)
|
|
||||||
|
|
||||||
histChar := getCharByUUID(WEIGHT_MEASUREMENT_HISTORY_CHARACTERISTIC, chars)
|
|
||||||
descriptors, _ := p.DiscoverDescriptors(nil, histChar)
|
|
||||||
descriptor := getDescriptorByUUID(WEIGHT_MEASUREMENT_CONFIG, descriptors)
|
|
||||||
p.WriteDescriptor(descriptor, []byte{0x02, 0x00})
|
|
||||||
|
|
||||||
p.SetNotifyValue(histChar, func(c *gatt.Characteristic, b []byte, e error) {
|
|
||||||
log.Printf("Got back from %s: %#x\n", c.UUID(), b)
|
|
||||||
//weight :=
|
|
||||||
if len(b) == 20 {
|
|
||||||
first := b[0:10]
|
|
||||||
parseData(first)
|
|
||||||
second := b[10:20]
|
|
||||||
parseData(second)
|
|
||||||
} else if len(b) == 10 {
|
|
||||||
parseData(b)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
p.WriteCharacteristic(histChar, []byte{0x02}, true)
|
|
||||||
|
|
||||||
fmt.Printf("Waiting for 5 seconds to get some notifiations, if any.\n")
|
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseData(weightBytes []byte) *Weight {
|
|
||||||
ctrlByte := weightBytes[0]
|
|
||||||
|
|
||||||
isWeightRemoved := isBitSet(ctrlByte, 7)
|
|
||||||
isStabilized := isBitSet(ctrlByte, 5)
|
|
||||||
isLBSUnit := isBitSet(ctrlByte, 0)
|
|
||||||
isCattyUnit := isBitSet(ctrlByte, 4)
|
|
||||||
|
|
||||||
// Only if the value is stabilized and the weight is *not* removed, the date is valid
|
|
||||||
if isStabilized && !isWeightRemoved {
|
|
||||||
wb4 := int(weightBytes[4])
|
|
||||||
wb3 := int(weightBytes[3])
|
|
||||||
year := (wb4<<8 | wb3)
|
|
||||||
month := int(weightBytes[5])
|
|
||||||
day := int(weightBytes[6])
|
|
||||||
hours := int(weightBytes[7])
|
|
||||||
min := int(weightBytes[8])
|
|
||||||
sec := int(weightBytes[9])
|
|
||||||
|
|
||||||
// Is the year plausible? Check if the year is in the range of 20 years...
|
|
||||||
now := time.Now()
|
|
||||||
if math.Abs(float64(now.Year()-year)) > 20 || year > now.Year() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
wb1 := int(weightBytes[1])
|
|
||||||
wb2 := int(weightBytes[2])
|
|
||||||
weightVal := 0.0
|
|
||||||
if isLBSUnit || isCattyUnit {
|
|
||||||
weightVal = float64(((wb2)<<8)|(wb1)) / 100.0
|
|
||||||
} else {
|
|
||||||
weightVal = float64(((wb2)<<8)|(wb1)) / 200.0
|
|
||||||
}
|
|
||||||
|
|
||||||
t := time.Date(year, time.Month(month), day, hours, min, sec, 0, time.Local)
|
|
||||||
weight := &Weight{Weight: weightVal, Time: t}
|
|
||||||
|
|
||||||
// fmt.Printf("Payload is: %#x\n", weightBytes)
|
|
||||||
fmt.Printf("[+] Year: %v, Month: %v, day: %v, hr: %v, min: %v, sec: %v = %v \n", year, month, day, hours, min, sec, weightVal)
|
|
||||||
return weight
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isBitSet(value byte, bit uint) bool {
|
|
||||||
return (value & (1 << bit)) != 0
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package workers
|
|
||||||
|
|
||||||
import "github.com/currantlabs/gatt"
|
|
||||||
|
|
||||||
type Worker interface {
|
|
||||||
OnDiscovery(p gatt.Peripheral, a *gatt.Advertisement, rssi int) bool
|
|
||||||
OnConnect(p gatt.Peripheral, err error) error
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package workers
|
|
||||||
|
|
||||||
import "github.com/currantlabs/gatt"
|
|
||||||
|
|
||||||
func getDescriptorByUUID(uuid gatt.UUID, descriptors []*gatt.Descriptor) *gatt.Descriptor {
|
|
||||||
for _, descriptor := range descriptors {
|
|
||||||
if !descriptor.UUID().Equal(uuid) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return descriptor
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getServiceByUUID(uuid gatt.UUID, services []*gatt.Service) *gatt.Service {
|
|
||||||
for _, service := range services {
|
|
||||||
if !service.UUID().Equal(uuid) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return service
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCharByUUID(uid gatt.UUID, chars []*gatt.Characteristic) *gatt.Characteristic {
|
|
||||||
for _, c := range chars {
|
|
||||||
if !c.UUID().Equal(uid) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user