Ver Fonte

update ota

liuxiulin há 7 meses atrás
pai
commit
19a46e8fc7

+ 8 - 7
pkg/otaUpgrade/ota_upgrade.go

@@ -1,6 +1,7 @@
 package otaUpgrade
 
 import (
+	"fmt"
 	"github.com/gogf/gf/database/gredis"
 )
 
@@ -11,7 +12,7 @@ const (
 )
 
 type File struct {
-	FileId   string
+	FileId   int
 	FileData []byte
 }
 
@@ -36,8 +37,8 @@ func NewOtaManager(host string, port, db int) *OtaManager {
 	return mgr
 }
 
-func (mgr *OtaManager) SavaFile(id string, fileData []byte) error {
-	key := FileKeyPrefix + id
+func (mgr *OtaManager) SavaFile(id int, fileData []byte) error {
+	key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
 	file := new(File)
 	file.FileId = id
 	file.FileData = fileData
@@ -52,8 +53,8 @@ func (mgr *OtaManager) SavaFile(id string, fileData []byte) error {
 	return nil
 }
 
-func (mgr *OtaManager) GetFile(id string) (*File, error) {
-	key := FileKeyPrefix + id
+func (mgr *OtaManager) GetFile(id int) (*File, error) {
+	key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
 	file := new(File)
 	// get status from redis
 	result, err := mgr.redisClient.DoVar("GET", key)
@@ -67,8 +68,8 @@ func (mgr *OtaManager) GetFile(id string) (*File, error) {
 	return file, nil
 }
 
-func (mgr *OtaManager) DelFile(id string) error {
-	key := FileKeyPrefix + id
+func (mgr *OtaManager) DelFile(id int) error {
+	key := fmt.Sprintf("%s%d", FileKeyPrefix, id)
 	_, err := mgr.redisClient.Do("DEL", key)
 	if err != nil {
 		return err

+ 1 - 1
pkg/rpcs/access.go

@@ -54,7 +54,7 @@ type ArgsUpgrade4G struct {
 
 type ChunkUpgrade struct {
 	DeviceId string
-	FileId   string
+	FileId   int
 	FileSize int64
 	Size     int64
 	Offset   int

+ 1 - 1
pkg/rpcs/devicemanager.go

@@ -38,7 +38,7 @@ type ArgsGetDeviceOnlineStatus ArgsDeviceId
 type ReplyGetDeviceOnlineStatus online.Status
 
 type ArgsOtaFile struct {
-	FileId   string
+	FileId   int
 	FileData []byte
 }
 

+ 1 - 3
services/emqx-agent/agent.go

@@ -179,7 +179,7 @@ func (a *Access) processDeviceUpgrade(deviceId string, message *gjson.Json) erro
 
 		args := &rpcs.ChunkUpgrade{
 			DeviceId: deviceId,
-			FileId:   params.GetString("fileId"),
+			FileId:   params.GetInt("fileId"),
 			FileSize: params.GetInt64("fileSize"),
 			Size:     params.GetInt64("size"),
 			Offset:   params.GetInt("offset"),
@@ -330,8 +330,6 @@ func (a *Access) chunkUpgrade(params rpcs.ChunkUpgrade) error {
 	data := fileReply.File[start:stop]
 	buf.Write(gbinary.BeEncodeUint16(gconv.Uint16(len(data))))
 	buf.Write(data)
-	// 生成随机的4位数字
-	//randomNumber := rand.Intn(9000) + 1000
 	server.Log.Infof("1----------填充文件:%2X", buf.Bytes())
 	var mCrc crc
 	checkSum := mCrc.reset().pushBytes(buf.Bytes()).value()

+ 2 - 2
services/knowoapi/services/device.go

@@ -2,7 +2,7 @@ package services
 
 import (
 	"github.com/gogf/gf/encoding/gjson"
-	"github.com/gogf/gf/util/guid"
+	"math/rand"
 	"sparrow/pkg/models"
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
@@ -159,7 +159,7 @@ func (a deviceservice) GetDevicesCountByVenderId(vendorid string) (map[string]in
 func (a deviceservice) Upgrade(params *models.UpgradeParams) error {
 	var fileArgs rpcs.ArgsOtaFile
 	fileArgs.FileData = params.File
-	fileArgs.FileId = guid.S()
+	fileArgs.FileId = rand.Intn(9000) + 1000
 	var reply rpcs.ReplyEmptyResult
 
 	err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.SavaFile", fileArgs, &reply)