|
@@ -1,8 +1,14 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "bytes"
|
|
"context"
|
|
"context"
|
|
|
|
+ "encoding/json"
|
|
|
|
+ "errors"
|
|
|
|
+ "fmt"
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
+ "github.com/gogf/gf/util/gconv"
|
|
|
|
+ "github.com/gogf/gf/v2/encoding/gbinary"
|
|
"sparrow/pkg/klink"
|
|
"sparrow/pkg/klink"
|
|
"sparrow/pkg/models"
|
|
"sparrow/pkg/models"
|
|
"sparrow/pkg/protocol"
|
|
"sparrow/pkg/protocol"
|
|
@@ -15,6 +21,12 @@ type Access struct {
|
|
client SubDev
|
|
client SubDev
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func NewAgent(client SubDev) *Access {
|
|
|
|
+ return &Access{
|
|
|
|
+ client: client,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// Message 收到设备上报消息处理
|
|
// Message 收到设备上报消息处理
|
|
func (a *Access) Message(topic string, payload []byte) error {
|
|
func (a *Access) Message(topic string, payload []byte) error {
|
|
|
|
|
|
@@ -173,8 +185,7 @@ func processDeviceUpgrade(deviceId string, message *gjson.Json) error {
|
|
Size: params.GetInt64("size"),
|
|
Size: params.GetInt64("size"),
|
|
Offset: params.GetInt("offset"),
|
|
Offset: params.GetInt("offset"),
|
|
}
|
|
}
|
|
-
|
|
|
|
- err := server.RPCCallByName(nil, rpcs.MQTTAccessName, "Access.ChunkUpgrade", args, &reply)
|
|
|
|
|
|
+ err := chunkUpgrade(*args)
|
|
if err != nil {
|
|
if err != nil {
|
|
server.Log.Errorf("分片下载发送失败:%v", err)
|
|
server.Log.Errorf("分片下载发送失败:%v", err)
|
|
}
|
|
}
|
|
@@ -294,10 +305,61 @@ func (a *Access) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus)
|
|
return a.SendCommand(cmdArgs, &cmdReply)
|
|
return a.SendCommand(cmdArgs, &cmdReply)
|
|
}
|
|
}
|
|
|
|
|
|
-func NewAgent(client SubDev) *Access {
|
|
|
|
- return &Access{
|
|
|
|
- client: client,
|
|
|
|
|
|
+func chunkUpgrade(params rpcs.ChunkUpgrade) error {
|
|
|
|
+ server.Log.Infof("4G模组OTA升级:%s", params.DeviceId)
|
|
|
|
+ reply := new(rpcs.ReplyEmptyResult)
|
|
|
|
+ cmd := &klink.CloudSend{
|
|
|
|
+ Action: "cloudSend",
|
|
|
|
+ MsgId: 0,
|
|
|
|
+ DeviceCode: params.DeviceId,
|
|
|
|
+ Timestamp: time.Now().Unix(),
|
|
|
|
+ Data: &klink.CloudSendData{
|
|
|
|
+ Cmd: "devUpgrade",
|
|
|
|
+ Params: map[string]interface{}{
|
|
|
|
+ "fileId": params.FileId,
|
|
|
|
+ "fileSize": params.FileSize,
|
|
|
|
+ "size": params.Size,
|
|
|
|
+ "offset": params.Offset,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ byteCmd, err := json.Marshal(cmd)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ buf := bytes.NewBuffer(gbinary.BeEncodeUint16(gconv.Uint16(len(byteCmd))))
|
|
|
|
+
|
|
|
|
+ server.Log.Infof("1----------填充数据长度:%2X", buf.Bytes())
|
|
|
|
+
|
|
|
|
+ buf.Write(byteCmd)
|
|
|
|
+ server.Log.Infof("2----------填充响应数据:%2X", buf.Bytes())
|
|
|
|
+ var fileArgs rpcs.ArgsOtaFile
|
|
|
|
+ fileArgs.FileId = params.FileId
|
|
|
|
+
|
|
|
|
+ var fileReply rpcs.ReplyOtaFile
|
|
|
|
+ err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetFile", fileArgs, &fileReply)
|
|
|
|
+ if err != nil {
|
|
|
|
+ server.Log.Errorf("OTA升级文件保存失败:%v", err)
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ server.Log.Infof("获取到OTA文件:%v", fileReply)
|
|
|
|
+ if fileReply.File == nil {
|
|
|
|
+ return errors.New(fmt.Sprintf("文件:%s 获取失败", params.FileId))
|
|
|
|
+ }
|
|
|
|
+ buf.Write(fileReply.File[params.Offset : params.Offset+int(params.Size)])
|
|
|
|
+ server.Log.Infof("3----------填充文件:%2X", buf.Bytes())
|
|
|
|
+ var mCrc crc
|
|
|
|
+ checkSum := mCrc.reset().pushBytes(buf.Bytes()).value()
|
|
|
|
+ buf.Write([]byte{byte(checkSum), byte(checkSum >> 8)})
|
|
|
|
+ server.Log.Infof("4----------填充CRC:%2X", buf.Bytes())
|
|
|
|
+ var SendByteArgs rpcs.ArgsSendByteData
|
|
|
|
+ SendByteArgs.DeviceId = params.DeviceId
|
|
|
|
+ SendByteArgs.Data = buf.Bytes()
|
|
|
|
+ err = server.RPCCallByName(nil, rpcs.EmqxAgentServiceName, "Access.SendByteData", SendByteArgs, &reply)
|
|
|
|
+
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
// SendByteData rpc 发送byte数组
|
|
// SendByteData rpc 发送byte数组
|