feat: devicelist add trasnport,ip,port

This commit is contained in:
pggiroro
2025-09-11 09:40:21 +08:00
parent ea512e1dd9
commit 246bea7bec
5 changed files with 1174 additions and 2932 deletions

View File

@@ -111,26 +111,35 @@ func (gb *GB28181Plugin) List(ctx context.Context, req *pb.GetDevicesRequest) (*
}) })
pbDevices = append(pbDevices, &pb.Device{ pbDevices = append(pbDevices, &pb.Device{
DeviceId: d.DeviceId, DeviceId: d.DeviceId,
Name: d.CustomName, Name: d.CustomName,
Manufacturer: d.Manufacturer, Manufacturer: d.Manufacturer,
Model: d.Model, Model: d.Model,
Status: string(d.Status), Status: string(d.Status),
Online: d.Online, Online: d.Online,
Longitude: d.Longitude, Longitude: d.Longitude,
Latitude: d.Latitude, Latitude: d.Latitude,
RegisterTime: timestamppb.New(d.RegisterTime), RegisterTime: timestamppb.New(d.RegisterTime),
UpdateTime: timestamppb.New(d.UpdateTime), UpdateTime: timestamppb.New(d.UpdateTime),
KeepAliveTime: timestamppb.New(d.KeepaliveTime), KeepAliveTime: timestamppb.New(d.KeepaliveTime),
ChannelCount: int32(d.ChannelCount), ChannelCount: int32(d.ChannelCount),
Channels: pbChannels, Channels: pbChannels,
MediaIp: d.MediaIp, MediaIp: d.MediaIp,
SipIp: d.SipIp, SipIp: d.SipIp,
Password: d.Password, Password: d.Password,
StreamMode: string(d.StreamMode), StreamMode: string(d.StreamMode),
Transport: d.Transport,
Ip: d.IP,
Port: int32(d.Port),
BroadcastPushAfterAck: d.BroadcastPushAfterAck,
}) })
} }
// 按deviceId对设备列表进行排序
sort.Slice(pbDevices, func(i, j int) bool {
return pbDevices[i].DeviceId < pbDevices[j].DeviceId
})
resp.Code = 0 resp.Code = 0
resp.Message = "success" resp.Message = "success"
resp.Data = pbDevices resp.Data = pbDevices

View File

@@ -47,10 +47,6 @@ func (d *DeviceKeepaliveTickTask) Tick(any) {
if d.device.KeepaliveInterval >= 5 { if d.device.KeepaliveInterval >= 5 {
keepaliveSeconds = d.device.KeepaliveInterval keepaliveSeconds = d.device.KeepaliveInterval
} }
d.Debug("keepLiveTick", "deviceID", d.device.DeviceId,
"keepaliveTime", d.device.KeepaliveTime,
"interval", d.device.KeepaliveInterval,
"count", d.device.KeepaliveCount)
if timeDiff := time.Since(d.device.KeepaliveTime); timeDiff > time.Duration(d.device.KeepaliveCount*keepaliveSeconds)*time.Second { if timeDiff := time.Since(d.device.KeepaliveTime); timeDiff > time.Duration(d.device.KeepaliveCount*keepaliveSeconds)*time.Second {
d.device.Online = false d.device.Online = false
d.device.Status = DeviceOfflineStatus d.device.Status = DeviceOfflineStatus
@@ -94,7 +90,7 @@ type Device struct {
Password string // 密码 Password string // 密码
SipIp string // SIP交互IP设备访问平台的IP SipIp string // SIP交互IP设备访问平台的IP
AsMessageChannel bool // 是否作为消息通道 AsMessageChannel bool // 是否作为消息通道
BroadcastPushAfterAck bool // 控制语音对讲流程释放收到ACK后发流 BroadcastPushAfterAck bool `gorm:"default:false" default:"false"` // 控制语音对讲流程释放收到ACK后发流
DeletedAt gorm.DeletedAt `yaml:"-"` DeletedAt gorm.DeletedAt `yaml:"-"`
// 删除强关联字段 // 删除强关联字段
// channels []gb28181.DeviceChannel `gorm:"foreignKey:DeviceDBID;references:ID"` // 设备通道列表 // channels []gb28181.DeviceChannel `gorm:"foreignKey:DeviceDBID;references:ID"` // 设备通道列表
@@ -203,7 +199,6 @@ func (c *catalogHandlerTask) Run() (err error) {
d := c.d d := c.d
msg := c.msg msg := c.msg
catalogReq, exists := d.catalogReqs.Get(msg.SN) catalogReq, exists := d.catalogReqs.Get(msg.SN)
d.Debug("into catalog", "msg.SN", msg.SN, "exists", exists)
if !exists { if !exists {
// 创建新的目录请求 // 创建新的目录请求
catalogReq = &CatalogRequest{ catalogReq = &CatalogRequest{
@@ -214,7 +209,6 @@ func (c *catalogHandlerTask) Run() (err error) {
Promise: util.NewPromise(context.Background()), Promise: util.NewPromise(context.Background()),
} }
d.catalogReqs.Set(catalogReq) d.catalogReqs.Set(catalogReq)
d.Debug("into catalog", "msg.SN", msg.SN, "d.catalogReqs", d.catalogReqs.Length)
} }
// 添加响应并获取是否是第一个响应 // 添加响应并获取是否是第一个响应
@@ -223,7 +217,6 @@ func (c *catalogHandlerTask) Run() (err error) {
// 更新设备信息到数据库 // 更新设备信息到数据库
// 如果是第一个响应将所有通道状态标记为OFF // 如果是第一个响应将所有通道状态标记为OFF
if isFirst { if isFirst {
d.Debug("将所有通道状态标记为OFF", "deviceId", d.DeviceId)
// 标记所有通道为OFF状态 // 标记所有通道为OFF状态
d.channels.Range(func(channel *Channel) bool { d.channels.Range(func(channel *Channel) bool {
if channel.DeviceChannel != nil { if channel.DeviceChannel != nil {
@@ -242,7 +235,6 @@ func (c *catalogHandlerTask) Run() (err error) {
if c.CustomChannelId == "" { if c.CustomChannelId == "" {
c.CustomChannelId = c.ChannelId c.CustomChannelId = c.ChannelId
} }
d.Debug("msg.DeviceList.DeviceChannelList range", "c.ChannelId", c.ChannelId, "c.Status", c.Status)
// 使用 Save 进行 upsert 操作 // 使用 Save 进行 upsert 操作
d.addOrUpdateChannel(c) d.addOrUpdateChannel(c)
catalogReq.TotalCount++ catalogReq.TotalCount++
@@ -251,21 +243,9 @@ func (c *catalogHandlerTask) Run() (err error) {
// 更新当前设备的通道数 // 更新当前设备的通道数
d.ChannelCount = msg.SumNum d.ChannelCount = msg.SumNum
d.UpdateTime = time.Now() d.UpdateTime = time.Now()
d.Debug("save channel", "deviceid", d.DeviceId, " d.channels.Length", d.channels.Length, "d.ChannelCount", d.ChannelCount, "d.UpdateTime", d.UpdateTime)
// 删除所有状态为OFF的通道
// d.channels.Range(func(channel *Channel) bool {
// if channel.DeviceChannel != nil && channel.DeviceChannel.Status == gb28181.ChannelOffStatus {
// d.Debug("删除不存在的通道", "channelId", channel.ID)
// d.channels.RemoveByKey(channel.ID)
// d.plugin.channels.RemoveByKey(channel.ID)
// }
// return true
// })
// 在所有通道都添加完成后,检查是否完成接收 // 在所有通道都添加完成后,检查是否完成接收
if catalogReq.IsComplete() { if catalogReq.IsComplete() {
d.Debug("IsComplete")
catalogReq.Resolve() catalogReq.Resolve()
d.catalogReqs.RemoveByKey(msg.SN) d.catalogReqs.RemoveByKey(msg.SN)
} }
@@ -273,7 +253,6 @@ func (c *catalogHandlerTask) Run() (err error) {
} }
func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28181.Message) (err error) { func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28181.Message) (err error) {
d.plugin.Debug("into onMessage", "deviceid is ", d.DeviceId, "msg is", msg)
source := req.Source() source := req.Source()
hostname, portStr, _ := net.SplitHostPort(source) hostname, portStr, _ := net.SplitHostPort(source)
port, _ := strconv.Atoi(portStr) port, _ := strconv.Atoi(portStr)
@@ -285,11 +264,6 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
d.Port = port d.Port = port
d.HostAddress = hostname + ":" + portStr d.HostAddress = hostname + ":" + portStr
var body []byte var body []byte
//d.Online = true
//if d.Status != DeviceOnlineStatus {
// d.Status = DeviceOnlineStatus
//}
//d.Debug("OnMessage", "cmdType", msg.CmdType, "body", string(req.Body()))
switch msg.CmdType { switch msg.CmdType {
case "Keepalive": case "Keepalive":
d.KeepaliveInterval = int(time.Since(d.KeepaliveTime).Seconds()) d.KeepaliveInterval = int(time.Since(d.KeepaliveTime).Seconds())
@@ -297,7 +271,6 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
d.KeepaliveInterval = 60 d.KeepaliveInterval = 60
} }
d.KeepaliveTime = time.Now() d.KeepaliveTime = time.Now()
d.Debug("into keeplive,deviceid is ", d.DeviceId, "d.KeepaliveTime is", d.KeepaliveTime, "d.KeepaliveInterval is", d.KeepaliveInterval)
if d.plugin.DB != nil { if d.plugin.DB != nil {
if err := d.plugin.DB.Model(d).Updates(map[string]interface{}{ if err := d.plugin.DB.Model(d).Updates(map[string]interface{}{
"keepalive_interval": d.KeepaliveInterval, "keepalive_interval": d.KeepaliveInterval,
@@ -399,7 +372,6 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
d.UpdateTime = time.Now() d.UpdateTime = time.Now()
case "DeviceInfo": case "DeviceInfo":
// 主设备信息 // 主设备信息
d.Info("DeviceInfo message", "body", req.Body(), "d.Name", d.Name, "d.DeviceId", d.DeviceId, "msg.DeviceName", msg.DeviceName)
if msg.DeviceName != "" { if msg.DeviceName != "" {
d.Name = msg.DeviceName d.Name = msg.DeviceName
if d.CustomName == "" { if d.CustomName == "" {
@@ -646,7 +618,6 @@ func (d *Device) addOrUpdateChannel(c gb28181.DeviceChannel) {
// 更新通道信息 // 更新通道信息
channel.DeviceChannel = &c channel.DeviceChannel = &c
d.channels.Range(func(channel *Channel) bool { d.channels.Range(func(channel *Channel) bool {
d.Debug("range d.channels", "channel.ChannelId", channel.ChannelId, "channel.status", channel.Status)
return true return true
}) })
} else { } else {

View File

@@ -917,29 +917,33 @@ func (x *Channel) GetLatitude() string {
} }
type Device struct { type Device struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
DeviceId string `protobuf:"bytes,1,opt,name=deviceId,proto3" json:"deviceId,omitempty"` DeviceId string `protobuf:"bytes,1,opt,name=deviceId,proto3" json:"deviceId,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Manufacturer string `protobuf:"bytes,3,opt,name=manufacturer,proto3" json:"manufacturer,omitempty"` Manufacturer string `protobuf:"bytes,3,opt,name=manufacturer,proto3" json:"manufacturer,omitempty"`
Model string `protobuf:"bytes,4,opt,name=model,proto3" json:"model,omitempty"` Model string `protobuf:"bytes,4,opt,name=model,proto3" json:"model,omitempty"`
Longitude string `protobuf:"bytes,5,opt,name=longitude,proto3" json:"longitude,omitempty"` Longitude string `protobuf:"bytes,5,opt,name=longitude,proto3" json:"longitude,omitempty"`
Latitude string `protobuf:"bytes,6,opt,name=latitude,proto3" json:"latitude,omitempty"` Latitude string `protobuf:"bytes,6,opt,name=latitude,proto3" json:"latitude,omitempty"`
Status string `protobuf:"bytes,7,opt,name=status,proto3" json:"status,omitempty"` Status string `protobuf:"bytes,7,opt,name=status,proto3" json:"status,omitempty"`
MediaIp string `protobuf:"bytes,8,opt,name=mediaIp,proto3" json:"mediaIp,omitempty"` MediaIp string `protobuf:"bytes,8,opt,name=mediaIp,proto3" json:"mediaIp,omitempty"`
RegisterTime *timestamppb.Timestamp `protobuf:"bytes,9,opt,name=registerTime,proto3" json:"registerTime,omitempty"` RegisterTime *timestamppb.Timestamp `protobuf:"bytes,9,opt,name=registerTime,proto3" json:"registerTime,omitempty"`
UpdateTime *timestamppb.Timestamp `protobuf:"bytes,10,opt,name=updateTime,proto3" json:"updateTime,omitempty"` UpdateTime *timestamppb.Timestamp `protobuf:"bytes,10,opt,name=updateTime,proto3" json:"updateTime,omitempty"`
KeepAliveTime *timestamppb.Timestamp `protobuf:"bytes,11,opt,name=keepAliveTime,proto3" json:"keepAliveTime,omitempty"` KeepAliveTime *timestamppb.Timestamp `protobuf:"bytes,11,opt,name=keepAliveTime,proto3" json:"keepAliveTime,omitempty"`
ChannelCount int32 `protobuf:"varint,12,opt,name=channelCount,proto3" json:"channelCount,omitempty"` ChannelCount int32 `protobuf:"varint,12,opt,name=channelCount,proto3" json:"channelCount,omitempty"`
Online bool `protobuf:"varint,13,opt,name=online,proto3" json:"online,omitempty"` Online bool `protobuf:"varint,13,opt,name=online,proto3" json:"online,omitempty"`
Channels []*Channel `protobuf:"bytes,14,rep,name=channels,proto3" json:"channels,omitempty"` Channels []*Channel `protobuf:"bytes,14,rep,name=channels,proto3" json:"channels,omitempty"`
SipIp string `protobuf:"bytes,15,opt,name=sipIp,proto3" json:"sipIp,omitempty"` SipIp string `protobuf:"bytes,15,opt,name=sipIp,proto3" json:"sipIp,omitempty"`
StreamMode string `protobuf:"bytes,16,opt,name=streamMode,proto3" json:"streamMode,omitempty"` StreamMode string `protobuf:"bytes,16,opt,name=streamMode,proto3" json:"streamMode,omitempty"`
Password string `protobuf:"bytes,17,opt,name=password,proto3" json:"password,omitempty"` Password string `protobuf:"bytes,17,opt,name=password,proto3" json:"password,omitempty"`
SubscribeCatalog bool `protobuf:"varint,18,opt,name=subscribeCatalog,proto3" json:"subscribeCatalog,omitempty"` SubscribeCatalog bool `protobuf:"varint,18,opt,name=subscribeCatalog,proto3" json:"subscribeCatalog,omitempty"`
SubscribePosition bool `protobuf:"varint,19,opt,name=subscribePosition,proto3" json:"subscribePosition,omitempty"` SubscribePosition bool `protobuf:"varint,19,opt,name=subscribePosition,proto3" json:"subscribePosition,omitempty"`
SubscribeAlarm bool `protobuf:"varint,20,opt,name=subscribeAlarm,proto3" json:"subscribeAlarm,omitempty"` SubscribeAlarm bool `protobuf:"varint,20,opt,name=subscribeAlarm,proto3" json:"subscribeAlarm,omitempty"`
unknownFields protoimpl.UnknownFields Transport string `protobuf:"bytes,21,opt,name=transport,proto3" json:"transport,omitempty"`
sizeCache protoimpl.SizeCache Ip string `protobuf:"bytes,22,opt,name=ip,proto3" json:"ip,omitempty"`
Port int32 `protobuf:"varint,23,opt,name=port,proto3" json:"port,omitempty"`
BroadcastPushAfterAck bool `protobuf:"varint,24,opt,name=broadcastPushAfterAck,proto3" json:"broadcastPushAfterAck,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
} }
func (x *Device) Reset() { func (x *Device) Reset() {
@@ -1112,6 +1116,34 @@ func (x *Device) GetSubscribeAlarm() bool {
return false return false
} }
func (x *Device) GetTransport() string {
if x != nil {
return x.Transport
}
return ""
}
func (x *Device) GetIp() string {
if x != nil {
return x.Ip
}
return ""
}
func (x *Device) GetPort() int32 {
if x != nil {
return x.Port
}
return 0
}
func (x *Device) GetBroadcastPushAfterAck() bool {
if x != nil {
return x.BroadcastPushAfterAck
}
return false
}
type ResponseList struct { type ResponseList struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
@@ -6332,7 +6364,7 @@ const file_gb28181_proto_rawDesc = "" +
"\x06status\x18\x10 \x01(\tR\x06status\x124\n" + "\x06status\x18\x10 \x01(\tR\x06status\x124\n" +
"\agpsTime\x18\x11 \x01(\v2\x1a.google.protobuf.TimestampR\agpsTime\x12\x1c\n" + "\agpsTime\x18\x11 \x01(\v2\x1a.google.protobuf.TimestampR\agpsTime\x12\x1c\n" +
"\tlongitude\x18\x12 \x01(\tR\tlongitude\x12\x1a\n" + "\tlongitude\x18\x12 \x01(\tR\tlongitude\x12\x1a\n" +
"\blatitude\x18\x13 \x01(\tR\blatitude\"\xdd\x05\n" + "\blatitude\x18\x13 \x01(\tR\blatitude\"\xd5\x06\n" +
"\x06Device\x12\x1a\n" + "\x06Device\x12\x1a\n" +
"\bdeviceId\x18\x01 \x01(\tR\bdeviceId\x12\x12\n" + "\bdeviceId\x18\x01 \x01(\tR\bdeviceId\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12\"\n" + "\x04name\x18\x02 \x01(\tR\x04name\x12\"\n" +
@@ -6358,7 +6390,11 @@ const file_gb28181_proto_rawDesc = "" +
"\bpassword\x18\x11 \x01(\tR\bpassword\x12*\n" + "\bpassword\x18\x11 \x01(\tR\bpassword\x12*\n" +
"\x10subscribeCatalog\x18\x12 \x01(\bR\x10subscribeCatalog\x12,\n" + "\x10subscribeCatalog\x18\x12 \x01(\bR\x10subscribeCatalog\x12,\n" +
"\x11subscribePosition\x18\x13 \x01(\bR\x11subscribePosition\x12&\n" + "\x11subscribePosition\x18\x13 \x01(\bR\x11subscribePosition\x12&\n" +
"\x0esubscribeAlarm\x18\x14 \x01(\bR\x0esubscribeAlarm\"d\n" + "\x0esubscribeAlarm\x18\x14 \x01(\bR\x0esubscribeAlarm\x12\x1c\n" +
"\ttransport\x18\x15 \x01(\tR\ttransport\x12\x0e\n" +
"\x02ip\x18\x16 \x01(\tR\x02ip\x12\x12\n" +
"\x04port\x18\x17 \x01(\x05R\x04port\x124\n" +
"\x15broadcastPushAfterAck\x18\x18 \x01(\bR\x15broadcastPushAfterAck\"d\n" +
"\fResponseList\x12\x12\n" + "\fResponseList\x12\x12\n" +
"\x04code\x18\x01 \x01(\x05R\x04code\x12\x18\n" + "\x04code\x18\x01 \x01(\x05R\x04code\x12\x18\n" +
"\amessage\x18\x02 \x01(\tR\amessage\x12&\n" + "\amessage\x18\x02 \x01(\tR\amessage\x12&\n" +

File diff suppressed because it is too large Load Diff

View File

@@ -607,6 +607,10 @@ message Device {
bool subscribeCatalog = 18; bool subscribeCatalog = 18;
bool subscribePosition = 19; bool subscribePosition = 19;
bool subscribeAlarm = 20; bool subscribeAlarm = 20;
string transport = 21;
string ip = 22;
int32 port = 23;
bool broadcastPushAfterAck =24;
} }
message ResponseList { message ResponseList {