Added Navigation service #5

Merged
Elara6331 merged 9 commits from yannickulrich/infinitime:navigation-service into master 2022-11-03 19:09:07 +00:00
1 changed files with 53 additions and 0 deletions
Showing only changes of commit e6a1baa32b - Show all commits

View File

@ -62,6 +62,41 @@ var charNames = map[string]string{
NavProgressChar: "Navigation Progress",
}
var NavFlagNames = []string{
yannickulrich marked this conversation as resolved Outdated

I think it would be better if these were individual constants rather than a slice. That way, they could have their own type and the compiler can guarantee that they're valid instead of needing a separate function. Since that will add quite a bit of code, I think the navigation service can be moved into a separate file.

I think it would be better if these were individual constants rather than a slice. That way, they could have their own type and the compiler can guarantee that they're valid instead of needing a separate function. Since that will add quite a bit of code, I think the navigation service can be moved into a separate file.

Maybe something like

type NavFlag string

const (
    NavFlagArrive     NavFlag = "arrive"
    NavFlagArriveLeft NavFlag = "arrive-left"
    // ...
)

Then, the NavigationEvent can take a NavFlag instead of string.

Maybe something like ```go type NavFlag string const ( NavFlagArrive NavFlag = "arrive" NavFlagArriveLeft NavFlag = "arrive-left" // ... ) ``` Then, the `NavigationEvent` can take a `NavFlag` instead of `string`.

Taken care of in d46f545

Taken care of in d46f545
"arrive", "arrive-left", "arrive-right", "arrive-straight",
"close",
"continue", "continue-left", "continue-right", "continue-slight-left",
"continue-slight-right", "continue-straight", "continue-uturn",
"depart", "depart-left", "depart-right", "depart-straight",
"end-of-road-left", "end-of-road-right",
"ferry", "flag",
"fork", "fork-left", "fork-right", "fork-straight",
"fork-slight-left", "fork-slight-right",
"invalid", "invalid-left", "invalid-right",
"invalid-slight-left", "invalid-slight-right",
"invalid-straight", "invalid-uturn",
"merge-left", "merge-right", "merge-slight-left",
"merge-slight-right", "merge-straight",
"new-name-left", "new-name-right", "new-name-sharp-left", "new-name-sharp-right",
"new-name-slight-left", "new-name-slight-right", "new-name-straight",
"notification-left", "notification-right",
"notification-sharp-left", "notification-sharp-right",
"notification-slight-left", "notification-slight-right",
"notification-straight",
"off-ramp-left", "off-ramp-right", "off-ramp-sharp-left", "off-ramp-sharp-right",
"off-ramp-slight-left", "off-ramp-slight-right", "off-ramp-straight",
"on-ramp-left", "on-ramp-right", "on-ramp-sharp-left", "on-ramp-sharp-right",
"on-ramp-slight-left", "on-ramp-slight-right", "on-ramp-straight",
"rotary", "rotary-left", "rotary-right", "rotary-sharp-left", "rotary-sharp-right",
"rotary-slight-left", "rotary-slight-right", "rotary-straight",
"roundabout", "roundabout-left", "roundabout-right",
"roundabout-sharp-left", "roundabout-sharp-right",
"roundabout-slight-left", "roundabout-slight-right", "roundabout-straight",
"turn-left", "turn-right", "turn-sharp-left", "turn-sharp-right",
"turn-slight-left", "turn-slight-right", "turn-stright",
"updown", "uturn",
}
type NavigationEvent struct {
flag string
narrative string
@ -99,6 +134,8 @@ var (
ErrNotConnected = errors.New("not connected")
ErrNoTimelineHeader = errors.New("events must contain the timeline header")
ErrPairTimeout = errors.New("reached timeout while pairing")
ErrNavProgress = errors.New("progress needs to between 0 and 100")
ErrNavInvalidFlag = errors.New("this flag is invalid")
)
type ErrCharNotAvail struct {
@ -751,8 +788,24 @@ func (i *Device) Notify(title, body string) error {
)
}
func CheckValidNavFlag(flag string) error {
for _, v := range NavFlagNames {
if v == flag {
return nil
}
}
return ErrNavInvalidFlag
}
// Navigation sends a NavigationEvent to the watch
func (i *Device) Navigation(flag string, narrative string, dist string, progress uint8) error {
yannickulrich marked this conversation as resolved Outdated

I'd prefer if this accepted a NavigationEvent rather than individual arguments. That also means all the fields in NavigationEvent should be capitalized to export them.

I'd prefer if this accepted a `NavigationEvent` rather than individual arguments. That also means all the fields in `NavigationEvent` should be capitalized to export them.

Sure! See c3a8727 but also my comment above

Sure! See c3a8727 but also my comment [above](https://gitea.arsenm.dev/Arsen6331/infinitime/pulls/5#issuecomment-322)
if progress > 100 {
return ErrNavProgress
}
if err := CheckValidNavFlag(flag); err != nil {
return err
}
if flag != i.navigationEv.flag {
log.Debug().Str("func", "Navigation").
Msg("Sending flag")