c2s.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package gatewayServer
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "lc-fangdaosha/global"
  7. "lc-fangdaosha/isapi"
  8. appModel "lc-fangdaosha/model/app"
  9. "lc-fangdaosha/service"
  10. appService "lc-fangdaosha/service/app"
  11. is "lc-fangdaosha/service/isapi"
  12. "net"
  13. "strings"
  14. )
  15. func ParseAndResponse(adu AppDataUnit, c net.Conn) {
  16. switch adu.CmdCode {
  17. case CmdCodeHeartbeatC2S:
  18. c2s4002(adu, c)
  19. case CmdCodeGetParamC2S:
  20. c2s4003(adu)
  21. case CmdCodeSetParameterC2S:
  22. c2s4004(adu)
  23. case CmdCodeRegisterC2S:
  24. c2s4001(adu, c)
  25. case CmdCodeServerC2S:
  26. c2s4000(adu, c)
  27. case CmdCodeUpdateC2S:
  28. default:
  29. fmt.Println(adu.String())
  30. }
  31. }
  32. // 请求管理服务器响应
  33. func c2s4000(adu AppDataUnit, c net.Conn) {
  34. a := AppDataUnit{
  35. CmdCode: CmdCodeServerS2C,
  36. SubCode: 0,
  37. DevId: adu.DevId,
  38. Data: ipAddress,
  39. }
  40. a.Debug()
  41. _, err := c.Write(Pack(a))
  42. if err != nil {
  43. logrus.Errorf("写入数据错误 %e\n", err)
  44. }
  45. logrus.Info(adu.DevId, "向登录服务器请求管理服务器")
  46. }
  47. // 注册登录响应
  48. func c2s4001(adu AppDataUnit, c net.Conn) {
  49. a := AppDataUnit{
  50. CmdCode: CmdCodeRegisterS2C,
  51. SubCode: 0,
  52. DevId: adu.DevId,
  53. }
  54. a.Data = "0"
  55. a.Debug()
  56. _, err := c.Write(Pack(a))
  57. if err != nil {
  58. logrus.Errorf("写入数据错误 %e\n", err)
  59. }
  60. ConnMap[adu.DevId] = c
  61. //1. 网关登录后检查是否已经注册到数据库,没有就要保存至数据库
  62. gs := service.ServiceGroupApp.AppServiceGroup.GatewayService
  63. gw, err := gs.CreateIfNotExist(adu.DevId)
  64. if err != nil {
  65. if err == appService.ExistError {
  66. logrus.Infof("网关[%s]登录成功", adu.DevId)
  67. } else {
  68. //如果错误不是"已注册",退出
  69. logrus.WithField("网关注册失败", adu.DevId).Error(err)
  70. return
  71. }
  72. }
  73. //2. 获取网关上摄像头和ip音柱的信息,保存至数据库
  74. var cs = service.ServiceGroupApp.AppServiceGroup.CameraService
  75. baseUrl := strings.Split(c.RemoteAddr().String(), ":")[0] + ":8848"
  76. resp, err := isapi.Com(baseUrl, isapi.GetDeviceInfo, nil)
  77. if err != nil {
  78. logrus.Error(err)
  79. return
  80. }
  81. var info = resp.(isapi.DeviceInfo)
  82. //创建camera记录并绑定gateway_id
  83. err = cs.CreateIfNotExist(info, gw.ID)
  84. if err != nil {
  85. logrus.WithField("摄像头注册失败", info.SerialNumber).Error(err)
  86. }
  87. //3.检查摄像头是否配置
  88. camera, err := cs.GetCameraByDeviceId(info.SerialNumber)
  89. if err != nil {
  90. logrus.Error(err)
  91. return
  92. }
  93. //已配置不执行下面
  94. if &camera != nil && camera.IsRegisted == 1 {
  95. return
  96. }
  97. //配置摄像头-事件监听服务器
  98. setServer(baseUrl)
  99. //配置摄像头-sip服务器
  100. setSIP(baseUrl, camera.ID, cs)
  101. }
  102. // 心跳响应
  103. func c2s4002(adu AppDataUnit, c net.Conn) {
  104. //todo 心跳处理
  105. a := AppDataUnit{
  106. CmdCode: CmdCodeHeartbeatS2C,
  107. SubCode: 0,
  108. DevId: adu.DevId,
  109. }
  110. a.Debug()
  111. _, err := c.Write(Pack(a))
  112. if err != nil {
  113. logrus.Errorf("写入数据错误 %e\n", err)
  114. }
  115. }
  116. // 查询参数响应
  117. func c2s4003(adu AppDataUnit) {
  118. logrus.Infof("参数响应%s,devId:%s,CmdCode=%X,SubCode=%X\n", adu.Data, adu.DevId, adu.CmdCode, adu.SubCode)
  119. }
  120. // 设置参数响应
  121. func c2s4004(adu AppDataUnit) {
  122. logrus.Infof("配置参数-响应码%s,devId:%s,CmdCode=%X,SubCode=%X\n", adu.Data, adu.DevId, adu.CmdCode, adu.SubCode)
  123. }
  124. // setServer 设置事件监听服务器
  125. func setServer(host string) uint {
  126. marshal, err := xml.Marshal(global.Config.HttpHostNotificationList)
  127. fmt.Println("host:", string(marshal))
  128. if err != nil {
  129. logrus.Errorf("事件主机配置文件解析错误: %e\n", err)
  130. return 0
  131. }
  132. data, err := isapi.Com(host, isapi.SetHosts, marshal)
  133. if err != nil {
  134. logrus.Error("配置监听主机错误", err)
  135. }
  136. var resp = data.(isapi.ResponseStatus)
  137. if err != nil || (resp.StatusCode != 0 && resp.StatusCode != 1) {
  138. logrus.Errorf("配置监听服务器响应 err:%s,StatusCode:%d,StatusString:%s", err.Error(), resp.StatusCode, resp.StatusString)
  139. return 0
  140. }
  141. return 1
  142. }
  143. // setSIP 设置sip服务器
  144. func setSIP(host string, cid uint, cs appService.CameraService) uint {
  145. //1. 准备数据
  146. var sipstr = `
  147. <?xml version: "1.0" encoding="utf-8"?><SIPServerList xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
  148. <SIPServer>
  149. <id>1</id>
  150. <localPort>5060</localPort>
  151. <streamID>1</streamID>
  152. <Standard>
  153. <registerStatus>false</registerStatus>
  154. <enabled>false</enabled>
  155. <registrar/>
  156. <registrarPort>5060</registrarPort>
  157. <proxy/>
  158. <proxyPort>5060</proxyPort>
  159. <displayName/>
  160. <userName/>
  161. <authID/>
  162. <password/>
  163. <expires>0</expires>
  164. </Standard>
  165. <GB28181>
  166. <registerStatus>true</registerStatus>
  167. <enabled>true</enabled>
  168. <registrar>106.52.134.22</registrar>
  169. <registrarPort>5060</registrarPort>
  170. <serverId>34020000002000000001</serverId>
  171. <serverDomain>3402000000</serverDomain>
  172. <userName>34020000002000000%03d</userName>
  173. <authID>34020000001320000%03d</authID>
  174. <password>kk176@lc</password>
  175. <expires>3600</expires>
  176. <liveTime>1</liveTime>
  177. <heartbeatTime>15</heartbeatTime>
  178. <heartbeatCount>3</heartbeatCount>
  179. <transportType>UDP</transportType>
  180. <registerInterval>60</registerInterval>
  181. <protocolVersion>GB/T28181-2016</protocolVersion>
  182. <platformNo>1</platformNo>
  183. <localPort>5060</localPort>
  184. <unidirectionEnabled>false</unidirectionEnabled>
  185. <voiceBroadcastEnabled>false</voiceBroadcastEnabled>
  186. </GB28181>
  187. </SIPServer>
  188. </SIPServerList>`
  189. var sipInfo = `
  190. <?xml version: "1.0" encoding="UTF-8"?>
  191. <SIPInfo xmlns="http://www.hikvision.com/ver20/XMLSchema" version="2.0">
  192. <videoID>34020000001320000001</videoID>
  193. <AlarmInList>
  194. </AlarmInList>
  195. <VideoInputList>
  196. <VideoInput>
  197. <id>1</id>
  198. <videoInputID>34020000001320000001</videoInputID>
  199. </VideoInput>
  200. </VideoInputList>
  201. <AudioOutputList>
  202. <AudioOutput>
  203. <id>1</id>
  204. <audioOutputID/>
  205. </AudioOutput>
  206. </AudioOutputList>
  207. </SIPInfo>`
  208. //获取自增ID,以51为起始值
  209. id, err := is.GetSipUserID()
  210. if err != nil {
  211. logrus.Error("事务错误", err)
  212. }
  213. //2. 配置SIP
  214. s := fmt.Sprintf(sipstr, id, id)
  215. data, err := isapi.Com(host, isapi.SetSIP, []byte(s))
  216. if err != nil {
  217. logrus.Error("配置SIP错误", err)
  218. return 0
  219. }
  220. var resp = data.(isapi.ResponseStatus)
  221. if err != nil {
  222. logrus.Errorf("配置SIP服务器err:%s", err.Error())
  223. return 0
  224. }
  225. //失败记录失败原因
  226. if resp.StatusCode != 0 && resp.StatusCode != 1 {
  227. logrus.Infof("配置SIP服务器响应 StatusCode:%d,StatusString:%s", resp.StatusCode, resp.StatusString)
  228. return 0
  229. }
  230. //3. 配置SIPInfo
  231. data, err = isapi.Com(host, isapi.SetSIPInfo, []byte(sipInfo))
  232. if err != nil {
  233. logrus.Error("配置SIPInfo错误", err)
  234. return 0
  235. }
  236. var resp1 = data.(isapi.ResponseStatus)
  237. if err != nil {
  238. logrus.Errorf("配置配置SIPInfo err:%s", err.Error())
  239. return 0
  240. }
  241. //记录失败原因
  242. if resp1.StatusCode != 0 && resp1.StatusCode != 1 {
  243. logrus.Infof("配置SIPInfo-响应 StatusCode:%d,StatusString:%s", resp1.StatusCode, resp1.StatusString)
  244. return 0
  245. }
  246. //成功保存数据库
  247. streamId := fmt.Sprintf("34020000002000000%03d@34020000001320000001", id)
  248. streamUrl := global.Config.Hikvision.StreamBaseUrl + streamId
  249. var camera = appModel.Camera{
  250. StreamId: streamId,
  251. StreamUrl: streamUrl,
  252. IsRegisted: 1,
  253. }
  254. camera.ID = cid
  255. err = cs.UpdateCamera(camera)
  256. if err != nil {
  257. logrus.Error(err)
  258. }
  259. return 1
  260. }