瀏覽代碼

camera 所有接口

terry 2 年之前
父節點
當前提交
c093dcc06d

+ 73 - 4
app/controller/cameraController.go

@@ -5,7 +5,9 @@ import (
 	"iot_manager_service/app/model"
 	"iot_manager_service/app/service"
 	"iot_manager_service/app/utils"
+	"math"
 	"net/http"
+	"strconv"
 )
 
 // 摄像头管理对象
@@ -14,14 +16,47 @@ var Camera = new(cameraCtl)
 type cameraCtl struct{}
 
 func (c *cameraCtl) Detail(ctx *gin.Context) {
-	var req *model.CameraDetail
-	if err := ctx.ShouldBind(&req); err != nil {
-		ctx.JSON(http.StatusOK, nil)
+	id, e := strconv.Atoi(ctx.Query("id"))
+	if e != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
+		return
+	}
+
+	device, err := service.CameraService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
 		return
 	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
 func (c *cameraCtl) List(ctx *gin.Context) {
+	searchValue := ctx.Query("searchValue")
+	cameraType := ctx.Query("cameraType")
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+
+	devices, err := service.CameraService.List(searchValue, cameraType, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(devices)) / float64(size))
+	rsp := model.RspCameraList{
+		Current: current,
+		Size:    size,
+		Total:   len(devices),
+		Pages:   int(pages),
+		Records: devices,
+	}
+
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
 }
 
 func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
@@ -33,12 +68,24 @@ func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
 	}
 	err := service.CameraService.CreateOrUpdate(req)
 	if err != nil {
-		ctx.JSON(http.StatusOK, nil)
+		ctx.JSON(http.StatusOK, err)
 		return
 	}
+	ctx.JSON(http.StatusOK, nil)
 }
 
 func (c *cameraCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqCameraRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.CameraService.Remove(req.IDs)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
 func (c *cameraCtl) Count(ctx *gin.Context) {
@@ -54,7 +101,29 @@ func (c *cameraCtl) ExportTemplate(ctx *gin.Context) {
 }
 
 func (c *cameraCtl) GetList(ctx *gin.Context) {
+	devices, err := service.CameraService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
+
 }
 
 func (c *cameraCtl) Enable(ctx *gin.Context) {
+	var req *model.ReqCameraEnable
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	if req.Status != model.Enable_Enable && req.Status != model.Enable_Disable {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(model.EnableInvalid, nil))
+		return
+	}
+	err := service.CameraService.Enable(req.ID, req.Status)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }

+ 23 - 6
app/dao/cameraDao.go

@@ -8,7 +8,7 @@ type CameraDevice struct {
 	DeviceName       string    `gorm:"type:varchar(64)" json:"deviceName"`             //设备名称
 	DeviceSN         string    `gorm:"type:varchar(64)" json:"deviceSn"`               //设备序列号
 	CameraType       int       `gorm:"type:int" json:"cameraType"`                     //摄像机类型 0=枪机,1=球机
-	GatewayId        string    `gorm:"type:varchar(32)" json:"gatewayId"`              //所属网关
+	GatewayId        int       `gorm:"type:int" json:"gatewayId"`                      //所属网关
 	LampPoleId       int       `gorm:"type:int" json:"lampPoleId"`                     //归属灯杆 灯杆ID
 	LampPoleName     string    `gorm:"type:varchar(64)" json:"lampPoleName"`           //灯杆名称
 	LampPoleSn       string    `gorm:"type:varchar(64)" json:"lampPoleSn"`             //灯杆sn
@@ -58,14 +58,31 @@ func (c *CameraDevice) GetDevice() error {
 	return err
 }
 
-func (c CameraDevice) GetDevices(offset, limit int) ([]Gateway, error) {
-	var devices []Gateway
+func (c CameraDevice) GetDevices(offset, limit int) ([]CameraDevice, error) {
+	var devices []CameraDevice
 	db := GDb.Model(&c)
 	if c.DeviceSN != "" {
-		db = db.Where("(device_name like ? or device_sn like ?) and is_deleted = 0", "%"+c.DeviceSN+"%",
-			"%"+c.DeviceSN+"%")
+		db = db.Where("device_name like ? or device_sn like ?", "%"+c.DeviceSN+"%", "%"+c.DeviceSN+"%")
 	}
+	if c.CameraType != -1 {
+		db = db.Where("camera_type = ?", c.CameraType)
+	}
+
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}
 
-	err := db.Offset(offset).Limit(limit).Find(&devices).Error
+func (c *CameraDevice) Delete() error {
+	return GDb.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
+		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
+}
+
+func (c CameraDevice) GetAllDevices() ([]*CameraDevice, error) {
+	var devices []*CameraDevice
+	err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
 	return devices, err
 }
+
+func (c *CameraDevice) UpdateEnable() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"is_enable": c.IsEnable}).Error
+}

+ 1 - 0
app/dao/common.go

@@ -33,6 +33,7 @@ func InitDB() {
 			&OnDemandGroup{},
 			&OnDemandSensor{},
 			&Zigbee{},
+			&CameraDevice{},
 		).Error
 		if err != nil {
 			panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 2 - 2
app/dao/gatewayDao.go

@@ -83,10 +83,10 @@ func (c Gateway) GetDevices(offset, limit int) ([]Gateway, error) {
 	var devices []Gateway
 	db := GDb.Model(&c)
 	if c.GatewaySN != "" {
-		db = db.Where("(gateway_name like ? or gateway_sn like ?) and is_deleted = 0", "%"+c.GatewaySN+"%", "%"+c.GatewaySN+"%")
+		db = db.Where("gateway_name like ? or gateway_sn like ?", "%"+c.GatewaySN+"%", "%"+c.GatewaySN+"%")
 	}
 
-	err := db.Offset(offset).Limit(limit).Find(&devices).Error
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 2 - 2
app/dao/lampPoleDao.go

@@ -86,7 +86,7 @@ func (c LampPole) GetDevices(offset, limit int) ([]LampPole, error) {
 
 	db := GDb.Model(&c)
 	if c.PoleSN != "" {
-		db = db.Where("(pole_name like ? or pole_sn like ?) and is_deleted = 0", "%"+c.PoleSN+"%", "%"+c.PoleSN+"%")
+		db = db.Where("pole_name like ? or pole_sn like ?", "%"+c.PoleSN+"%", "%"+c.PoleSN+"%")
 	}
 	if c.GroupId != -1 {
 		db = db.Where("group_id = ?", c.GroupId)
@@ -94,7 +94,7 @@ func (c LampPole) GetDevices(offset, limit int) ([]LampPole, error) {
 	if c.BoxId != -1 {
 		db = db.Where("box_id = ?", c.BoxId)
 	}
-	err := db.Offset(offset).Limit(limit).Find(&devices).Error
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 2 - 2
app/dao/lightControlDao.go

@@ -94,7 +94,7 @@ func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
 
 	db := GDb.Model(&c)
 	if c.SN != "" {
-		db = db.Where("(name like ? or sn like ?) and is_deleted = 0", "%"+c.SN+"%", "%"+c.SN+"%")
+		db = db.Where("name like ? or sn like ?", "%"+c.SN+"%", "%"+c.SN+"%")
 	}
 	if c.ControlType != 0 {
 		db = db.Where("control_type = ?", c.ControlType)
@@ -103,7 +103,7 @@ func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
 		db = db.Where("zigbee_id = ?", c.ZigbeeId)
 	}
 
-	err := db.Offset(offset).Limit(limit).Find(&devices).Error
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 1 - 2
app/dao/onDemandGroupDao.go

@@ -68,8 +68,7 @@ func (c *OnDemandGroup) GetDevice() error {
 
 func (c OnDemandGroup) GetDevices(offset, limit int) ([]OnDemandGroup, error) {
 	var devices []OnDemandGroup
-	err := GDb.Model(&c).Where("(pole_group_name like ? or sn like ?) and is_deleted = 0", "%"+c.PoleGroupName+"%",
-		"%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where("(pole_group_name like ? or sn like ?) and is_deleted = 0", "%"+c.PoleGroupName+"%", "%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 31 - 0
app/model/camera.go

@@ -4,4 +4,35 @@ import "iot_manager_service/app/dao"
 
 type CameraDetail struct {
 	dao.CameraDevice
+	PoleGroupName   string `json:"poleGroupName"`   //品牌名称
+	Brand           string `json:"brand"`           //型号名称
+	Model           string `json:"model"`           //摄像头类型名称
+	CameraTypeName  string `json:"cameraTypeName"`  //运行状态
+	RunState        string `json:"runState"`        //网络状态
+	NetworkState    string `json:"networkState"`    //网络状态
+	EndLineTime     string `json:"endLineTime"`     //最后在线时间
+	QueryGatewayIds string `json:"queryGatewayIds"` //虚拟字段---用作网关调用这里关联
+	GatewayName     string `json:"gatewayName"`     //所属网关名称
+	GatewaySn       string `json:"gatewaySn"`       //所属网关编码
+}
+
+type RspCameraList struct {
+	Records []CameraDetail `json:"records"` //记录列表
+	Current int            `json:"current"` //当前分页
+	Size    int            `json:"size"`    //每页数量
+	Total   int            `json:"total"`   //总数
+	Pages   int            `json:"pages"`   //总页数
+}
+
+type ReqCameraRemove struct {
+	IDs  int    `json:"ids"`  //分组编码
+	SN   int    `json:"sn"`   //sn
+	Name string `json:"name"` //名称
+}
+
+type ReqCameraEnable struct {
+	ID     int `json:"id"`     //编码
+	Status int `json:"status"` //启用禁用:1启用2禁用
+	Name   int `json:"name"`   //名称
+	SN     int `json:"sn"`     //sn
 }

+ 7 - 0
app/model/common.go

@@ -25,6 +25,13 @@ const (
 	Enable_Disable = 2 //停用
 )
 
+const (
+	CameraType_Ball      = 1 //球机
+	CameraType_Gun       = 2 //枪机
+	CameraType_Ball_Name = "球机"
+	CameraType_Gun_Name  = "枪机"
+)
+
 type AlarmTerminal struct {
 }
 type CaptureUnit struct {

+ 106 - 0
app/service/cameraService.go

@@ -2,8 +2,10 @@ package service
 
 import (
 	"fmt"
+	"iot_manager_service/app/dao"
 	"iot_manager_service/app/model"
 	"iot_manager_service/app/utils"
+	"strconv"
 	"time"
 )
 
@@ -57,3 +59,107 @@ func (s *cameraService) CreateOrUpdate(req *model.CameraDetail) *utils.Errors {
 	//todo operation record
 	return utils.SuccessResponse(utils.Succeeded, nil)
 }
+
+func (s *cameraService) Get(id int) (*model.CameraDetail, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.CameraDevice{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	detail := &model.CameraDetail{CameraDevice: *device}
+	if device.CameraType == model.CameraType_Ball {
+		detail.CameraTypeName = model.CameraType_Ball_Name
+	} else if device.CameraType == model.CameraType_Gun {
+		detail.CameraTypeName = model.CameraType_Gun_Name
+	}
+
+	//todo 获取运行状态
+	// runState = 	forVidiconService.queryRtStatus();
+	return detail, nil
+}
+
+func (s *cameraService) List(searchValue, cameraType string, current, size int) ([]model.CameraDetail, *utils.Errors) {
+	var details []model.CameraDetail
+	device := dao.CameraDevice{}
+
+	if searchValue != "" {
+		device.DeviceSN = searchValue
+	}
+	if cameraType != "" {
+		t, _ := strconv.Atoi(cameraType)
+		device.CameraType = t
+	} else {
+		device.CameraType = -1
+	}
+
+	offset := (current - 1) * size
+	limit := size
+	devices, err := device.GetDevices(offset, limit)
+
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	for _, d := range devices {
+		details = append(details, model.CameraDetail{
+			CameraDevice: d,
+			RunState:     "", // todo queryRtStatus
+		})
+	}
+
+	return details, nil
+}
+
+func (s *cameraService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.CameraDevice{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: "TODO", // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
+
+	//todo operation record
+	err := device.Delete()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *cameraService) GetList() ([]dao.CameraDevice, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.CameraDevice{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+	devices, err := device.GetAllDevices()
+	var results []dao.CameraDevice
+	// todo 不要返回多余字段,只需要id和name
+	for _, device := range devices {
+		results = append(results, dao.CameraDevice{ID: device.ID, DeviceName: device.DeviceName + "(" + device.DeviceSN + ")"})
+	}
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return results, nil
+}
+
+func (s *cameraService) Enable(id, status int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.CameraDevice{
+		ID:       id,
+		IsEnable: status,
+	}
+
+	err := device.UpdateEnable()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}