Remove check for unchanged values and refactor code

This commit is contained in:
Elara 2022-11-05 20:33:30 -07:00
parent 0aa25353fd
commit 9e73961af9
2 changed files with 55 additions and 79 deletions

View File

@ -41,21 +41,21 @@ const (
) )
var charNames = map[string]string{ var charNames = map[string]string{
NewAlertChar: "New Alert", NewAlertChar: "New Alert",
NotifEventChar: "Notification Event", NotifEventChar: "Notification Event",
StepCountChar: "Step Count", StepCountChar: "Step Count",
MotionValChar: "Motion Values", MotionValChar: "Motion Values",
FirmwareVerChar: "Firmware Version", FirmwareVerChar: "Firmware Version",
CurrentTimeChar: "Current Time", CurrentTimeChar: "Current Time",
BatteryLvlChar: "Battery Level", BatteryLvlChar: "Battery Level",
HeartRateChar: "Heart Rate", HeartRateChar: "Heart Rate",
FSTransferChar: "Filesystem Transfer", FSTransferChar: "Filesystem Transfer",
FSVersionChar: "Filesystem Version", FSVersionChar: "Filesystem Version",
WeatherDataChar: "Weather Data", WeatherDataChar: "Weather Data",
NavFlagsChar: "Navigation Icon", NavFlagsChar: "Navigation Icon",
NavNarrativeChar:"Navigation Instruction", NavNarrativeChar: "Navigation Instruction",
NavManDistChar: "Navigation Distance to next event", NavManDistChar: "Navigation Distance to next event",
NavProgressChar: "Navigation Progress", NavProgressChar: "Navigation Progress",
} }
type Device struct { type Device struct {
@ -75,9 +75,8 @@ type Device struct {
notifEventCh chan uint8 notifEventCh chan uint8
notifEventDone bool notifEventDone bool
Music MusicCtrl Music MusicCtrl
Navigation NavigationCtrl Navigation NavigationService
DFU DFU DFU DFU
navigationEv NavigationEvent
} }
var ( var (
@ -738,7 +737,6 @@ func (i *Device) Notify(title, body string) error {
) )
} }
// These constants represent the possible call statuses selected by the user // These constants represent the possible call statuses selected by the user
const ( const (
CallStatusDeclined uint8 = iota CallStatusDeclined uint8 = iota

View File

@ -1,27 +1,26 @@
package infinitime package infinitime
import ( import (
"bytes"
"encoding/binary"
"errors" "errors"
"github.com/muka/go-bluetooth/bluez/profile/gatt" "github.com/muka/go-bluetooth/bluez/profile/gatt"
) )
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"
NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0" NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0"
NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0" NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0"
) )
type NavigationCtrl struct { type NavigationService struct {
flagsChar *gatt.GattCharacteristic1 flagsChar *gatt.GattCharacteristic1
narrativeChar*gatt.GattCharacteristic1 narrativeChar *gatt.GattCharacteristic1
mandistChar *gatt.GattCharacteristic1 mandistChar *gatt.GattCharacteristic1
progressChar *gatt.GattCharacteristic1 progressChar *gatt.GattCharacteristic1
} }
type NavFlag string type NavFlag string
const ( const (
NavFlagArrive NavFlag = "arrive" NavFlagArrive NavFlag = "arrive"
NavFlagArriveLeft NavFlag = "arrive-left" NavFlagArriveLeft NavFlag = "arrive-left"
@ -117,16 +116,13 @@ const (
) )
type NavigationEvent struct { type NavigationEvent struct {
Flag NavFlag Flag NavFlag
Narrative string Narrative string
Dist string Dist string
Progress uint8 Progress uint8
} }
var ( var ErrNavProgress = errors.New("progress needs to between 0 and 100")
ErrNavProgress = errors.New("progress needs to between 0 and 100")
ErrNavInvalidFlag = errors.New("this flag is invalid")
)
// Navigation sends a NavigationEvent to the watch // Navigation sends a NavigationEvent to the watch
func (i *Device) SetNavigation(ev NavigationEvent) error { func (i *Device) SetNavigation(ev NavigationEvent) error {
@ -134,54 +130,36 @@ func (i *Device) SetNavigation(ev NavigationEvent) error {
return ErrNavProgress return ErrNavProgress
} }
if ev.Flag != i.navigationEv.Flag { log.Debug().Str("func", "SetNavigation").Msg("Sending flag")
log.Debug().Str("func", "Navigation"). if err := i.checkStatus(i.Navigation.flagsChar, NavFlagsChar); err != nil {
Msg("Sending flag") return err
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 err := i.Navigation.flagsChar.WriteValue([]byte(ev.Flag), nil); err != nil {
return err
}
i.navigationEv.Flag = ev.Flag
} }
if ev.Narrative != i.navigationEv.Narrative { log.Debug().Str("func", "SetNavigation").Msg("Sending narrative")
log.Debug().Str("func", "Navigation"). if err := i.checkStatus(i.Navigation.narrativeChar, NavNarrativeChar); err != nil {
Msg("Sending narrative") return err
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 err := i.Navigation.narrativeChar.WriteValue([]byte(ev.Narrative), nil); err != nil {
return err
}
i.navigationEv.Narrative = ev.Narrative
} }
if ev.Dist != i.navigationEv.Dist { log.Debug().Str("func", "SetNavigation").Msg("Sending mandist")
log.Debug().Str("func", "Navigation"). if err := i.checkStatus(i.Navigation.mandistChar, NavManDistChar); err != nil {
Msg("Sending mandist") return err
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 err := i.Navigation.mandistChar.WriteValue([]byte(ev.Dist), nil); err != nil {
return err
}
i.navigationEv.Dist = ev.Dist
} }
if ev.Progress != i.navigationEv.Progress { log.Debug().Str("func", "SetNavigation").Msg("Sending progress")
log.Debug().Str("func", "Navigation"). if err := i.checkStatus(i.Navigation.progressChar, NavProgressChar); err != nil {
Msg("Sending progress") return err
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
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
} }
return nil return nil