定义控制器和注册路由(服务层和领域层没实现)
This commit is contained in:
@@ -84,11 +84,14 @@ func (a *API) setupRoutes() {
|
||||
// 区域主控相关路由组
|
||||
areaControllerGroup := authGroup.Group("/area-controllers")
|
||||
{
|
||||
areaControllerGroup.POST("", a.areaControllerController.CreateAreaController) // 创建区域主控
|
||||
areaControllerGroup.GET("", a.areaControllerController.ListAreaControllers) // 获取区域主控列表
|
||||
areaControllerGroup.GET("/:id", a.areaControllerController.GetAreaController) // 获取单个区域主控
|
||||
areaControllerGroup.PUT("/:id", a.areaControllerController.UpdateAreaController) // 更新区域主控
|
||||
areaControllerGroup.DELETE("/:id", a.areaControllerController.DeleteAreaController) // 删除区域主控
|
||||
areaControllerGroup.POST("", a.areaControllerController.CreateAreaController) // 创建区域主控
|
||||
areaControllerGroup.GET("", a.areaControllerController.ListAreaControllers) // 获取区域主控列表
|
||||
areaControllerGroup.GET("/:id", a.areaControllerController.GetAreaController) // 获取单个区域主控
|
||||
areaControllerGroup.PUT("/:id", a.areaControllerController.UpdateAreaController) // 更新区域主控
|
||||
areaControllerGroup.DELETE("/:id", a.areaControllerController.DeleteAreaController) // 删除区域主控
|
||||
areaControllerGroup.POST("/:id/ota/start", a.areaControllerController.StartUpgrade) // 开始升级
|
||||
areaControllerGroup.GET("/ota/progress/:taskId", a.areaControllerController.GetUpgradeProgress) // 获取升级进度
|
||||
areaControllerGroup.POST("/ota/tasks/:taskId/stop", a.areaControllerController.StopUpgrade) // 停止升级
|
||||
}
|
||||
logger.Debug("区域主控相关接口注册成功 (需要认证和审计)")
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package dto
|
||||
|
||||
import "git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
import (
|
||||
"mime/multipart"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
// CreateDeviceRequest 定义了创建设备时需要传入的参数
|
||||
type CreateDeviceRequest struct {
|
||||
@@ -101,3 +105,22 @@ type DeviceTemplateResponse struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// OtaUpgradeRequest 定义了 OTA 升级请求时需要传入的参数
|
||||
type OtaUpgradeRequest struct {
|
||||
FirmwareFile *multipart.FileHeader `form:"firmware_file" validate:"required"` // 固件压缩包文件
|
||||
}
|
||||
|
||||
// OtaUpgradeResponse 定义了 OTA 升级响应的结构
|
||||
type OtaUpgradeResponse struct {
|
||||
TaskID uint32 `json:"task_id"` // OTA 升级任务ID
|
||||
}
|
||||
|
||||
// OtaUpgradeProgressResponse 定义了 OTA 升级进度响应的结构
|
||||
type OtaUpgradeProgressResponse struct {
|
||||
TaskID uint32 `json:"task_id"` // OTA 升级任务ID
|
||||
CurrentStage models.OTATaskStatus `json:"current_stage"` // 当前阶段
|
||||
ExecutedNum uint32 `json:"executed_num"` // 已执行步骤数
|
||||
TotalNum uint32 `json:"total_num"` // 总步骤数
|
||||
Message string `json:"message"` // 状态消息
|
||||
}
|
||||
|
||||
@@ -18,6 +18,13 @@ type AreaControllerService interface {
|
||||
ListAreaControllers(ctx context.Context) ([]*dto.AreaControllerResponse, error)
|
||||
UpdateAreaController(ctx context.Context, id uint32, req *dto.UpdateAreaControllerRequest) (*dto.AreaControllerResponse, error)
|
||||
DeleteAreaController(ctx context.Context, id uint32) error
|
||||
|
||||
// StartUpgrade 用于启动一个 OTA 升级任务。
|
||||
StartUpgrade(ctx context.Context, areaControllerID uint32, firmware *dto.OtaUpgradeRequest) (*dto.OtaUpgradeResponse, error)
|
||||
// GetUpgradeProgress 用于查询指定 OTA 任务的进度。
|
||||
GetUpgradeProgress(ctx context.Context, taskID uint32) (*dto.OtaUpgradeProgressResponse, error)
|
||||
// StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
|
||||
StopUpgrade(ctx context.Context, taskID uint32) error
|
||||
}
|
||||
|
||||
// areaControllerService 是 AreaControllerService 接口的具体实现。
|
||||
@@ -138,3 +145,21 @@ func (s *areaControllerService) DeleteAreaController(ctx context.Context, id uin
|
||||
// 3. 执行删除
|
||||
return s.areaControllerRepo.Delete(serviceCtx, id)
|
||||
}
|
||||
|
||||
// StartUpgrade 用于启动一个 OTA 升级任务。
|
||||
func (s *areaControllerService) StartUpgrade(ctx context.Context, areaControllerID uint32, firmware *dto.OtaUpgradeRequest) (*dto.OtaUpgradeResponse, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// GetUpgradeProgress 用于查询指定 OTA 任务的进度。
|
||||
func (s *areaControllerService) GetUpgradeProgress(ctx context.Context, taskID uint32) (*dto.OtaUpgradeProgressResponse, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
|
||||
func (s *areaControllerService) StopUpgrade(ctx context.Context, taskID uint32) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
@@ -79,14 +79,13 @@ type OtaService interface {
|
||||
// StartUpgrade 用于启动一个 OTA 升级任务。
|
||||
// areaControllerID: 目标区域主控的设备 ID。
|
||||
// firmwarePath: 新固件文件所在的临时目录的绝对路径。
|
||||
// targetVersion: 目标固件的版本号。
|
||||
// 返回创建的 OTA 任务 ID 和可能发生的错误。
|
||||
StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath, targetVersion string) (uint32, error)
|
||||
StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath string) (uint32, error)
|
||||
|
||||
// GetUpgradeProgress 用于查询指定 OTA 任务的进度。
|
||||
// taskID: 要查询的 OTA 任务 ID。
|
||||
// 返回 OTA 任务的当前状态和可能发生的错误。
|
||||
GetUpgradeProgress(ctx context.Context, taskID uint32) (status models.OTATaskStatus, err error)
|
||||
// 返回 OTA 任务的已执行步骤数和总步骤数和当前阶段和可能发生的错误。
|
||||
GetUpgradeProgress(ctx context.Context, taskID uint32) (executed, total uint32, CurrentStage models.OTATaskStatus, err error)
|
||||
|
||||
// StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
|
||||
// taskID: 要停止的 OTA 任务 ID。
|
||||
|
||||
@@ -21,12 +21,12 @@ func NewOtaService(otaRepo repository.OtaRepository, deviceRepo repository.Devic
|
||||
}
|
||||
}
|
||||
|
||||
func (o *otaServiceImpl) StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath, targetVersion string) (uint32, error) {
|
||||
func (o *otaServiceImpl) StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath string) (uint32, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (o *otaServiceImpl) GetUpgradeProgress(ctx context.Context, taskID uint32) (status models.OTATaskStatus, err error) {
|
||||
func (o *otaServiceImpl) GetUpgradeProgress(ctx context.Context, taskID uint32) (executed, total uint32, CurrentStage models.OTATaskStatus, err error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user