Improve navigation API

This commit is contained in:
Arsen Musayelyan 2022-11-06 20:08:13 -08:00
parent 8e5bbafba8
commit 31f4c51a61
2 changed files with 30 additions and 44 deletions

View File

@ -136,6 +136,7 @@ func Connect(ctx context.Context, opts *Options) (*Device, error) {
// Create new device
out := &Device{device: btDev}
out.Navigation = NavigationService{dev: out}
// Resolve characteristics
err = out.resolveChars()

View File

@ -5,6 +5,8 @@ import (
"github.com/muka/go-bluetooth/bluez/profile/gatt"
)
var ErrNavProgress = errors.New("progress needs to be between 0 and 100")
const (
NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0"
NavNarrativeChar = "00010002-78fc-48fe-8e23-433b3a1942d0"
@ -13,6 +15,7 @@ const (
)
type NavigationService struct {
dev *Device
flagsChar *gatt.GattCharacteristic1
narrativeChar *gatt.GattCharacteristic1
mandistChar *gatt.GattCharacteristic1
@ -115,52 +118,34 @@ const (
NavFlagUturn NavFlag = "uturn"
)
type NavigationEvent struct {
Flag NavFlag
Narrative string
Dist string
Progress uint8
func (n *NavigationService) SetFlag(flag string) error {
log.Debug().Str("func", "SetFlag").Msg("Sending flag")
if err := n.dev.checkStatus(n.flagsChar, NavFlagsChar); err != nil {
return err
}
return n.flagsChar.WriteValue([]byte(flag), nil)
}
var ErrNavProgress = errors.New("progress needs to be between 0 and 100")
// Navigation sends a NavigationEvent to the watch
func (i *Device) SetNavigation(ev NavigationEvent) error {
if ev.Progress > 100 {
return ErrNavProgress
}
log.Debug().Str("func", "SetNavigation").Msg("Sending flag")
if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil {
func (n *NavigationService) SetNarrative(narrative string) error {
log.Debug().Str("func", "SetNarrative").Msg("Sending narrative")
if err := n.dev.checkStatus(n.narrativeChar, NavNarrativeChar); err != nil {
return err
}
if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil {
return err
}
log.Debug().Str("func", "SetNavigation").Msg("Sending narrative")
if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != nil {
return err
}
if err := i.Navigation.narrativeChar.WriteValue([]byte(ev.Narrative), nil); err != nil {
return err
}
log.Debug().Str("func", "SetNavigation").Msg("Sending mandist")
if err := i.checkStatus(i.Navigation.mandistChar, NavManDistChar); err != nil {
return err
}
if err := i.Navigation.mandistChar.WriteValue([]byte(ev.Dist), nil); err != nil {
return err
}
log.Debug().Str("func", "SetNavigation").Msg("Sending progress")
if err := i.checkStatus(i.Navigation.progressChar, NavProgressChar); err != nil {
return err
}
if err := i.Navigation.progressChar.WriteValue([]byte{ev.Progress}, nil); err != nil {
return err
}
return nil
return n.narrativeChar.WriteValue([]byte(narrative), nil)
}
func (n *NavigationService) SetManDist(manDist string) error {
log.Debug().Str("func", "SetNarrative").Msg("Sending maneuver distance")
if err := n.dev.checkStatus(n.mandistChar, NavManDistChar); err != nil {
return err
}
return n.mandistChar.WriteValue([]byte(manDist), nil)
}
func (n *NavigationService) SetProgress(progress uint8) error {
log.Debug().Str("func", "SetNarrative").Msg("Sending progress")
if err := n.dev.checkStatus(n.progressChar, NavProgressChar); err != nil {
return err
}
return n.progressChar.WriteValue([]byte{progress}, nil)
}