|
@@ -3,7 +3,12 @@ package config
|
|
|
import (
|
|
|
"cicv-data-closedloop/common/config/c_log"
|
|
|
"cicv-data-closedloop/common/util"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gorilla/websocket"
|
|
|
"gopkg.in/yaml.v3"
|
|
|
+ "net/url"
|
|
|
"os"
|
|
|
"strings"
|
|
|
"sync"
|
|
@@ -66,6 +71,25 @@ type cloudConfig struct {
|
|
|
Monitor MonitorStruct `yaml:"monitor"`
|
|
|
}
|
|
|
|
|
|
+// Request 结构体定义
|
|
|
+type Request struct {
|
|
|
+ Type string `json:"type"`
|
|
|
+ UUID string `json:"uuid"`
|
|
|
+ CommandID string `json:"commandId"`
|
|
|
+ Parameter interface{} `json:"parameter"`
|
|
|
+}
|
|
|
+
|
|
|
+// Response 结构体定义
|
|
|
+type Response struct {
|
|
|
+ CommandID string `json:"commandId"`
|
|
|
+ ErrorCode string `json:"errorCode"`
|
|
|
+ Results map[string]string `json:"results"`
|
|
|
+ Status string `json:"status"`
|
|
|
+ Time int64 `json:"time"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ UUID string `json:"uuid"`
|
|
|
+}
|
|
|
+
|
|
|
var (
|
|
|
CloudConfig cloudConfig
|
|
|
CloudConfigMutex sync.RWMutex
|
|
@@ -78,16 +102,11 @@ func InitCloudConfig() {
|
|
|
|
|
|
for {
|
|
|
time.Sleep(time.Duration(2) * time.Second)
|
|
|
- var command []string
|
|
|
- command = append(command, "get")
|
|
|
- command = append(command, "sn")
|
|
|
- _, snOutput, err := util.ExecuteSync(LocalConfig.RosparamPath, command...)
|
|
|
+ snCode, err := getSnCode()
|
|
|
if err != nil {
|
|
|
- c_log.GlobalLogger.Error("执行获取sn码命令", command, "出错:", err)
|
|
|
+ c_log.GlobalLogger.Error("获取sn码失败:", err.Error())
|
|
|
continue
|
|
|
}
|
|
|
- c_log.GlobalLogger.Info("执行获取sn码命令", command, "成功,结果为:", snOutput)
|
|
|
- snCode = strings.Replace(strings.Replace(snOutput, " ", "", -1), "\n", "", -1)
|
|
|
LocalConfig.SecretKey = snCode
|
|
|
LocalConfig.EquipmentNo = "pjibot-" + snCode
|
|
|
break
|
|
@@ -211,3 +230,85 @@ func checkCloudConfig(check cloudConfig) bool {
|
|
|
}
|
|
|
return true
|
|
|
}
|
|
|
+
|
|
|
+func getSnCode() (string, error) {
|
|
|
+ if LocalConfig.Type == "1" {
|
|
|
+ var command []string
|
|
|
+ command = append(command, "get")
|
|
|
+ command = append(command, "sn")
|
|
|
+ _, snOutput, err := util.ExecuteSync(LocalConfig.RosparamPath, command...)
|
|
|
+ if err != nil {
|
|
|
+ return "", errors.New("执行获取sn码命令" + util.ToString(command) + "出错:" + util.ToString(err))
|
|
|
+ }
|
|
|
+ c_log.GlobalLogger.Info("执行获取sn码命令", command, "成功,结果为:", snOutput)
|
|
|
+ snCode := strings.Replace(strings.Replace(snOutput, " ", "", -1), "\n", "", -1)
|
|
|
+ return snCode, nil
|
|
|
+ } else if LocalConfig.Type == "2" || LocalConfig.Type == "3" {
|
|
|
+ // 示例使用
|
|
|
+ serverURL := "192.168.1.104:9002"
|
|
|
+ path := "/"
|
|
|
+ request := Request{
|
|
|
+ Type: "request",
|
|
|
+ UUID: "",
|
|
|
+ CommandID: "getRobotBaseInfo",
|
|
|
+ Parameter: nil,
|
|
|
+ }
|
|
|
+
|
|
|
+ sn, err := SendWebsocketRequest(serverURL, path, request)
|
|
|
+ if err != nil {
|
|
|
+ return "", errors.New("通过api获取sn码失败:" + util.ToString(err))
|
|
|
+ }
|
|
|
+ return sn, nil
|
|
|
+ } else {
|
|
|
+ return "", errors.New("未知的机器人类型【" + LocalConfig.Type + "】,请修改local-config.yaml文件")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// SendWebsocketRequest 发送WebSocket请求并返回sn字段的值
|
|
|
+func SendWebsocketRequest(serverURL, path string, request Request) (string, error) {
|
|
|
+ // 构建WebSocket连接URL
|
|
|
+ u := url.URL{Scheme: "ws", Host: serverURL, Path: path}
|
|
|
+
|
|
|
+ // 创建一个Dialer实例,用于建立WebSocket连接
|
|
|
+ dialer := websocket.Dialer{
|
|
|
+ ReadBufferSize: 1024,
|
|
|
+ WriteBufferSize: 1024,
|
|
|
+ // 可选:设置超时等
|
|
|
+ HandshakeTimeout: 5 * time.Second,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 建立WebSocket连接
|
|
|
+ conn, _, err := dialer.Dial(u.String(), nil)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("dial: %w", err)
|
|
|
+ }
|
|
|
+ defer conn.Close()
|
|
|
+
|
|
|
+ // 将请求JSON编码为字节
|
|
|
+ requestJSON, err := json.Marshal(request)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("marshal request: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送WebSocket消息
|
|
|
+ err = conn.WriteMessage(websocket.TextMessage, requestJSON)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("write: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取WebSocket响应
|
|
|
+ _, responseBytes, err := conn.ReadMessage()
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("read: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将响应字节解码为JSON
|
|
|
+ var response Response
|
|
|
+ err = json.Unmarshal(responseBytes, &response)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("unmarshal response: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回sn字段的值
|
|
|
+ return response.Results["sn"], nil
|
|
|
+}
|