engineerper vor 1 Jahr
Ursprung
Commit
fd2e95b22f
7 geänderte Dateien mit 83 neuen und 44 gelöschten Zeilen
  1. 13 5
      api/v1/app/gateway.go
  2. 4 4
      api/v1/isapi/system.go
  3. 0 5
      gatewayServer/c2s.go
  4. 34 12
      isapi/client.go
  5. 5 5
      isapi/response.go
  6. 21 4
      isapi/sip.go
  7. 6 9
      service/app/gateway.go

+ 13 - 5
api/v1/app/gateway.go

@@ -1,12 +1,14 @@
 package app
 
 import (
+	"fmt"
 	"github.com/gin-gonic/gin"
-	"github.com/sirupsen/logrus"
 	"lc-fangdaosha/model/app"
 	"lc-fangdaosha/model/app/request"
 	"lc-fangdaosha/model/common/response"
 	"lc-fangdaosha/service"
+	"lc-fangdaosha/utils"
+	"strconv"
 )
 
 type GatewayApi struct {
@@ -17,10 +19,16 @@ var gs = service.ServiceGroupApp.AppServiceGroup.GatewayService
 // List 获取网关列表
 func (api *GatewayApi) List(c *gin.Context) {
 	var req request.GatewayRequest
-	err := c.ShouldBindJSON(&req)
-	if err != nil {
-		logrus.Error("请求数据绑定错误", err)
-	}
+	id := utils.GetUserID(c)
+	req.Uid = int(id)
+	req.Name = c.Query("gatewayName")
+	req.Osn = c.Query("osn")
+	req.CameraSn = c.Query("cameraSn")
+	v2 := c.Query("pageSize")
+	req.PageSize, _ = strconv.Atoi(v2)
+	v3 := c.Query("page")
+	req.Page, _ = strconv.Atoi(v3)
+	fmt.Printf("参数:%+v\n", req)
 	list, err := gs.List(req)
 	if err != nil {
 		response.FailWithMessage(err.Error(), c)

+ 4 - 4
api/v1/isapi/system.go

@@ -50,11 +50,11 @@ func (i SystemApi) PutEmail(c *gin.Context) {
 
 // GetHost 获取设备连接的host
 func GetHost(c *gin.Context) (string, error) {
-	devId := c.Query("devId")
-	if devId == "" {
-		return "", errors.New("设备Id为空")
+	isn := c.GetHeader("isn")
+	if isn == "" {
+		return "", errors.New("网关isn为空")
 	}
-	conn, ok := gatewayServer.ConnMap[devId]
+	conn, ok := gatewayServer.ConnMap[isn]
 	if !ok {
 		return "", errors.New("获取设备Id对应连接失败")
 	}

+ 0 - 5
gatewayServer/c2s.go

@@ -27,7 +27,6 @@ func ParseAndResponse(adu AppDataUnit, c net.Conn) {
 	case CmdCodeServerC2S:
 		c2s4000(adu, c)
 	case CmdCodeUpdateC2S:
-		//更新固件
 	default:
 		fmt.Println(adu.String())
 	}
@@ -67,7 +66,6 @@ func c2s4001(adu AppDataUnit, c net.Conn) {
 	gs := service.ServiceGroupApp.AppServiceGroup.GatewayService
 	gw, err := gs.CreateIfNotExist(adu.DevId)
 	if err != nil {
-		//if errors.Is(err, appService.ExistError) {
 		if err == appService.ExistError {
 			logrus.Infof("网关[%s]登录成功", adu.DevId)
 		} else {
@@ -119,7 +117,6 @@ func c2s4002(adu AppDataUnit, c net.Conn) {
 	if err != nil {
 		logrus.Errorf("写入数据错误 %e\n", err)
 	}
-	//todo 网关注册后,网关再接入摄像头如何注册摄像头--
 }
 
 // 查询参数响应
@@ -145,12 +142,10 @@ func setServer(host string) uint {
 		logrus.Error("配置监听主机错误", err)
 	}
 	var resp = data.(isapi.ResponseStatus)
-	//err = xml.Unmarshal(bytes, &resp)
 	if err != nil || (resp.StatusCode != 0 && resp.StatusCode != 1) {
 		logrus.Errorf("配置监听服务器响应 err:%s,StatusCode:%d,StatusString:%s", err.Error(), resp.StatusCode, resp.StatusString)
 		return 0
 	}
-	//logrus.Info("配置监听主机响应", string(bytes))
 	return 1
 }
 

+ 34 - 12
isapi/client.go

@@ -24,7 +24,6 @@ type Client struct {
 	BaseURL string
 }
 
-// NewClient 获取新的连接
 func NewClient(host, username, password string) (*Client, error) {
 	u, err := url.Parse("http://" + host)
 	if err != nil {
@@ -38,23 +37,32 @@ func NewClient(host, username, password string) (*Client, error) {
 	}, nil
 }
 
-// TouChuan 透传
-func TouChuan(host string, o Operation, data []byte, c *gin.Context) ([]byte, error) {
-	info, err := Com(host, o, data)
+func TouChuan(host string, o Operation, data []byte, c *gin.Context) (resp any, err error) {
+	resp, err = Com(host, o, data)
 	if err != nil {
-		return nil, fmt.Errorf("TouChuan错误: %e", err)
+		logrus.Error(err)
+		response.FailWithDetailed(resp.(ResponseStatus), err.Error(), c)
+		return
 	}
-	if err != nil {
-		response.FailWithMessage(err.Error(), c)
-		logrus.Errorf("%s error: %e", o, err)
-		return nil, err
+	//断言成功,说明是配置响应,检查配置结果
+	r, ok := resp.(ResponseStatus)
+	if ok {
+		if r.StatusCode == 0 || r.StatusCode == 1 {
+			response.OkWithDetailed(resp, "操作成功", c)
+			return
+		} else {
+			response.FailWithDetailed(resp, "操作失败,请联系管理员", c)
+			return
+		}
 	}
-	c.Data(200, `application/xml; charset="UTF-8"`, info)
-	return info, nil
+	//断言失败,说明是查询响应,直接返回数据
+	response.OkWithData(resp, c)
+	return
 }
 
 // Com 透传
-func Com(host string, o Operation, data []byte) ([]byte, error) {
+func Com(host string, o Operation, data []byte) (any, error) {
+	//TODO 应该使用公共的http client
 	c, err := NewClient(host, "admin", "kk176@lc")
 	if err != nil {
 		return nil, err
@@ -66,6 +74,16 @@ func Com(host string, o Operation, data []byte) ([]byte, error) {
 		return c.GetSystemCap()
 	case GetChanCap:
 		return c.GetOneChanEventCap()
+	//遮盖侦测
+	//case GetTamperDetection:
+	//	return c.GetTamperDetection("1")
+	//case PutTamperDetection:
+	//	return c.PutTamperDetection("1", data)
+	//移动侦测
+	//case GetMotionDetection:
+	//	return c.GetMotionDetection("1")
+	//case PutMotionDetection:
+	//	return c.PutMotionDetection("1", data)
 	//越界侦测
 	case GetLineDetectionCap:
 		return c.GetLineDetectionCap()
@@ -100,6 +118,10 @@ func Com(host string, o Operation, data []byte) ([]byte, error) {
 	case PutRegionEntrance:
 		return c.PutRegionEntrance(data)
 		//离开区域
+		//case GetRegionExiting:
+		//	return c.GetRegionExiting()
+		//case PutRegionExiting:
+		//	return c.PutRegionExiting(data)
 	case SetHosts:
 		return c.PutHost(data)
 	case SetSIP:

+ 5 - 5
isapi/response.go

@@ -1,9 +1,9 @@
 package isapi
 
 type ResponseStatus struct {
-	ResponseUrl   string `xml:"responseUrl"`
-	StatusCode    int    `xml:"statusCode"`
-	StatusString  string `xml:"statusString"`
-	SubStatusCode string `xml:"subStatusCode"`
-	Description   string `xml:"description"`
+	ResponseUrl   string `xml:"responseUrl"json:"responseURL"`
+	StatusCode    int    `xml:"statusCode"json:"statusCode"`
+	StatusString  string `xml:"statusString"json:"statusString"`
+	SubStatusCode string `xml:"subStatusCode"json:"subStatusCode"`
+	Description   string `xml:"description"json:"description"`
 }

+ 21 - 4
isapi/sip.go

@@ -1,5 +1,10 @@
 package isapi
 
+import (
+	"encoding/xml"
+	"github.com/sirupsen/logrus"
+)
+
 type SIPServerList struct {
 	SIPServer []struct {
 		ID        string `xml:"id" yaml:"ID"`
@@ -43,12 +48,24 @@ type SIPServerList struct {
 	} `xml:"SIPServer"`
 }
 
-func (c *Client) PutSip(data []byte) ([]byte, error) {
-	return c.CommonPut(data, "/ISAPI/System/Network/SIP")
+func (c *Client) PutSip(data []byte) (resp ResponseStatus, err error) {
+	bytes, err := c.CommonPut(data, "/ISAPI/System/Network/SIP")
+	if err != nil {
+		logrus.Error("请求出错", err)
+		return
+	}
+	err = xml.Unmarshal(bytes, &resp)
+	return
 }
 
-func (c *Client) PutSipInfo(data []byte) ([]byte, error) {
-	return c.CommonPut(data, "/ISAPI/System/Network/SIP/1/SIPInfo")
+func (c *Client) PutSipInfo(data []byte) (resp ResponseStatus, err error) {
+	bytes, err := c.CommonPut(data, "/ISAPI/System/Network/SIP/1/SIPInfo")
+	if err != nil {
+		logrus.Error("请求出错", err)
+		return
+	}
+	err = xml.Unmarshal(bytes, &resp)
+	return
 }
 
 type SIPInfo struct {

+ 6 - 9
service/app/gateway.go

@@ -15,7 +15,6 @@ var ExistError = errors.New("已存在")
 type GatewayService struct {
 }
 
-// CreateIfNotExist 创建
 func (gs *GatewayService) CreateIfNotExist(isn string) (*app.Gateway, error) {
 	var gate *app.Gateway
 	err := global.Db.Select("id, isn").Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", isn).Scan(&gate).Debug().Error
@@ -36,7 +35,7 @@ func (gs *GatewayService) CreateIfNotExist(isn string) (*app.Gateway, error) {
 
 // List 该用户下所有设备数据
 func (gs *GatewayService) List(req request.GatewayRequest) (list []response.Gateways, err error) {
-	db := global.Db.Select("g.id,g.gateway_name,g.osn,g.description,c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url,i.device_id AS ipcast_id,i.state AS ipcast_state").
+	db := global.Db.Select("g.id,g.gateway_name,g.osn,g.description,c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url").
 		Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
 		Where("g.is_deleted = 0")
 	//条件查询设备,网关名,网关osn,摄像头sn
@@ -47,7 +46,7 @@ func (gs *GatewayService) List(req request.GatewayRequest) (list []response.Gate
 		db.Where("g.osn = ?", req.Osn)
 	}
 	if req.CameraSn != "" {
-		db.Where("c.camera_sn LIKE ?", "%"+req.CameraSn)
+		db.Where("c.sn LIKE ?", "%"+req.CameraSn)
 	}
 	if req.Name != "" {
 		db.Where("gateway_name LIKE ?", "%"+req.Name+"%")
@@ -104,7 +103,6 @@ func (gs *GatewayService) DeleteGateways(gw app.Gateway) (err error) {
 			logrus.Error("删除摄像头失败", err)
 		}
 	}
-
 	return err
 }
 
@@ -118,18 +116,17 @@ func (gs *GatewayService) UnbindGateways(gw app.Gateway) error {
 			err = errors.Join(err1)
 		}
 	}
-
 	if err == nil {
 		logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Info("解绑成功")
 	}
 	return err
 }
 
-// DevIds 获取设备的id
+// DevIds 查询网关及关联设备的id
 func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
-	//查询网关及关联设备的id
-	global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn,i.id AS iid").
+	global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn").
 		Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
-		Where("g.id = ?", id).Find(&devs)
+		Where("g.id = ?", id).
+		Find(&devs)
 	return
 }