diff --git a/NearCardAttendance.Service/TcpServer/Handler/RegisterHandler.cs b/NearCardAttendance.Service/TcpServer/Handler/RegisterHandler.cs index 690a0ea..624ce0a 100644 --- a/NearCardAttendance.Service/TcpServer/Handler/RegisterHandler.cs +++ b/NearCardAttendance.Service/TcpServer/Handler/RegisterHandler.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Channels; using System.Threading.Tasks; namespace NearCardAttendance.Service.TcpServer.Handler @@ -59,44 +60,51 @@ namespace NearCardAttendance.Service.TcpServer.Handler using (_logger.BeginScope(new Dictionary { ["RequestId"] = parser.SeqNo })) { //认证 - if (parser.FuncNo.Equals("10")) - { - #region 认证业务 - // PHONE_AUTHEN - ProtocolWrapper phoneAuthWrapper = new(parser.FuncNo, parser.SeqNo, "1"); - await SendToTcpClientAsync(phoneAuthWrapper, context.Channel); - - // ABT_STATUS 延时3秒 给终端同步时间 - await context.Channel.EventLoop.Schedule(async () => - { - ProtocolWrapper abtStatusWrapper = new("82", parser.SeqNo, DateTime.Now.ToString("yyyyMMddHHmmss")); - await SendToTcpClientAsync(abtStatusWrapper, context.Channel); - },TimeSpan.FromSeconds(3)); - - #endregion - } - else if (parser.FuncNo.Equals("82")) - { - // 认证成功 - _logger.LogInformation($"认证成功{parser.Data}."); - } - // 心跳 - else if (parser.FuncNo.Equals("05")) - { - ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo,""); - await SendToTcpClientAsync(stdtSignRecsWrapper, context.Channel); - } // STDT_SIGN_RECS - else if (parser.FuncNo.Equals("04")) - { - // 回应设备 - ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo,"1"); - await SendToTcpClientAsync(stdtSignRecsWrapper, context.Channel); - // 刷卡考勤信息,需要推送给第三方平台 + //if (parser.FuncNo.Equals("10")) + //{ + // #region 认证业务 + // // PHONE_AUTHEN + // ProtocolWrapper phoneAuthWrapper = new(parser.FuncNo, parser.SeqNo, "1"); + // await SendToTcpClientAsync(phoneAuthWrapper, context.Channel); + + // // ABT_STATUS 延时3秒 给终端同步时间 + // await context.Channel.EventLoop.Schedule(async () => + // { + // ProtocolWrapper abtStatusWrapper = new("82", parser.SeqNo, DateTime.Now.ToString("yyyyMMddHHmmss")); + // await SendToTcpClientAsync(abtStatusWrapper, context.Channel); + // },TimeSpan.FromSeconds(3)); + + // #endregion + //} + //else if (parser.FuncNo.Equals("82")) + //{ + // // 认证成功 + // _logger.LogInformation($"认证成功{parser.Data}."); + //} + //// 心跳 + //else if (parser.FuncNo.Equals("05")) + //{ + // ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo,""); + // await SendToTcpClientAsync(stdtSignRecsWrapper, context.Channel); + //} // STDT_SIGN_RECS + //else if (parser.FuncNo.Equals("04")) + //{ + // // 回应设备 + // ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo,"1"); + // await SendToTcpClientAsync(stdtSignRecsWrapper, context.Channel); + // // 刷卡考勤信息,需要推送给第三方平台 - //var url = ""; + // //var url = ""; - //await _httpHelper.HttpToPostAsync(url, new object()); - } + // //await _httpHelper.HttpToPostAsync(url, new object()); + //} + //// STDT_SCHOOL_RECS + //else if (parser.FuncNo.Equals("08")) + //{ + // _logger.LogInformation($"接受STDT_SCHOOL_RECS 08 成功{parser.Data}."); + // ProtocolWrapper stdtSchoolRecsWrapper = new(parser.FuncNo, parser.SeqNo, "1"); + // await SendToTcpClientAsync(stdtSchoolRecsWrapper, context.Channel); + //} //switch (parser.FuncNo) @@ -109,6 +117,28 @@ namespace NearCardAttendance.Service.TcpServer.Handler // default: // break; //} + + switch (parser.FuncNo) + { + case "10": + await HandleAuthenticationAsync(parser, context.Channel); + break; + case "82": + await HandleAuthenticationSuccessAsync(parser, context.Channel); + break; + case "05": + await SendHeartbeatAsync(parser, context.Channel); + break; + case "04": + await HandleSignRecsAsync(parser, context.Channel); + break; + case "08": + await HandleStdtSchoolRecsAsync(parser, context.Channel); + break; + default: + _logger.LogInformation($"未处理业务,func_no:{parser.FuncNo},seq_no{parser.SeqNo},data:{parser.Data}"); + break; + } } } catch (Exception ex) @@ -138,5 +168,79 @@ namespace NearCardAttendance.Service.TcpServer.Handler }); } + /// + /// 认证业务 + /// + /// + /// + /// + private async Task HandleAuthenticationAsync(ProtocolParser parser, IChannel channel) + { + ProtocolWrapper phoneAuthWrapper = new(parser.FuncNo, parser.SeqNo, "1"); + await SendToTcpClientAsync(phoneAuthWrapper, channel); + + await channel.EventLoop.Schedule(async () => + { + ProtocolWrapper abtStatusWrapper = new("82", parser.SeqNo, DateTime.Now.ToString("yyyyMMddHHmmss")); + await SendToTcpClientAsync(abtStatusWrapper, channel); + }, TimeSpan.FromSeconds(3)); + } + + /// + /// 认证业务成功 + /// + /// + /// + /// + private async Task HandleAuthenticationSuccessAsync(ProtocolParser parser, IChannel channel) + { + _logger.LogInformation($"认证成功: func_no:{parser.FuncNo},seq_no{parser.SeqNo},data:{parser.Data}."); + await Task.CompletedTask; + + } + + /// + /// CONNECT_STATUS 网络连接状态查询[取值:05] (心跳业务) + /// + /// + /// + /// + private async Task SendHeartbeatAsync(ProtocolParser parser, IChannel channel) + { + ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo, ""); + await SendToTcpClientAsync(stdtSignRecsWrapper, channel); + } + + /// + /// STDT_SIGN_RECS 学生签到记录[取值:04] + /// + /// + /// + /// + private async Task HandleSignRecsAsync(ProtocolParser parser, IChannel channel) + { + ProtocolWrapper stdtSignRecsWrapper = new(parser.FuncNo, parser.SeqNo, "1"); + await SendToTcpClientAsync(stdtSignRecsWrapper, channel); + + // Push card attendance information to third-party platform + //var url = "https://midplat.xinhualeyu.com/dev-api/user/electronicCardAttendance/receiveTbAttendanceRecord"; + //await _httpHelper.HttpToPostAsync(url, new object()); + } + + /// + /// STDT_SCHOOL_RECS 学生进校离校记录[取值:08] + /// + /// + /// + /// + private async Task HandleStdtSchoolRecsAsync(ProtocolParser parser, IChannel channel) + { + _logger.LogInformation($"Received STDT_SCHOOL_RECS successfully: {parser.Data}."); + ProtocolWrapper stdtSchoolRecsWrapper = new(parser.FuncNo, parser.SeqNo, "1"); + await SendToTcpClientAsync(stdtSchoolRecsWrapper, channel); + + //var url = "https://midplat.xinhualeyu.com/dev-api/user/electronicCardAttendance/receiveTbAttendanceRecord"; + //await _httpHelper.HttpToPostAsync(url, new object()); + } } }