Browse Source

设备注册时更新在线状态

longan 1 year ago
parent
commit
b04a9546d4

+ 1 - 1
api/v1/ipcast/enter.go

@@ -16,7 +16,7 @@ var (
 )
 
 func IpcastBaseUrl(c *gin.Context) string {
-	header := c.GetHeader("gateway-sn")
+	header := c.GetHeader("isn")
 	conn, ok := gatewayServer.ConnMap[header]
 	if !ok {
 		return ""

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

@@ -52,11 +52,12 @@ func (i SystemApi) PutEmail(c *gin.Context) {
 }
 
 func GetHost(c *gin.Context) (string, error) {
-	devId := c.Query("devId")
-	if devId == "" {
-		return "", errors.New("设备Id为空")
+	//devId := c.Query("devId")
+	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对应连接失败")
 	}

+ 20 - 3
gatewayServer/c2s.go

@@ -1,16 +1,19 @@
 package gatewayServer
 
 import (
+	"encoding/json"
 	"encoding/xml"
 	"fmt"
 	"github.com/sirupsen/logrus"
 	"lcfns/global"
 	"lcfns/isapi"
 	appModel "lcfns/model/app"
+	ipResp "lcfns/model/ipcast/response"
 	"lcfns/service"
 	appService "lcfns/service/app"
 	is "lcfns/service/isapi"
 	"net"
+	"net/http"
 	"strings"
 )
 
@@ -118,6 +121,23 @@ func c2s4001(adu AppDataUnit, c net.Conn) {
 	setServer(baseUrl)
 	//配置摄像头-sip服务器
 	setSIP(baseUrl, camera.ID, cs)
+	//检查ip音柱
+	url := "http://" + strings.Split(c.RemoteAddr().String(), ":")[0] + ":8849" + "/v1/check_alive"
+	ipcastService := service.ServiceGroupApp.IpcastServiceGroup.IpcastService
+	ipcastAppService := service.ServiceGroupApp.AppServiceGroup.IpcastService
+	all, err := ipcastService.Common(url, http.MethodGet, nil)
+	if err != nil {
+		logrus.Error(err)
+		return
+	}
+	var resp1 ipResp.PingResp
+	json.Unmarshal(all, &resp1)
+	if resp1.Code == 200 {
+		ipcastAppService.Create(&appModel.Ipcast{
+			Gid:   gw.ID,
+			State: 1,
+		})
+	}
 }
 
 // 心跳响应
@@ -133,8 +153,6 @@ func c2s4002(adu AppDataUnit, c net.Conn) {
 	if err != nil {
 		logrus.Errorf("写入数据错误 %e\n", err)
 	}
-	//logrus.Info("heartbeat :", adu.DevId)
-	//todo 网关注册后,网关再接入摄像头如何注册摄像头--
 }
 
 // 查询参数响应
@@ -149,7 +167,6 @@ func c2s4004(adu AppDataUnit) {
 
 func setServer(host string) uint {
 	marshal, err := xml.Marshal(global.Config.HttpHostNotificationList)
-	fmt.Println("host:", string(marshal))
 	if err != nil {
 		logrus.Errorf("事件主机配置文件解析错误: %e\n", err)
 		return 0

+ 2 - 0
isapi/client.go

@@ -39,6 +39,7 @@ func NewClient(host, username, password string) (*Client, error) {
 }
 
 func TouChuan(host string, o Operation, data []byte, c *gin.Context) (resp any, err error) {
+	fmt.Println("请求参数: ", string(data))
 	resp, err = Com(host, o, data)
 	if err != nil {
 		logrus.Error(err)
@@ -56,6 +57,7 @@ func TouChuan(host string, o Operation, data []byte, c *gin.Context) (resp any,
 
 // Com 透传
 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

+ 0 - 3
isapi/isapi_smart_fieldDetection.go

@@ -3,7 +3,6 @@ package isapi
 import (
 	"encoding/json"
 	"encoding/xml"
-	"fmt"
 	"github.com/sirupsen/logrus"
 )
 
@@ -79,8 +78,6 @@ func (c *Client) GetSizeFd() (resp SmartCalibrationList, err error) {
 		return
 	}
 	err = xml.Unmarshal(bytes, &resp)
-	fmt.Printf("%+v\n", string(bytes))
-	fmt.Printf("%+v\n", resp)
 	return
 }
 

+ 0 - 7
isapi/isapi_smart_lineDetection.go

@@ -3,7 +3,6 @@ package isapi
 import (
 	"encoding/json"
 	"encoding/xml"
-	"fmt"
 	"github.com/sirupsen/logrus"
 )
 
@@ -93,12 +92,6 @@ func (c *Client) GetLineDetection() (resp LineDetectionParam, err error) {
 		return
 	}
 	err = xml.Unmarshal(bytes, &resp)
-	marshal, err := json.Marshal(resp)
-	if err != nil {
-		logrus.Error("请求出错", err)
-		return
-	}
-	fmt.Println("json数据:", string(marshal))
 	return
 }
 

+ 0 - 7
isapi/isapi_smart_regionEntrance.go

@@ -3,7 +3,6 @@ package isapi
 import (
 	"encoding/json"
 	"encoding/xml"
-	"fmt"
 	"github.com/sirupsen/logrus"
 )
 
@@ -89,12 +88,6 @@ func (c *Client) GetRegionEntrance() (resp RegionEntranceParam, err error) {
 		return
 	}
 	err = xml.Unmarshal(bytes, &resp)
-	marshal, err := json.Marshal(resp)
-	if err != nil {
-		logrus.Error("请求出错", err)
-		return
-	}
-	fmt.Println("json数据:", string(marshal))
 	return
 }
 

+ 1 - 0
model/app/camera.go

@@ -13,6 +13,7 @@ type Camera struct {
 	IsDeleted  uint   `gorm:"column:is_deleted"`
 	SN         string `json:"sn" gorm:"column:sn"`
 	MacAddress string `json:"macAddress" gorm:"column:mac_address"`
+	State      uint8  `json:"state" gorm:"column:state"`
 	IsRegisted uint   `gorm:"column:is_registed;comment:是否已注册"`
 	StreamId   string `json:"streamId" gorm:"column:stream_id"`
 	StreamUrl  string `json:"streamUrl" form:"streamUrl" gorm:"column:stream_url;comment:流地址;"`

+ 2 - 1
model/app/gateway.go

@@ -12,7 +12,8 @@ type Gateway struct {
 	Osn         string `json:"osn" form:"osn" gorm:"column:osn;comment:外部sn;"`
 	//CreatedAt time.Time
 	//DeletedAt time.Time
-	IsDeleted   uint   `json:"isDeleted" gorm:"column:is_deleted"`
+	State       uint8  `json:"state"  gorm:"column:state;comment:在线状态;"`
+	IsDeleted   uint8  `json:"isDeleted" gorm:"column:is_deleted"`
 	Description string `json:"description" gorm:"column:description;comment:描述"`
 }
 

+ 10 - 5
model/app/ipcast.go

@@ -1,9 +1,14 @@
 package app
 
 type Ipcast struct {
-	ID        uint `json:"id"`
-	DeviceId  int  `json:"deviceId"`
-	Gid       uint `json:"gid"`
-	State     uint `json:"state"`
-	IsDeleted uint `json:"isDeleted"`
+	Name      string `json:"name" gorm:"column:name"`
+	DevId     string `json:"devId" gorm:"column:device_id"`
+	Gid       uint   `json:"gid" gorm:"column:gid"`
+	State     uint8  `json:"state" gorm:"column:state"`
+	IsDeleted uint8  `json:"isDeleted" gorm:"column:is_deleted"`
+}
+
+// TableName Ipcast 表名
+func (Ipcast) TableName() string {
+	return "ipcast"
 }

+ 14 - 11
model/app/response/gateway.go

@@ -1,15 +1,18 @@
 package response
 
 type Gateways struct {
-	ID          int    `json:"id" gorm:"column:id"`
-	GatewayName string `json:"gatewayName" gorm:"column:gateway_name"`
-	Osn         string `json:"osn" gorm:"column:osn"`
-	Description string `json:"description" gorm:"column:description"`
-	CameraName  string `json:"cameraName" gorm:"column:camera_name"`
-	CameraSn    string `json:"cameraSn" gorm:"column:camera_sn"`
-	StreamId    string `json:"streamId" gorm:"column:stream_id"`
-	StreamUrl   string `json:"streamUrl" gorm:"column:stream_url"`
-	IpcastId    int    `json:"ipcastId" gorm:"column:ipcast_id"`
-	IpcastName  string `json:"ipcastName" gorm:"column:ipcast_name"`
-	Status      int    `json:"ipcastState" gorm:"column:ipcast_state"`
+	ID           int    `json:"id" gorm:"column:id"`
+	GatewayName  string `json:"gatewayName" gorm:"column:gateway_name"`
+	Isn          string `json:"isn" gorm:"column:isn"`
+	Osn          string `json:"osn" gorm:"column:osn"`
+	GatewayState uint8  `json:"gatewayState" gorm:"column:gateway_state"`
+	Description  string `json:"description" gorm:"column:description"`
+	CameraName   string `json:"cameraName" gorm:"column:camera_name"`
+	CameraSn     string `json:"cameraSn" gorm:"column:camera_sn"`
+	StreamId     string `json:"streamId" gorm:"column:stream_id"`
+	StreamUrl    string `json:"streamUrl" gorm:"column:stream_url"`
+	CameraState  uint8  `json:"cameraState" gorm:"column:camera_state"`
+	//IpcastId     int    `json:"ipcastId" gorm:"column:ipcast_id"`
+	//IpcastName   string `json:"ipcastName" gorm:"column:ipcast_name"`
+	IpcastState uint8 `json:"ipcastState" gorm:"column:ipcast_state"`
 }

+ 0 - 1
model/ipcast/ipcast.go

@@ -1 +0,0 @@
-package ipcast

+ 2 - 4
service/app/camera.go

@@ -30,14 +30,13 @@ func (cameraService *CameraService) CreateIfNotExist(info isapi.DeviceInfo, gwId
 	if err != nil {
 		return err
 	}
-	fmt.Println(camera)
 	if camera.SN != "" {
 		//已注册至数据库,绑定至网关
 		global.Db.Model(&app.Camera{}).Where("id = ?", camera.ID).Update("gid", gwId)
 		return errors.New("摄像头已注册")
 	}
 	//注册
-	var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress}
+	var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress, State: 1}
 	err = global.Db.Create(ca).Debug().Error
 	if err != nil {
 		return err
@@ -80,8 +79,7 @@ func (cameraService *CameraService) DeleteCameraByIds(ids request.IdsReq, delete
 // Author [piexlmax](https://github.com/piexlmax)
 func (cameraService *CameraService) UpdateCamera(camera app.Camera) (err error) {
 	//err = global.Db.Debug().Omit("created_at, deleted_at, id, Gid, created_by, deleted_by, user_name, password").SaveEventPicture(&camera).Error
-	fmt.Println("camera", camera)
-	err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name, sn,mac_address, deleted_by").Save(&camera).Error
+	err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name,state, sn,mac_address, deleted_by").Save(&camera).Error
 	return err
 }
 

+ 1 - 0
service/app/enter.go

@@ -5,4 +5,5 @@ type ServiceGroup struct {
 	GatewayService
 	EventService
 	PictureService
+	IpcastService
 }

+ 0 - 2
service/app/event.go

@@ -1,7 +1,6 @@
 package app
 
 import (
-	"fmt"
 	"github.com/sirupsen/logrus"
 	"lcfns/global"
 	"lcfns/model/app"
@@ -17,7 +16,6 @@ type EventService struct {
 func (EventService) List(req request.Event) (events []response.Event, err error) {
 	//SELECT id,event_type,event_code,start_time,handle_time
 	//FROM `event` WHERE mac_address IN (SELECT c.mac_address FROM gateway g JOIN camera c ON g.id = c.gid WHERE g.uid = 1);
-	fmt.Printf("参数:%+v\n", req)
 	//查找用户所有摄像头的mac_address:
 	//SELECT c.mac_address FROM gateway g JOIN camera c ON g.id = c.gid WHERE g.uid = ?
 	var macs []string

+ 5 - 2
service/app/gateway.go

@@ -24,7 +24,7 @@ func (gs *GatewayService) CreateIfNotExist(isn string) (*app.Gateway, error) {
 	if gate != nil {
 		return gate, ExistError
 	}
-	var gateway = &app.Gateway{Isn: isn}
+	var gateway = &app.Gateway{Isn: isn, State: 1}
 	err = global.Db.Create(gateway).Debug().Error
 	if err != nil {
 		return nil, err
@@ -39,7 +39,10 @@ func (gs *GatewayService) List(req request.GatewayRequest) (list []response.Gate
 	//	c.`name` AS camera_name, RIGHT(c.sn,9) AS sn,stream_id,stream_url,
 	//	l.device_id AS ls_id,l.`status` AS ls_status
 	//FROM gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN loudspeaker l ON  g.id = l.gid ;
-	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.isn,g.osn,g.state AS gateway_state,g.description," +
+		"c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url,c.state AS camera_state," +
+		//"i.device_id AS ipcast_id," +
+		"i.state AS ipcast_state").
 		Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON  g.id = i.gid").
 		Where("g.is_deleted = 0")
 	//条件查询设备,网关名,网关osn,摄像头sn,ip音柱devid,

+ 14 - 0
service/app/ipcast.go

@@ -0,0 +1,14 @@
+package app
+
+import (
+	"lcfns/global"
+	"lcfns/model/app"
+)
+
+type IpcastService struct {
+}
+
+func (is *IpcastService) Create(ipcast *app.Ipcast) (err error) {
+	err = global.Db.Create(ipcast).Error
+	return
+}

+ 0 - 2
service/isapi/SipService.go

@@ -1,7 +1,6 @@
 package isapi
 
 import (
-	"fmt"
 	"gorm.io/gorm"
 	"lcfns/global"
 	"lcfns/model/isapi"
@@ -22,7 +21,6 @@ func GetSipUserID() (int, error) {
 		if err != nil {
 			return err
 		}
-		fmt.Println("user_id ", is.UserId)
 		return nil
 	})
 	return is.UserId, erro

+ 5 - 3
utils/email.go

@@ -1,7 +1,7 @@
 package utils
 
 import (
-	"fmt"
+	"github.com/sirupsen/logrus"
 	mail "github.com/xhit/go-simple-mail/v2"
 	"log"
 )
@@ -27,7 +27,8 @@ func EmailPicture(To, subject, data string, pic *mail.File) {
 	//client.Password = "kk992471"            // 替换为SMTP服务器的密码
 	smtpClient, err := emailClient.Connect()
 	if err != nil {
-		fmt.Println("client.Connect()", err)
+		logrus.Error("email client Connect err", err)
+		return
 	}
 	// 创建邮件对象
 	email := mail.NewMSG()
@@ -65,7 +66,8 @@ func EmailPicture(To, subject, data string, pic *mail.File) {
 func Email(To, subject string, body string) {
 	smtpClient, err := emailClient.Connect()
 	if err != nil {
-		fmt.Println("client.Connect()", err)
+		logrus.Error("email client connect err", err)
+		return
 	}
 	// 创建邮件对象
 	email := mail.NewMSG()