71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"otaupdate/services"
|
|
|
|
"github.com/fiskerinc/cloud-services/pkg/common"
|
|
"github.com/fiskerinc/cloud-services/pkg/redis"
|
|
"github.com/fiskerinc/cloud-services/pkg/utils"
|
|
"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
|
|
)
|
|
|
|
// HandleVehiclesLocations godoc
|
|
// @Summary Get locations of cars
|
|
// @Description Returns car locations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string false "Bearer <ID token>"
|
|
// @Param Api-Key header string false "<API token>"
|
|
// @Param order query string false "Sort on column with asc or desc"
|
|
// @Success 200 {object} JSONCarLocations
|
|
// @Failure 400 {object} common.JSONError "Bad request"
|
|
// @Failure 401 {object} common.JSONError "Unauthorized"
|
|
// @Failure 503 {object} common.JSONError "Service unavailable"
|
|
// @Router /carslocations [get]
|
|
func HandleVehiclesLocations(w http.ResponseWriter, r *http.Request) {
|
|
conn := services.RedisClientPool().GetFromPool()
|
|
defer conn.Close()
|
|
|
|
data, err := conn.GetObjectRaw(redis.CarLocationsKey())
|
|
if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
|
|
return
|
|
}
|
|
|
|
locations := make([]JSONCarLocation, len(data))
|
|
i := 0
|
|
for key, value := range data {
|
|
var location common.Location
|
|
err = location.Unmarshal(value)
|
|
if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
|
|
return
|
|
}
|
|
|
|
locations[i] = JSONCarLocation{
|
|
Location: common.Location{
|
|
Altitude: location.Altitude,
|
|
Longitude: location.Longitude,
|
|
Latitude: location.Latitude,
|
|
},
|
|
VIN: key,
|
|
}
|
|
i++
|
|
}
|
|
|
|
utils.RespJSON(w, http.StatusOK, JSONCarLocations{
|
|
Data: locations,
|
|
Total: len(locations),
|
|
})
|
|
}
|
|
|
|
// JSONCarsResult wraps query in json
|
|
type JSONCarLocations struct {
|
|
Data []JSONCarLocation `json:"data"`
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
type JSONCarLocation struct {
|
|
common.Location
|
|
VIN string `json:"vin"`
|
|
}
|