From a77904484f4385b2fabb7726ebdc48eaf635ee63 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Thu, 20 Dec 2018 21:53:39 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E7=A0=81=202.=E6=95=B4=E7=90=86=E5=BE=85?= =?UTF-8?q?=E5=81=9A=E4=BA=8B=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JT808.DotNetty/Dtos/JT808ResultDto.cs | 1 + .../JT808UnificationSendServiceDefaultImpl.cs | 17 +++++++++++++---- src/JT808.DotNetty/JT808SessionManager.cs | 13 ++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/JT808.DotNetty/Dtos/JT808ResultDto.cs b/src/JT808.DotNetty/Dtos/JT808ResultDto.cs index da5cbf5..dabc8f2 100644 --- a/src/JT808.DotNetty/Dtos/JT808ResultDto.cs +++ b/src/JT808.DotNetty/Dtos/JT808ResultDto.cs @@ -18,6 +18,7 @@ namespace JT808.DotNetty.Dtos public const int Ok = 200; public const int Empty = 201; public const int NotFound = 404; + public const int Fail = 400; public const int Error = 500; } } diff --git a/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs b/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs index d6c8075..8c8581a 100644 --- a/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs +++ b/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs @@ -24,15 +24,24 @@ namespace JT808.DotNetty.Internal var session = jT808SessionManager.GetSessionByTerminalPhoneNo(terminalPhoneNo); if (session != null) { - session.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = true; + if (session.Channel.Open) + { + session.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); + resultDto.Code = JT808ResultCode.Ok; + resultDto.Data = true; + } + else + { + resultDto.Code = JT808ResultCode.Ok; + resultDto.Data = false; + resultDto.Message = "offline"; + } } else { resultDto.Code = JT808ResultCode.Ok; resultDto.Data = false; - resultDto.Message = "not session"; + resultDto.Message = "offline"; } } catch (Exception ex) diff --git a/src/JT808.DotNetty/JT808SessionManager.cs b/src/JT808.DotNetty/JT808SessionManager.cs index 49f812f..d2c9a50 100644 --- a/src/JT808.DotNetty/JT808SessionManager.cs +++ b/src/JT808.DotNetty/JT808SessionManager.cs @@ -121,7 +121,15 @@ namespace JT808.DotNetty public void TryAddOrUpdateSession(JT808Session appSession) { SessionIdDict.TryAdd(appSession.SessionID, appSession); - TerminalPhoneNo_SessionId_Dict.TryAdd(appSession.TerminalPhoneNo, appSession.SessionID); + if(TerminalPhoneNo_SessionId_Dict.TryAdd(appSession.TerminalPhoneNo, appSession.SessionID)) + { + //使用场景: + //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, + //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 + //todo: 有设备关联上来可以进行通知 + //todo: 使用Redis发布订阅 + + } } public JT808Session RemoveSessionByID(string sessionID) @@ -137,6 +145,9 @@ namespace JT808.DotNetty { TerminalPhoneNo_SessionId_Dict.TryRemove(key, out string sessionid); } + //todo: 设备离线可以进行通知 + //todo: 使用Redis 发布订阅 + logger.LogInformation($">>>{sessionID}-{string.Join(",",removeKeys)} Session Remove."); return session; }