Skip to content

Commit

Permalink
Merge pull request #54 from joestarzxh/master
Browse files Browse the repository at this point in the history
[fix]gb channel判断parentId逻辑修改
  • Loading branch information
ZSC714725 authored Mar 5, 2024
2 parents 7b5dbe4 + 7f2860f commit 3e2bd91
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 74 deletions.
50 changes: 25 additions & 25 deletions gb28181/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ type Channel struct {

// Channel 通道
type ChannelInfo struct {
DeviceID string // 设备id
ParentID string //父目录Id
Name string //设备名称
Manufacturer string //制造厂商
Model string //型号
Owner string //设备归属
CivilCode string //行政区划编码
Address string //地址
Port int //端口
Parental int //存在子设备,这里表明有子目录存在 1代表有子目录,0表示没有
SafetyWay int //信令安全模式(可选)缺省为 0;0:不采用;2:S/MIME 签名方式;3:S/MIME 加密签名同时采用方式;4:数字摘要方式
RegisterWay int //标准的认证注册模式
Secrecy int //0 表示不涉密
Status ChannelStatus // 状态 on 在线 off离线
Longitude string // 经度
Latitude string // 纬度
StreamName string
ChannelId string `xml:"DeviceID"` // 设备id
ParentId string `xml:"ParentID"` //父目录Id
Name string `xml:"Name"` //设备名称
Manufacturer string `xml:"Manufacturer"` //制造厂商
Model string `xml:"Model"` //型号
Owner string `xml:"Owner"` //设备归属
CivilCode string `xml:"CivilCode"` //行政区划编码
Address string `xml:"Address"` //地址
Port int `xml:"Port"` //端口
Parental int `xml:"Parental"` //存在子设备,这里表明有子目录存在 1代表有子目录,0表示没有
SafetyWay int `xml:"SafetyWay"` //信令安全模式(可选)缺省为 0;0:不采用;2:S/MIME 签名方式;3:S/MIME 加密签名同时采用方式;4:数字摘要方式
RegisterWay int `xml:"RegisterWay"` //标准的认证注册模式
Secrecy int `xml:"Secrecy"` //0 表示不涉密
Status ChannelStatus `xml:"Status"` // 状态 on 在线 off离线
Longitude string `xml:"Longitude"` // 经度
Latitude string `xml:"Latitude"` // 纬度
StreamName string `xml:"-"`
}

type ChannelStatus string
Expand All @@ -61,8 +61,8 @@ func (channel *Channel) TryAutoInvite(opt *InviteOptions, conf config.GB28181Con
}

func (channel *Channel) CanInvite(streamName string) bool {
if len(channel.DeviceID) != 20 || channel.Status == ChannelOffStatus {
nazalog.Info("return false, channel.DeviceID:", len(channel.DeviceID), " channel.Status:", channel.Status)
if len(channel.ChannelId) != 20 || channel.Status == ChannelOffStatus {
nazalog.Info("return false, channel.DeviceID:", len(channel.ChannelId), " channel.Status:", channel.Status)
return false
}
if channel.Parental != 0 {
Expand All @@ -78,7 +78,7 @@ func (channel *Channel) CanInvite(streamName string) bool {
}

// 11~13位是设备类型编码
typeID := channel.DeviceID[10:13]
typeID := channel.ChannelId[10:13]
if typeID == "132" || typeID == "131" {
return true
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func (channel *Channel) Invite(opt *InviteOptions, conf config.GB28181Config, st
if channel.number > 999 {
channel.number = 1
}
opt.CreateSSRC(channel.DeviceID, channel.number)
opt.CreateSSRC(channel.ChannelId, channel.number)

protocol := ""
networkType := conf.SipNetwork
Expand All @@ -166,7 +166,7 @@ func (channel *Channel) Invite(opt *InviteOptions, conf config.GB28181Config, st

sdpInfo := []string{
"v=0",
fmt.Sprintf("o=%s 0 0 IN IP4 %s", channel.DeviceID, d.mediaIP),
fmt.Sprintf("o=%s 0 0 IN IP4 %s", channel.ChannelId, d.mediaIP),
"s=" + s,
"c=IN IP4 " + d.mediaIP,
opt.String(),
Expand All @@ -190,7 +190,7 @@ func (channel *Channel) Invite(opt *InviteOptions, conf config.GB28181Config, st
invite.SetBody(strings.Join(sdpInfo, "\r\n")+"\r\n", true)

subject := sip.GenericHeader{
HeaderName: "Subject", Contents: fmt.Sprintf("%s:%s,%s:0", channel.DeviceID, opt.ssrc, ""),
HeaderName: "Subject", Contents: fmt.Sprintf("%s:%s,%s:0", channel.ChannelId, opt.ssrc, ""),
}
invite.AppendHeader(&subject)
inviteRes, err := d.SipRequestForResponse(invite)
Expand Down Expand Up @@ -270,7 +270,7 @@ func (channel *Channel) CreateRequst(Method sip.RequestMethod, conf config.GB281
}
//非同一域的目标地址需要使用@host
host := conf.Realm
if channel.DeviceID[0:9] != host {
if channel.ChannelId[0:9] != host {
if channel.Port != 0 {
deviceIp := d.NetAddr
deviceIp = deviceIp[0:strings.LastIndex(deviceIp, ":")]
Expand All @@ -281,7 +281,7 @@ func (channel *Channel) CreateRequst(Method sip.RequestMethod, conf config.GB281
}

channelAddr := sip.Address{
Uri: &sip.SipUri{FUser: sip.String{Str: channel.DeviceID}, FHost: host},
Uri: &sip.SipUri{FUser: sip.String{Str: channel.ChannelId}, FHost: host},
}
req = sip.NewRequest(
"",
Expand Down
58 changes: 30 additions & 28 deletions gb28181/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (d *Device) syncChannels(conf config.GB28181Config) {
func (d *Device) UpdateChannels(conf config.GB28181Config, list ...ChannelInfo) {
for _, c := range list {
//当父设备非空且存在时、父设备节点增加通道
if c.ParentID != "" {
path := strings.Split(c.ParentID, "/")
if c.ParentId != "" {
path := strings.Split(c.ParentId, "/")
parentId := path[len(path)-1]
//如果父ID并非本身所属设备,一般情况下这是因为下级设备上传了目录信息,该信息通常不需要处理。
// 暂时不考虑级联目录的实现
Expand All @@ -80,28 +80,26 @@ func (d *Device) UpdateChannels(conf config.GB28181Config, list ...ChannelInfo)
parent := v.(*Device)
parent.addOrUpdateChannel(c)
continue
} else {
c.Model = "Directory " + c.Model
c.Status = "NoParent"
}
}
}
c.ParentId = d.ID
//本设备增加通道
d.addOrUpdateChannel(c)
//channel.TryAutoInvite(&InviteOptions{}, conf)
}
}

func (d *Device) addOrUpdateChannel(info ChannelInfo) (c *Channel) {
if old, ok := d.channelMap.Load(info.DeviceID); ok {
if old, ok := d.channelMap.Load(info.ChannelId); ok {
c = old.(*Channel)
c.ChannelInfo = info
} else {
c = &Channel{
device: d,
ChannelInfo: info,
}
d.channelMap.Store(info.DeviceID, c)
d.channelMap.Store(info.ChannelId, c)
}
return
}
Expand Down Expand Up @@ -221,21 +219,21 @@ func (d *Device) UpdateChannelStatus(deviceList []*notifyMessage, conf config.GB
switch v.Event {
case "ON":
nazalog.Debug("receive channel online notify")
d.channelOnline(v.DeviceID)
d.channelOnline(v.ChannelId)
case "OFF":
nazalog.Debug("receive channel offline notify")
d.channelOffline(v.DeviceID)
d.channelOffline(v.ChannelId)
case "VLOST":
nazalog.Debug("receive channel video lost notify")
d.channelOffline(v.DeviceID)
d.channelOffline(v.ChannelId)
case "DEFECT":
nazalog.Debug("receive channel video defect notify")
d.channelOffline(v.DeviceID)
d.channelOffline(v.ChannelId)
case "ADD":
nazalog.Debug("receive channel add notify")
channel := ChannelInfo{
DeviceID: v.DeviceID,
ParentID: v.ParentID,
ChannelId: v.ChannelId,
ParentId: v.ParentId,
Name: v.Name,
Manufacturer: v.Manufacturer,
Model: v.Model,
Expand All @@ -247,19 +245,21 @@ func (d *Device) UpdateChannelStatus(deviceList []*notifyMessage, conf config.GB
SafetyWay: v.SafetyWay,
RegisterWay: v.RegisterWay,
Secrecy: v.Secrecy,
Status: ChannelStatus(v.Status),
Status: v.Status,
Longitude: v.Longitude,
Latitude: v.Latitude,
}
d.addOrUpdateChannel(channel)
case "DEL":
//删除
nazalog.Debug("receive channel delete notify")
d.deleteChannel(v.DeviceID)
d.deleteChannel(v.ChannelId)
case "UPDATE":
nazalog.Debug("receive channel update notify")
// 更新通道
channel := ChannelInfo{
DeviceID: v.DeviceID,
ParentID: v.ParentID,
ChannelId: v.ChannelId,
ParentId: v.ParentId,
Name: v.Name,
Manufacturer: v.Manufacturer,
Model: v.Model,
Expand All @@ -271,35 +271,37 @@ func (d *Device) UpdateChannelStatus(deviceList []*notifyMessage, conf config.GB
SafetyWay: v.SafetyWay,
RegisterWay: v.RegisterWay,
Secrecy: v.Secrecy,
Status: ChannelStatus(v.Status),
Status: v.Status,
Longitude: v.Longitude,
Latitude: v.Latitude,
}
d.UpdateChannels(conf, channel)
}
}
}

func (d *Device) channelOnline(DeviceID string) {
if v, ok := d.channelMap.Load(DeviceID); ok {
func (d *Device) channelOnline(channelId string) {
if v, ok := d.channelMap.Load(channelId); ok {
c := v.(*Channel)
c.Status = ChannelOnStatus
nazalog.Debug("channel online, channelId: ", DeviceID)
nazalog.Debug("channel online, channelId: ", channelId)
} else {
nazalog.Debug("update channel status failed, not found, channelId: ", DeviceID)
nazalog.Debug("update channel status failed, not found, channelId: ", channelId)
}
}

func (d *Device) channelOffline(DeviceID string) {
if v, ok := d.channelMap.Load(DeviceID); ok {
func (d *Device) channelOffline(channelId string) {
if v, ok := d.channelMap.Load(channelId); ok {
c := v.(*Channel)
c.Status = ChannelOffStatus
nazalog.Debug("channel offline, channelId: ", DeviceID)
nazalog.Debug("channel offline, channelId: ", channelId)
} else {
nazalog.Debug("update channel status failed, not found, channelId: ", DeviceID)
nazalog.Debug("update channel status failed, not found, channelId: ", channelId)
}
}

func (d *Device) deleteChannel(DeviceID string) {
d.channelMap.Delete(DeviceID)
func (d *Device) deleteChannel(channelId string) {
d.channelMap.Delete(channelId)
}

// UpdateChannelPosition 更新通道GPS坐标
Expand Down
29 changes: 8 additions & 21 deletions gb28181/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *GB28181Server) getDeviceInfos() (deviceInfos *DeviceInfos) {
d.channelMap.Range(func(key, value any) bool {
ch := value.(*Channel)
channel := &ChannelItem{
ChannelId: ch.DeviceID,
ChannelId: ch.ChannelId,
Name: ch.Name,
Manufacturer: ch.Manufacturer,
Owner: ch.Owner,
Expand All @@ -178,16 +178,16 @@ func (s *GB28181Server) GetAllSyncChannels() {
return true
})
}
func (s *GB28181Server) GetSyncChannels(parentID string) {
if v, ok := Devices.Load(parentID); ok {
func (s *GB28181Server) GetSyncChannels(deviceId string) {
if v, ok := Devices.Load(deviceId); ok {
d := v.(*Device)
d.syncChannels(s.conf)
}
}
func (s *GB28181Server) FindChannel(parentID string, deviceID string) (channel *Channel) {
if v, ok := Devices.Load(parentID); ok {
func (s *GB28181Server) FindChannel(deviceId string, channelId string) (channel *Channel) {
if v, ok := Devices.Load(deviceId); ok {
d := v.(*Device)
if ch, ok := d.channelMap.Load(deviceID); ok {
if ch, ok := d.channelMap.Load(channelId); ok {
channel = ch.(*Channel)
return channel
} else {
Expand Down Expand Up @@ -454,7 +454,7 @@ func (s *GB28181Server) StoreDevice(id string, req sip.Request) (d *Device) {
Uri: from.Address,
}
deviceIp := req.Source()
if _d, loaded := Devices.Load(id); loaded {
if _d, ok := Devices.Load(id); ok {
d = _d.(*Device)
d.UpdateTime = time.Now()
d.NetAddr = deviceIp
Expand Down Expand Up @@ -559,20 +559,7 @@ func GbkToUtf8(s []byte) ([]byte, error) {
}

type notifyMessage struct {
DeviceID string //设备id
ParentID string //父目录Id
Name string //设备名称
Manufacturer string //制造厂商
Model string //型号
Owner string //设备归属
CivilCode string //行政区划编码
Address string //地址
Port int //端口
Parental int //存在子设备,这里表明有子目录存在 1代表有子目录,0表示没有
SafetyWay int //信令安全模式(可选)缺省为 0;0:不采用;2:S/MIME 签名方式;3:S/MIME 加密签名同时采用方式;4:数字摘要方式
RegisterWay int //标准的认证注册模式
Secrecy int //0 表示不涉密
Status string // 状态 on 在线 off离线
ChannelInfo

//状态改变事件 ON:上线,OFF:离线,VLOST:视频丢失,DEFECT:故障,ADD:增加,DEL:删除,UPDATE:更新(必选)
Event string
Expand Down

0 comments on commit 3e2bd91

Please sign in to comment.