c2s.go 7.4 KB

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