1. 完善websocket通信逻辑
2. 实现Switch接口
This commit is contained in:
@@ -43,6 +43,21 @@ type SwitchResponseData struct {
|
||||
DeviceType string `json:"device_type"`
|
||||
DeviceID string `json:"device_id"`
|
||||
Action string `json:"action"`
|
||||
Status string `json:"status"` // 添加状态字段
|
||||
Message string `json:"message"` // 添加消息字段
|
||||
}
|
||||
|
||||
// RelayControlData 发送给中继设备的控制数据结构体
|
||||
type RelayControlData struct {
|
||||
DeviceType string `json:"device_type"`
|
||||
DeviceID string `json:"device_id"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
// RelayControlResponseData 中继设备控制响应数据结构体
|
||||
type RelayControlResponseData struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// Switch 设备控制接口
|
||||
@@ -67,31 +82,45 @@ func (c *Controller) Switch(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
// 通过WebSocket向中继设备发送控制指令
|
||||
// 这里假设中继设备ID为"relay-001",在实际应用中应该根据设备层级结构动态获取
|
||||
controlData := map[string]interface{}{
|
||||
"device_type": req.DeviceType,
|
||||
"device_id": req.DeviceID,
|
||||
"action": req.Action,
|
||||
controlData := RelayControlData{
|
||||
DeviceType: req.DeviceType,
|
||||
DeviceID: req.DeviceID,
|
||||
Action: req.Action,
|
||||
}
|
||||
|
||||
err := c.websocketService.SendCommand("relay-001", "control_device", controlData)
|
||||
// 发送指令并等待响应
|
||||
response, err := c.websocketService.SendCommandAndWait("relay-001", "control_device", controlData, 0)
|
||||
if err != nil {
|
||||
c.logger.Error("通过WebSocket发送设备控制指令失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "设备控制失败")
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "设备控制失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 使用响应中的状态和消息
|
||||
status := "解析失败"
|
||||
message := "消息解析失败"
|
||||
|
||||
// 如果响应中没有明确的状态和消息,则从数据中提取
|
||||
if status == "" && message == "" {
|
||||
// 解析响应数据
|
||||
var responseData RelayControlResponseData
|
||||
if err := response.ParseData(&responseData); err == nil {
|
||||
status = responseData.Status
|
||||
message = responseData.Message
|
||||
}
|
||||
}
|
||||
|
||||
// 创建设备控制记录
|
||||
if err := c.createDeviceControlRecord(
|
||||
user.ID,
|
||||
req.DeviceID,
|
||||
req.DeviceType,
|
||||
req.Action,
|
||||
"success",
|
||||
"设备控制成功",
|
||||
status,
|
||||
message,
|
||||
); err != nil {
|
||||
c.logger.Error("创建设备控制记录失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "设备控制失败")
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "记录控制历史失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -99,6 +128,8 @@ func (c *Controller) Switch(ctx *gin.Context) {
|
||||
DeviceType: req.DeviceType,
|
||||
DeviceID: req.DeviceID,
|
||||
Action: req.Action,
|
||||
Status: status,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "设备控制成功", data)
|
||||
|
||||
@@ -13,13 +13,15 @@ import (
|
||||
type Controller struct {
|
||||
websocketService *service.WebSocketService
|
||||
logger *logs.Logger
|
||||
service *service.WebSocketService
|
||||
}
|
||||
|
||||
// NewController 创建远程控制控制器实例
|
||||
func NewController(websocketService *service.WebSocketService) *Controller {
|
||||
func NewController(websocketService *service.WebSocketService, service *service.WebSocketService) *Controller {
|
||||
return &Controller{
|
||||
websocketService: websocketService,
|
||||
logger: logs.NewLogger(),
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +34,17 @@ type SendCommandRequest struct {
|
||||
|
||||
// SendCommandResponseData 发送指令响应数据结构体
|
||||
type SendCommandResponseData struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
Command string `json:"command"`
|
||||
Status string `json:"status"`
|
||||
DeviceID string `json:"device_id"`
|
||||
Command string `json:"command"`
|
||||
Status string `json:"status"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// RelayCommandData 发送到中继设备的命令数据结构体
|
||||
type RelayCommandData struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
Command string `json:"command"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// SendCommand 向设备发送指令接口
|
||||
@@ -54,7 +64,14 @@ func (c *Controller) SendCommand(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
// 通过WebSocket服务向设备发送指令
|
||||
err := c.websocketService.SendCommand(req.DeviceID, req.Command, req.Data)
|
||||
commandData := RelayCommandData{
|
||||
DeviceID: req.DeviceID,
|
||||
Command: req.Command,
|
||||
Data: req.Data,
|
||||
}
|
||||
|
||||
// 发送指令并等待响应
|
||||
response, err := c.websocketService.SendCommandAndWait(req.DeviceID, req.Command, commandData, 0)
|
||||
if err != nil {
|
||||
c.logger.Error("发送指令失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "发送指令失败: "+err.Error())
|
||||
@@ -65,6 +82,7 @@ func (c *Controller) SendCommand(ctx *gin.Context) {
|
||||
DeviceID: req.DeviceID,
|
||||
Command: req.Command,
|
||||
Status: "sent",
|
||||
Data: response.Data,
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "指令发送成功", data)
|
||||
|
||||
Reference in New Issue
Block a user