Explorar el Código

完成安防-多屏监控接口

sixian hace 2 años
padre
commit
6ca62319f4

+ 8 - 0
app/device/dao/cameraDao.go

@@ -110,3 +110,11 @@ func (c CameraDevice) GetDevicesByLampPoleGroup() []CameraDevice {
 		c.GroupId).Find(&devices)
 	return devices
 }
+
+func (c CameraDevice) GetDevicesByIds(ids string) []CameraDevice {
+	var devices []CameraDevice
+	db := Db.Debug().Model(&c).Where("is_deleted = 0")
+	db.Where("id in (" + ids + ")")
+	db.Find(&devices)
+	return devices
+}

+ 51 - 0
app/security/controller/multiscreenController.go

@@ -0,0 +1,51 @@
+package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/middleware"
+	edgeService "iot_manager_service/app/security/edge_service"
+	"iot_manager_service/app/security/service"
+	"iot_manager_service/util/common"
+	"net/http"
+	"strconv"
+)
+
+// TODO: 未记录日志
+var Multiscreen = new(multiscreenCtl)
+
+type multiscreenCtl struct{}
+
+func (c *multiscreenCtl) Ptzhome(ctx *gin.Context) {
+	code := ctx.Query("sn")
+	name := ctx.Query("name")
+	flag, _ := strconv.Atoi(ctx.Query("flag"))
+	vidiconService := edgeService.ForVidiconService{}
+	operate, err := vidiconService.Ptzhome(code, name, flag)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+	}
+	if !operate {
+		ctx.JSON(http.StatusOK, nil)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
+}
+
+func (c *multiscreenCtl) List(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	list, err := service.MultiscreenService.List(claims)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, list))
+}
+
+func (c *multiscreenCtl) Submit(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	site, _ := strconv.Atoi(ctx.Query("site"))
+	id, _ := strconv.Atoi(ctx.Query("id"))
+	service.MultiscreenService.Save(claims, site, id)
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
+}

+ 36 - 2
app/security/controller/securityController.go

@@ -3,12 +3,14 @@ package controller
 import (
 	"github.com/gin-gonic/gin"
 	"iot_manager_service/app/middleware"
+	edgeService "iot_manager_service/app/security/edge_service"
 	"iot_manager_service/app/security/service"
 	"iot_manager_service/util/common"
 	"net/http"
+	"strconv"
 )
 
-// Security 安防
+// TODO: 未记录日志
 var Security = new(securityCtl)
 
 type securityCtl struct{}
@@ -16,7 +18,6 @@ type securityCtl struct{}
 func (c *securityCtl) GetCameraLiveList(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
-
 	list, err := service.SecurityService.GetCameraLiveList(claims.TenantId)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
@@ -24,3 +25,36 @@ func (c *securityCtl) GetCameraLiveList(ctx *gin.Context) {
 	}
 	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, list))
 }
+
+func (c *securityCtl) Operate(ctx *gin.Context) {
+	code := ctx.Query("code")
+	name := ctx.Query("name")
+	atoi, _ := strconv.Atoi(ctx.Query("direction"))
+	speed, _ := strconv.Atoi(ctx.Query("speed"))
+	vidiconService := edgeService.ForVidiconService{}
+	operate, err := vidiconService.Operate(code, name, atoi, speed)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+	}
+	if !operate {
+		ctx.JSON(http.StatusOK, nil)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
+}
+
+func (c *securityCtl) Ptzhome(ctx *gin.Context) {
+	code := ctx.Query("sn")
+	name := ctx.Query("name")
+	flag, _ := strconv.Atoi(ctx.Query("flag"))
+	vidiconService := edgeService.ForVidiconService{}
+	operate, err := vidiconService.Ptzhome(code, name, flag)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+	}
+	if !operate {
+		ctx.JSON(http.StatusOK, nil)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
+}

+ 65 - 0
app/security/edge_service/forVidiconService.go

@@ -0,0 +1,65 @@
+package edge_service
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"iot_manager_service/config"
+	"net/http"
+)
+
+// Security 安防service
+type ForVidiconService struct {
+}
+
+// VidiconRespose 返回respose结构体
+type VidiconRespose struct {
+	Code int
+	Msg  string
+}
+
+// Operate 操作球机
+func (f *ForVidiconService) Operate(code string, name string, direction int, speed int) (bool, error) {
+	cfg := config.Instance()
+	api := cfg.Foreign.IotEdgeUrl + "/camera/v1/control/ptz"
+	url := fmt.Sprintf("%v?code=%v&direction=%d&speed=%d", api, code, direction, speed)
+	return f.getVidiconApi(url)
+}
+
+// Ptzhome 复位相关
+func (f *ForVidiconService) Ptzhome(code string, name string, flag int) (bool, error) {
+	cfg := config.Instance()
+	api := cfg.Foreign.IotEdgeUrl + "/camera/v1/control/ptzhome"
+	url := fmt.Sprintf("%v?code=%v&flag=%d", api, code, flag)
+	return f.getVidiconApi(url)
+}
+
+// getVidiconApi 公用请求接口
+func (f *ForVidiconService) getVidiconApi(url string) (bool, error) {
+	//fmt.Printf("url = %v", url)
+	method := "GET"
+	client := &http.Client{}
+	req, err := http.NewRequest(method, url, nil)
+	if err != nil {
+		return false, err
+	}
+	res, err := client.Do(req)
+	if err != nil {
+		return false, err
+	}
+	defer res.Body.Close()
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return false, err
+	}
+	//fmt.Printf("body = %v", string(body))
+	result := VidiconRespose{}
+	err = json.Unmarshal(body, &result)
+	if err != nil {
+		return false, err
+	}
+	if result.Code != 0 {
+		panic(result.Msg)
+	}
+	return true, nil
+}

+ 9 - 0
app/security/model/multiscreen.go

@@ -0,0 +1,9 @@
+package model
+
+type Placement struct {
+	Slot int    `json:"slot"`
+	ID   int    `json:"id"`
+	Name string `json:"name"`
+	Sn   string `json:"sn"`
+	Url  string `json:"url"`
+}

+ 1 - 0
app/security/model/security.go

@@ -13,4 +13,5 @@ type CameraLive struct {
 	MonitorAddress string `json:"monitorAddress"`
 	StreamId       string `json:"streamId"`
 	RunState       string `json:"runState"`
+	CameraType     int    `json:"cameraType"`
 }

+ 89 - 0
app/security/service/multiscreenService.go

@@ -0,0 +1,89 @@
+package service
+
+import (
+	deviceDao "iot_manager_service/app/device/dao"
+	"iot_manager_service/app/middleware"
+	"iot_manager_service/app/security/model"
+	"iot_manager_service/app/system/dao"
+	"log"
+	"strconv"
+	"strings"
+)
+
+var MultiscreenService = new(multiscreenService)
+
+type multiscreenService struct{}
+
+func (s multiscreenService) List(claims *middleware.Claims) ([]model.Placement, error) {
+	securitySixScreen := s.getSlot(claims.UserId)
+	idsSplit := strings.Split(securitySixScreen, ",")
+	var ids []string
+	for _, id := range idsSplit {
+		idInt, _ := strconv.Atoi(id)
+		if idInt < 1 {
+			continue
+		}
+		ids = append(ids, strconv.Itoa(idInt))
+	}
+	//devices := deviceDao.CameraDevice{}.GetDevicesByIds(strings.Join(ids, ","))
+	//fmt.Printf("devices = %v", devices)
+	var list []model.Placement
+	for i, id := range idsSplit {
+		idInt, _ := strconv.Atoi(id)
+		placement := model.Placement{}
+		placement.ID = idInt
+		placement.Slot = i + 1
+		if idInt != -1 {
+			device := deviceDao.CameraDevice{ID: idInt}
+			err := device.GetDevice()
+			if err != nil {
+				continue
+			}
+			placement.Name = device.DeviceName
+			placement.Sn = device.DeviceSN
+			placement.Url = device.MonitorAddress
+		}
+		list = append(list, placement)
+	}
+	//fmt.Printf("list = %v", list)
+	return list, nil
+}
+
+// Save 保存 多屏监控位置
+func (s multiscreenService) Save(claims *middleware.Claims, site, id int) error {
+	securitySixScreen := s.getSlot(claims.UserId)
+	idsSplit := strings.Split(securitySixScreen, ",")
+	ids := ""
+	for i := range idsSplit {
+		if i == (site - 1) {
+			idsSplit[i] = strconv.Itoa(id)
+		}
+		ids += idsSplit[i]
+		if i < (len(idsSplit) - 1) {
+			ids += ","
+		}
+	}
+	user := &dao.User{ID: claims.UserId}
+	user.SecuritySixScreen = ids
+	err := user.Update()
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (s multiscreenService) getSlot(userId int64) string {
+	user := &dao.User{ID: userId}
+	err := user.GetUser()
+	if err != nil {
+		log.Fatalln(err)
+		panic("用户取失败了")
+	}
+	ids := ""
+	if user.SecuritySixScreen == "" {
+		ids = "-1,-1,-1,-1,-1,-1"
+	} else {
+		ids = user.SecuritySixScreen
+	}
+	return ids
+}

+ 1 - 0
app/security/service/securityService.go

@@ -35,6 +35,7 @@ func (s *securityService) GetCameraLiveList(tenantId int) ([]model.CameraLiveDet
 				MonitorAddress: cameraDevice.MonitorAddress,
 				StreamId:       cameraDevice.StreamId,
 				RunState:       state,
+				CameraType:     cameraDevice.CameraType,
 			})
 		}
 		rsp = append(rsp, model.CameraLiveDetail{

+ 9 - 0
router/router.go

@@ -457,6 +457,15 @@ func InitRouter(engine *gin.Engine) {
 	securityGroup := engine.Group("/api/longchi/security")
 	{
 		securityGroup.GET("/getCameraLiveList", security.Security.GetCameraLiveList)
+		securityGroup.GET("/operate", security.Security.Operate)
+		securityGroup.POST("/ptzhome", security.Security.Ptzhome)
+
+		securityMultiscreenGroup := securityGroup.Group("/multiscreen")
+		{
+			securityMultiscreenGroup.GET("list", security.Multiscreen.List)
+			securityMultiscreenGroup.GET("cameraList", security.Security.GetCameraLiveList)
+			securityMultiscreenGroup.POST("submit", security.Multiscreen.Submit)
+		}
 	}
 
 	// 媒体素材