54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"otaupdate/services"
|
|
|
|
"github.com/fiskerinc/cloud-services/pkg/cache"
|
|
"github.com/fiskerinc/cloud-services/pkg/common"
|
|
"github.com/fiskerinc/cloud-services/pkg/utils"
|
|
"github.com/fiskerinc/cloud-services/pkg/validator"
|
|
"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
|
|
)
|
|
|
|
// HandleVehicleState godoc
|
|
// @Summary Get state of car
|
|
// @Description Returns the state of the car derived from CAN bus messages.
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param Authorization header string false "Bearer <ID token>"
|
|
// @Param Api-Key header string false "<API token>"
|
|
// @Param vin query string false "Car vin"
|
|
// @Success 200 {object} JSONCarStateMessage
|
|
// @Failure 400 {object} common.JSONError "Bad request"
|
|
// @Failure 401 {object} common.JSONError "Unauthorized"
|
|
// @Failure 503 {object} common.JSONError "Service unavailable"
|
|
// @Router /carstate [get]
|
|
func HandleVehicleState(w http.ResponseWriter, r *http.Request) {
|
|
vin := r.URL.Query().Get("vin")
|
|
if vin == "" {
|
|
loggerdataresp.BadDataErrorResp(w, ErrMissingVIN, http.StatusBadRequest)
|
|
return
|
|
}
|
|
ok := validator.ValidateVINSimple(vin)
|
|
if !ok {
|
|
loggerdataresp.BadDataErrorResp(w, ErrInvalidVIN, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientPool := services.RedisClientPool()
|
|
parser := cache.NewVehicleState(clientPool)
|
|
state, err := parser.Get(vin)
|
|
if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
|
|
return
|
|
}
|
|
|
|
utils.RespJSON(w, http.StatusOK, &JSONCarStateMessage{
|
|
Data: state,
|
|
})
|
|
}
|
|
|
|
type JSONCarStateMessage struct {
|
|
Data common.CarState `json:"data"`
|
|
}
|