2018-11-30 15:21:58 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-14 09:45:13 +01:00
|
|
|
"context"
|
2018-11-30 15:21:58 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
"miflorad/common"
|
2018-12-14 09:45:13 +01:00
|
|
|
impl "miflorad/common/ble"
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
"github.com/go-ble/ble"
|
|
|
|
"github.com/go-ble/ble/examples/lib/dev"
|
2018-11-30 15:21:58 +01:00
|
|
|
)
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
const discoveryTimeout = 10 * time.Second
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
func readData(client ble.Client) {
|
2018-11-30 15:21:58 +01:00
|
|
|
prefix := flag.Args()[0]
|
2018-12-20 13:46:39 +01:00
|
|
|
id := common.MifloraGetAlphaNumericID(flag.Args()[1])
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
metaData, err := impl.RequestVersionBattery(client)
|
2018-11-30 15:21:58 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Firmware version: %s\n", metaData.FirmwareVersion)
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.battery_level %d %d\n", prefix, id, metaData.BatteryLevel, time.Now().Unix())
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.firmware_version %d %d\n", prefix, id, metaData.NumericFirmwareVersion(), time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
if metaData.RequiresModeChangeBeforeRead() {
|
2018-12-22 12:19:43 +01:00
|
|
|
err2 := impl.RequestModeChange(client)
|
2018-11-30 15:21:58 +01:00
|
|
|
if err2 != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
sensorData, err3 := impl.RequestSensorData(client)
|
2018-11-30 15:21:58 +01:00
|
|
|
if err3 != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.temperature %.1f %d\n", prefix, id, sensorData.Temperature, time.Now().Unix())
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.brightness %d %d\n", prefix, id, sensorData.Brightness, time.Now().Unix())
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.moisture %d %d\n", prefix, id, sensorData.Moisture, time.Now().Unix())
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.conductivity %d %d\n", prefix, id, sensorData.Conductivity, time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) != 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] prefix peripheral-id\n", os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
prefix := flag.Args()[0]
|
|
|
|
id := common.MifloraGetAlphaNumericID(flag.Args()[1])
|
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
{
|
|
|
|
device, err := dev.NewDevice("default")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to open device, err: %s\n", err)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-12-14 09:45:13 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
ble.SetDefaultDevice(device)
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
// only way to get back the found advertisement, must be buffered!
|
|
|
|
foundAdvertisementChannel := make(chan ble.Advertisement, 1)
|
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
filter := func(adv ble.Advertisement) bool {
|
2018-12-20 13:46:39 +01:00
|
|
|
if strings.ToUpper(adv.Addr().String()) == strings.ToUpper(flag.Args()[1]) {
|
|
|
|
foundAdvertisementChannel <- adv
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2018-12-14 09:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
timeConnectStart := time.Now()
|
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "Scanning...")
|
|
|
|
ctx := ble.WithSigHandler(context.WithTimeout(context.Background(), discoveryTimeout))
|
|
|
|
client, err := ble.Connect(ctx, filter)
|
2018-11-30 15:21:58 +01:00
|
|
|
if err != nil {
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to connect to %s, err: %s\n", flag.Args()[1], err)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
timeConnectTook := time.Since(timeConnectStart).Seconds()
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.connect_time %.2f %d\n", prefix, id, timeConnectTook, time.Now().Unix())
|
|
|
|
|
|
|
|
foundAdvertisement := <-foundAdvertisementChannel
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.rssi %d %d\n", prefix, id, foundAdvertisement.RSSI(), time.Now().Unix())
|
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
// Source: https://github.com/go-ble/ble/blob/master/examples/basic/explorer/main.go#L53
|
|
|
|
// Make sure we had the chance to print out the message.
|
|
|
|
done := make(chan struct{})
|
|
|
|
// Normally, the connection is disconnected by us after our exploration.
|
|
|
|
// However, it can be asynchronously disconnected by the remote peripheral.
|
|
|
|
// So we wait(detect) the disconnection in the go routine.
|
|
|
|
go func() {
|
|
|
|
<-client.Disconnected()
|
|
|
|
fmt.Fprintln(os.Stderr, "Disconnected")
|
|
|
|
close(done)
|
|
|
|
}()
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "Connected")
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
timeReadoutStart := time.Now()
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
if _, err := client.DiscoverProfile(true); err != nil {
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Failed to discover profile, err: %s\n", err)
|
2018-12-20 13:46:39 +01:00
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.failed 1 %d\n", prefix, id, time.Now().Unix())
|
2018-11-30 15:21:58 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-12-22 12:19:43 +01:00
|
|
|
readData(client)
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-20 13:46:39 +01:00
|
|
|
timeReadoutTook := time.Since(timeReadoutStart).Seconds()
|
|
|
|
fmt.Fprintf(os.Stdout, "%s.miflora.%s.readout_time %.2f %d\n", prefix, id, timeReadoutTook, time.Now().Unix())
|
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "Connection done")
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
client.CancelConnection()
|
2018-11-30 15:21:58 +01:00
|
|
|
|
2018-12-14 09:45:13 +01:00
|
|
|
<-done
|
2018-11-30 15:21:58 +01:00
|
|
|
}
|