feat(controller): set device alias
This commit is contained in:
parent
2918c57125
commit
641aae883b
|
|
@ -2,10 +2,13 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/leandrofars/oktopus/internal/bridge"
|
||||||
|
local "github.com/leandrofars/oktopus/internal/nats"
|
||||||
"github.com/leandrofars/oktopus/internal/utils"
|
"github.com/leandrofars/oktopus/internal/utils"
|
||||||
"github.com/nats-io/nats.go/jetstream"
|
"github.com/nats-io/nats.go/jetstream"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
|
@ -76,12 +79,25 @@ func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
skip = 0
|
skip = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Create filters
|
//TODO: fix status ordering
|
||||||
//TODO: Create sorting
|
statusOrder := r.URL.Query().Get("status")
|
||||||
|
if statusOrder != "" {
|
||||||
|
if statusOrder == "asc" {
|
||||||
|
statusOrder = "1"
|
||||||
|
} else if statusOrder == "desc" {
|
||||||
|
statusOrder = "-1"
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode("Status order must be 'asc' or 'desc'")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sort := bson.M{}
|
sort := bson.M{}
|
||||||
sort["status"] = 1
|
sort["status"] = 1
|
||||||
|
|
||||||
|
//TODO: Create filters
|
||||||
|
|
||||||
filter := bson.A{
|
filter := bson.A{
|
||||||
//bson.M{"$match": filter},
|
//bson.M{"$match": filter},
|
||||||
bson.M{"$sort": sort}, // shows online devices first
|
bson.M{"$sort": sort}, // shows online devices first
|
||||||
|
|
@ -232,3 +248,37 @@ func (a *Api) deviceAuth(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Api) setDeviceAlias(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
id := r.URL.Query().Get("id")
|
||||||
|
if id == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
utils.MarshallEncoder("No id provided", w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
utils.MarshallEncoder("Error to decode payload: "+err.Error(), w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadLen := len(payload)
|
||||||
|
if payloadLen == 0 {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
utils.MarshallEncoder("No alias provided", w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if payloadLen > 50 {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
utils.MarshallEncoder("Alias too long", w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = bridge.NatsReq[[]byte](local.NATS_ADAPTER_SUBJECT+id+".device.alias", payload, w, a.nc)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user