|
- using DotNetty.Buffers;
- using DotNetty.Transport.Channels;
- using Microsoft.Extensions.Logging;
- using NearCardAttendance.Common.helper;
- using NearCardAttendance.Service.TcpServer.Mapper;
- using NearCardAttendance.Service.TcpServer.Protocol;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace NearCardAttendance.Service.TcpServer.Handler
- {
- public class RegisterHandler : ChannelHandlerAdapter
- {
- private readonly ILogger<RegisterHandler> _logger;
- private readonly HttpHelper _httpHelper = default!;
- private readonly IDisposable _loggerScope = default!;
-
-
- private readonly TcpClientsManager _managerTcpClients;
- private readonly ScheduleResendManager _managerScheduleResend;
-
- public RegisterHandler(ILogger<RegisterHandler> logger,HttpHelper httpHelper)
- {
- _logger = logger;
- _httpHelper = httpHelper;
- }
-
- public override void ChannelActive(IChannelHandlerContext context)
- {
- base.ChannelActive(context); // Pass the event to the next handler
- }
-
- public override async void ChannelInactive(IChannelHandlerContext context)
- {
- try
- {
-
- }
- catch (Exception ex)
- {
- _logger.LogInformation($"{nameof(RegisterHandler)} --- {nameof(ChannelInactive)} 出错\n{ex.Message}\n{ex.StackTrace}");
- }
- }
-
- public override void HandlerRemoved(IChannelHandlerContext context)
- {
- base.HandlerRemoved(context);
- }
-
- public override async void ChannelRead(IChannelHandlerContext context, object message)
- {
- if (message is ProtocolParser parser)
- {
- try
- {
- using (_logger.BeginScope(new Dictionary<string, object> { ["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);
- // 刷卡考勤信息,需要推送给第三方平台
-
- //var url = "";
-
- //await _httpHelper.HttpToPostAsync(url, new object());
- }
-
-
- //switch (parser.FuncNo)
- //{
- // case "10":
- // break;
-
- // case "82":
- // break;
- // default:
- // break;
- //}
- }
- }
- catch (Exception ex)
- {
- _logger.LogInformation($"{nameof(RegisterHandler)} --- {nameof(ChannelRead)} 处理消息 {parser.SeqNo} 出错\n{ex.Message}\n{ex.StackTrace}");
-
- }
-
- }
- }
-
- /// <summary>
- /// 发送到TCP客户端
- /// </summary>
- /// <param name="wrapper"></param>
- /// <param name="channel"></param>
- private async Task SendToTcpClientAsync(ProtocolWrapper wrapper, IChannel channel)
- {
-
- string protocolMsg = wrapper.GenerateProtocolString();
-
- // 发送protocolMsg到tcp客户端
- await channel
- .WriteAndFlushAsync(Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(protocolMsg)))
- .ContinueWith(Action => {
- _logger.LogInformation($"{nameof(RegisterHandler)} -- {nameof(SendToTcpClientAsync)} -- 下发设备内容:\n{protocolMsg}");
- });
- }
-
- }
- }
|