1. 优化前端显示
2. 优化日志输出
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/storage/repository"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
@@ -68,16 +69,30 @@ type WebSocketService struct {
|
||||
|
||||
// logger 日志记录器
|
||||
logger *logs.Logger
|
||||
|
||||
// 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(),
|
||||
deviceRepo: deviceRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
@@ -89,7 +104,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 移除设备连接
|
||||
@@ -97,9 +113,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))
|
||||
}
|
||||
|
||||
// SendCommand 向指定设备发送指令
|
||||
@@ -108,8 +126,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)
|
||||
}
|
||||
|
||||
// 构造消息
|
||||
@@ -122,7 +142,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
|
||||
@@ -146,7 +166,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)
|
||||
}
|
||||
|
||||
// 更新心跳时间
|
||||
@@ -159,7 +179,7 @@ func (ws *WebSocketService) HandleMessage(deviceID string, message []byte) error
|
||||
}
|
||||
|
||||
// 记录消息日志
|
||||
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