POST /api/v1/location/{arrive,leave} records place intervals (home or a
named place, and away in between) from Shortcuts automations. A stale
"leave" for a place the device has already left is ignored. The Health
Auto Export endpoint and its normalizer are removed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package ingest
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Location transitions come from phone geofence automations (e.g. iOS
|
|
// Shortcuts "Arrive"/"Leave"), which can only report the moment of crossing.
|
|
// The store turns them into back-to-back intervals with kind "place",
|
|
// labelled by the place, or Away between places.
|
|
const (
|
|
LocationSource = "location"
|
|
PlaceKind = "place"
|
|
Away = "away"
|
|
DefaultPlace = "home"
|
|
)
|
|
|
|
// LocationEvent is the optional body of POST /location/{arrive,leave}. An
|
|
// empty body means "home, now".
|
|
type LocationEvent struct {
|
|
Place string `json:"place"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
// NormalizeLocation fills defaults and validates a transition.
|
|
func NormalizeLocation(e LocationEvent, now time.Time) (LocationEvent, error) {
|
|
e.Place = strings.ToLower(strings.TrimSpace(e.Place))
|
|
if e.Place == "" {
|
|
e.Place = DefaultPlace
|
|
}
|
|
if e.At.IsZero() {
|
|
e.At = now
|
|
}
|
|
e.At = e.At.UTC()
|
|
switch {
|
|
case e.Place == Away:
|
|
return e, fmt.Errorf("%q is reserved for time between places", Away)
|
|
case len(e.Place) > maxLabelLength:
|
|
return e, fmt.Errorf("place longer than %d characters", maxLabelLength)
|
|
case e.At.After(now.Add(maxFutureSkew)):
|
|
return e, fmt.Errorf("at is in the future")
|
|
case e.At.Before(now.Add(-maxDuration)):
|
|
return e, fmt.Errorf("at is more than %s ago", maxDuration)
|
|
}
|
|
return e, nil
|
|
}
|