miflorad: fix some linter errors

This commit is contained in:
cn 2020-12-06 13:18:48 +01:00
parent a6d6a14149
commit 5508b0dff3
3 changed files with 38 additions and 18 deletions

View File

@ -21,9 +21,6 @@ import (
const mqttConnectTimeout = 10 * time.Second const mqttConnectTimeout = 10 * time.Second
// program version, will be populated on build
var version string
var ( var (
scanTimeout = flag.Duration("scantimeout", 10*time.Second, "timeout after that a scan per peripheral will be aborted") scanTimeout = flag.Duration("scantimeout", 10*time.Second, "timeout after that a scan per peripheral will be aborted")
readRetries = flag.Int("readretries", 2, "number of times reading will be attempted per peripheral") readRetries = flag.Int("readretries", 2, "number of times reading will be attempted per peripheral")
@ -94,21 +91,14 @@ func checkTooShortInterval() error {
numPeripherals := int64(len(flag.Args())) numPeripherals := int64(len(flag.Args()))
numReadRetries := int64(*readRetries) numReadRetries := int64(*readRetries)
if (*scanTimeout).Nanoseconds()*numReadRetries*numPeripherals >= (*interval).Nanoseconds() { if (*scanTimeout).Nanoseconds()*numReadRetries*numPeripherals >= (*interval).Nanoseconds() {
return errors.New(fmt.Sprintf( return errors.Errorf(
"The interval of %s is too short given the scan timeout of %s for %d peripheral(s) with %d retries each! Exiting...\n", "The interval of %s is too short given the scan timeout of %s "+
*interval, *scanTimeout, numPeripherals, *readRetries)) "for %d peripheral(s) with %d retries each! Exiting...\n",
*interval, *scanTimeout, numPeripherals, *readRetries)
} }
return nil return nil
} }
func getVersion() string {
if version == "" {
return "dev"
} else {
return version
}
}
func getMQTTOptions() *mqtt.ClientOptions { func getMQTTOptions() *mqtt.ClientOptions {
if *brokerUseTLS { if *brokerUseTLS {
return mqtt.NewClientOptions(). return mqtt.NewClientOptions().
@ -156,7 +146,7 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error {
foundAdvertisementChannel := make(chan ble.Advertisement, 1) foundAdvertisementChannel := make(chan ble.Advertisement, 1)
filter := func(adv ble.Advertisement) bool { filter := func(adv ble.Advertisement) bool {
if strings.ToUpper(adv.Addr().String()) == strings.ToUpper(peripheral.id) { if strings.EqualFold(adv.Addr().String(), peripheral.id) {
foundAdvertisementChannel <- adv foundAdvertisementChannel <- adv
return true return true
} }
@ -194,7 +184,7 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error {
timeReadoutTook := time.Since(timeReadoutStart).Seconds() timeReadoutTook := time.Since(timeReadoutStart).Seconds()
client.CancelConnection() err3 := client.CancelConnection()
<-done <-done
@ -202,6 +192,10 @@ func connectPeripheral(peripheral *peripheral, send chan mifloraMetric) error {
return errors.Wrap(err2, "can't read data") return errors.Wrap(err2, "can't read data")
} }
if err3 != nil {
return errors.Wrap(err3, "can't disconnect after reading data")
}
send <- mifloraDataMetric{ send <- mifloraDataMetric{
peripheralId: common.MifloraGetAlphaNumericID(peripheral.id), peripheralId: common.MifloraGetAlphaNumericID(peripheral.id),
sensorData: sensorData, sensorData: sensorData,
@ -253,7 +247,8 @@ func readAllPeripherals(quit chan struct{}, send chan mifloraMetric) {
func main() { func main() {
flag.Parse() flag.Parse()
if len(flag.Args()) < 1 { if len(flag.Args()) < 1 {
fmt.Fprintf(os.Stderr, "Usage: %s [options] peripheral-id [peripheral-ids...] \n", os.Args[0]) fmt.Fprintf(os.Stderr,
"Usage: %s [options] peripheral-id [peripheral-ids...] \n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)
} }
@ -270,7 +265,8 @@ func main() {
case "influx": case "influx":
format = influxFormat format = influxFormat
default: default:
fmt.Fprintf(os.Stderr, "Unrecognized publish format %s! Exiting...\n", *publishFormatFlag) fmt.Fprintf(os.Stderr, "Unrecognized publish format %s! Exiting...\n",
*publishFormatFlag)
os.Exit(1) os.Exit(1)
} }

12
cmd/miflorad/version.go Normal file
View File

@ -0,0 +1,12 @@
package main
// program version, will be populated on build
var version string
func getVersion() string {
if version == "" {
return "dev"
} else {
return version
}
}

12
common/misc_test.go Normal file
View File

@ -0,0 +1,12 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessage(t *testing.T) {
assert.Equal(t, "", MifloraGetAlphaNumericID(""))
assert.Equal(t, "1234567890ab", MifloraGetAlphaNumericID("12:34:56:78:90:ab"))
}