fix: api getDevices,getDevice,getChannels

This commit is contained in:
pggiroro
2025-07-11 23:10:34 +08:00
parent 28c71545db
commit 56c4ea5907
9 changed files with 119 additions and 137 deletions

View File

@@ -84,7 +84,8 @@ func (gb *GB28181Plugin) List(ctx context.Context, req *pb.GetDevicesRequest) (*
// 从设备的内存通道集合中获取通道信息
d.channels.Range(func(channel *Channel) bool {
pbChannels = append(pbChannels, &pb.Channel{
DeviceId: channel.ChannelID,
DeviceId: channel.ChannelId,
ChannelId: channel.ChannelId,
ParentId: d.DeviceId,
Name: channel.Name,
Manufacturer: channel.Manufacturer,
@@ -158,20 +159,18 @@ func (gb *GB28181Plugin) GetDevice(ctx context.Context, req *pb.GetDeviceRequest
// 先从内存中获取
d, ok := gb.devices.Get(req.DeviceId)
if !ok && gb.DB != nil {
// 如果内存中没有且数据库存在,则从数据库查询
var device Device
if err := gb.DB.Where("id = ?", req.DeviceId).First(&device).Error; err == nil {
d = &device
}
if !ok {
resp.Code = 404
resp.Message = "设备不存在"
}
if d != nil {
var channels []*pb.Channel
for c := range d.channels.Range {
channels = append(channels, &pb.Channel{
DeviceId: c.DeviceID,
ParentId: c.ParentID,
DeviceId: c.ChannelId,
ChannelId: c.ChannelId,
ParentId: d.DeviceId,
Name: c.Name,
Manufacturer: c.Manufacturer,
Model: c.Model,
@@ -219,68 +218,50 @@ func (gb *GB28181Plugin) GetDevice(ctx context.Context, req *pb.GetDeviceRequest
func (gb *GB28181Plugin) GetDevices(ctx context.Context, req *pb.GetDevicesRequest) (*pb.DevicesPageInfo, error) {
resp := &pb.DevicesPageInfo{}
if gb.DB == nil {
resp.Code = 500
resp.Message = "数据库未初始化"
return resp, nil
}
var devices []Device
var total int64
// 构建查询条件
query := gb.DB.Model(&Device{})
if req.Query != "" {
query = query.Where("device_id LIKE ? OR name LIKE ?",
"%"+req.Query+"%", "%"+req.Query+"%")
}
if req.Status {
query = query.Where("online = ?", true)
}
// 获取总数
if err := query.Count(&total).Error; err != nil {
resp.Code = 500
resp.Message = fmt.Sprintf("查询总数失败: %v", err)
return resp, nil
}
// 查询设备列表
// 当Page和Count都为0时不做分页返回所有数据
if req.Page == 0 && req.Count == 0 {
// 不分页,查询所有数据
if err := query.Find(&devices).Error; err != nil {
resp.Code = 500
resp.Message = fmt.Sprintf("查询设备列表失败: %v", err)
return resp, nil
}
} else {
// 分页查询设备,并预加载通道数据
if err := query.
Offset(int(req.Page-1) * int(req.Count)).
Limit(int(req.Count)).
Find(&devices).Error; err != nil {
resp.Code = 500
resp.Message = fmt.Sprintf("查询设备列表失败: %v", err)
return resp, nil
}
}
// 转换为proto消息
// 从内存中获取设备列表
var pbDevices []*pb.Device
for _, d := range devices {
// 查询设备对应的通道
var channels []gb28181.DeviceChannel
if err := gb.DB.Where(&gb28181.DeviceChannel{DeviceID: d.DeviceId}).Find(&channels).Error; err != nil {
gb.Error("查询通道失败", "error", err)
var total int64
var filteredDevices []*Device
// 遍历内存中的设备
for d := range gb.devices.Range {
// 应用查询条件过滤
if req.Query != "" && !strings.Contains(d.DeviceId, req.Query) && !strings.Contains(d.Name, req.Query) {
continue
}
if req.Status && !d.Online {
continue
}
filteredDevices = append(filteredDevices, d)
total++
}
// 处理分页
startIdx := 0
endIdx := len(filteredDevices)
// 当Page和Count都不为0时进行分页处理
if req.Page > 0 && req.Count > 0 {
startIdx = int((req.Page - 1) * req.Count)
endIdx = int(req.Page * req.Count)
if startIdx >= len(filteredDevices) {
startIdx = len(filteredDevices)
}
if endIdx > len(filteredDevices) {
endIdx = len(filteredDevices)
}
}
// 处理分页后的设备列表
for _, d := range filteredDevices[startIdx:endIdx] {
// 从内存中获取通道
var pbChannels []*pb.Channel
for _, c := range channels {
for c := range d.channels.Range {
pbChannels = append(pbChannels, &pb.Channel{
DeviceId: c.ChannelID,
ParentId: c.ParentID,
ChannelId: c.ChannelId,
DeviceId: c.ChannelId,
ParentId: d.DeviceId,
Name: c.Name,
Manufacturer: c.Manufacturer,
Model: c.Model,
@@ -331,28 +312,27 @@ func (gb *GB28181Plugin) GetDevices(ctx context.Context, req *pb.GetDevicesReque
func (gb *GB28181Plugin) GetChannels(ctx context.Context, req *pb.GetChannelsRequest) (*pb.ChannelsPageInfo, error) {
resp := &pb.ChannelsPageInfo{}
// 从内存中获取
// 直接从内存中获取设备
d, ok := gb.devices.Get(req.DeviceId)
if !ok && gb.DB != nil {
// 如果内存中没有且数据库存在,则从数据库查询
var device Device
if err := gb.DB.Where(Device{DeviceId: req.DeviceId}).First(&device).Error; err == nil {
d = &device
}
if !ok {
// 如果内存中没有设备,返回设备未找到的错误
resp.Code = 404
resp.Message = "设备未找到"
return resp, nil
}
if d != nil {
var channels []*pb.Channel
total := 0
for c := range d.channels.Range {
// TODO: 实现查询条件过滤
if req.Query != "" && !strings.Contains(c.DeviceID, req.Query) && !strings.Contains(c.Name, req.Query) {
// 实现查询条件过滤
if req.Query != "" && !strings.Contains(c.DeviceId, req.Query) && !strings.Contains(c.Name, req.Query) {
continue
}
if req.Online && string(c.Status) != "ON" {
continue
}
if req.ChannelType && c.ParentID == "" {
if req.ChannelType && c.ParentId == "" {
continue
}
total++
@@ -361,8 +341,9 @@ func (gb *GB28181Plugin) GetChannels(ctx context.Context, req *pb.GetChannelsReq
if req.Page == 0 && req.Count == 0 {
// 不分页,添加所有符合条件的通道
channels = append(channels, &pb.Channel{
DeviceId: c.DeviceID,
ParentId: c.ParentID,
DeviceId: c.ChannelId,
ChannelId: c.ChannelId,
ParentId: c.ParentId,
Name: c.Name,
Manufacturer: c.Manufacturer,
Model: c.Model,
@@ -388,8 +369,9 @@ func (gb *GB28181Plugin) GetChannels(ctx context.Context, req *pb.GetChannelsReq
continue
}
channels = append(channels, &pb.Channel{
DeviceId: c.DeviceID,
ParentId: c.ParentID,
DeviceId: c.ChannelId,
ChannelId: c.ChannelId,
ParentId: c.ParentId,
Name: c.Name,
Manufacturer: c.Manufacturer,
Model: c.Model,
@@ -414,7 +396,7 @@ func (gb *GB28181Plugin) GetChannels(ctx context.Context, req *pb.GetChannelsReq
resp.Message = "success"
} else {
resp.Code = 404
resp.Message = "device not found"
resp.Message = "设备未找到"
}
return resp, nil
}
@@ -1866,7 +1848,7 @@ func (gb *GB28181Plugin) GetGroupChannels(ctx context.Context, req *pb.GetGroupC
// 从内存中获取所有通道
for channel := range gb.channels.Range {
// 如果有设备ID过滤条件则只处理匹配的设备
if req.DeviceId != "" && channel.DeviceID != req.DeviceId {
if req.DeviceId != "" && channel.DeviceId != req.DeviceId {
continue
}
filteredChannels = append(filteredChannels, channel)
@@ -1877,10 +1859,10 @@ func (gb *GB28181Plugin) GetGroupChannels(ctx context.Context, req *pb.GetGroupC
// 应用排序按设备ID和通道ID排序
sort.Slice(filteredChannels, func(i, j int) bool {
if filteredChannels[i].DeviceID == filteredChannels[j].DeviceID {
return filteredChannels[i].ChannelID < filteredChannels[j].ChannelID
if filteredChannels[i].DeviceId == filteredChannels[j].DeviceId {
return filteredChannels[i].ChannelId < filteredChannels[j].ChannelId
}
return filteredChannels[i].DeviceID < filteredChannels[j].DeviceID
return filteredChannels[i].DeviceId < filteredChannels[j].DeviceId
})
// 应用分页
@@ -1898,20 +1880,20 @@ func (gb *GB28181Plugin) GetGroupChannels(ctx context.Context, req *pb.GetGroupC
// 处理分页后的通道数据
for _, channel := range filteredChannels[start:end] {
key := channel.DeviceID + ":" + channel.ChannelID
key := channel.DeviceId + ":" + channel.ChannelId
id, inGroup := inGroupMap[key]
// 获取设备名称
deviceName := ""
if device, ok := gb.devices.Get(channel.DeviceID); ok {
if device, ok := gb.devices.Get(channel.DeviceId); ok {
deviceName = device.Name
}
channelInfo := &pb.GroupChannel{
Id: int32(id),
GroupId: req.GroupId,
ChannelId: channel.ChannelID,
DeviceId: channel.DeviceID,
ChannelId: channel.ChannelId,
DeviceId: channel.DeviceId,
ChannelName: channel.Name,
DeviceName: deviceName,
Status: string(channel.Status),
@@ -1919,7 +1901,7 @@ func (gb *GB28181Plugin) GetGroupChannels(ctx context.Context, req *pb.GetGroupC
}
// 从内存中获取设备信息以获取传输协议
if device, ok := gb.devices.Get(channel.DeviceID); ok {
if device, ok := gb.devices.Get(channel.DeviceId); ok {
channelInfo.StreamMode = device.StreamMode
}

View File

@@ -206,9 +206,9 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
// 更新通道信息
for _, c := range msg.DeviceChannelList {
// 设置关联的设备数据库ID
c.ChannelID = c.DeviceID
c.DeviceID = d.DeviceId
c.ID = d.DeviceId + "_" + c.ChannelID
c.ChannelId = c.DeviceId
c.DeviceId = d.DeviceId
c.ID = d.DeviceId + "_" + c.ChannelId
// 使用 Save 进行 upsert 操作
d.addOrUpdateChannel(c)
}
@@ -454,8 +454,8 @@ func (d *Device) Go() (err error) {
// case []gb28181.DeviceChannel:
// for _, c := range v {
// //当父设备非空且存在时、父设备节点增加通道
// if c.ParentID != "" {
// path := strings.Split(c.ParentID, "/")
// if c.ParentId != "" {
// path := strings.Split(c.ParentId, "/")
// parentId := path[len(path)-1]
// //如果父ID并非本身所属设备一般情况下这是因为下级设备上传了目录信息该信息通常不需要处理。
// // 暂时不考虑级联目录的实现
@@ -825,7 +825,7 @@ func (d *Device) handleCatalog(msg *gb28181.Message) error {
// 遍历并更新设备列表
for _, item := range msg.DeviceChannelList {
channel := &gb28181.DeviceChannel{
DeviceID: item.DeviceID,
DeviceId: item.DeviceId,
Name: item.Name,
Manufacturer: item.Manufacturer,
Model: item.Model,
@@ -833,7 +833,7 @@ func (d *Device) handleCatalog(msg *gb28181.Message) error {
CivilCode: item.CivilCode,
Address: item.Address,
Parental: item.Parental,
ParentID: item.ParentID,
ParentId: item.ParentId,
SafetyWay: item.SafetyWay,
RegisterWay: item.RegisterWay,
Secrecy: item.Secrecy,
@@ -846,10 +846,10 @@ func (d *Device) handleCatalog(msg *gb28181.Message) error {
// 如果需要,保存到数据库
if d.plugin.DB != nil {
var existingChannel gb28181.DeviceChannel
result := d.plugin.DB.Where("channel_id = ?", channel.DeviceID).First(&existingChannel)
result := d.plugin.DB.Where("channel_id = ?", channel.DeviceId).First(&existingChannel)
if result.Error != nil {
// 通道不存在,创建新通道
channel.DeviceID = d.DeviceId // 设置设备ID
channel.DeviceId = d.DeviceId // 设置设备ID
if err := d.plugin.DB.Create(channel).Error; err != nil {
d.Error("create channel error", "err", err)
}

View File

@@ -339,7 +339,7 @@ func (d *Dialog) Dispose() {
// 如果没有设置tcp端口则将MediaPort设置为0表示不再使用
d.gb.tcpPorts <- d.MediaPort
}
d.Info("dialog dispose", "ssrc", d.SSRC, "mediaPort", d.MediaPort, "streamMode", d.StreamMode, "deviceId", d.Channel.DeviceID, "channelId", d.Channel.ChannelID)
d.Info("dialog dispose", "ssrc", d.SSRC, "mediaPort", d.MediaPort, "streamMode", d.StreamMode, "deviceId", d.Channel.DeviceId, "channelId", d.Channel.ChannelId)
if d.session != nil {
err := d.session.Bye(d)
if err != nil {

View File

@@ -346,7 +346,7 @@ func (gb *GB28181Plugin) checkDeviceExpire() (err error) {
device.channels.OnAdd(func(c *Channel) {
if absDevice, ok := gb.Server.PullProxies.SafeFind(func(absDevice m7s.IPullProxy) bool {
conf := absDevice.GetConfig()
return conf.Type == "gb28181" && conf.URL == fmt.Sprintf("%s/%s", device.DeviceId, c.ChannelID)
return conf.Type == "gb28181" && conf.URL == fmt.Sprintf("%s/%s", device.DeviceId, c.ChannelId)
}); ok {
c.PullProxyTask = absDevice.(*PullProxy)
absDevice.ChangeStatus(m7s.PullProxyStatusOnline)
@@ -367,7 +367,7 @@ func (gb *GB28181Plugin) checkDeviceExpire() (err error) {
// 加载设备的通道
var channels []gb28181.DeviceChannel
if err := gb.DB.Where(&gb28181.DeviceChannel{DeviceID: device.DeviceId}).Find(&channels).Error; err != nil {
if err := gb.DB.Where(&gb28181.DeviceChannel{DeviceId: device.DeviceId}).Find(&channels).Error; err != nil {
gb.Error("加载通道失败", "error", err, "deviceId", device.DeviceId)
continue
}
@@ -396,7 +396,7 @@ func (gb *GB28181Plugin) checkDeviceExpire() (err error) {
}
// 更新通道状态到数据库
if err := gb.DB.Model(&gb28181.DeviceChannel{}).Where(&gb28181.DeviceChannel{ID: channel.ID}).Update("status", channel.Status).Error; err != nil {
gb.Error("更新通道状态到数据库失败", "error", err, "channelId", channel.ChannelID)
gb.Error("更新通道状态到数据库失败", "error", err, "channelId", channel.ChannelId)
}
device.addOrUpdateChannel(channel)
}
@@ -718,7 +718,7 @@ func (gb *GB28181Plugin) GetPullableList() []string {
for d := range gb.devices.Range {
for c := range d.channels.Range {
if c.Status == gb28181.ChannelOnStatus {
yield(fmt.Sprintf("%s/%s", d.DeviceId, c.ChannelID))
yield(fmt.Sprintf("%s/%s", d.DeviceId, c.ChannelId))
}
}
}
@@ -834,25 +834,25 @@ func (gb *GB28181Plugin) OnInvite(req *sip.Request, tx sip.ServerTransaction) {
var channel *Channel
platform.channels.Range(func(channelTmp *Channel) bool {
if channelTmp.ChannelID == inviteInfo.TargetChannelId {
if channelTmp.ChannelId == inviteInfo.TargetChannelId {
channel = channelTmp
}
return true
})
gb.Info("OnInvite", "action", "channel found", "channelId", channel.ChannelID, "channelName", channel.Name)
gb.Info("OnInvite", "action", "channel found", "channelId", channel.ChannelId, "channelName", channel.Name)
var channelTmp *Channel
if deviceFound, ok := gb.devices.Get(channel.DeviceID); ok {
if deviceFound, ok := gb.devices.Get(channel.DeviceId); ok {
if channelFound, ok := deviceFound.channels.Get(channel.ID); ok {
channelTmp = channelFound
} else {
gb.Error("OnInvite", "channel not found memory,ChannelID is ", channel.ChannelID)
gb.Error("OnInvite", "channel not found memory,ChannelId is ", channel.ChannelId)
_ = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "SSRC Not Found", nil))
return
}
} else {
gb.Error("OnInvite", "device not found memory,deviceID is ", channel.DeviceID)
gb.Error("OnInvite", "device not found memory,deviceID is ", channel.DeviceId)
_ = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "SSRC Not Found", nil))
return
}
@@ -899,7 +899,7 @@ func (gb *GB28181Plugin) OnInvite(req *sip.Request, tx sip.ServerTransaction) {
// 构建SDP内容参考Java代码createSendSdp方法
content := []string{
"v=0",
fmt.Sprintf("o=%s 0 0 IN IP4 %s", channel.ChannelID, sdpIP),
fmt.Sprintf("o=%s 0 0 IN IP4 %s", channel.ChannelId, sdpIP),
fmt.Sprintf("s=%s", inviteInfo.SessionName),
fmt.Sprintf("c=IN IP4 %s", sdpIP),
}
@@ -999,7 +999,7 @@ func (gb *GB28181Plugin) OnInvite(req *sip.Request, tx sip.ServerTransaction) {
return
}
gb.Info("OnInvite", "action", "complete", "platformId", inviteInfo.RequesterId, "channelId", channel.ChannelID,
gb.Info("OnInvite", "action", "complete", "platformId", inviteInfo.RequesterId, "channelId", channel.ChannelId,
"ip", inviteInfo.IP, "port", inviteInfo.Port, "tcp", inviteInfo.TCP, "tcpActive", inviteInfo.TCPActive)
return
//} else {
@@ -1026,8 +1026,8 @@ func (gb *GB28181Plugin) OnAck(req *sip.Request, tx sip.ServerTransaction) {
if forwardDialog, ok := gb.forwardDialogs.Find(func(dialog *ForwardDialog) bool {
return dialog.platformCallId == callID
}); ok {
pullUrl := fmt.Sprintf("%s/%s", forwardDialog.channel.DeviceID, forwardDialog.channel.ChannelID)
streamPath := fmt.Sprintf("platform_%d/%s/%s", time.Now().UnixMilli(), forwardDialog.channel.DeviceID, forwardDialog.channel.ChannelID)
pullUrl := fmt.Sprintf("%s/%s", forwardDialog.channel.DeviceId, forwardDialog.channel.ChannelId)
streamPath := fmt.Sprintf("platform_%d/%s/%s", time.Now().UnixMilli(), forwardDialog.channel.DeviceId, forwardDialog.channel.ChannelId)
// 创建配置
pullConf := config.Pull{

View File

@@ -18,9 +18,9 @@ type DeviceChannel struct {
//CommonGBChannel // 通过组合继承 CommonGBChannel 的字段
ID string `gorm:"primaryKey" json:"ID"` // 数据库自增长ID
ChannelID string `json:"channelID" xml:"ChannelID"`
DeviceID string `json:"deviceID" xml:"DeviceID"` // 设备国标编号
ParentID string `json:"parentId" xml:"ParentID"` // 父节点ID
ChannelId string `json:"channelID" xml:"ChannelID"`
DeviceId string `json:"deviceID" xml:"DeviceID"` // 设备国标编号
ParentId string `json:"parentId" xml:"ParentID"` // 父节点ID
Name string `json:"name" xml:"Name"` // 通道名称
Manufacturer string `json:"manufacturer" xml:"Manufacturer"` // 设备厂商
Model string `json:"model" xml:"Model"` // 设备型号
@@ -114,12 +114,12 @@ func (d *DeviceChannel) Encode(event string, serverDeviceID string) string {
switch event {
case "DEL", "DEFECT", "VLOST":
return "<Item>\n" +
"<DeviceId>" + d.DeviceID + "</DeviceId>\n" +
"<DeviceID>" + d.DeviceId + "</DeviceID>\n" +
"<Event>" + event + "</Event>\n" +
"</Item>\n"
case "ON", "OFF":
return "<Item>\n" +
"<DeviceId>" + d.DeviceID + "</DeviceId>\n" +
"<DeviceID>" + d.DeviceId + "</DeviceID>\n" +
"<Event>" + event + "</Event>\n" +
"</Item>\n"
case "ADD", "UPDATE":
@@ -132,11 +132,11 @@ func (d *DeviceChannel) Encode(event string, serverDeviceID string) string {
// getFullContent 生成完整的通道信息XML内容
func (d *DeviceChannel) getFullContent(event string, serverDeviceID string) string {
content := "<Item>\n" +
"<DeviceId>" + d.DeviceID + "</DeviceId>\n" +
"<DeviceID>" + d.DeviceId + "</DeviceID>\n" +
"<Name>" + d.Name + "</Name>\n"
if len(d.DeviceID) > 8 {
typeCode := d.DeviceID[10:13]
if len(d.DeviceId) > 8 {
typeCode := d.DeviceId[10:13]
switch typeCode {
case "200":
// 业务分组目录项
@@ -172,8 +172,8 @@ func (d *DeviceChannel) getFullContent(event string, serverDeviceID string) stri
if d.CivilCode != "" {
content += "<CivilCode>" + d.CivilCode + "</CivilCode>\n"
}
if d.ParentID != "" {
content += "<ParentID>" + d.ParentID + "</ParentID>\n"
if d.ParentId != "" {
content += "<ParentID>" + d.ParentId + "</ParentID>\n"
}
content += "<BusinessGroupID>" + d.BusinessGroupID + "</BusinessGroupID>\n"
default:
@@ -209,8 +209,8 @@ func (d *DeviceChannel) appendCommonInfo(content *string) {
if d.Address != "" {
*content += "<Address>" + d.Address + "</Address>\n"
}
if d.ParentID != "" {
*content += "<ParentID>" + d.ParentID + "</ParentID>\n"
if d.ParentId != "" {
*content += "<ParentID>" + d.ParentId + "</ParentID>\n"
}
if d.SafetyWay != 0 {
*content += "<SafetyWay>" + strconv.Itoa(d.SafetyWay) + "</SafetyWay>\n"

View File

@@ -25,7 +25,7 @@ func (g *Group) TableName() string {
// NewGroupFromChannel 从 DeviceChannel 创建 Group 实例
func NewGroupFromChannel(channel *DeviceChannel) *Group {
gbCode := DecodeGBCode(channel.DeviceID)
gbCode := DecodeGBCode(channel.DeviceId)
if gbCode == nil || (gbCode.TypeCode != "215" && gbCode.TypeCode != "216") {
return nil
}
@@ -33,17 +33,17 @@ func NewGroupFromChannel(channel *DeviceChannel) *Group {
now := time.Now().Format("2006-01-02 15:04:05")
group := &Group{
Name: channel.Name,
DeviceID: channel.DeviceID,
DeviceID: channel.DeviceId,
CreateTime: now,
UpdateTime: now,
}
switch gbCode.TypeCode {
case "215":
group.BusinessGroup = channel.DeviceID
group.BusinessGroup = channel.DeviceId
case "216":
group.BusinessGroup = channel.BusinessGroupID // 注意:需要在 DeviceChannel 中添加 BusinessGroupID 字段
group.ParentDeviceID = channel.ParentID
group.ParentDeviceID = channel.ParentId
}
if group.BusinessGroup == "" {

View File

@@ -66,13 +66,13 @@ func NewRegionFromChannel(channel *DeviceChannel) *Region {
now := time.Now().Format("2006-01-02 15:04:05")
region := &Region{
Name: channel.Name,
DeviceID: channel.DeviceID,
DeviceID: channel.DeviceId,
CreateTime: now,
UpdateTime: now,
}
// 获取父级编码
parentCode := GetInstance().GetParentCode(channel.DeviceID)
parentCode := GetInstance().GetParentCode(channel.DeviceId)
if parentCode != nil {
region.ParentDeviceID = parentCode.Code
}

View File

@@ -28,7 +28,7 @@ type Platform struct {
DialogClient *sipgo.DialogClientCache `gorm:"-" json:"-"` // SIP对话客户端
Recipient sip.Uri `gorm:"-" json:"-"` // 接收者地址
ContactHDR *sip.ContactHeader `gorm:"-" json:"-"` // 联系人头部
UserAgentHDR sip.Header `gorm:"-" json:"-"` //
UserAgentHDR sip.Header `gorm:"-" json:"-"` //
MaxForwardsHDR sip.MaxForwardsHeader `gorm:"-" json:"-"`
// 运行时字段
@@ -796,7 +796,7 @@ func (p *Platform) sendCatalogResponse(req *sip.Request, sn string, fromTag stri
// buildChannelItem 构建单个通道的XML项
func (p *Platform) buildChannelItem(channel gb28181.DeviceChannel) string {
// 确保字符串字段不为空
deviceID := channel.ChannelID
deviceID := channel.ChannelId
if deviceID == "" {
deviceID = "unknown_device" // 如果没有设备ID使用默认值
}
@@ -820,7 +820,7 @@ func (p *Platform) buildChannelItem(channel gb28181.DeviceChannel) string {
if address == "" {
address = "未知地址"
}
parentID := channel.ParentID
parentID := channel.ParentId
if parentID == "" {
parentID = p.PlatformModel.DeviceGBID // 使用平台ID作为父ID
}
@@ -845,7 +845,7 @@ func (p *Platform) buildChannelItem(channel gb28181.DeviceChannel) string {
channel.RegisterWay, // 直接使用整数值
channel.Secrecy, // 直接使用整数值
parentID,
channel.Parental, // 直接使用整数值
channel.Parental, // 直接使用整数值
channel.SafetyWay) // 直接使用整数值
}
@@ -1113,7 +1113,7 @@ func (p *Platform) handleDeviceInfo(req *sip.Request, tx sip.ServerTransaction,
}
// 3. 判断通道类型
if channel.DeviceID == "" {
if channel.DeviceId == "" {
// 非国标通道不支持设备信息查询
response := sip.NewResponseFromRequest(req, sip.StatusForbidden, "non-gb channel not supported", nil)
return tx.Respond(response)
@@ -1122,7 +1122,7 @@ func (p *Platform) handleDeviceInfo(req *sip.Request, tx sip.ServerTransaction,
// 4. 查询设备信息
var device Device
if p.plugin.DB != nil {
if err := p.plugin.DB.First(&device, channel.DeviceID).Error; err != nil {
if err := p.plugin.DB.First(&device, channel.DeviceId).Error; err != nil {
// 设备不存在返回404
response := sip.NewResponseFromRequest(req, sip.StatusNotFound, "device not found", nil)
return tx.Respond(response)

View File

@@ -446,7 +446,7 @@ func (task *registerHandlerTask) StoreDevice(deviceid string, req *sip.Request,
d.channels.OnAdd(func(c *Channel) {
if absDevice, ok := task.gb.Server.PullProxies.SafeFind(func(absDevice m7s.IPullProxy) bool {
conf := absDevice.GetConfig()
return conf.Type == "gb28181" && conf.URL == fmt.Sprintf("%s/%s", d.DeviceId, c.ChannelID)
return conf.Type == "gb28181" && conf.URL == fmt.Sprintf("%s/%s", d.DeviceId, c.ChannelId)
}); ok {
c.PullProxyTask = absDevice.(*PullProxy)
absDevice.ChangeStatus(m7s.PullProxyStatusOnline)