Improve navigation API
This commit is contained in:
parent
70ab2e9f03
commit
4479cad6f9
@ -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()
|
||||
|
@ -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
|
||||
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
|
||||
}
|
||||
return n.narrativeChar.WriteValue([]byte(narrative), nil)
|
||||
}
|
||||
|
||||
log.Debug().Str("func", "SetNavigation").Msg("Sending flag")
|
||||
if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != 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
|
||||
}
|
||||
if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil {
|
||||
return err
|
||||
return n.mandistChar.WriteValue([]byte(manDist), nil)
|
||||
}
|
||||
|
||||
log.Debug().Str("func", "SetNavigation").Msg("Sending narrative")
|
||||
if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != 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
|
||||
}
|
||||
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.progressChar.WriteValue([]byte{progress}, nil)
|
||||
}
|
||||
|
Reference in New Issue
Block a user