@@ -62,6 +62,7 @@ type Device struct {
6262 task.Job `gorm:"-:all"`
6363 DeviceId string `gorm:"primaryKey"` // 设备国标编号
6464 Name string // 设备名
65+ CustomName string // 自定义名称
6566 Manufacturer string // 生产厂商
6667 Model string // 型号
6768 Firmware string // 固件版本
@@ -136,6 +137,7 @@ func (d *Device) Dispose() {
136137 if channel .PullProxyTask != nil {
137138 channel .PullProxyTask .ChangeStatus (m7s .PullProxyStatusOffline )
138139 }
140+ d .channels .RemoveByKey (channel .ID )
139141 d .plugin .channels .RemoveByKey (channel .ID )
140142 return true
141143 })
@@ -199,6 +201,9 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
199201 switch msg .CmdType {
200202 case "Keepalive" :
201203 d .KeepaliveInterval = int (time .Since (d .KeepaliveTime ).Seconds ())
204+ if d .KeepaliveInterval < 60 {
205+ d .KeepaliveInterval = 60
206+ }
202207 d .KeepaliveTime = time .Now ()
203208 d .Debug ("into keeplive,deviceid is " , d .DeviceId , "d.KeepaliveTime is" , d .KeepaliveTime , "d.KeepaliveInterval is" , d .KeepaliveInterval )
204209 if d .plugin .DB != nil {
@@ -227,30 +232,46 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
227232 isFirst := catalogReq .AddResponse ()
228233
229234 // 更新设备信息到数据库
230- if d .plugin .DB != nil {
231- // 如果是第一个响应,先清空现有通道
232- if isFirst {
233- d .Trace ("清空现有通道" , "deviceId" , d .DeviceId )
234- // 清空内存中的通道缓存
235- d .channels .Clear ()
236- }
235+ // 如果是第一个响应,将所有通道状态标记为OFF
236+ if isFirst {
237+ d .Trace ("将所有通道状态标记为OFF" , "deviceId" , d .DeviceId )
238+ // 标记所有通道为OFF状态
239+ d .channels .Range (func (channel * Channel ) bool {
240+ if channel .DeviceChannel != nil {
241+ channel .DeviceChannel .Status = gb28181 .ChannelOffStatus
242+ }
243+ return true
244+ })
245+ }
237246
238- // 更新通道信息
239- for _ , c := range msg .DeviceChannelList {
240- // 设置关联的设备数据库ID
241- c .ChannelId = c .DeviceId
242- c .DeviceId = d .DeviceId
243- c .ID = d .DeviceId + "_" + c .ChannelId
244- // 使用 Save 进行 upsert 操作
245- d . addOrUpdateChannel ( c )
247+ // 更新通道信息
248+ for _ , c := range msg .DeviceChannelList {
249+ // 设置关联的设备数据库ID
250+ c .ChannelId = c .DeviceId
251+ c .DeviceId = d .DeviceId
252+ c .ID = d .DeviceId + "_" + c .ChannelId
253+ if c . CustomChannelId == "" {
254+ c . CustomChannelId = c . ChannelId
246255 }
247-
248- // 更新当前设备的通道数
249- d .ChannelCount = msg .SumNum
250- d .UpdateTime = time .Now ()
251- d .Debug ("save channel" , "deviceid" , d .DeviceId , " d.channels.Length" , d .channels .Length , "d.ChannelCount" , d .ChannelCount , "d.UpdateTime" , d .UpdateTime )
256+ // 使用 Save 进行 upsert 操作
257+ d .addOrUpdateChannel (c )
252258 }
253259
260+ // 更新当前设备的通道数
261+ d .ChannelCount = msg .SumNum
262+ d .UpdateTime = time .Now ()
263+ d .Debug ("save channel" , "deviceid" , d .DeviceId , " d.channels.Length" , d .channels .Length , "d.ChannelCount" , d .ChannelCount , "d.UpdateTime" , d .UpdateTime )
264+
265+ // 删除所有状态为OFF的通道
266+ // d.channels.Range(func(channel *Channel) bool {
267+ // if channel.DeviceChannel != nil && channel.DeviceChannel.Status == gb28181.ChannelOffStatus {
268+ // d.Debug("删除不存在的通道", "channelId", channel.ID)
269+ // d.channels.RemoveByKey(channel.ID)
270+ // d.plugin.channels.RemoveByKey(channel.ID)
271+ // }
272+ // return true
273+ // })
274+
254275 // 在所有通道都添加完成后,检查是否完成接收
255276 if catalogReq .IsComplete (d .channels .Length ) {
256277 catalogReq .Resolve ()
@@ -344,8 +365,11 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
344365 case "DeviceInfo" :
345366 // 主设备信息
346367 d .Info ("DeviceInfo message" , "body" , req .Body (), "d.Name" , d .Name , "d.DeviceId" , d .DeviceId , "msg.DeviceName" , msg .DeviceName )
347- if d . Name == "" && msg .DeviceName != "" {
368+ if msg .DeviceName != "" {
348369 d .Name = msg .DeviceName
370+ if d .CustomName == "" {
371+ d .CustomName = msg .DeviceName
372+ }
349373 }
350374 d .Manufacturer = msg .Manufacturer
351375 d .Model = msg .Model
@@ -569,9 +593,28 @@ func (d *Device) frontEndCmdString(cmdCode int32, parameter1 int32, parameter2 i
569593}
570594
571595func (d * Device ) addOrUpdateChannel (c gb28181.DeviceChannel ) {
596+ // 设置通道状态为在线
597+ c .Status = gb28181 .ChannelOnStatus
598+
572599 if channel , ok := d .channels .Get (c .ID ); ok {
600+ // 通道已存在,保留自定义字段
601+ if channel .DeviceChannel != nil {
602+ // 保存原有的自定义字段
603+ customName := channel .DeviceChannel .CustomName
604+ customChannelId := channel .DeviceChannel .CustomChannelId
605+
606+ // 如果原有字段有值,则保留
607+ if customName != "" {
608+ c .CustomName = customName
609+ }
610+ if customChannelId != "" {
611+ c .CustomChannelId = customChannelId
612+ }
613+ }
614+ // 更新通道信息
573615 channel .DeviceChannel = & c
574616 } else {
617+ // 创建新通道
575618 channel = & Channel {
576619 Device : d ,
577620 Logger : d .Logger .With ("channel" , c .ID ),
0 commit comments