From 31f4c51a6141d9a12c0cf98a7b1a0aaaacc6183e Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sun, 6 Nov 2022 20:08:13 -0800 Subject: [PATCH] Improve navigation API --- infinitime.go | 1 + navigation.go | 73 ++++++++++++++++++++------------------------------- 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/infinitime.go b/infinitime.go index a042483..10e9f25 100644 --- a/infinitime.go +++ b/infinitime.go @@ -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() diff --git a/navigation.go b/navigation.go index bc6dfd9..73d93b0 100644 --- a/navigation.go +++ b/navigation.go @@ -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) }