定义控制器和注册路由(服务层和领域层没实现)

This commit is contained in:
2025-12-05 16:08:06 +08:00
parent 7017ffa128
commit 2bb187071f
9 changed files with 805 additions and 38 deletions

View File

@@ -198,3 +198,113 @@ func (c *AreaControllerController) DeleteAreaController(ctx echo.Context) error
logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
}
// StartUpgrade godoc
// @Summary 启动区域主控OTA升级
// @Description 为指定的区域主控上传固件并启动一个OTA升级任务
// @Tags 区域主控管理
// @Security BearerAuth
// @Accept mpfd
// @Produce json
// @Param id path string true "区域主控ID"
// @Param firmware_file formData file true "固件压缩包文件"
// @Success 200 {object} controller.Response{data=dto.OtaUpgradeResponse}
// @Router /api/v1/area-controllers/{id}/ota/start [post]
func (c *AreaControllerController) StartUpgrade(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StartUpgrade")
const actionType = "启动区域主控OTA升级"
acID := ctx.Param("id")
id, err := strconv.ParseUint(acID, 10, 64)
if err != nil {
logger.Errorf("%s: 无效的区域主控ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID: "+acID, actionType, "无效的ID", acID)
}
var req dto.OtaUpgradeRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求: "+err.Error(), actionType, "请求绑定失败", req)
}
resp, err := c.areaControllerService.StartUpgrade(reqCtx, uint32(id), &req)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
}
logger.Errorf("%s: 服务层启动升级失败: %v, ID: %s", actionType, err, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "启动升级失败: "+err.Error(), actionType, "服务层启动升级失败", acID)
}
logger.Infof("%s: 升级任务启动成功, 任务ID: %d", actionType, resp.TaskID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "升级任务启动成功", resp, actionType, "升级任务启动成功", resp)
}
// GetUpgradeProgress godoc
// @Summary 查询OTA升级进度
// @Description 根据任务ID查询指定OTA升级任务的当前进度
// @Tags 区域主控管理
// @Security BearerAuth
// @Produce json
// @Param taskId path string true "OTA任务ID"
// @Success 200 {object} controller.Response{data=dto.OtaUpgradeProgressResponse}
// @Router /api/v1/area-controllers/ota/progress/{taskId} [get]
func (c *AreaControllerController) GetUpgradeProgress(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetUpgradeProgress")
const actionType = "查询OTA升级进度"
taskIDStr := ctx.Param("taskId")
taskID, err := strconv.ParseUint(taskIDStr, 10, 64)
if err != nil {
logger.Errorf("%s: 无效的任务ID: %s", actionType, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID: "+taskIDStr, actionType, "无效的ID", taskIDStr)
}
resp, err := c.areaControllerService.GetUpgradeProgress(reqCtx, uint32(taskID))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warnf("%s: 升级任务不存在, ID: %s", actionType, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "升级任务未找到", actionType, "升级任务不存在", taskIDStr)
}
logger.Errorf("%s: 服务层查询进度失败: %v, ID: %s", actionType, err, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询进度失败: "+err.Error(), actionType, "服务层查询进度失败", taskIDStr)
}
logger.Infof("%s: 查询进度成功, 任务ID: %d", actionType, resp.TaskID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "查询进度成功", resp, actionType, "查询进度成功", resp)
}
// StopUpgrade godoc
// @Summary 停止OTA升级任务
// @Description 根据任务ID请求停止一个正在进行的OTA升级任务
// @Tags 区域主控管理
// @Security BearerAuth
// @Produce json
// @Param taskId path string true "OTA任务ID"
// @Success 200 {object} controller.Response
// @Router /api/v1/area-controllers/ota/tasks/{taskId}/stop [post]
func (c *AreaControllerController) StopUpgrade(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "StopUpgrade")
const actionType = "停止OTA升级任务"
taskIDStr := ctx.Param("taskId")
taskID, err := strconv.ParseUint(taskIDStr, 10, 64)
if err != nil {
logger.Errorf("%s: 无效的任务ID: %s", actionType, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID: "+taskIDStr, actionType, "无效的ID", taskIDStr)
}
err = c.areaControllerService.StopUpgrade(reqCtx, uint32(taskID))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
logger.Warnf("%s: 升级任务不存在, ID: %s", actionType, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "升级任务未找到", actionType, "升级任务不存在", taskIDStr)
}
logger.Errorf("%s: 服务层停止任务失败: %v, ID: %s", actionType, err, taskIDStr)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "停止任务失败: "+err.Error(), actionType, "服务层停止任务失败", taskIDStr)
}
logger.Infof("%s: 停止任务请求成功, 任务ID: %s", actionType, taskIDStr)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "停止任务请求成功", nil, actionType, "停止任务请求成功", taskIDStr)
}