feat: ping + traceroute init
This commit is contained in:
parent
698ce9f1ec
commit
2918c57125
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user