From 2864f8754d0b97e46316106b815d6991bcd52c8e Mon Sep 17 00:00:00 2001 From: leandrofars Date: Fri, 21 Jun 2024 19:00:06 -0300 Subject: [PATCH] feat(controller): connected devices --- .../services/controller/internal/api/api.go | 1 + .../controller/internal/api/enterprise.go | 56 +++++++++++++++++++ .../controller/internal/api/sitesurvey.go | 36 ------------ 3 files changed, 57 insertions(+), 36 deletions(-) delete mode 100644 backend/services/controller/internal/api/sitesurvey.go diff --git a/backend/services/controller/internal/api/api.go b/backend/services/controller/internal/api/api.go index fcc8d2f..7e16a65 100644 --- a/backend/services/controller/internal/api/api.go +++ b/backend/services/controller/internal/api/api.go @@ -78,6 +78,7 @@ func (a *Api) StartApi() { 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}/connecteddevices", a.deviceConnectedDevices).Methods("GET") } iot.HandleFunc("/{sn}/wifi", a.deviceWifi).Methods("PUT", "GET") dash := r.PathPrefix("/api/info").Subrouter() diff --git a/backend/services/controller/internal/api/enterprise.go b/backend/services/controller/internal/api/enterprise.go index 5c1abf6..2734368 100644 --- a/backend/services/controller/internal/api/enterprise.go +++ b/backend/services/controller/internal/api/enterprise.go @@ -3,8 +3,10 @@ package api import ( "net/http" + "github.com/gorilla/mux" "github.com/leandrofars/oktopus/internal/bridge" "github.com/leandrofars/oktopus/internal/entity" + "github.com/leandrofars/oktopus/internal/utils" ) func (a *Api) getEnterpriseResource( @@ -24,3 +26,57 @@ func (a *Api) getEnterpriseResource( err = bridge.NatsEnterpriseInteraction("enterprise.v1."+protocol+"."+datamodel+"."+model+"."+sn+"."+resource+"."+action, body, w, a.nc) return err } + +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")) + } +} + +func (a *Api) deviceConnectedDevices(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("connecteddevices", "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")) + } +} diff --git a/backend/services/controller/internal/api/sitesurvey.go b/backend/services/controller/internal/api/sitesurvey.go deleted file mode 100644 index c3fe71b..0000000 --- a/backend/services/controller/internal/api/sitesurvey.go +++ /dev/null @@ -1,36 +0,0 @@ -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")) - } -}