feat(api): firmware update

This commit is contained in:
leandrofars 2023-10-23 23:43:59 -03:00
parent fa1af141e4
commit 4a5a72c1cc
3 changed files with 6 additions and 101 deletions

View File

@ -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)

View File

@ -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"]

View File

@ -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,