diff --git a/go.mod b/go.mod index 3f701c1..97af23c 100644 --- a/go.mod +++ b/go.mod @@ -1,26 +1,26 @@ module github.com/fako1024/btscale -go 1.18 +go 1.20 require ( github.com/fako1024/gatt v1.0.4 github.com/fatih/stopwatch v1.0.0 - github.com/gofiber/fiber/v2 v2.51.0 - go.uber.org/zap v1.26.0 + github.com/gofiber/fiber/v2 v2.52.5 + go.uber.org/zap v1.27.0 ) require ( - github.com/andybalholm/brotli v1.0.6 // indirect - github.com/google/uuid v1.5.0 // indirect - github.com/klauspost/compress v1.17.4 // indirect + github.com/andybalholm/brotli v1.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/klauspost/compress v1.17.9 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-pointer v0.0.1 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/rivo/uniseg v0.4.4 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.51.0 // indirect + github.com/valyala/fasthttp v1.55.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.22.0 // indirect ) diff --git a/go.sum b/go.sum index d35843c..a3799f8 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,16 @@ -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/fako1024/gatt v1.0.4 h1:5euK7RK4nhaHYgg4v1iS53zxWK+lGRBeZjQFBxtaUYs= github.com/fako1024/gatt v1.0.4/go.mod h1:TTf+fxGvaVhUZJWD9h+MMhjxsbWRBTtNC77jrCL0HtU= github.com/fatih/stopwatch v1.0.0 h1:sTac5Q8e+Ql27wjVze0rOHWVQqkKmhSlPmQbNlAsZz0= github.com/fatih/stopwatch v1.0.0/go.mod h1:OJI5FjXD3U1Y019APt3bVM7Fm94Ambcu2RTXh5FUVMs= -github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= -github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= -github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= -github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= +github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -18,26 +18,26 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0= github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= -github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8= +github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pkg/felicita/felicita.go b/pkg/felicita/felicita.go index 7090279..70355de 100644 --- a/pkg/felicita/felicita.go +++ b/pkg/felicita/felicita.go @@ -42,8 +42,10 @@ type Felicita struct { timer *stopwatch.Stopwatch - deviceID string - deviceName string + deviceID string + deviceName string + forceBuzzerSettingOnConnect BuzzerSetting + hasReceivedData bool stateChangeHandler func(status scale.ConnectionStatus) stateChangeChan chan scale.ConnectionStatus @@ -254,7 +256,7 @@ func (f *Felicita) ElapsedTime() time.Duration { func (f *Felicita) Close() error { close(f.doneChan) - f.btDevice.StopScanning() + _ = f.btDevice.StopScanning() return f.btDevice.RemoveAllServices() } @@ -354,7 +356,7 @@ func (f *Felicita) onPeriphConnected(p gatt.Peripheral, connErr error) { f.setStatus(scale.StateConnected, nil) defer func() { - p.Device().CancelConnection(p) + _ = p.Device().CancelConnection(p) f.setStatus(scale.StateDisconnected, connErr) }() @@ -405,7 +407,7 @@ func (f *Felicita) onPeriphConnected(p gatt.Peripheral, connErr error) { f.logger.Debugf("released peripheral `%s/%s`", p.Name(), p.ID()) } -func (f *Felicita) onPeriphDisconnected(p gatt.Peripheral, err error) { +func (f *Felicita) onPeriphDisconnected(p gatt.Peripheral, _ error) { if !f.thisDevice(p) { return @@ -437,7 +439,7 @@ func (f *Felicita) disconnect() { } } -func (f *Felicita) receiveData(c *gatt.Characteristic, req []byte, err error) { +func (f *Felicita) receiveData(_ *gatt.Characteristic, req []byte, err error) { if err != nil || len(req) != 18 { return @@ -456,6 +458,11 @@ func (f *Felicita) receiveData(c *gatt.Characteristic, req []byte, err error) { f.isBuzzingOnTouch = parseSignalFlag(req[14]) f.unit = dataPoint.Unit + // Upon first data reception, check if the Buzzer is configured as expected and + // attempt to force the setting if not (unles not configured) + f.forceBuzzerSetting() + f.hasReceivedData = true + // Call handler function, if any if f.dataHandler != nil { f.dataHandler(dataPoint) @@ -495,6 +502,17 @@ func (f *Felicita) waitForBuzzer(targetState bool) error { return fmt.Errorf("target buzzer state %v was not reached within %v", targetState, time.Duration(btSettleRetries)*btSettleDelay) } +func (f *Felicita) forceBuzzerSetting() { + if !f.hasReceivedData && f.forceBuzzerSettingOnConnect != "" { + if f.isBuzzingOnTouch && f.forceBuzzerSettingOnConnect == BuzzerSettingOff || + !f.isBuzzingOnTouch && f.forceBuzzerSettingOnConnect == BuzzerSettingOn { + if err := f.ToggleBuzzingOnTouch(); err != nil { + f.logger.Warnf("failed to force buzzer setting to `%s`: %s", f.forceBuzzerSettingOnConnect, err) + } + } + } +} + //////////////////////////////////////////////////////////////////////////////// func parseUnit(data []byte) scale.Unit { diff --git a/pkg/felicita/options.go b/pkg/felicita/options.go index c18d3fa..a224d65 100644 --- a/pkg/felicita/options.go +++ b/pkg/felicita/options.go @@ -5,6 +5,18 @@ import ( "github.com/fako1024/gatt" ) +// BuzzerSetting denotes the basic Buzzer (on touch) setting of the scale +type BuzzerSetting string + +const ( + + // BuzzerSettingOn denotes that the basic Buzzer (on touch) setting is on + BuzzerSettingOn = "ON" + + // BuzzerSettingOff denotes that the basic Buzzer (on touch) setting is off + BuzzerSettingOff = "OFF" +) + // WithDeviceID sets the Bluetooth device ID func WithDeviceID(deviceID string) func(*Felicita) { return func(f *Felicita) { @@ -32,3 +44,11 @@ func WithLogger(logger scale.Logger) func(*Felicita) { f.logger = logger } } + +// WithForceBuzzerSettingOnConnect ensures that the basic Buzzer (on touch) setting +// is correct upon connection +func WithForceBuzzerSettingOnConnect(setting BuzzerSetting) func(*Felicita) { + return func(f *Felicita) { + f.forceBuzzerSettingOnConnect = setting + } +}