From 2918c571251fa782b4fcd349def28d18bf032aa0 Mon Sep 17 00:00:00 2001 From: leandrofars Date: Sun, 30 Jun 2024 19:59:03 -0300 Subject: [PATCH] feat: ping + traceroute init --- .../services/controller/internal/api/api.go | 1 + .../controller/internal/api/enterprise.go | 107 ++++++++++++++++++ .../controller/internal/bridge/bridge.go | 2 +- 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/backend/services/controller/internal/api/api.go b/backend/services/controller/internal/api/api.go index de3be17..12f2626 100644 --- a/backend/services/controller/internal/api/api.go +++ b/backend/services/controller/internal/api/api.go @@ -82,6 +82,7 @@ func (a *Api) StartApi() { iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET") iot.HandleFunc("/{sn}/traceroute", a.deviceTraceRoute).Methods("GET", "PUT") iot.HandleFunc("/{sn}/speedtest", a.deviceSpeedTest).Methods("PUT") + iot.HandleFunc("/{sn}/ping", a.devicePing).Methods("PUT", "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 2734368..f7ce633 100644 --- a/backend/services/controller/internal/api/enterprise.go +++ b/backend/services/controller/internal/api/enterprise.go @@ -1,6 +1,7 @@ package api import ( + "io" "net/http" "github.com/gorilla/mux" @@ -80,3 +81,109 @@ func (a *Api) deviceConnectedDevices(w http.ResponseWriter, r *http.Request) { w.Write(utils.Marshall("Device is Offline")) } } + +func (a *Api) deviceTraceRoute(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 { + w.WriteHeader(http.StatusNotImplemented) + w.Write(utils.Marshall("Get traceroute configuration is not implemented yet")) + } + } + + if r.Method == http.MethodPut { + if device.Cwmp == entity.Online { + a.getEnterpriseResource("traceroute", "set", 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) deviceSpeedTest(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + sn := vars["sn"] + + device, err := getDeviceInfo(w, sn, a.nc) + if err != nil { + return + } + + payload, err := io.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write(utils.Marshall("Error reading request body")) + return + + } + + if device.Cwmp == entity.Online { + a.getEnterpriseResource("speedTest", "set", device, sn, w, payload, "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) devicePing(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 device.Cwmp != entity.Online { + w.WriteHeader(http.StatusBadRequest) + w.Write(utils.Marshall("Device is Offline")) + } + + if r.Method == http.MethodGet { + if device.Cwmp == entity.Online { + a.getEnterpriseResource("ping", "get", device, sn, w, []byte{}, "cwmp", "098") + return + } + } else { + payload, err := io.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write(utils.Marshall("Error reading request body")) + return + + } + + if device.Cwmp == entity.Online { + a.getEnterpriseResource("ping", "set", device, sn, w, payload, "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 + } +} diff --git a/backend/services/controller/internal/bridge/bridge.go b/backend/services/controller/internal/bridge/bridge.go index a972e22..f9697ed 100644 --- a/backend/services/controller/internal/bridge/bridge.go +++ b/backend/services/controller/internal/bridge/bridge.go @@ -259,7 +259,7 @@ func NatsEnterpriseInteraction( var answer entity.MsgAnswer[[]byte] - msg, err := nc.Request(subj, body, local.NATS_REQUEST_TIMEOUT) + msg, err := nc.Request(subj, body, local.NATS_REQUEST_TIMEOUT+20*time.Second) if err != nil { if err == nats.ErrNoResponders { w.WriteHeader(http.StatusInternalServerError)