feat(controller): site survey + multiple data model support

This commit is contained in:
leandrofars 2024-06-21 17:53:51 -03:00
parent 7461cbe121
commit 28ffde5fef
5 changed files with 45 additions and 4 deletions

View File

@ -76,6 +76,9 @@ func (a *Api) StartApi() {
iot.HandleFunc("/{sn}/{mtp}/instances", a.deviceGetParameterInstances).Methods("PUT") iot.HandleFunc("/{sn}/{mtp}/instances", a.deviceGetParameterInstances).Methods("PUT")
iot.HandleFunc("/{sn}/{mtp}/operate", a.deviceOperateMsg).Methods("PUT") iot.HandleFunc("/{sn}/{mtp}/operate", a.deviceOperateMsg).Methods("PUT")
iot.HandleFunc("/{sn}/{mtp}/fw_update", a.deviceFwUpdate).Methods("PUT") //TODO: put it to work and generalize for usp and cwmp iot.HandleFunc("/{sn}/{mtp}/fw_update", a.deviceFwUpdate).Methods("PUT") //TODO: put it to work and generalize for usp and cwmp
if a.enterpise {
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET")
}
iot.HandleFunc("/{sn}/wifi", a.deviceWifi).Methods("PUT", "GET") iot.HandleFunc("/{sn}/wifi", a.deviceWifi).Methods("PUT", "GET")
dash := r.PathPrefix("/api/info").Subrouter() dash := r.PathPrefix("/api/info").Subrouter()
dash.HandleFunc("/vendors", a.vendorsInfo).Methods("GET") dash.HandleFunc("/vendors", a.vendorsInfo).Methods("GET")

View File

@ -14,11 +14,13 @@ func (a *Api) getEnterpriseResource(
sn string, sn string,
w http.ResponseWriter, w http.ResponseWriter,
body []byte, body []byte,
protocol, datamodel string,
) error { ) error {
model, err := cwmpGetDeviceModel(device, w) model, err := cwmpGetDeviceModel(device, w)
if err != nil { if err != nil {
return err return err
} }
err = bridge.NatsEnterpriseInteraction("enterprise.v1.cwmp."+model+"."+sn+"."+resource+"."+action, body, w, a.nc)
err = bridge.NatsEnterpriseInteraction("enterprise.v1."+protocol+"."+datamodel+"."+model+"."+sn+"."+resource+"."+action, body, w, a.nc)
return err return err
} }

View File

@ -0,0 +1,36 @@
package api
import (
"net/http"
"github.com/gorilla/mux"
"github.com/leandrofars/oktopus/internal/entity"
"github.com/leandrofars/oktopus/internal/utils"
)
func (a *Api) deviceSiteSurvey(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sn := vars["sn"]
device, err := getDeviceInfo(w, sn, a.nc)
if err != nil {
return
}
if r.Method == http.MethodGet {
if device.Cwmp == entity.Online {
a.getEnterpriseResource("sitesurvey", "get", device, sn, w, []byte{}, "cwmp", "098")
return
}
if device.Mqtt == entity.Online || device.Stomp == entity.Online || device.Websockets == entity.Online {
w.WriteHeader(http.StatusNotImplemented)
w.Write(utils.Marshall("This feature is only working with CWMP devices"))
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write(utils.Marshall("Device is Offline"))
}
}

View File

@ -214,7 +214,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) {
if device.Cwmp == entity.Online { if device.Cwmp == entity.Online {
if a.enterpise { if a.enterpise {
a.getEnterpriseResource("wifi", "get", device, sn, w, []byte{}) a.getEnterpriseResource("wifi", "get", device, sn, w, []byte{}, "cwmp", "098")
return return
} }
@ -354,7 +354,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) {
w.Write(utils.Marshall(err.Error())) w.Write(utils.Marshall(err.Error()))
return return
} }
a.getEnterpriseResource("wifi", "set", device, sn, w, payload) a.getEnterpriseResource("wifi", "set", device, sn, w, payload, "cwmp", "098")
return return
} }

View File

@ -267,7 +267,7 @@ func NatsEnterpriseInteraction(
return err return err
} }
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write(utils.Marshall("Error to communicate with nats: " + err.Error())) w.Write(utils.Marshall("Error to communicate with nats:" + err.Error()))
return err return err
} }