Skip to content

Commit

Permalink
1.增加获取当前JT2GB列表接口;2.级联上线自动关联当前在线的JT2GB车机;
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjoge committed Sep 27, 2024
1 parent 96881e0 commit 71452ec
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 50 deletions.
19 changes: 19 additions & 0 deletions GBWeb/Controllers/JT2GBController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,24 @@ public async Task<ApiResult> Offline(List<JTKey> lst)
return new ApiResult(500) { message = "err" };
}
}
/// <summary>
/// 获取当前有效JT1078车机
/// </summary>
/// <returns></returns>
[HttpGet]
public ApiResult<List<JTItem>> GetAll()
{
try
{
return new ApiResult<List<JTItem>>()
{
data = Program.sipServer.JT2GB.GetAll()
};
}
catch
{
return new ApiResult<List<JTItem>>(500) { message = "err" };
}
}
}
}
26 changes: 24 additions & 2 deletions SipServer/Cascade/CascadeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using GB28181.MANSRTSP;
using GB28181.XML;
using JTServer.Model.RTVS;
using SipServer.DBModel;
using SipServer.JT2GB;
using SipServer.Models;
using SIPSorcery.Net;
Expand Down Expand Up @@ -273,7 +274,28 @@ public override void Stop(bool waitStop = true)
}
}

protected internal void AddChannel(SuperiorChannel item, JT2GBChannel j2gChannel)
protected internal void AddChannel(JT2GBChannel j2gChannel)
{
AddChannel(new Models.SuperiorChannel(new TCatalog
{
ChannelId = j2gChannel.JTItem.GBChannelId,
DeviceId = j2gChannel.JTItem.GBDeviceId,
Name = j2gChannel.JTItem.GBChannelName,
Manufacturer = "RTVS",
Model = "gbsip",
Owner = "Owner",
CivilCode = j2gChannel.JTItem.GBChannelId.Substring(0, 6),
Address = "Address",
RegisterWay = 1,
Secrecy = false,
DType = 1001,
Online = true,
ParentId = DeviceID + "/" + j2gChannel.JTItem.GBGroupID,
Status = "ON",
}, Key, j2gChannel.JTItem.GBChannelId, j2gChannel.JTItem.GBGroupID), j2gChannel);
}

protected void AddChannel(SuperiorChannel item, JT2GBChannel j2gChannel)
{
var channel_id = item.GetChannelId();
//ditChannels[channel_id] = item;
Expand Down Expand Up @@ -326,4 +348,4 @@ private static string Empty2Null(string val)
return string.IsNullOrEmpty(val) ? null : val;
}
}
}
}
17 changes: 15 additions & 2 deletions SipServer/Cascade/CascadeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ async Task<bool> AddClient(TSuperiorInfo sinfo)
return new List<CascadeClient>();
});
lst.Add(client);

if (sipServer.JT2GB.ditGroupChannels.TryGetValue(groupId, out var hs)) {
lock (hs)
{
foreach (var channel in hs)
{
client.AddChannel(channel);
}
}
}

}
return true;
}
Expand Down Expand Up @@ -205,8 +216,10 @@ public CascadeClient GetClient(string key)
}
public List<CascadeClient> GetClientByGroupId(string groupId)
{
ditGroupClients.TryGetValue(groupId, out var lst);
return lst;
return ditGroupClients.GetOrAdd(groupId, key =>
{
return new List<CascadeClient>();
});
}
}
}
81 changes: 56 additions & 25 deletions SipServer/JT2GB/JT2GBChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using SipServer.Cascade;
using System;
using SipServer.Models.JT;

namespace SipServer.JT2GB
{
Expand All @@ -16,48 +17,50 @@ public class JT2GBChannel
/// </summary>
public string Key { get; protected set; }
/// <summary>
/// Sim卡号(JT1078)
///
/// </summary>
string sim;
/// <summary>
/// 通道号(JT1078)
/// </summary>
int channel;
/// <summary>
/// 2019版本
/// </summary>
bool is2019;
public JTItem JTItem { get; set; }

JT2GBClient client;
/// <summary>
/// 在线状态
/// </summary>
private bool online;
/// <summary>
/// 过期时间(秒) 小于等于0表示不过期
/// </summary>
private long expiresIn;
/// <summary>
/// 绑定的级联客户端列表
/// 绑定的级联客户端列表(引用传递 此类不要做添加删除操作)
/// </summary>
private List<CascadeClient> cascadeClients;

private DateTime heartbeatTime;

public JT2GBChannel(JT2GBClient client, string gbChannelId, string sim, int channel, bool is2019)
//public JT2GBChannel(JT2GBClient client, string gbChannelId, string sim, int channel, bool is2019)
//{
// this.client = client;
// this.sim = sim;
// this.channel = channel;
// this.is2019 = is2019;
// this.online = true;
// this.Key = gbChannelId;
//}
public JT2GBChannel(JT2GBClient client, JTItem item)
{
this.client = client;
this.sim = sim;
this.channel = channel;
this.is2019 = is2019;
this.JTItem = item;
//this.sim = item.JTSim;
//this.channel = item.JTChannel;
//this.is2019 = item.JTVer == 1;
this.online = true;
this.Key = gbChannelId;
this.Key = item.GBChannelId;
}
private bool Is2019()
{
return JTItem.JTVer == 1;
}


public async Task<SendRTPTask> INVITE_API(SDP28181 sdp)
{
var str = await HttpHelperByHttpClient.HttpRequestHtml(client.manager.sipServer.Settings.RTVSAPI + $"api/GB/CreateSendRTPTask?Protocol={(is2019 ? "1" : "0")}&Sim={sim}&Channel={channel}&RTPServer={sdp.RtpIp}&RTPPort={sdp.RtpPort}&UseUdp={(sdp.NetType == SDP28181.RTPNetType.TCP ? "false" : "true")}", false, CancellationToken.None);
var str = await HttpHelperByHttpClient.HttpRequestHtml(client.manager.sipServer.Settings.RTVSAPI + $"api/GB/CreateSendRTPTask?Protocol={(Is2019() ? "1" : "0")}&Sim={JTItem.JTSim}&Channel={JTItem.JTChannel}&RTPServer={sdp.RtpIp}&RTPPort={sdp.RtpPort}&UseUdp={(sdp.NetType == SDP28181.RTPNetType.TCP ? "false" : "true")}", false, CancellationToken.None);
return str.ParseJSON<SendRTPTask>();
}

Expand All @@ -70,15 +73,43 @@ protected internal void Offline()
{
item.RemoveChannel(Key);
}
cascadeClients = null;
}
cascadeClients = null;
if (client.manager.ditGroupChannels.TryGetValue(JTItem.GBGroupID, out var lst))
{
lock (lst)
{
lst.Remove(this);
}
}
}

protected internal void Online(long expiresIn, List<CascadeClient> lstCascadeClient)
protected internal void Online(JTItem item, List<CascadeClient> lstCascadeClient)
{
this.heartbeatTime = DateTime.Now;
this.expiresIn = expiresIn;
cascadeClients = lstCascadeClient;

if (item.GBGroupID != JTItem.GBGroupID)
{
//移除之前分组记录
if (client.manager.ditGroupChannels.TryGetValue(item.GBGroupID, out var lst))
{
lock (lst)
{
lst.Remove(this);
}
}
}
JTItem = item;
//添加当前分组记录
var lstChannel = client.manager.ditGroupChannels.GetOrAdd(item.GBGroupID, key =>
{
return new HashSet<JT2GBChannel>();
});
lock (lstChannel)
{
lstChannel.Add(this);
}
}
public bool IsOnline()
{
Expand All @@ -90,7 +121,7 @@ public bool IsOnline()
/// <returns></returns>
public bool IsTimeOut()
{
if (expiresIn > 0 && heartbeatTime.DiffNowSec() > expiresIn)
if (JTItem.ExpiresIn > 0 && heartbeatTime.DiffNowSec() > JTItem.ExpiresIn)
{
Offline();
return true;
Expand Down
17 changes: 15 additions & 2 deletions SipServer/JT2GB/JT2GBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SIPSorcery.SIP;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Collections.Generic;

namespace SipServer.JT2GB
{
Expand Down Expand Up @@ -136,10 +137,13 @@ public async Task<bool> On_BYE(string fromTag)
return false;
}


/// <summary>
/// 超时检查
/// </summary>
/// <returns></returns>
public bool Check()
{
var clients = ditChannels.Values.ToList();
var clients = GetAllChannels();
foreach (var item in clients)
{
if (item.IsTimeOut())
Expand All @@ -149,5 +153,14 @@ public bool Check()
}
return ditChannels.Count > 0;
}
/// <summary>
/// 获取所有通道
/// </summary>
/// <returns></returns>
public List<JT2GBChannel> GetAllChannels()
{
var channels = ditChannels.Values.ToList();
return channels;
}
}
}
37 changes: 18 additions & 19 deletions SipServer/JT2GB/JT2GBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class JT2GBManager
/// 客户端
/// </summary>
protected ConcurrentDictionary<string, JT2GBClient> ditClient = new ConcurrentDictionary<string, JT2GBClient>();
protected internal ConcurrentDictionary<string, HashSet<JT2GBChannel>> ditGroupChannels = new ConcurrentDictionary<string, HashSet<JT2GBChannel>>();
protected internal SipServer sipServer;
public JT2GBManager(SipServer sipServer)
{
Expand All @@ -39,33 +40,17 @@ public async Task AddJTItems(List<JTItem> lst)
});
var channel = client.GetOrAddChannel(item.GBChannelId, key =>
{
return new JT2GBChannel(client, item.GBChannelId, item.JTSim, item.JTChannel, item.JTVer == 1);
return new JT2GBChannel(client, item);
});
if (item.GBGroupID != null)
{
var lstCascadeClient = sipServer.Cascade.GetClientByGroupId(item.GBGroupID);
channel.Online(item.ExpiresIn, lstCascadeClient);
channel.Online(item, lstCascadeClient);
if (lstCascadeClient != null && lstCascadeClient.Count > 0)
{
foreach (var cascadeClient in lstCascadeClient)
{
cascadeClient.AddChannel(new Models.SuperiorChannel(new TCatalog
{
ChannelId = item.GBChannelId,
DeviceId = item.GBDeviceId,
Name = item.GBChannelName,
Manufacturer = "RTVS",
Model = "gbsip",
Owner = "Owner",
CivilCode = item.GBChannelId.Substring(0, 6),
Address = "Address",
RegisterWay = 1,
Secrecy = false,
DType = 1001,
Online = true,
ParentId = cascadeClient.DeviceID + "/" + item.GBGroupID,
Status = "ON",
}, cascadeClient.Key, item.GBChannelId, item.GBGroupID), channel);
cascadeClient.AddChannel(channel);
}
}
}
Expand Down Expand Up @@ -102,5 +87,19 @@ public void Check()
{
}
}

public List<JTItem> GetAll()
{
List<JTItem> lst = new List<JTItem>();
var arr = ditGroupChannels.Values.ToList();
foreach (var hs in arr)
{
foreach (var channel in hs)
{
lst.Add(channel.JTItem);
}
}
return lst;
}
}
}

0 comments on commit 71452ec

Please sign in to comment.