package v1 import ( "apsClient/conf" "apsClient/model/request" "apsClient/model/response" _ "apsClient/model/response" "apsClient/pkg/contextx" "apsClient/pkg/ecode" "apsClient/pkg/logx" "apsClient/service" "github.com/gin-gonic/gin" ) type DeviceApi struct{} // SetCurrentDeviceId // @Tags 设备 // @Summary 设置当前设备id // @Produce application/json // @Param object body request.SetCurrentDevice true "查询参数" // @Success 200 {object} contextx.Response{} "成功" // @Router /v1/device/setCurrentDeviceId [post] func (slf *DeviceApi) SetCurrentDeviceId(c *gin.Context) { var params request.SetCurrentDevice ctx, ok := contextx.NewContext(c, ¶ms) if !ok { return } if params.CurrentDeviceID == "" { ctx.Fail(ecode.ParamsErr) return } list, err := service.GetDeviceIDList() if err != nil { logx.Errorf("SetCurrentDeviceId GetDeviceIDList err:%v", err) ctx.Fail(ecode.DBErr) return } findFlag := false for _, item := range list { if item == params.CurrentDeviceID { findFlag = true } } if !findFlag { ctx.Fail(ecode.ParamsErr) return } service.SetDeviceIDToFile(params.CurrentDeviceID) conf.Conf.CurrentDeviceID = params.CurrentDeviceID ctx.Ok() } // Config // @Tags 设备 // @Summary 设置设备一些配置 // @Produce application/json // @Param object body request.DeviceConfig true "查询参数" // @Success 200 {object} contextx.Response{} "成功" // @Router /v1/device/config [post] func (slf *DeviceApi) Config(c *gin.Context) { var params request.DeviceConfig ctx, ok := contextx.NewContext(c, ¶ms) if !ok { return } if conf.Conf.CurrentDeviceID == "" { ctx.FailWithMsg(ecode.UnknownErr, "当前设备为空,请检查") return } err := service.UpdateDevice(conf.Conf.CurrentDeviceID, params.NeedSetProcessParams) if err != nil { logx.Errorf("save device config err:%v", err) ctx.Fail(ecode.DBErr) return } ctx.Ok() } // DeviceList // @Tags 设备 // @Summary 获取当前面板绑定的设备列表 // @Produce application/json // @Success 200 {object} contextx.Response{data=response.DeviceListResponse} "成功" // @Router /v1/device/list [get] func (slf *DeviceApi) DeviceList(c *gin.Context) { ctx, ok := contextx.NewContext(c, nil) if !ok { return } list, err := service.GetDeviceList() if err != nil { ctx.Fail(ecode.DBErr) return } resp := response.DeviceListResponse{ SystemDeviceID: conf.Conf.System.DeviceId, CurrentDeviceID: conf.Conf.CurrentDeviceID, DeviceList: list, SystemDeviceStatus: response.SystemDeviceStatusNormal, ClusterStatus: conf.Conf.SerfClusterStatus, ClusterNodeQuantity: conf.Conf.ClusterNodeQuantity, SystemDeviceRunSince: conf.Conf.SystemDeviceRunSince, } ctx.OkWithDetailed(resp) }