c2s.go 8.1 KB

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