From 165db63debdb7bd12e36a5176606204bf30077ba Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 16 Apr 2022 04:24:21 -0700 Subject: [PATCH] Log when too many disconnects occur rather than removing the device (Arsen6331/itd#10) --- infinitime.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/infinitime.go b/infinitime.go index a90fb2b..265f604 100644 --- a/infinitime.go +++ b/infinitime.go @@ -14,6 +14,7 @@ import ( "github.com/muka/go-bluetooth/bluez/profile/adapter" "github.com/muka/go-bluetooth/bluez/profile/device" "github.com/muka/go-bluetooth/bluez/profile/gatt" + "github.com/rs/zerolog" "go.arsenm.dev/infinitime/blefs" ) @@ -67,11 +68,15 @@ type Options struct { Whitelist []string OnReqPasskey func() (uint32, error) OnReconnect func() + Logger zerolog.Logger + LogLevel zerolog.Level } var DefaultOptions = &Options{ AttemptReconnect: true, WhitelistEnabled: false, + Logger: zerolog.Nop(), + LogLevel: zerolog.Disabled, } // Connect will attempt to connect to a @@ -239,10 +244,10 @@ func reconnect(opts *Options, dev *device.Device1) { amtDisconnects = 0 } - // If less than 3 seconds have past and more than 6 + // If less than 3 seconds have passed and more than 6 // disconnects have occurred, remove the device and reset if secsSince <= 3 && amtDisconnects >= 6 { - defaultAdapter.RemoveDevice(dev.Path()) + opts.Logger.Warn().Msg("At least 6 disconnects have occurred in the last three seconds. If this continues, try removing the InfiniTime device from bluetooth.") lastDisconnect = time.Unix(0, 0) amtDisconnects = 0 } @@ -254,7 +259,7 @@ func reconnect(opts *Options, dev *device.Device1) { for i := 0; i < 6; i++ { // If three tries failed, remove device if i == 3 { - defaultAdapter.RemoveDevice(dev.Path()) + opts.Logger.Warn().Msg("Multiple connection attempts have failed. If this continues, try removing the InfiniTime device from bluetooth.") } // Connect to device newDev, err := connect(opts, false)