package apis import ( "fmt" "IotAdmin/core/sdk/api" "IotAdmin/core/sdk/pkg/jwt-auth/user" _ "IotAdmin/core/sdk/pkg/response" "github.com/gin-gonic/gin" "IotAdmin/app/iot/models" "IotAdmin/app/iot/service" "IotAdmin/app/iot/service/dto" "IotAdmin/common/permission" ) // IotDeviceApi 设备接口 type IotDeviceApi struct { api.Api } // GetPage 获取设备列表 // @Summary 获取设备列表 // @Description 获取设备列表 // @Tags 设备管理 // @Param groupId query int false "分组ID" // @Param sn query string false "设备编码" // @Param name query string false "设备名称" // @Param type query int false "设备类型 1:网关 2:表计" // @Param protocol query string false "表计协议" // @Param pageSize query int false "页条数" // @Param pageIndex query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]models.IotDevice}} "{"code": 200, "data": [...]}" // @Router /api/iot-device [get] // @Security Bearer func (e IotDeviceApi) GetPage(c *gin.Context) { req := dto.IotDeviceGetPageReq{} s := service.IotDeviceService{} err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := permission.GetPermissionFromContext(c) if req.OrgId != 0 { // 传入orgId ,就查询该组织及以下的数据 p.DataScope = permission.DataPermissionSelfOrgChildren p.OrgId = req.OrgId } list := make([]models.IotDevice, 0) var count int64 err = s.GetPage(&req, p, &list, &count) if err != nil { e.Error(500, err, fmt.Sprintf("获取设备失败,\r\n失败信息 %s", err.Error())) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } // Get 获取设备 // @Summary 获取设备 // @Description 获取设备 // @Tags 设备管理 // @Param id path int false "id" // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "data": [...]}" // @Router /api/iot-device/{id} [get] // @Security Bearer func (e IotDeviceApi) Get(c *gin.Context) { req := dto.IotDeviceGetReq{} s := service.IotDeviceService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object models.IotDevice p := permission.GetPermissionFromContext(c) err = s.Get(&req, p, &object) if err != nil { e.Error(500, err, fmt.Sprintf("获取设备失败,\r\n失败信息 %s", err.Error())) return } e.OK(object, "查询成功") } // Insert 添加设备 // @Summary 添加设备 // @Description 添加设备 // @Tags 设备管理 // @Accept application/json // @Product application/json // @Param data body dto.IotDeviceInsertReq true "data" // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}" // @Router /api/iot-device [post] // @Security Bearer func (e IotDeviceApi) Insert(c *gin.Context) { req := dto.IotDeviceInsertReq{} s := service.IotDeviceService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 设置创建人 req.SetCreateBy(user.GetUserId(c)) p := permission.GetPermissionFromContext(c) err = s.Insert(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("添加设备失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "添加成功") } // Update 修改设备 // @Summary 修改设备 // @Description 修改设备 // @Tags 设备管理 // @Accept application/json // @Product application/json // @Param id path int true "id" // @Param data body dto.IotDeviceUpdateReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}" // @Router /api/iot-device/{id} [put] // @Security Bearer func (e IotDeviceApi) Update(c *gin.Context) { req := dto.IotDeviceUpdateReq{} s := service.IotDeviceService{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } req.SetUpdateBy(user.GetUserId(c)) p := permission.GetPermissionFromContext(c) err = s.Update(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("修改设备失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "修改成功") } // Delete 删除设备 // @Summary 删除设备 // @Description 删除设备 // @Tags 设备管理 // @Param data body dto.IotDeviceDeleteReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}" // @Router /api/iot-device [delete] // @Security Bearer func (e IotDeviceApi) Delete(c *gin.Context) { s := service.IotDeviceService{} req := dto.IotDeviceDeleteReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // req.SetUpdateBy(user.GetUserId(c)) p := permission.GetPermissionFromContext(c) err = s.Remove(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("删除设备失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "删除成功") } // Refresh 刷新设备列表 // @Summary 刷新设备列表 // @Description 刷新设备列表 // @Tags 设备管理 // @Param data body dto.IotDeviceDeleteReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "刷新成功"}" // @Router /api/iot-device/refresh [post] // @Security Bearer func (e IotDeviceApi) Refresh(c *gin.Context) { s := service.IotDeviceService{} err := e.MakeContext(c).MakeOrm().MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } err = s.Refresh() if err != nil { return } e.OK("", "刷新成功") } // GetDeviceProtocols 获取表计协议列表 // @Summary 获取表计协议列表 // @Description 获取表计协议列表 // @Tags 设备管理 // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "data": [...]}" // @Router /api/iot-device/device-protocol [get] // @Security Bearer func (e IotDeviceApi) GetDeviceProtocols(c *gin.Context) { s := &service.IotDeviceService{} err := e.MakeContext(c).MakeOrm().MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } list := make([]string, 0) s.GetDeviceProtocols(&list) e.OK(list, "查询成功") } // GetReportProtocols 获取上报协议列表 // @Summary 获取上报协议列表 // @Description 获取上报协议列表 // @Tags 设备管理 // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "data": [...]}" // @Router /api/iot-device/report-protocol [get] // @Security Bearer func (e IotDeviceApi) GetReportProtocols(c *gin.Context) { s := &service.IotDeviceService{} err := e.MakeContext(c).MakeOrm().MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } list := make([]string, 0) s.GetReportProtocols(&list) e.OK(list, "查询成功") } // GetDeviceConfig 获取表计硬件配置 // @Summary 获取表计硬件配置 // @Description 获取表计硬件配置 // @Tags 设备管理 // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "data": [...]}" // @Router /api/iot-device/config/{id} [get] // @Security Bearer func (e IotDeviceApi) GetDeviceConfig(c *gin.Context) { s := &service.IotDeviceService{} req := dto.IotDeviceGetReq{} err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } data := make(map[string]interface{}) p := permission.GetPermissionFromContext(c) err = s.GetDeviceConfig(&req, p, &data) if err != nil { e.Error(500, err, err.Error()) return } e.OK(data, "查询成功") } // SetDeviceConfig 设置表计硬件配置 // @Summary 设置表计硬件配置 // @Description 设置表计硬件配置 // @Tags 设备管理 // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "message": "设置成功"}" // @Router /api/iot-device/config [put] // @Security Bearer func (e IotDeviceApi) SetDeviceConfig(c *gin.Context) { s := &service.IotDeviceService{} req := dto.IotDeviceSetConfigReq{} err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := permission.GetPermissionFromContext(c) err = s.SetDeviceConfig(&req, p) if err != nil { e.Error(500, err, err.Error()) return } e.OK(nil, "设置成功") } // GetDeviceLastData 查询表计最新数据 // @Summary 查询表计最新数据 // @Description 查询表计最新数据 // @Tags 设备管理 // @Success 200 {object} response.Response{data=models.IotDevice} "{"code": 200, "data": [...]}" // @Router /api/iot-device/data/{id} [get] // @Security Bearer func (e IotDeviceApi) GetDeviceLastData(c *gin.Context) { s := &service.IotDeviceService{} req := dto.IotDeviceGetReq{} err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.Service).Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } data := make(map[string]interface{}) p := permission.GetPermissionFromContext(c) err = s.GetDeviceLastData(&req, p, &data) if err != nil { e.Error(500, err, err.Error()) return } e.OK(data, "查询成功") }