1. 完善websocket通信逻辑
2. 实现Switch接口
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
@@ -52,6 +53,9 @@ type DeviceConnection struct {
|
||||
|
||||
// LastHeartbeat 最后心跳时间
|
||||
LastHeartbeat time.Time
|
||||
|
||||
// ResponseChan 响应通道
|
||||
ResponseChan chan *WebSocketMessage
|
||||
}
|
||||
|
||||
// WebSocketService WebSocket服务
|
||||
@@ -64,16 +68,25 @@ type WebSocketService struct {
|
||||
|
||||
// logger 日志记录器
|
||||
logger *logs.Logger
|
||||
|
||||
// defaultTimeout 默认超时时间(秒)
|
||||
defaultTimeout int
|
||||
}
|
||||
|
||||
// NewWebSocketService 创建WebSocket服务实例
|
||||
func NewWebSocketService() *WebSocketService {
|
||||
return &WebSocketService{
|
||||
connections: make(map[string]*DeviceConnection),
|
||||
logger: logs.NewLogger(),
|
||||
connections: make(map[string]*DeviceConnection),
|
||||
logger: logs.NewLogger(),
|
||||
defaultTimeout: 5, // 默认5秒超时
|
||||
}
|
||||
}
|
||||
|
||||
// SetDefaultTimeout 设置默认超时时间
|
||||
func (ws *WebSocketService) SetDefaultTimeout(timeout int) {
|
||||
ws.defaultTimeout = timeout
|
||||
}
|
||||
|
||||
// AddConnection 添加设备连接
|
||||
func (ws *WebSocketService) AddConnection(deviceID string, conn *websocket.Conn) {
|
||||
ws.mutex.Lock()
|
||||
@@ -98,6 +111,16 @@ func (ws *WebSocketService) RemoveConnection(deviceID string) {
|
||||
ws.logger.Info(fmt.Sprintf("设备 %s 已断开连接", deviceID))
|
||||
}
|
||||
|
||||
// SetResponseHandler 设置响应处理器
|
||||
func (ws *WebSocketService) SetResponseHandler(deviceID string, responseChan chan *WebSocketMessage) {
|
||||
ws.mutex.Lock()
|
||||
defer ws.mutex.Unlock()
|
||||
|
||||
if deviceConn, exists := ws.connections[deviceID]; exists {
|
||||
deviceConn.ResponseChan = responseChan
|
||||
}
|
||||
}
|
||||
|
||||
// SendCommand 向指定设备发送指令
|
||||
func (ws *WebSocketService) SendCommand(deviceID, command string, data interface{}) error {
|
||||
ws.mutex.RLock()
|
||||
@@ -124,6 +147,99 @@ func (ws *WebSocketService) SendCommand(deviceID, command string, data interface
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandResponse WebSocket命令响应结构体
|
||||
type CommandResponse struct {
|
||||
// DeviceID 设备ID
|
||||
DeviceID string `json:"device_id,omitempty"`
|
||||
|
||||
// Command 命令名称
|
||||
Command string `json:"command,omitempty"`
|
||||
|
||||
// Data 响应数据
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
|
||||
// Status 响应状态
|
||||
Status string `json:"status,omitempty"`
|
||||
|
||||
// Message 响应消息
|
||||
Message string `json:"message,omitempty"`
|
||||
|
||||
// Timestamp 时间戳
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// ParseData 将响应数据解析到目标结构体
|
||||
func (cr *CommandResponse) ParseData(target interface{}) error {
|
||||
dataBytes, err := json.Marshal(cr.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(dataBytes, target)
|
||||
}
|
||||
|
||||
// CommandResult WebSocket命令执行结果
|
||||
type CommandResult struct {
|
||||
// Response 响应消息
|
||||
Response *CommandResponse
|
||||
|
||||
// Error 错误信息
|
||||
Error error
|
||||
}
|
||||
|
||||
// SendCommandAndWait 发送指令并等待响应
|
||||
func (ws *WebSocketService) SendCommandAndWait(deviceID, command string, data interface{}, timeout int) (*CommandResponse, error) {
|
||||
// 如果未指定超时时间,使用默认超时时间
|
||||
if timeout <= 0 {
|
||||
timeout = ws.defaultTimeout
|
||||
}
|
||||
|
||||
// 创建用于接收响应的通道
|
||||
responseChan := make(chan *WebSocketMessage, 1)
|
||||
ws.SetResponseHandler(deviceID, responseChan)
|
||||
|
||||
// 发送指令
|
||||
if err := ws.SendCommand(deviceID, command, data); err != nil {
|
||||
return nil, fmt.Errorf("发送指令失败: %v", err)
|
||||
}
|
||||
|
||||
// 等待设备响应,设置超时
|
||||
var response *WebSocketMessage
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case response = <-responseChan:
|
||||
// 成功接收到响应
|
||||
// 转换为CommandResponse结构体
|
||||
commandResponse := &CommandResponse{
|
||||
DeviceID: response.DeviceID,
|
||||
Command: response.Command,
|
||||
Data: response.Data,
|
||||
Timestamp: response.Timestamp,
|
||||
}
|
||||
|
||||
// 尝试提取状态和消息字段
|
||||
if responseData, ok := response.Data.(map[string]interface{}); ok {
|
||||
if status, exists := responseData["status"]; exists {
|
||||
if statusStr, ok := status.(string); ok {
|
||||
commandResponse.Status = statusStr
|
||||
}
|
||||
}
|
||||
|
||||
if message, exists := responseData["message"]; exists {
|
||||
if messageStr, ok := message.(string); ok {
|
||||
commandResponse.Message = messageStr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return commandResponse, nil
|
||||
case <-ctx.Done():
|
||||
// 超时处理
|
||||
return nil, fmt.Errorf("等待设备响应超时")
|
||||
}
|
||||
}
|
||||
|
||||
// GetConnectedDevices 获取已连接的设备列表
|
||||
func (ws *WebSocketService) GetConnectedDevices() []string {
|
||||
ws.mutex.RLock()
|
||||
@@ -154,6 +270,22 @@ func (ws *WebSocketService) HandleMessage(deviceID string, message []byte) error
|
||||
ws.mutex.Unlock()
|
||||
}
|
||||
|
||||
// 处理响应消息
|
||||
if msg.Type == MessageTypeResponse {
|
||||
ws.mutex.RLock()
|
||||
if deviceConn, exists := ws.connections[deviceID]; exists && deviceConn.ResponseChan != nil {
|
||||
// 发送响应到通道
|
||||
select {
|
||||
case deviceConn.ResponseChan <- &msg:
|
||||
// 成功发送
|
||||
default:
|
||||
// 通道已满,丢弃消息
|
||||
ws.logger.Warn(fmt.Sprintf("设备 %s 的响应通道已满,丢弃响应消息", deviceID))
|
||||
}
|
||||
}
|
||||
ws.mutex.RUnlock()
|
||||
}
|
||||
|
||||
// 记录消息日志
|
||||
ws.logger.Info(fmt.Sprintf("收到来自设备 %s 的消息: %v", deviceID, msg))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user