Improve navigation API

This commit is contained in:
Elara Musayelyan 2022-11-06 20:08:13 -08:00
parent 70ab2e9f03
commit 4479cad6f9
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 // Create new device
out := &Device{device: btDev} out := &Device{device: btDev}
out.Navigation = NavigationService{dev: out}
// Resolve characteristics // Resolve characteristics
err = out.resolveChars() err = out.resolveChars()

View file

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