From 397468ddc1191bf0ce9339e544ff345d1d4e6786 Mon Sep 17 00:00:00 2001
From: qixiaoning <jony.kee@outlook.com>
Date: 星期四, 11 九月 2025 11:33:10 +0800
Subject: [PATCH] 删除摄像机增加限制
---
sync-service/models/camera.go | 207 ++++++++++++++++++++++++++-------------------------
1 files changed, 107 insertions(+), 100 deletions(-)
diff --git a/sync-service/models/camera.go b/sync-service/models/camera.go
index 6e623c9..6a1e2fa 100644
--- a/sync-service/models/camera.go
+++ b/sync-service/models/camera.go
@@ -1,97 +1,104 @@
package models
import (
- "basic.com/valib/logger.git"
"fmt"
"strconv"
"strings"
"vamicro/config"
+
+ "basic.com/valib/logger.git"
)
const (
- Default_Layer = -9999 //鎽勫儚鏈烘坊鍔犺繘鏉ョ殑榛樿妤煎眰
- TYPE_LOCAL_CAMERA = 0 //鏈湴鎽勫儚鏈�
- TYPE_GB28181_CAMERA = 1 //鍥芥爣鎽勫儚鏈�
+ Default_Layer = -9999 //鎽勫儚鏈烘坊鍔犺繘鏉ョ殑榛樿妤煎眰
+ TYPE_LOCAL_CAMERA = 0 //鏈湴鎽勫儚鏈�
+ TYPE_GB28181_CAMERA = 1 //鍥芥爣鎽勫儚鏈�
- TYPE_RUNTYPE_VIDEO = -1 //鍗曠函鐨勭洃鎺э紝涓嶅仛鍒嗘瀽
- TYPE_RUNTYPE_POLL = 0 //杞鍋氫换鍔�
- TYPE_RUNTYPE_REALTIME = 1 //瀹炴椂鍋氫换鍔�
+ TYPE_RUNTYPE_VIDEO = -1 //鍗曠函鐨勭洃鎺э紝涓嶅仛鍒嗘瀽
+ TYPE_RUNTYPE_POLL = 0 //杞鍋氫换鍔�
+ TYPE_RUNTYPE_REALTIME = 1 //瀹炴椂鍋氫换鍔�
Camera_Status_NoRule = 0 //鏈厤瑙勫垯
- Camera_Status_Wait = 1 //绛夊緟澶勭悊
- Camera_Status_Doing = 2 //澶勭悊涓�
- Camera_Status_Other = -1 //鍏朵粬鎯呭喌
+ Camera_Status_Wait = 1 //绛夊緟澶勭悊
+ Camera_Status_Doing = 2 //澶勭悊涓�
+ Camera_Status_Other = -1 //鍏朵粬鎯呭喌
)
+// 鏅烘煡浠诲姟
+type Task struct {
+ TaskId int64 `db:"task_id" json:"taskId"`
+ TaskName string `db:"task_name" json:"taskName"`
+}
+
type Camera struct {
- Id string `gorm:"primary_key;column:id;type:varchar(100);unique;" json:"id"`
- Name string `gorm:"column:name" json:"name" bind:"required"`
- Alias string `gorm:"column:alias" json:"alias"` //鎽勫儚鏈虹殑鍒悕
- SnapshotUrl string `gorm:"column:snapshot_url" json:"snapshot_url"`//蹇収鍦板潃
- Type int `gorm:"column:type" json:"type" `
- Addr string `gorm:"column:addr" json:"addr"`
- Longitude float32 `gorm:"column:longitude" json:"longitude"`
- Latitude float32 `gorm:"column:latitude" json:"latitude"`
- Floor int `gorm:"column:floor" json:"floor"`//妤煎眰
- Rtsp string `gorm:"column:rtsp" json:"rtsp"`
- Ip string `gorm:"column:ip" json:"ip"`
- Port int `gorm:"column:port" json:"port"`
- Username string `gorm:"column:username" json:"username"`
- Password string `gorm:"column:password" json:"password"`
- Brand string `gorm:"column:brand" json:"brand"`
- Reserved string `gorm:"column:reserved" json:"reserved"`
- IsRunning bool `gorm:"column:is_running" json:"is_running"`//鏄惁姝e湪瑙g爜
- RunEnable bool `gorm:"column:run_enable" json:"run_enable"`//鎺у埗瀹炴椂澶勭悊鎴栬疆璇㈠鐞嗙殑寮�鍏�
- RunType int `gorm:"column:run_type" json:"run_type"`//澶勭悊绫诲瀷锛�0锛氳疆璇紝1锛氬疄鏃�,-1:鏃犱换鍔★紝涓嶅仛鍒嗘瀽鎴栬�呭垎鏋愪换鍔¤鍏充簡
- RunServerId string `gorm:"column:run_server_id" json:"run_server_id"`//褰撳墠姝e湪澶勭悊鐨勫垎鏋愭湇鍔″櫒id
+ Id string `gorm:"primary_key;column:id;type:varchar(100);unique;" json:"id"`
+ Name string `gorm:"column:name" json:"name" bind:"required"`
+ Alias string `gorm:"column:alias" json:"alias"` //鎽勫儚鏈虹殑鍒悕
+ SnapshotUrl string `gorm:"column:snapshot_url" json:"snapshot_url"` //蹇収鍦板潃
+ Type int `gorm:"column:type" json:"type" `
+ Addr string `gorm:"column:addr" json:"addr"`
+ Longitude float32 `gorm:"column:longitude" json:"longitude"`
+ Latitude float32 `gorm:"column:latitude" json:"latitude"`
+ Floor int `gorm:"column:floor" json:"floor"` //妤煎眰
+ Rtsp string `gorm:"column:rtsp" json:"rtsp"`
+ Ip string `gorm:"column:ip" json:"ip"`
+ Port int `gorm:"column:port" json:"port"`
+ Username string `gorm:"column:username" json:"username"`
+ Password string `gorm:"column:password" json:"password"`
+ Brand string `gorm:"column:brand" json:"brand"`
+ Reserved string `gorm:"column:reserved" json:"reserved"`
+ IsRunning bool `gorm:"column:is_running" json:"is_running"` //鏄惁姝e湪瑙g爜
+ RunEnable bool `gorm:"column:run_enable" json:"run_enable"` //鎺у埗瀹炴椂澶勭悊鎴栬疆璇㈠鐞嗙殑寮�鍏�
+ RunType int `gorm:"column:run_type" json:"run_type"` //澶勭悊绫诲瀷锛�0锛氳疆璇紝1锛氬疄鏃�,-1:鏃犱换鍔★紝涓嶅仛鍒嗘瀽鎴栬�呭垎鏋愪换鍔¤鍏充簡
+ RunServerId string `gorm:"column:run_server_id" json:"run_server_id"` //褰撳墠姝e湪澶勭悊鐨勫垎鏋愭湇鍔″櫒id
- ResolutionWidth int `gorm:"column:resolution_width;default:0" json:"resolution_width"`//鍒嗚鲸鐜囧
- ResolutionHeight int `gorm:"column:resolution_height;default:0" json:"resolution_height"`//鍒嗚鲸鐜囬珮
+ ResolutionWidth int `gorm:"column:resolution_width;default:0" json:"resolution_width"` //鍒嗚鲸鐜囧
+ ResolutionHeight int `gorm:"column:resolution_height;default:0" json:"resolution_height"` //鍒嗚鲸鐜囬珮
- VoiceEnable bool `gorm:"column:voiceEnable;default:0" json:"voiceEnable"`
- VoiceId string `gorm:"column:voiceId" json:"voiceId"`
+ VoiceEnable bool `gorm:"column:voiceEnable;default:0" json:"voiceEnable"`
+ VoiceId string `gorm:"column:voiceId" json:"voiceId"`
}
type CameraTreeNode struct {
- Id string `json:"id"`
- Areaid string `json:"areaid"`
- Name string `json:"name"`
- CameraType int `json:"cameratype"`//鏄湰鍦拌繕鏄浗鏍�
- Rtsp string `json:"rtsp"`
+ Id string `json:"id"`
+ Areaid string `json:"areaid"`
+ Name string `json:"name"`
+ CameraType int `json:"cameratype"` //鏄湰鍦拌繕鏄浗鏍�
+ Rtsp string `json:"rtsp"`
}
-func (camera *Camera) GetIdNameMap() (map[string]string) {
- cameraEMap :=make(map[string]string,0)
+func (camera *Camera) GetIdNameMap() map[string]string {
+ cameraEMap := make(map[string]string, 0)
allCams, err := camera.FindAll()
- if err ==nil && allCams !=nil && len(allCams) >0 {
- for _,model :=range allCams {
- cameraEMap[model.Id]=model.Name
+ if err == nil && allCams != nil && len(allCams) > 0 {
+ for _, model := range allCams {
+ cameraEMap[model.Id] = model.Name
}
}
return cameraEMap
}
-//cameraType (0:鏌ュ叏閮�,1:鍒嗘瀽鎽勫儚鏈�,2:鐩戞帶鎽勫儚鏈�)
-//cameraName
+// cameraType (0:鏌ュ叏閮�,1:鍒嗘瀽鎽勫儚鏈�,2:鐩戞帶鎽勫儚鏈�)
+// cameraName
func (camera *Camera) Find(cameraType int, cameraName string, camType int, isPlatform int) (camMenus []CameraTreeNode, err error) {
- logger.Debug("camType:",camType)
+ logger.Debug("camType:", camType)
cType := strconv.Itoa(camType)
- var sql = "select c.id,ca.areaid as areaid,case ifnull(c.alias,'') when '' then c.name else c.alias end as name,c.type as cameratype,c.rtsp from camera_area ca join cameras c on ca.cameraid=c.id where c.type="+cType+""
+ var sql = "select c.id,ca.areaid as areaid,case ifnull(c.alias,'') when '' then c.name else c.alias end as name,c.type as cameratype,c.rtsp from camera_area ca join cameras c on ca.cameraid=c.id where c.type=" + cType + ""
if cameraName != "" {
sql += " and c.name like '%" + cameraName + "%'"
}
if cameraType == 1 { //鏌ュ垎鏋愭憚鍍忔満
- sql += " and (c.run_type="+strconv.Itoa(TYPE_RUNTYPE_POLL)+" or c.run_type="+strconv.Itoa(TYPE_RUNTYPE_REALTIME)+")"
+ sql += " and (c.run_type=" + strconv.Itoa(TYPE_RUNTYPE_POLL) + " or c.run_type=" + strconv.Itoa(TYPE_RUNTYPE_REALTIME) + ")"
} else if cameraType == 2 { //鏌ョ洃鎺ф憚鍍忔満
- sql += " and c.run_type="+strconv.Itoa(TYPE_RUNTYPE_VIDEO)+""
+ sql += " and c.run_type=" + strconv.Itoa(TYPE_RUNTYPE_VIDEO) + ""
} else if cameraType == 3 { //鏌ヨ仈鍔ㄦ憚鍍忔満
sql += " and c.id in (select crga.camera_id from camera_rule_group_arg crga join camera_task_link ctl on crga.group_id=ctl.link_task_id)"
}
- if camType == TYPE_LOCAL_CAMERA {//鍥芥爣鐨勫缁堟樉绀烘暣妫垫爲锛屾湰鍦版爲鏈満鏌ユ湰鏈猴紝骞冲彴鏌ユ墍鏈�
- if isPlatform == 0 {//鏌ユ湰鏈�
- sql += " and c.run_server_id='"+config.Server.AnalyServerId+"'"
+ if camType == TYPE_LOCAL_CAMERA { //鍥芥爣鐨勫缁堟樉绀烘暣妫垫爲锛屾湰鍦版爲鏈満鏌ユ湰鏈猴紝骞冲彴鏌ユ墍鏈�
+ if isPlatform == 0 { //鏌ユ湰鏈�
+ sql += " and c.run_server_id='" + config.Server.AnalyServerId + "'"
}
}
@@ -103,31 +110,31 @@
return camMenus, nil
}
-func (cam *Camera) FindAllByServer(serverId string,cameraName string)(list []Camera,err error) {
+func (cam *Camera) FindAllByServer(serverId string, cameraName string) (list []Camera, err error) {
var sql = "select * from cameras where 1=1 "
if serverId != "" {
- sql += " and (run_server_id='"+serverId+"' or (run_server_id ='' or run_server_id is NULL))"
+ sql += " and (run_server_id='" + serverId + "' or (run_server_id ='' or run_server_id is NULL))"
}
if cameraName != "" {
- sql += " and name like '%"+cameraName+"%'"
+ sql += " and name like '%" + cameraName + "%'"
}
- if err := db.Raw(sql).Scan(&list).Error;err !=nil{
- return nil,err
+ if err := db.Raw(sql).Scan(&list).Error; err != nil {
+ return nil, err
}
- return list,nil
+ return list, nil
}
-func (camera *Camera) GetCamerasByRunType(runType int,cameraName string) (list []Camera,err error) {
- analyServerId := config.Server.AnalyServerId//褰撳墠鍒嗘瀽鏈嶅姟鍣ㄧ殑id(analyServerId,鍦ㄩ厤缃枃浠朵腑)
+func (camera *Camera) GetCamerasByRunType(runType int, cameraName string) (list []Camera, err error) {
+ analyServerId := config.Server.AnalyServerId //褰撳墠鍒嗘瀽鏈嶅姟鍣ㄧ殑id(analyServerId,鍦ㄩ厤缃枃浠朵腑)
sql := "select * from cameras where run_type=? and run_server_id=? "
- if cameraName !=""{
- sql = sql+" and name like ?"
+ if cameraName != "" {
+ sql = sql + " and name like ?"
}
- if err := db.Raw(sql,runType,analyServerId,"%"+cameraName+"%").Scan(&list).Error;err !=nil{
- return nil,err
+ if err := db.Raw(sql, runType, analyServerId, "%"+cameraName+"%").Scan(&list).Error; err != nil {
+ return nil, err
}
- return list,nil
+ return list, nil
}
func (camera *Camera) Insert() (err error) {
@@ -164,10 +171,10 @@
}
func (camera *Camera) FindAllMap() map[string]Camera {
- m := make(map[string]Camera,0)
+ m := make(map[string]Camera, 0)
cams, err := camera.FindAll()
- if err ==nil && cams !=nil {
- for _,model :=range cams {
+ if err == nil && cams != nil {
+ for _, model := range cams {
m[model.Id] = model
}
}
@@ -175,14 +182,14 @@
}
func (camera *Camera) Update() (err error) {
- logger.Debug("camera:",camera)
- longitude := fmt.Sprintf("%3.4f",camera.Longitude)
- latitude := fmt.Sprintf("%3.4f",camera.Latitude)
+ logger.Debug("camera:", camera)
+ longitude := fmt.Sprintf("%3.4f", camera.Longitude)
+ latitude := fmt.Sprintf("%3.4f", camera.Latitude)
voiceEnable := 0
if camera.VoiceEnable {
voiceEnable = 1
}
- sql := "update cameras set name='"+camera.Name+"',alias='"+camera.Alias+"',type="+strconv.Itoa(camera.Type)+",addr='"+camera.Addr+"',longitude="+longitude+",latitude="+latitude+",rtsp='"+camera.Rtsp+"',ip='"+camera.Ip+"',port="+strconv.Itoa(camera.Port)+",username='"+camera.Username+"',password='"+camera.Password+"',brand='"+camera.Brand+"',reserved='"+camera.Reserved+"',run_server_id='"+camera.RunServerId+"',resolution_width="+strconv.Itoa(camera.ResolutionWidth)+",resolution_height="+strconv.Itoa(camera.ResolutionHeight)+",voiceEnable="+strconv.Itoa(voiceEnable)+",voiceId='"+camera.VoiceId+"' where id='"+camera.Id+"'"
+ sql := "update cameras set name='" + camera.Name + "',alias='" + camera.Alias + "',type=" + strconv.Itoa(camera.Type) + ",addr='" + camera.Addr + "',longitude=" + longitude + ",latitude=" + latitude + ",rtsp='" + camera.Rtsp + "',ip='" + camera.Ip + "',port=" + strconv.Itoa(camera.Port) + ",username='" + camera.Username + "',password='" + camera.Password + "',brand='" + camera.Brand + "',reserved='" + camera.Reserved + "',run_server_id='" + camera.RunServerId + "',resolution_width=" + strconv.Itoa(camera.ResolutionWidth) + ",resolution_height=" + strconv.Itoa(camera.ResolutionHeight) + ",voiceEnable=" + strconv.Itoa(voiceEnable) + ",voiceId='" + camera.VoiceId + "' where id='" + camera.Id + "'"
if err := db.Exec(sql).Error; err != nil {
return err
@@ -191,87 +198,87 @@
return nil
}
-func (camera *Camera) UpdateRunEnable(cameraId string,runEnable bool) bool{
+func (camera *Camera) UpdateRunEnable(cameraId string, runEnable bool) bool {
result := db.Exec("update cameras set run_enable=? where id=?", runEnable, cameraId)
- if result.Error !=nil {
+ if result.Error != nil {
return false
}
- return result.RowsAffected>0
+ return result.RowsAffected > 0
}
-func (camera *Camera) ChangeRunType(cameraId string,runType int) bool{
+func (camera *Camera) ChangeRunType(cameraId string, runType int) bool {
result := db.Exec("update cameras set run_type=? where id=?", runType, cameraId)
- if result.Error !=nil {
+ if result.Error != nil {
return false
}
- return result.RowsAffected>0
+ return result.RowsAffected > 0
}
-func (camera *Camera) UpdateIsRunningState(cameraId string,isRunning bool) bool{
+func (camera *Camera) UpdateIsRunningState(cameraId string, isRunning bool) bool {
isRunningStr := "0"
if isRunning {
isRunningStr = "1"
}
- result := db.Exec("update cameras set is_running="+isRunningStr+" where id='"+cameraId+"'")
- if result.Error !=nil {
+ result := db.Exec("update cameras set is_running=" + isRunningStr + " where id='" + cameraId + "'")
+ if result.Error != nil {
return false
}
- return result.RowsAffected>0
+ return result.RowsAffected > 0
}
func (camera *Camera) UpdateIsRunningAll(camIds []string) bool {
analyServerId := config.Server.AnalyServerId
- sql := "update cameras set is_running=0 where run_server_id='"+analyServerId+"'"
+ sql := "update cameras set is_running=0 where run_server_id='" + analyServerId + "'"
uIds := ""
if camIds != nil {
- for _,id := range camIds {
- uIds = uIds + "'"+id+"',"
+ for _, id := range camIds {
+ uIds = uIds + "'" + id + "',"
}
}
uIds = strings.Trim(uIds, ",")
if uIds != "" {
- sql += " and id not in ("+uIds+");update cameras set is_running=1 where run_server_id='"+analyServerId+"' and id in ("+uIds+");"
+ sql += " and id not in (" + uIds + ");update cameras set is_running=1 where run_server_id='" + analyServerId + "' and id in (" + uIds + ");"
}
logger.Debug("UpdateIsRunningAll sql:", sql)
result := db.Exec(sql)
- if result.Error !=nil {
+ if result.Error != nil {
return false
}
return true
}
-func (camera *Camera) UpdateSnapshot(cameraId string, snapshot string) bool{
+func (camera *Camera) UpdateSnapshot(cameraId string, snapshot string) bool {
result := db.Exec("update cameras set snapshot_url=? where id=?", snapshot, cameraId)
- if result.Error !=nil {
+ if result.Error != nil {
return false
}
- return result.RowsAffected>0
+ return result.RowsAffected > 0
}
func (camera *Camera) Delete(cid string) (int64, error) {
var err error
tx := GetDB().Begin()
defer func() {
- if err !=nil && tx !=nil{
+ if err != nil && tx != nil {
tx.Rollback()
}
}()
- dbdel := tx.Exec("delete from cameras where id=?",cid)
+ dbdel := tx.Exec("delete from cameras where id=?", cid)
err = dbdel.Error
if err != nil || dbdel.RowsAffected == 0 {
return 0, err
}
- if err = tx.Exec("delete from camera_polygon where camera_id=?",cid).Error;err !=nil{
- return 0,err
+ if err = tx.Exec("delete from camera_polygon where camera_id=?", cid).Error; err != nil {
+ return 0, err
}
- if err = tx.Exec("delete from camera_area where cameraId=?",cid).Error;err !=nil {
- return 0,err
+ if err = tx.Exec("delete from camera_area where cameraId=?", cid).Error; err != nil {
+ return 0, err
}
- if err = tx.Exec("delete from camera_sensor where camera_id=?",cid).Error;err !=nil {
- return 0,err
+ if err = tx.Exec("delete from camera_sensor where camera_id=?", cid).Error; err != nil {
+ return 0, err
}
tx.Commit()
//鍙戝竷鏁版嵁鏇存敼娑堟伅
return dbdel.RowsAffected, nil
-}
\ No newline at end of file
+}
--
Gitblit v1.8.0