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 = `
1
5060
1
false
false
5060
5060
0
true
true
106.52.134.22
5060
34020000002000000001
3402000000
34020000002000000%03d
34020000001320000%03d
kk176@lc
3600
1
15
3
UDP
60
GB/T28181-2016
1
5060
false
false
`
var sipInfo = `
34020000001320000001
1
34020000001320000001
1
`
//获取自增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
}