chore(api): init device multiple mtp connection handlers
This commit is contained in:
parent
ac015bfa85
commit
e550ff37d1
|
|
@ -94,7 +94,7 @@ func StartApi(a Api) {
|
|||
log.Println("Running Api at port", a.Port)
|
||||
}
|
||||
|
||||
func (a *Api) uspCall(msg usp_msg.Msg, sn string, w http.ResponseWriter) {
|
||||
func (a *Api) uspCall(msg usp_msg.Msg, sn string, w http.ResponseWriter, device db.Device) {
|
||||
|
||||
encodedMsg, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
|
|
@ -113,6 +113,7 @@ func (a *Api) uspCall(msg usp_msg.Msg, sn string, w http.ResponseWriter) {
|
|||
a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg)
|
||||
a.QMutex.Unlock()
|
||||
log.Println("Sending Msg:", msg.Header.MsgId)
|
||||
//TODO: Check what MTP the device is connected to
|
||||
a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn, false)
|
||||
|
||||
select {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/leandrofars/oktopus/internal/db"
|
||||
usp_msg "github.com/leandrofars/oktopus/internal/usp_message"
|
||||
"github.com/leandrofars/oktopus/internal/utils"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
|
|
@ -14,7 +15,7 @@ import (
|
|||
func (a *Api) deviceGetSupportedParametersMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.GetSupportedDM
|
||||
|
||||
|
|
@ -26,7 +27,7 @@ func (a *Api) deviceGetSupportedParametersMsg(w http.ResponseWriter, r *http.Req
|
|||
}
|
||||
|
||||
msg := utils.NewGetSupportedParametersMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -47,7 +48,7 @@ func (a *Api) retrieveDevices(w http.ResponseWriter, r *http.Request) {
|
|||
func (a *Api) deviceCreateMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Add
|
||||
|
||||
|
|
@ -59,14 +60,14 @@ func (a *Api) deviceCreateMsg(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg := utils.NewCreateMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
func (a *Api) deviceGetMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Get
|
||||
|
||||
|
|
@ -78,14 +79,14 @@ func (a *Api) deviceGetMsg(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg := utils.NewGetMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
func (a *Api) deviceOperateMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Operate
|
||||
|
||||
|
|
@ -97,13 +98,13 @@ func (a *Api) deviceOperateMsg(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg := utils.NewOperateMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
func (a *Api) deviceDeleteMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Delete
|
||||
|
||||
|
|
@ -115,7 +116,7 @@ func (a *Api) deviceDeleteMsg(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg := utils.NewDelMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
|
||||
//a.Broker.Request(tr369Message, usp_msg.Header_GET, "oktopus/v1/agent/"+sn, "oktopus/v1/get/"+sn)
|
||||
|
||||
|
|
@ -124,7 +125,7 @@ func (a *Api) deviceDeleteMsg(w http.ResponseWriter, r *http.Request) {
|
|||
func (a *Api) deviceUpdateMsg(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.Set
|
||||
|
||||
|
|
@ -136,26 +137,26 @@ func (a *Api) deviceUpdateMsg(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg := utils.NewSetMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
func (a *Api) deviceExists(sn string, w http.ResponseWriter) {
|
||||
_, err := a.Db.RetrieveDevice(sn)
|
||||
func (a *Api) deviceExists(sn string, w http.ResponseWriter) db.Device {
|
||||
device, 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
|
||||
return device
|
||||
}
|
||||
return device
|
||||
}
|
||||
|
||||
func (a *Api) deviceGetParameterInstances(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var receiver usp_msg.GetInstances
|
||||
|
||||
|
|
@ -167,5 +168,5 @@ func (a *Api) deviceGetParameterInstances(w http.ResponseWriter, r *http.Request
|
|||
}
|
||||
|
||||
msg := utils.NewGetParametersInstancesMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ type FwUpdate struct {
|
|||
func (a *Api) deviceFwUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
sn := vars["sn"]
|
||||
a.deviceExists(sn, w)
|
||||
device := a.deviceExists(sn, w)
|
||||
|
||||
var payload FwUpdate
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ func (a *Api) deviceFwUpdate(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
msg = utils.NewOperateMsg(receiver)
|
||||
a.uspCall(msg, sn, w)
|
||||
a.uspCall(msg, sn, w, device)
|
||||
}
|
||||
|
||||
// Check which fw image is activated
|
||||
|
|
|
|||
|
|
@ -1,10 +1,20 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"log"
|
||||
)
|
||||
|
||||
type MTP int32
|
||||
|
||||
const (
|
||||
UNDEFINED MTP = iota
|
||||
MQTT
|
||||
STOMP
|
||||
WEBSOCKETS
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
|
|
@ -14,6 +24,7 @@ type Device struct {
|
|||
Vendor string
|
||||
Version string
|
||||
Status uint8
|
||||
MTP []map[string]string
|
||||
}
|
||||
|
||||
func (d *Database) CreateDevice(device Device) error {
|
||||
|
|
@ -59,3 +70,17 @@ func (d *Database) RetrieveDevice(sn string) (Device, error) {
|
|||
func (d *Database) DeleteDevice() {
|
||||
|
||||
}
|
||||
|
||||
func (m MTP) String() string {
|
||||
switch m {
|
||||
case UNDEFINED:
|
||||
return "unknown"
|
||||
case MQTT:
|
||||
return "mqtt"
|
||||
case STOMP:
|
||||
return "stomp"
|
||||
case WEBSOCKETS:
|
||||
return "websockets"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -285,7 +285,12 @@ func (m *Mqtt) handleNewDevicesResponse(p []byte, sn string) {
|
|||
device.Model = msg.ReqPathResults[1].ResolvedPathResults[0].ResultParams["ModelName"]
|
||||
device.Version = msg.ReqPathResults[2].ResolvedPathResults[0].ResultParams["SoftwareVersion"]
|
||||
device.SN = sn
|
||||
device.Status = utils.Online
|
||||
|
||||
mtp := map[string]string{
|
||||
db.MQTT.String(): "online",
|
||||
}
|
||||
|
||||
device.MTP = append(device.MTP, mtp)
|
||||
|
||||
err = m.DB.CreateDevice(device)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user