1. 优化前端显示
2. 优化日志输出
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/storage/repository"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
@@ -71,14 +72,18 @@ type WebSocketService struct {
|
||||
|
||||
// defaultTimeout 默认超时时间(秒)
|
||||
defaultTimeout int
|
||||
|
||||
// deviceRepo 设备仓库
|
||||
deviceRepo repository.DeviceRepo
|
||||
}
|
||||
|
||||
// NewWebSocketService 创建WebSocket服务实例
|
||||
func NewWebSocketService() *WebSocketService {
|
||||
func NewWebSocketService(deviceRepo repository.DeviceRepo) *WebSocketService {
|
||||
return &WebSocketService{
|
||||
connections: make(map[string]*DeviceConnection),
|
||||
logger: logs.NewLogger(),
|
||||
defaultTimeout: 5, // 默认5秒超时
|
||||
deviceRepo: deviceRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +92,16 @@ func (ws *WebSocketService) SetDefaultTimeout(timeout int) {
|
||||
ws.defaultTimeout = timeout
|
||||
}
|
||||
|
||||
// getDeviceDisplayName 获取设备显示名称
|
||||
func (ws *WebSocketService) getDeviceDisplayName(deviceID string) string {
|
||||
if ws.deviceRepo != nil {
|
||||
if device, err := ws.deviceRepo.FindByIDString(deviceID); err == nil && device != nil {
|
||||
return fmt.Sprintf("%s(id:%s)", device.Name, deviceID)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("未知设备(id:%s)", deviceID)
|
||||
}
|
||||
|
||||
// AddConnection 添加设备连接
|
||||
func (ws *WebSocketService) AddConnection(deviceID string, conn *websocket.Conn) {
|
||||
ws.mutex.Lock()
|
||||
@@ -98,7 +113,8 @@ func (ws *WebSocketService) AddConnection(deviceID string, conn *websocket.Conn)
|
||||
LastHeartbeat: time.Now(),
|
||||
}
|
||||
|
||||
ws.logger.Info(fmt.Sprintf("设备 %s 已连接", deviceID))
|
||||
deviceName := ws.getDeviceDisplayName(deviceID)
|
||||
ws.logger.Info(fmt.Sprintf("设备 %s 已连接", deviceName))
|
||||
}
|
||||
|
||||
// RemoveConnection 移除设备连接
|
||||
@@ -106,9 +122,11 @@ func (ws *WebSocketService) RemoveConnection(deviceID string) {
|
||||
ws.mutex.Lock()
|
||||
defer ws.mutex.Unlock()
|
||||
|
||||
deviceName := ws.getDeviceDisplayName(deviceID)
|
||||
|
||||
delete(ws.connections, deviceID)
|
||||
|
||||
ws.logger.Info(fmt.Sprintf("设备 %s 已断开连接", deviceID))
|
||||
ws.logger.Info(fmt.Sprintf("设备 %s 已断开连接", deviceName))
|
||||
}
|
||||
|
||||
// SetResponseHandler 设置响应处理器
|
||||
@@ -127,8 +145,10 @@ func (ws *WebSocketService) SendCommand(deviceID, command string, data interface
|
||||
deviceConn, exists := ws.connections[deviceID]
|
||||
ws.mutex.RUnlock()
|
||||
|
||||
deviceName := ws.getDeviceDisplayName(deviceID)
|
||||
|
||||
if !exists {
|
||||
return fmt.Errorf("设备 %s 未连接", deviceID)
|
||||
return fmt.Errorf("设备 %s 未连接", deviceName)
|
||||
}
|
||||
|
||||
// 构造消息
|
||||
@@ -141,7 +161,7 @@ func (ws *WebSocketService) SendCommand(deviceID, command string, data interface
|
||||
|
||||
// 发送消息
|
||||
if err := deviceConn.Connection.WriteJSON(msg); err != nil {
|
||||
return fmt.Errorf("向设备 %s 发送指令失败: %v", deviceID, err)
|
||||
return fmt.Errorf("向设备 %s 发送指令失败: %v", deviceName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -188,6 +208,8 @@ type CommandResult struct {
|
||||
|
||||
// SendCommandAndWait 发送指令并等待响应
|
||||
func (ws *WebSocketService) SendCommandAndWait(deviceID, command string, data interface{}, timeout int) (*CommandResponse, error) {
|
||||
deviceName := ws.getDeviceDisplayName(deviceID)
|
||||
|
||||
// 如果未指定超时时间,使用默认超时时间
|
||||
if timeout <= 0 {
|
||||
timeout = ws.defaultTimeout
|
||||
@@ -236,7 +258,7 @@ func (ws *WebSocketService) SendCommandAndWait(deviceID, command string, data in
|
||||
return commandResponse, nil
|
||||
case <-ctx.Done():
|
||||
// 超时处理
|
||||
return nil, fmt.Errorf("等待设备响应超时")
|
||||
return nil, fmt.Errorf("等待设备 %s 响应超时", deviceName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +280,7 @@ func (ws *WebSocketService) HandleMessage(deviceID string, message []byte) error
|
||||
// 解析消息
|
||||
var msg WebSocketMessage
|
||||
if err := json.Unmarshal(message, &msg); err != nil {
|
||||
return fmt.Errorf("解析设备 %s 消息失败: %v", deviceID, err)
|
||||
return fmt.Errorf("解析设备 %s 消息失败: %v", ws.getDeviceDisplayName(deviceID), err)
|
||||
}
|
||||
|
||||
// 更新心跳时间
|
||||
@@ -280,14 +302,14 @@ func (ws *WebSocketService) HandleMessage(deviceID string, message []byte) error
|
||||
// 成功发送
|
||||
default:
|
||||
// 通道已满,丢弃消息
|
||||
ws.logger.Warn(fmt.Sprintf("设备 %s 的响应通道已满,丢弃响应消息", deviceID))
|
||||
ws.logger.Warn(fmt.Sprintf("设备 %s 的响应通道已满,丢弃响应消息", ws.getDeviceDisplayName(deviceID)))
|
||||
}
|
||||
}
|
||||
ws.mutex.RUnlock()
|
||||
}
|
||||
|
||||
// 记录消息日志
|
||||
ws.logger.Info(fmt.Sprintf("收到来自设备 %s 的消息: %v", deviceID, msg))
|
||||
ws.logger.Info(fmt.Sprintf("收到来自设备 %s 的消息: %v", ws.getDeviceDisplayName(deviceID), msg))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user