This commit is contained in:
Monster
2018-02-10 17:14:29 +03:00
parent a626e6e376
commit 358c16a3a5
4 changed files with 41 additions and 16 deletions
Executable
BIN
View File
Binary file not shown.
+31 -6
View File
@@ -1,7 +1,10 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"strings" "strings"
"sync" "sync"
@@ -12,6 +15,8 @@ import (
) )
var done = make(chan struct{}) var done = make(chan struct{})
var weights []*Weight
var isdone bool
func onStateChanged(d gatt.Device, s gatt.State) { func onStateChanged(d gatt.Device, s gatt.State) {
fmt.Println("State:", s) fmt.Println("State:", s)
@@ -74,7 +79,6 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
fmt.Printf("Failed to discover services, err: %s\n", err) fmt.Printf("Failed to discover services, err: %s\n", err)
return return
} }
service := getServiceByUUID(WEIGHT_MEASUREMENT_SERVICE, services) service := getServiceByUUID(WEIGHT_MEASUREMENT_SERVICE, services)
fmt.Printf("Service Found %s\n", service.Name()) fmt.Printf("Service Found %s\n", service.Name())
chars, _ := p.DiscoverCharacteristics(nil, service) chars, _ := p.DiscoverCharacteristics(nil, service)
@@ -94,17 +98,26 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
descriptor := getDescriptorByUUID(WEIGHT_MEASUREMENT_CONFIG, descriptors) descriptor := getDescriptorByUUID(WEIGHT_MEASUREMENT_CONFIG, descriptors)
p.WriteDescriptor(descriptor, []byte{0x02, 0x00}) p.WriteDescriptor(descriptor, []byte{0x02, 0x00})
isdone := false isdone = false
p.SetNotifyValue(histChar, func(c *gatt.Characteristic, b []byte, e error) { p.SetNotifyValue(histChar, func(c *gatt.Characteristic, b []byte, e error) {
log.Printf("Got back from %s: %#x\n", c.UUID(), b) log.Printf("Got back from %s: %#x\n", c.UUID(), b)
//weight := //weight :=
if len(b) == 20 { if len(b) == 20 {
first := b[0:10] first := b[0:10]
parseData(first) weight := parseData(first)
if weight != nil {
weights = append(weights, weight)
}
second := b[10:20] second := b[10:20]
parseData(second) weight = parseData(second)
if weight != nil {
weights = append(weights, weight)
}
} else if len(b) == 10 { } else if len(b) == 10 {
parseData(b) weight := parseData(b)
if weight != nil {
weights = append(weights, weight)
}
} else if (len(b) == 1) && b[0] == 0x3 { } else if (len(b) == 1) && b[0] == 0x3 {
isdone = true isdone = true
return return
@@ -174,5 +187,17 @@ func main() {
d.Init(onStateChanged) d.Init(onStateChanged)
<-done <-done
fmt.Println("Done") if isdone {
writeToFile()
}
}
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)
} }
+8 -8
View File
@@ -14,14 +14,14 @@ func parseData(weightBytes []byte) *Weight {
isLBSUnit := isBitSet(ctrlByte, 0) isLBSUnit := isBitSet(ctrlByte, 0)
isCattyUnit := isBitSet(ctrlByte, 4) isCattyUnit := isBitSet(ctrlByte, 4)
// fmt.Printf("IsWeightRemoved: %v\n", isWeightRemoved) fmt.Printf("IsWeightRemoved: %v\n", isWeightRemoved)
// fmt.Printf("6 LSB Unknown: %v\n", isBitSet(ctrlByte, 6)) fmt.Printf("6 LSB Unknown: %v\n", isBitSet(ctrlByte, 6))
// fmt.Printf("IsStabilized: %v\n", isStabilized) fmt.Printf("IsStabilized: %v\n", isStabilized)
// fmt.Printf("IsCattyOrKg: %v\n", isCattyUnit) fmt.Printf("IsCattyOrKg: %v\n", isCattyUnit)
// fmt.Printf("3 LSB Unknown: %v\n", isBitSet(ctrlByte, 3)) fmt.Printf("3 LSB Unknown: %v\n", isBitSet(ctrlByte, 3))
// fmt.Printf("2 LSB Unknown: %v\n", isBitSet(ctrlByte, 2)) fmt.Printf("2 LSB Unknown: %v\n", isBitSet(ctrlByte, 2))
// fmt.Printf("1 LSB Unknown: %v\n", isBitSet(ctrlByte, 1)) fmt.Printf("1 LSB Unknown: %v\n", isBitSet(ctrlByte, 1))
// fmt.Printf("IsLBS: %v\n", isLBSUnit) fmt.Printf("IsLBS: %v\n", isLBSUnit)
// // Only if the value is stabilized and the weight is *not* removed, the date is valid // // Only if the value is stabilized and the weight is *not* removed, the date is valid
if isStabilized && !isWeightRemoved { if isStabilized && !isWeightRemoved {
+2 -2
View File
@@ -5,6 +5,6 @@ import (
) )
type Weight struct { type Weight struct {
Time time.Time Time time.Time `json:"datetime"`
Weight float64 Weight float64 `json:"weight"`
} }