Remove check for unchanged values and refactor code

This commit is contained in:
Elara 2022-11-05 20:33:30 -07:00
parent 1f5301f5de
commit e6fb402c0c
2 changed files with 55 additions and 79 deletions

View File

@ -53,7 +53,7 @@ var charNames = map[string]string{
FSVersionChar: "Filesystem Version",
WeatherDataChar: "Weather Data",
NavFlagsChar: "Navigation Icon",
NavNarrativeChar:"Navigation Instruction",
NavNarrativeChar: "Navigation Instruction",
NavManDistChar: "Navigation Distance to next event",
NavProgressChar: "Navigation Progress",
}
@ -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

View File

@ -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"
NavNarrativeChar = "00010002-78fc-48fe-8e23-433b3a1942d0"
NavManDistChar = "00010003-78fc-48fe-8e23-433b3a1942d0"
NavProgressChar = "00010004-78fc-48fe-8e23-433b3a1942d0"
)
type NavigationCtrl struct {
type NavigationService struct {
flagsChar *gatt.GattCharacteristic1
narrativeChar*gatt.GattCharacteristic1
narrativeChar *gatt.GattCharacteristic1
mandistChar *gatt.GattCharacteristic1
progressChar *gatt.GattCharacteristic1
}
type NavFlag string
const (
NavFlagArrive NavFlag = "arrive"
NavFlagArriveLeft NavFlag = "arrive-left"
@ -123,10 +122,7 @@ type NavigationEvent struct {
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,55 +130,37 @@ func (i *Device) SetNavigation(ev NavigationEvent) error {
return ErrNavProgress
}
if ev.Flag != i.navigationEv.Flag {
log.Debug().Str("func", "Navigation").
Msg("Sending 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
}
i.navigationEv.Flag = ev.Flag
}
if ev.Narrative != i.navigationEv.Narrative {
log.Debug().Str("func", "Navigation").
Msg("Sending 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
}
i.navigationEv.Narrative = ev.Narrative
}
if ev.Dist != i.navigationEv.Dist {
log.Debug().Str("func", "Navigation").
Msg("Sending mandist")
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
}
i.navigationEv.Dist = ev.Dist
}
if ev.Progress != i.navigationEv.Progress {
log.Debug().Str("func", "Navigation").
Msg("Sending progress")
log.Debug().Str("func", "SetNavigation").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 {
if err := i.Navigation.progressChar.WriteValue([]byte{ev.Progress}, nil); err != nil {
return err
}
i.navigationEv.Progress = ev.Progress
}
return nil
}