Merge pull request #35 from leandrofars/dev
CRUD in device via TR-369 close #25
This commit is contained in:
commit
841279e159
14
README.en.md
14
README.en.md
|
|
@ -16,5 +16,19 @@ This solution is inspired by the project <a href="https://github.com/genieacs/ge
|
|||
|
||||

|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<h4>API:</h4>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://documenter.getpostman.com/view/18932104/2s93eR3vQY#10c46751-ede9-4ea1-8ea4-264ebf539e5e">Documentation </a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://www.postman.com/docking-module-astronomer-46169629/workspace/oktopus">Testing and development workspace</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<br/>
|
||||
If you are interested in internal information about the team and our intentions, visit our <a href="https://github.com/leandrofars/oktopus/wiki">Wiki</a>.
|
||||
|
|
|
|||
14
README.es.md
14
README.es.md
|
|
@ -16,6 +16,20 @@ Esta solución se inspira en el proyecto. <a href="https://github.com/genieacs/g
|
|||
|
||||

|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<h4>API:</h4>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://documenter.getpostman.com/view/18932104/2s93eR3vQY#10c46751-ede9-4ea1-8ea4-264ebf539e5e">Documentación </a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://www.postman.com/docking-module-astronomer-46169629/workspace/oktopus">Espacio de trabajo de pruebas y desarrollo</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<br/>
|
||||
Si está interesado en información interna sobre el equipo y nuestras intenciones, visite nuestro <a href="https://github.com/leandrofars/oktopus/wiki">Wiki</a>.
|
||||
|
||||
|
|
|
|||
14
README.md
14
README.md
|
|
@ -16,6 +16,20 @@ Esta solução tem como inspirações o projeto <a href="https://github.com/geni
|
|||
|
||||

|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<h4>API:</h4>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="https://documenter.getpostman.com/view/18932104/2s93eR3vQY#10c46751-ede9-4ea1-8ea4-264ebf539e5e">Documentação </a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="https://www.postman.com/docking-module-astronomer-46169629/workspace/oktopus">Workspace de testes e desenvolvimento</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<br/>
|
||||
Caso você tenha interesse em informações internas sobre o time e nossas pretensões acesse nossa <a href="https://github.com/leandrofars/oktopus/wiki">Wiki</a>.
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/leandrofars/oktopus/internal/api/middleware"
|
||||
"github.com/leandrofars/oktopus/internal/db"
|
||||
"github.com/leandrofars/oktopus/internal/mtp"
|
||||
usp_msg "github.com/leandrofars/oktopus/internal/usp_message"
|
||||
|
|
@ -39,8 +40,15 @@ func StartApi(a Api) {
|
|||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
})
|
||||
r.HandleFunc("/devices", a.retrieveDevices)
|
||||
r.HandleFunc("/devices/{sn}/get", a.deviceGetMsg)
|
||||
r.HandleFunc("/devices", a.retrieveDevices).Methods("GET")
|
||||
r.HandleFunc("/device/{sn}/get", a.deviceGetMsg).Methods("PUT")
|
||||
r.HandleFunc("/device/{sn}/add", a.deviceCreateMsg).Methods("PUT")
|
||||
r.HandleFunc("/device/{sn}/del", a.deviceDeleteMsg).Methods("PUT")
|
||||
r.HandleFunc("/device/{sn}/set", a.deviceUpdateMsg).Methods("PUT")
|
||||
|
||||
r.Use(func(handler http.Handler) http.Handler {
|
||||
return middleware.Middleware(handler)
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: "0.0.0.0:" + a.Port,
|
||||
|
|
@ -74,27 +82,60 @@ func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
func (a *Api) deviceCreateMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Add
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(receiver)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := utils.NewCreateMsg(receiver)
|
||||
encodedMsg, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
record := utils.NewUspRecord(encodedMsg, sn)
|
||||
tr369Message, err := proto.Marshal(&record)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to encode tr369 record:", err)
|
||||
}
|
||||
|
||||
//a.Broker.Request(tr369Message, usp_msg.Header_GET, "oktopus/v1/agent/"+sn, "oktopus/v1/get/"+sn)
|
||||
a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg)
|
||||
a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn)
|
||||
|
||||
select {
|
||||
case msg := <-a.MsgQueue[msg.Header.MsgId]:
|
||||
log.Printf("Received Msg: %s", msg.Header.MsgId)
|
||||
json.NewEncoder(w).Encode(msg.Body.GetResponse().GetAddResp())
|
||||
return
|
||||
case <-time.After(time.Second * 5):
|
||||
log.Printf("Request %s Timed Out", msg.Header.MsgId)
|
||||
w.WriteHeader(http.StatusGatewayTimeout)
|
||||
json.NewEncoder(w).Encode("Request Timed Out")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Api) deviceGetMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
_, err := a.Db.RetrieveDevice(sn)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode("No device with serial number " + sn + " was found")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Get
|
||||
//data := []byte(`{"param_paths": {'Device.DeviceInfo.'},"max_depth": 2}`)
|
||||
//data := []byte("'opa'")
|
||||
//var jsonBlob = []byte(`{
|
||||
// "param_paths": ["Device.DeviceInfo.","Device.ManagementServer."],
|
||||
// "max_depth": 2
|
||||
//}`)
|
||||
err = json.NewDecoder(r.Body).Decode(receiver)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&receiver)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
|
@ -115,19 +156,121 @@ func (a *Api) deviceGetMsg(w http.ResponseWriter, r *http.Request) {
|
|||
log.Fatalln("Failed to encode tr369 record:", err)
|
||||
}
|
||||
|
||||
a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg)
|
||||
a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn)
|
||||
|
||||
select {
|
||||
case msg := <-a.MsgQueue[msg.Header.MsgId]:
|
||||
log.Printf("Received Msg: %s", msg.Header.MsgId)
|
||||
json.NewEncoder(w).Encode(msg.Body.GetResponse().GetGetResp())
|
||||
return
|
||||
case <-time.After(time.Second * 5):
|
||||
log.Printf("Request %s Timed Out", msg.Header.MsgId)
|
||||
w.WriteHeader(http.StatusGatewayTimeout)
|
||||
json.NewEncoder(w).Encode("Request Timed Out")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Api) deviceDeleteMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Delete
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&receiver)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := utils.NewDelMsg(receiver)
|
||||
encodedMsg, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
record := utils.NewUspRecord(encodedMsg, sn)
|
||||
tr369Message, err := proto.Marshal(&record)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to encode tr369 record:", err)
|
||||
}
|
||||
|
||||
//a.Broker.Request(tr369Message, usp_msg.Header_GET, "oktopus/v1/agent/"+sn, "oktopus/v1/get/"+sn)
|
||||
a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg)
|
||||
a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn)
|
||||
|
||||
select {
|
||||
case msg := <-a.MsgQueue[msg.Header.MsgId]:
|
||||
log.Printf("Received Msg")
|
||||
json.NewEncoder(w).Encode(msg)
|
||||
log.Printf("Received Msg: %s", msg.Header.MsgId)
|
||||
json.NewEncoder(w).Encode(msg.Body.GetResponse().GetDeleteResp())
|
||||
return
|
||||
case <-time.After(time.Second * 5):
|
||||
log.Printf("Request Timed Out")
|
||||
log.Printf("Request %s Timed Out", msg.Header.MsgId)
|
||||
w.WriteHeader(http.StatusGatewayTimeout)
|
||||
json.NewEncoder(w).Encode("Request Timed Out")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Api) deviceUpdateMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Set
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&receiver)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := utils.NewSetMsg(receiver)
|
||||
encodedMsg, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
record := utils.NewUspRecord(encodedMsg, sn)
|
||||
tr369Message, err := proto.Marshal(&record)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to encode tr369 record:", err)
|
||||
}
|
||||
|
||||
//a.Broker.Request(tr369Message, usp_msg.Header_GET, "oktopus/v1/agent/"+sn, "oktopus/v1/get/"+sn)
|
||||
a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg)
|
||||
a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn)
|
||||
|
||||
select {
|
||||
case msg := <-a.MsgQueue[msg.Header.MsgId]:
|
||||
log.Printf("Received Msg: %s", msg.Header.MsgId)
|
||||
json.NewEncoder(w).Encode(msg.Body.GetResponse().GetSetResp())
|
||||
return
|
||||
case <-time.After(time.Second * 5):
|
||||
log.Printf("Request %s Timed Out", msg.Header.MsgId)
|
||||
w.WriteHeader(http.StatusGatewayTimeout)
|
||||
json.NewEncoder(w).Encode("Request Timed Out")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Api) deviceExists(sn string, w http.ResponseWriter) {
|
||||
_, err := a.Db.RetrieveDevice(sn)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode("No device with serial number " + sn + " was found")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
func Middleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
next.ServeHTTP(w, r)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -44,6 +44,24 @@ func NewUspRecord(p []byte, toId string) usp_record.Record {
|
|||
}
|
||||
}
|
||||
|
||||
func NewCreateMsg(createStuff usp_msg.Add) usp_msg.Msg {
|
||||
return usp_msg.Msg{
|
||||
Header: &usp_msg.Header{
|
||||
MsgId: uuid.NewString(),
|
||||
MsgType: usp_msg.Header_ADD,
|
||||
},
|
||||
Body: &usp_msg.Body{
|
||||
MsgBody: &usp_msg.Body_Request{
|
||||
Request: &usp_msg.Request{
|
||||
ReqType: &usp_msg.Request_Add{
|
||||
Add: &createStuff,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewGetMsg(getStuff usp_msg.Get) usp_msg.Msg {
|
||||
return usp_msg.Msg{
|
||||
Header: &usp_msg.Header{
|
||||
|
|
@ -61,3 +79,39 @@ func NewGetMsg(getStuff usp_msg.Get) usp_msg.Msg {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewDelMsg(getStuff usp_msg.Delete) usp_msg.Msg {
|
||||
return usp_msg.Msg{
|
||||
Header: &usp_msg.Header{
|
||||
MsgId: uuid.NewString(),
|
||||
MsgType: usp_msg.Header_DELETE,
|
||||
},
|
||||
Body: &usp_msg.Body{
|
||||
MsgBody: &usp_msg.Body_Request{
|
||||
Request: &usp_msg.Request{
|
||||
ReqType: &usp_msg.Request_Delete{
|
||||
Delete: &getStuff,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewSetMsg(updateStuff usp_msg.Set) usp_msg.Msg {
|
||||
return usp_msg.Msg{
|
||||
Header: &usp_msg.Header{
|
||||
MsgId: uuid.NewString(),
|
||||
MsgType: usp_msg.Header_SET,
|
||||
},
|
||||
Body: &usp_msg.Body{
|
||||
MsgBody: &usp_msg.Body_Request{
|
||||
Request: &usp_msg.Request{
|
||||
ReqType: &usp_msg.Request_Set{
|
||||
Set: &updateStuff,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user