From e60e507882d7df331a572084359821026f4505bb Mon Sep 17 00:00:00 2001 From: cn Date: Fri, 7 Dec 2018 17:01:05 +0100 Subject: [PATCH] polish --- cmd/mifloractl/main.go | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/mifloractl/main.go b/cmd/mifloractl/main.go index ecfee68..31003a3 100644 --- a/cmd/mifloractl/main.go +++ b/cmd/mifloractl/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "regexp" "strings" "time" @@ -38,8 +39,7 @@ func onStateChanged(device gatt.Device, state gatt.State) { } func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) { - id := strings.ToUpper(flag.Args()[0]) - // fmt.Fprintln(os.Stderr, p.ID()) + id := strings.ToUpper(flag.Args()[1]) if strings.ToUpper(p.ID()) != id { return } @@ -52,6 +52,8 @@ func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) { func onPeriphConnected(p gatt.Peripheral, err error) { fmt.Fprintln(os.Stderr, "Connected") + + // Note: can hang due when device has terminated the connection on it's own already // defer p.Device().CancelConnection(p) if err := p.SetMTU(500); err != nil { @@ -74,13 +76,24 @@ func onPeriphConnected(p gatt.Peripheral, err error) { } } + prefix := flag.Args()[0] + + regexNonAlphaNumeric, err4 := regexp.Compile("[^a-z0-9]+") + if err4 != nil { + fmt.Fprintf(os.Stderr, "Failed to compile regex, err: %s\n", err4) + } + id := regexNonAlphaNumeric.ReplaceAllString(strings.ToLower(flag.Args()[1]), "") + metaData, err := common.MifloraRequestVersionBattery(p) if err != nil { fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err) return } - fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.BatteryLevel) - fmt.Fprintf(os.Stdout, "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()) + // fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.BatteryLevel) + // fmt.Fprintf(os.Stdout, "%s.miflora.%s.firmware_version %s %d\n", prefix, id, metaData.FirmwareVersion, time.Now().Unix()) + fmt.Fprintf(os.Stderr, "Firmware version: %s\n", metaData.FirmwareVersion) // for the newer models a magic number must be written before we can read the current data if metaData.FirmwareVersion >= "2.6.6" { @@ -96,10 +109,14 @@ func onPeriphConnected(p gatt.Peripheral, err error) { fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3) return } - fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.Temperature) - fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.Brightness) - fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.Moisture) - fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.Conductivity) + 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()) + // fmt.Fprintf(os.Stdout, "Temperature: %.1f °C\n", sensorData.Temperature) + // fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.Brightness) + // fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.Moisture) + // fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.Conductivity) } func onPeriphDisconnected(p gatt.Peripheral, err error) { @@ -109,8 +126,8 @@ func onPeriphDisconnected(p gatt.Peripheral, err error) { func main() { flag.Parse() - if len(flag.Args()) != 1 { - fmt.Fprintf(os.Stderr, "Usage: %s [options] peripheral-id\n", os.Args[0]) + if len(flag.Args()) != 2 { + fmt.Fprintf(os.Stderr, "Usage: %s [options] prefix peripheral-id\n", os.Args[0]) os.Exit(1) } @@ -152,12 +169,10 @@ func main() { fmt.Fprintln(os.Stderr, "Connection done") case <-time.After(connectionTimeout): fmt.Fprintln(os.Stderr, "Connection timed out") - fmt.Fprintln(os.Stderr, "A") + // TODO: can hang due when device has terminated the connection on it's own already // device.CancelConnection(discoveryResult.p) - fmt.Fprintln(os.Stderr, "B") } - fmt.Fprintln(os.Stderr, "C") + // Note: calls CancelConnetcion() and thus suffers the same problem, kernel will cleanup after our process finishes // device.Stop() - fmt.Fprintln(os.Stderr, "D") }