Improve navigation API

This commit is contained in:
Elara 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 // 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 { return err
if ev.Progress > 100 { }
return ErrNavProgress return n.narrativeChar.WriteValue([]byte(narrative), nil)
} }
log.Debug().Str("func", "SetNavigation").Msg("Sending flag") func (n *NavigationService) SetManDist(manDist string) error {
if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil { log.Debug().Str("func", "SetNarrative").Msg("Sending maneuver distance")
if err := n.dev.checkStatus(n.mandistChar, NavManDistChar); err != nil {
return err return err
} }
if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil { return n.mandistChar.WriteValue([]byte(manDist), nil)
return err
} }
log.Debug().Str("func", "SetNavigation").Msg("Sending narrative") func (n *NavigationService) SetProgress(progress uint8) error {
if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != nil { log.Debug().Str("func", "SetNarrative").Msg("Sending progress")
if err := n.dev.checkStatus(n.progressChar, NavProgressChar); err != nil {
return err return err
} }
if err := i.Navigation.narrativeChar.WriteValue([]byte(ev.Narrative), nil); err != nil { return n.progressChar.WriteValue([]byte{progress}, 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
} }