You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
package endpoints
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"reflect"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/dustinpianalto/errors"
|
|
"github.com/dustinpianalto/weather"
|
|
"github.com/dustinpianalto/weather/pkg/services"
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
func AmbientHandler(w http.ResponseWriter, r *http.Request) {
|
|
const method errors.Method = "endpoints/AmbientHandler"
|
|
entry := &weather.AmbientEntry{}
|
|
decoder := schema.NewDecoder()
|
|
decoder.RegisterConverter(time.Time{}, timeConverter)
|
|
if err := decoder.Decode(entry, r.URL.Query()); err != nil {
|
|
log.Println(errors.E(method, errors.Malformed, "error decoding AmbientEntry", err))
|
|
return
|
|
}
|
|
if entry.MAC == "" && entry.PASSKEY != "" {
|
|
entry.MAC = entry.PASSKEY
|
|
}
|
|
entry, err := services.EventService.AddEvent(entry)
|
|
if err != nil {
|
|
log.Println(errors.E(method, errors.Internal, "error adding entry to database", err))
|
|
return
|
|
}
|
|
log.Printf("%#v\n\n", entry)
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte{})
|
|
}
|
|
|
|
func timeConverter(value string) reflect.Value {
|
|
if v, err := time.Parse("2006-01-02 15:04:05", value); err == nil {
|
|
return reflect.ValueOf(v)
|
|
} else if i, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
return reflect.ValueOf(time.Unix(i, 0))
|
|
}
|
|
return reflect.Value{}
|
|
}
|