From 4a5a72c1cca50a912fd74baba3c5e6aa699d5def Mon Sep 17 00:00:00 2001 From: leandrofars Date: Mon, 23 Oct 2023 23:43:59 -0300 Subject: [PATCH] feat(api): firmware update --- .../services/controller/cmd/oktopus/main.go | 2 +- .../controller/internal/api/device.go | 97 ------------------- backend/services/controller/internal/ws/ws.go | 8 +- 3 files changed, 6 insertions(+), 101 deletions(-) diff --git a/backend/services/controller/cmd/oktopus/main.go b/backend/services/controller/cmd/oktopus/main.go index 038f83f..8bb9f0b 100755 --- a/backend/services/controller/cmd/oktopus/main.go +++ b/backend/services/controller/cmd/oktopus/main.go @@ -49,7 +49,7 @@ func main() { App variables priority: 1º - Flag through command line. 2º - Env variables. - 3º - Default flag value + 3º - Default flag value. */ log.Println("Starting Oktopus Project TR-369 Controller Version:", VERSION) diff --git a/backend/services/controller/internal/api/device.go b/backend/services/controller/internal/api/device.go index c715e06..b5d98ae 100644 --- a/backend/services/controller/internal/api/device.go +++ b/backend/services/controller/internal/api/device.go @@ -4,110 +4,13 @@ import ( "encoding/json" "log" "net/http" - "time" "github.com/gorilla/mux" usp_msg "github.com/leandrofars/oktopus/internal/usp_message" "github.com/leandrofars/oktopus/internal/utils" "go.mongodb.org/mongo-driver/mongo" - "google.golang.org/protobuf/proto" ) -func (a *Api) deviceFwUpdate(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - sn := vars["sn"] - a.deviceExists(sn, w) - - msg := utils.NewGetMsg(usp_msg.Get{ - ParamPaths: []string{"Device.DeviceInfo.FirmwareImage.*.Status"}, - MaxDepth: 1, - }) - encodedMsg, err := proto.Marshal(&msg) - if err != nil { - log.Println(err) - w.WriteHeader(http.StatusBadRequest) - return - } - - record := utils.NewUspRecord(encodedMsg, sn) - tr369Message, err := proto.Marshal(&record) - if err != nil { - log.Fatalln("Failed to encode tr369 record:", err) - } - - a.QMutex.Lock() - a.MsgQueue[msg.Header.MsgId] = make(chan usp_msg.Msg) - a.QMutex.Unlock() - log.Println("Sending Msg:", msg.Header.MsgId) - a.Broker.Publish(tr369Message, "oktopus/v1/agent/"+sn, "oktopus/v1/api/"+sn, false) - - var getMsgAnswer *usp_msg.GetResp - - select { - case msg := <-a.MsgQueue[msg.Header.MsgId]: - log.Printf("Received Msg: %s", msg.Header.MsgId) - a.QMutex.Lock() - delete(a.MsgQueue, msg.Header.MsgId) - a.QMutex.Unlock() - log.Println("requests queue:", a.MsgQueue) - getMsgAnswer = msg.Body.GetResponse().GetGetResp() - case <-time.After(time.Second * 55): - log.Printf("Request %s Timed Out", msg.Header.MsgId) - w.WriteHeader(http.StatusGatewayTimeout) - a.QMutex.Lock() - delete(a.MsgQueue, msg.Header.MsgId) - a.QMutex.Unlock() - log.Println("requests queue:", a.MsgQueue) - json.NewEncoder(w).Encode("Request Timed Out") - return - } - - // Check which fw image is activated - partition := checkAvaiableFwPartition(getMsgAnswer.ReqPathResults) - if partition < 0 { - log.Println("Error to get device available firmware partition, probably it has only one partition") - w.WriteHeader(http.StatusInternalServerError) - json.NewEncoder(w).Encode("Server don't have the hability to update device with only one partition") - return - //TODO: update device with only one partition - } - - var receiver = usp_msg.Operate{ - Command: "Device.DeviceInfo.FirmwareImage.1.Download()", - CommandKey: "Download()", - SendResp: true, - InputArgs: map[string]string{ - "URL": "http://cronos.intelbras.com.br/download/PON/121AC/beta/121AC-2.3-230620-77753201df4f1e2c607a7236746c8491.tar", //TODO: use dynamic url - "AutoActivate": "true", - //"Username": "", - //"Password": "", - "FileSize": "0", //TODO: send firmware length - //"CheckSumAlgorithm": "", - //"CheckSum": "", - }, - } - - msg = utils.NewOperateMsg(receiver) - a.uspCall(msg, sn, w) -} - -// Check which fw image is activated -func checkAvaiableFwPartition(reqPathResult []*usp_msg.GetResp_RequestedPathResult) int { - for _, x := range reqPathResult { - partitionsNumber := len(x.ResolvedPathResults) - if partitionsNumber > 1 { - log.Printf("Device has %d firmware partitions", partitionsNumber) - } - for i, y := range x.ResolvedPathResults { - if y.ResultParams["Status"] == "Available" { - log.Printf("Partition %d is avaiable", i) - return i - } - } - } - return -1 -} - func (a *Api) deviceGetSupportedParametersMsg(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) sn := vars["sn"] diff --git a/backend/services/controller/internal/ws/ws.go b/backend/services/controller/internal/ws/ws.go index 0a211c0..2750c22 100644 --- a/backend/services/controller/internal/ws/ws.go +++ b/backend/services/controller/internal/ws/ws.go @@ -2,6 +2,10 @@ package ws import ( "fmt" + "log" + "net/http" + "time" + socketio "github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io/engineio" "github.com/googollee/go-socket.io/engineio/transport" @@ -9,11 +13,9 @@ import ( "github.com/googollee/go-socket.io/engineio/transport/websocket" "github.com/gorilla/mux" "github.com/leandrofars/oktopus/internal/api/cors" - "log" - "net/http" - "time" ) +/* ----------- [Deprecated code] migrated to Socketio with NodeJs ----------- */ func Ws() { server := socketio.NewServer(&engineio.Options{ PingTimeout: 5 * time.Second,