Skip to content

Commit

Permalink
Add simple location endpoint
Browse files Browse the repository at this point in the history
Addresses #41
  • Loading branch information
schollz committed Mar 27, 2018
1 parent 62b9a8b commit f2e6c94
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,37 @@ GET /api/v1/by_location/FAMILY
```
>>


 

> ### Get simple location of a single device {location-basic}
> **Request**
```
GET /api/v1/location_basic/FAMILY/DEVICE
```
>
> **Response**
>
> This is a much simplified response for use with embedded evices. The `data` has the latest location (`loc`) and probability (`p`) for the specified deivce.
>
> Additionaly it specifies how long ago the device was last seen at that location, in seaconds (`seen`).
>
> Example:
>
```
{
"data":{
"loc":"zakhome floor 2 office",
"prob":0.97,
"seen":1387
},
"message":"ok",
"success":true
}
```
>>
## API requests?

If you have API requests, please [file an idea on Github](https://github.com/schollz/find3/issues/new?title=Feature:%20).
Expand Down
41 changes: 41 additions & 0 deletions server/main/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func Run() (err error) {
r.GET("/api/v1/devices/*family", handlerApiV1Devices)
r.GET("/api/v1/location/:family/*device", handlerApiV1Location)
r.GET("/api/v1/locations/:family", handlerApiV1Locations)
r.GET("/api/v1/location_basic/:family/*device", handlerApiV1LocationSimple)
r.GET("/api/v1/by_location/:family", handlerApiV1ByLocation)
r.GET("/api/v1/calibrate/*family", handlerApiV1Calibrate)
r.POST("/api/v1/settings/passive", handlerReverseSettings)
Expand Down Expand Up @@ -490,6 +491,46 @@ func handlerApiV1Location(c *gin.Context) {
}
}

func handlerApiV1LocationSimple(c *gin.Context) {
s, analysis, err := func(c *gin.Context) (s models.SensorData, analysis models.LocationAnalysis, err error) {
family := strings.TrimSpace(c.Param("family"))
device := strings.TrimSpace(c.Param("device")[1:])

d, err := database.Open(family, true)
if err != nil {
return
}
s, err = d.GetLatest(device)
d.Close()
if err != nil {
return
}
analysis, err = api.AnalyzeSensorData(s)
if err != nil {
err = api.Calibrate(family, true)
if err != nil {
logger.Log.Warn(err)
return
}
}
return
}(c)
if err != nil {
c.JSON(http.StatusOK, gin.H{"message": err.Error(), "success": err == nil})
} else {
simpleLocation := struct {
Location string `json:"loc"`
Probability float64 `json:"prob"`
LastSeenTimeAgo int64 `json:"seen"`
}{
Location: analysis.Guesses[0].Location,
Probability: analysis.Guesses[0].Probability,
LastSeenTimeAgo: time.Now().UTC().UnixNano()/int64(time.Second) - (s.Timestamp / 1000),
}
c.JSON(http.StatusOK, gin.H{"message": "ok", "success": err == nil, "data": simpleLocation})
}
}

func handlerApiV1Calibrate(c *gin.Context) {
family := strings.TrimSpace(c.Param("family")[1:])
var err error
Expand Down

0 comments on commit f2e6c94

Please sign in to comment.