From 9e73961af997511678aeade1c0717cb42cdba650 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 5 Nov 2022 20:33:30 -0700 Subject: [PATCH] Remove check for unchanged values and refactor code --- infinitime.go | 34 ++++++++--------- navigation.go | 100 ++++++++++++++++++++------------------------------ 2 files changed, 55 insertions(+), 79 deletions(-) diff --git a/infinitime.go b/infinitime.go index e234949..a042483 100644 --- a/infinitime.go +++ b/infinitime.go @@ -41,21 +41,21 @@ const ( ) var charNames = map[string]string{ - NewAlertChar: "New Alert", - NotifEventChar: "Notification Event", - StepCountChar: "Step Count", - MotionValChar: "Motion Values", - FirmwareVerChar: "Firmware Version", - CurrentTimeChar: "Current Time", - BatteryLvlChar: "Battery Level", - HeartRateChar: "Heart Rate", - FSTransferChar: "Filesystem Transfer", - FSVersionChar: "Filesystem Version", - WeatherDataChar: "Weather Data", - NavFlagsChar: "Navigation Icon", - NavNarrativeChar:"Navigation Instruction", - NavManDistChar: "Navigation Distance to next event", - NavProgressChar: "Navigation Progress", + NewAlertChar: "New Alert", + NotifEventChar: "Notification Event", + StepCountChar: "Step Count", + MotionValChar: "Motion Values", + FirmwareVerChar: "Firmware Version", + CurrentTimeChar: "Current Time", + BatteryLvlChar: "Battery Level", + HeartRateChar: "Heart Rate", + FSTransferChar: "Filesystem Transfer", + FSVersionChar: "Filesystem Version", + WeatherDataChar: "Weather Data", + NavFlagsChar: "Navigation Icon", + NavNarrativeChar: "Navigation Instruction", + NavManDistChar: "Navigation Distance to next event", + NavProgressChar: "Navigation Progress", } type Device struct { @@ -75,9 +75,8 @@ type Device struct { notifEventCh chan uint8 notifEventDone bool Music MusicCtrl - Navigation NavigationCtrl + Navigation NavigationService DFU DFU - navigationEv NavigationEvent } var ( @@ -738,7 +737,6 @@ func (i *Device) Notify(title, body string) error { ) } - // These constants represent the possible call statuses selected by the user const ( CallStatusDeclined uint8 = iota diff --git a/navigation.go b/navigation.go index 16e7da4..faecd50 100644 --- a/navigation.go +++ b/navigation.go @@ -1,27 +1,26 @@ package infinitime import ( - "bytes" - "encoding/binary" "errors" "github.com/muka/go-bluetooth/bluez/profile/gatt" ) const ( - NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0" - NavNarrativeChar= "00010002-78fc-48fe-8e23-433b3a1942d0" - NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" - NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" + NavFlagsChar = "00010001-78fc-48fe-8e23-433b3a1942d0" + NavNarrativeChar = "00010002-78fc-48fe-8e23-433b3a1942d0" + NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" + NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" ) -type NavigationCtrl struct { - flagsChar *gatt.GattCharacteristic1 - narrativeChar*gatt.GattCharacteristic1 - mandistChar *gatt.GattCharacteristic1 - progressChar *gatt.GattCharacteristic1 +type NavigationService struct { + flagsChar *gatt.GattCharacteristic1 + narrativeChar *gatt.GattCharacteristic1 + mandistChar *gatt.GattCharacteristic1 + progressChar *gatt.GattCharacteristic1 } type NavFlag string + const ( NavFlagArrive NavFlag = "arrive" NavFlagArriveLeft NavFlag = "arrive-left" @@ -117,16 +116,13 @@ const ( ) type NavigationEvent struct { - Flag NavFlag - Narrative string - Dist string - Progress uint8 + Flag NavFlag + Narrative string + Dist string + Progress uint8 } -var ( - ErrNavProgress = errors.New("progress needs to between 0 and 100") - ErrNavInvalidFlag = errors.New("this flag is invalid") -) +var ErrNavProgress = errors.New("progress needs to between 0 and 100") // Navigation sends a NavigationEvent to the watch func (i *Device) SetNavigation(ev NavigationEvent) error { @@ -134,54 +130,36 @@ func (i *Device) SetNavigation(ev NavigationEvent) error { return ErrNavProgress } - if ev.Flag != i.navigationEv.Flag { - log.Debug().Str("func", "Navigation"). - Msg("Sending flag") - if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil { - return err - } - if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil { - return err - } - i.navigationEv.Flag = ev.Flag + log.Debug().Str("func", "SetNavigation").Msg("Sending flag") + if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil { + return err + } + if err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil { + return err } - if ev.Narrative != i.navigationEv.Narrative { - log.Debug().Str("func", "Navigation"). - 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 - } - i.navigationEv.Narrative = ev.Narrative + 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 } - if ev.Dist != i.navigationEv.Dist { - log.Debug().Str("func", "Navigation"). - 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 - } - i.navigationEv.Dist = ev.Dist + 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 } - if ev.Progress != i.navigationEv.Progress { - log.Debug().Str("func", "Navigation"). - Msg("Sending progress") - if err := i.checkStatus(i.Navigation.progressChar, NavProgressChar); err != nil { - return err - } - buf := &bytes.Buffer{} - binary.Write(buf, binary.LittleEndian, ev.Progress) - if err := i.Navigation.progressChar.WriteValue(buf.Bytes(), nil); err != nil { - return err - } - i.navigationEv.Progress = ev.Progress + 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