123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package gatewayServer
- import (
- "encoding/xml"
- "fmt"
- "github.com/sirupsen/logrus"
- "lc-fangdaosha/global"
- "lc-fangdaosha/isapi"
- appModel "lc-fangdaosha/model/app"
- "lc-fangdaosha/service"
- appService "lc-fangdaosha/service/app"
- is "lc-fangdaosha/service/isapi"
- "net"
- "strings"
- )
- func ParseAndResponse(adu AppDataUnit, c net.Conn) {
- switch adu.CmdCode {
- case CmdCodeHeartbeatC2S:
- c2s4002(adu, c)
- case CmdCodeGetParamC2S:
- c2s4003(adu)
- case CmdCodeSetParameterC2S:
- c2s4004(adu)
- case CmdCodeRegisterC2S:
- c2s4001(adu, c)
- case CmdCodeServerC2S:
- c2s4000(adu, c)
- case CmdCodeUpdateC2S:
- default:
- fmt.Println(adu.String())
- }
- }
- // 请求管理服务器响应
- func c2s4000(adu AppDataUnit, c net.Conn) {
- a := AppDataUnit{
- CmdCode: CmdCodeServerS2C,
- SubCode: 0,
- DevId: adu.DevId,
- Data: ipAddress,
- }
- a.Debug()
- _, err := c.Write(Pack(a))
- if err != nil {
- logrus.Errorf("写入数据错误 %e\n", err)
- }
- logrus.Info(adu.DevId, "向登录服务器请求管理服务器")
- }
- // 注册登录响应
- func c2s4001(adu AppDataUnit, c net.Conn) {
- a := AppDataUnit{
- CmdCode: CmdCodeRegisterS2C,
- SubCode: 0,
- DevId: adu.DevId,
- }
- a.Data = "0"
- a.Debug()
- _, err := c.Write(Pack(a))
- if err != nil {
- logrus.Errorf("写入数据错误 %e\n", err)
- }
- ConnMap[adu.DevId] = c
- //1. 网关登录后检查是否已经注册到数据库,没有就要保存至数据库
- gs := service.ServiceGroupApp.AppServiceGroup.GatewayService
- gw, err := gs.CreateIfNotExist(adu.DevId)
- if err != nil {
- if err == appService.ExistError {
- logrus.Infof("网关[%s]登录成功", adu.DevId)
- } else {
- //如果错误不是"已注册",退出
- logrus.WithField("网关注册失败", adu.DevId).Error(err)
- return
- }
- }
- //2. 获取网关上摄像头和ip音柱的信息,保存至数据库
- var cs = service.ServiceGroupApp.AppServiceGroup.CameraService
- baseUrl := strings.Split(c.RemoteAddr().String(), ":")[0] + ":8848"
- resp, err := isapi.Com(baseUrl, isapi.GetDeviceInfo, nil)
- if err != nil {
- logrus.Error(err)
- return
- }
- var info = resp.(isapi.DeviceInfo)
- //创建camera记录并绑定gateway_id
- err = cs.CreateIfNotExist(info, gw.ID)
- if err != nil {
- logrus.WithField("摄像头注册失败", info.SerialNumber).Error(err)
- }
- //3.检查摄像头是否配置
- camera, err := cs.GetCameraByDeviceId(info.SerialNumber)
- if err != nil {
- logrus.Error(err)
- return
- }
- //已配置不执行下面
- if &camera != nil && camera.IsRegisted == 1 {
- return
- }
- //配置摄像头-事件监听服务器
- setServer(baseUrl)
- //配置摄像头-sip服务器
- setSIP(baseUrl, camera.ID, cs)
- }
- // 心跳响应
- func c2s4002(adu AppDataUnit, c net.Conn) {
- //todo 心跳处理
- a := AppDataUnit{
- CmdCode: CmdCodeHeartbeatS2C,
- SubCode: 0,
- DevId: adu.DevId,
- }
- a.Debug()
- _, err := c.Write(Pack(a))
- if err != nil {
- logrus.Errorf("写入数据错误 %e\n", err)
- }
- }
- // 查询参数响应
- func c2s4003(adu AppDataUnit) {
- logrus.Infof("参数响应%s,devId:%s,CmdCode=%X,SubCode=%X\n", adu.Data, adu.DevId, adu.CmdCode, adu.SubCode)
- }
- // 设置参数响应
- func c2s4004(adu AppDataUnit) {
- logrus.Infof("配置参数-响应码%s,devId:%s,CmdCode=%X,SubCode=%X\n", adu.Data, adu.DevId, adu.CmdCode, adu.SubCode)
- }
- // setServer 设置事件监听服务器
- 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
- }
- data, err := isapi.Com(host, isapi.SetHosts, marshal)
- if err != nil {
- logrus.Error("配置监听主机错误", err)
- }
- var resp = data.(isapi.ResponseStatus)
- 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
- }
- return 1
- }
- // setSIP 设置sip服务器
- func setSIP(host string, cid uint, cs appService.CameraService) uint {
- //1. 准备数据
- var sipstr = `
- <?xml version: "1.0" encoding="utf-8"?><SIPServerList xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
- <SIPServer>
- <id>1</id>
- <localPort>5060</localPort>
- <streamID>1</streamID>
- <Standard>
- <registerStatus>false</registerStatus>
- <enabled>false</enabled>
- <registrar/>
- <registrarPort>5060</registrarPort>
- <proxy/>
- <proxyPort>5060</proxyPort>
- <displayName/>
- <userName/>
- <authID/>
- <password/>
- <expires>0</expires>
- </Standard>
- <GB28181>
- <registerStatus>true</registerStatus>
- <enabled>true</enabled>
- <registrar>106.52.134.22</registrar>
- <registrarPort>5060</registrarPort>
- <serverId>34020000002000000001</serverId>
- <serverDomain>3402000000</serverDomain>
- <userName>34020000002000000%03d</userName>
- <authID>34020000001320000%03d</authID>
- <password>kk176@lc</password>
- <expires>3600</expires>
- <liveTime>1</liveTime>
- <heartbeatTime>15</heartbeatTime>
- <heartbeatCount>3</heartbeatCount>
- <transportType>UDP</transportType>
- <registerInterval>60</registerInterval>
- <protocolVersion>GB/T28181-2016</protocolVersion>
- <platformNo>1</platformNo>
- <localPort>5060</localPort>
- <unidirectionEnabled>false</unidirectionEnabled>
- <voiceBroadcastEnabled>false</voiceBroadcastEnabled>
- </GB28181>
- </SIPServer>
- </SIPServerList>`
- var sipInfo = `
- <?xml version: "1.0" encoding="UTF-8"?>
- <SIPInfo xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
- <videoID>34020000001320000001</videoID>
- <AlarmInList>
- </AlarmInList>
- <VideoInputList>
- <VideoInput>
- <id>1</id>
- <videoInputID>34020000001320000001</videoInputID>
- </VideoInput>
- </VideoInputList>
- <AudioOutputList>
- <AudioOutput>
- <id>1</id>
- <audioOutputID/>
- </AudioOutput>
- </AudioOutputList>
- </SIPInfo>`
- //获取自增ID,以51为起始值
- id, err := is.GetSipUserID()
- if err != nil {
- logrus.Error("事务错误", err)
- }
- //2. 配置SIP
- s := fmt.Sprintf(sipstr, id, id)
- data, err := isapi.Com(host, isapi.SetSIP, []byte(s))
- if err != nil {
- logrus.Error("配置SIP错误", err)
- return 0
- }
- var resp = data.(isapi.ResponseStatus)
- if err != nil {
- logrus.Errorf("配置SIP服务器err:%s", err.Error())
- return 0
- }
- //失败记录失败原因
- if resp.StatusCode != 0 && resp.StatusCode != 1 {
- logrus.Infof("配置SIP服务器响应 StatusCode:%d,StatusString:%s", resp.StatusCode, resp.StatusString)
- return 0
- }
- //3. 配置SIPInfo
- data, err = isapi.Com(host, isapi.SetSIPInfo, []byte(sipInfo))
- if err != nil {
- logrus.Error("配置SIPInfo错误", err)
- return 0
- }
- var resp1 = data.(isapi.ResponseStatus)
- if err != nil {
- logrus.Errorf("配置配置SIPInfo err:%s", err.Error())
- return 0
- }
- //记录失败原因
- if resp1.StatusCode != 0 && resp1.StatusCode != 1 {
- logrus.Infof("配置SIPInfo-响应 StatusCode:%d,StatusString:%s", resp1.StatusCode, resp1.StatusString)
- return 0
- }
- //成功保存数据库
- streamId := fmt.Sprintf("34020000002000000%03d@34020000001320000001", id)
- streamUrl := global.Config.Hikvision.StreamBaseUrl + streamId
- var camera = appModel.Camera{
- StreamId: streamId,
- StreamUrl: streamUrl,
- IsRegisted: 1,
- }
- camera.ID = cid
- err = cs.UpdateCamera(camera)
- if err != nil {
- logrus.Error(err)
- }
- return 1
- }
|