HKC/DM_Weight/Port/SocketHelper.cs

158 lines
5.5 KiB
C#
Raw Normal View History

2024-12-23 09:52:48 +08:00
using DM_Weight.Finger;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.util;
using DM_Weight.ViewModels;
using DM_Weight.Views;
2024-12-16 16:44:02 +08:00
using log4net;
using Mina.Core.Future;
2025-01-18 15:08:04 +08:00
using Mina.Core.Session;
2024-12-16 16:44:02 +08:00
using Mina.Filter.Codec;
using Mina.Filter.Logging;
using Mina.Transport.Socket;
using Prism.Events;
2024-12-16 16:44:02 +08:00
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
2025-01-04 11:32:32 +08:00
using System.Speech.Synthesis;
2024-12-16 16:44:02 +08:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DM_Weight.Port
{
public class SocketHelper
{
private readonly ILog logger = LogManager.GetLogger(typeof(SocketHelper));
AsyncSocketConnector acceptor = new AsyncSocketConnector();
IConnectFuture iConnectFuture;
2025-01-18 15:08:04 +08:00
public bool OpenStatus = false;
public bool ConnectedStatus = false;
//多线程退出标识
public bool IsMultiThread = false;
2024-12-16 16:44:02 +08:00
string ip = ConfigurationManager.AppSettings["modbusIp"].ToString();
2025-01-18 15:08:04 +08:00
int port = Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
IEventAggregator _eventAggregator;
2024-12-23 09:52:48 +08:00
public DateTime dateTime { get; set; } = DateTime.Now;
FingerprintUtil _fingerprintUtil;
public SocketHelper(IEventAggregator eventAggregator, FingerprintUtil fingerprintUtil)
2024-12-16 16:44:02 +08:00
{
2024-12-23 09:52:48 +08:00
_eventAggregator = eventAggregator;
acceptor.ExceptionCaught += (o, e) =>
{
logger.Error($"网口通信超时:{e.Exception}");
AlertMsg alertMsg = new AlertMsg
{
Message = $"网口通信超时,正在重试!",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
};
2024-12-16 16:44:02 +08:00
//acceptor.FilterChain.AddLast("logger", new LoggingFilter());
acceptor.FilterChain.AddLast("encoding", new ProtocolCodecFilter(new MyMinaCodecFactory()));
acceptor.SessionIdle += (o, e) => logger.Info("IDLE " + e.Session.GetIdleCount(e.IdleStatus));
acceptor.MessageReceived += (o, e) =>
{
OpenStatus = (bool)e.Message;
logger.Info("MessageReceived>>>>>>>>>>>>>>>>" + OpenStatus);
2024-12-23 09:52:48 +08:00
//if(OpenStatus)
//{
// HomeWindow.idleTimer.Stop();
//}
//else
//{
// HomeWindow.idleTimer.Interval = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0") * 1000;
// HomeWindow.idleTimer.Start();
2024-12-23 09:52:48 +08:00
//}
2024-12-16 16:44:02 +08:00
};
2025-01-18 15:08:04 +08:00
acceptor.SessionClosed += new EventHandler<IoSessionEventArgs>(SessionCloseMethod);
//acceptor.SessionClosed += (o, e) =>
//{
// logger.Info("SessionClosed");
// //Task.Delay(50).Wait();
// SocketConnect();
// Task.Factory.StartNew(() =>
// {
// _fingerprintUtil.FingerDisconnect();
// });
//};
2024-12-16 16:44:02 +08:00
this.SocketConnect();
2024-12-23 09:52:48 +08:00
_fingerprintUtil = fingerprintUtil;
2024-12-16 16:44:02 +08:00
}
2024-12-23 09:52:48 +08:00
int i = 0;
2024-12-16 16:44:02 +08:00
public void SocketConnect()
{
2024-12-23 09:52:48 +08:00
Thread.Sleep(50);
i++;
2024-12-16 16:44:02 +08:00
iConnectFuture = acceptor.Connect(new IPEndPoint(IPAddress.Parse(ip), port)).Await();
2024-12-23 09:52:48 +08:00
logger.Info($"第{i}次连接结果:{iConnectFuture.Connected.ToString()}");
if (!iConnectFuture.Connected)
{
if (i <= 3)
{
2025-01-18 15:08:04 +08:00
Thread.Sleep(50);
2024-12-23 09:52:48 +08:00
//没连上会再连两次
SocketConnect();
}
else
{
logger.Error($"尝试{i}次连接后均连接不上");
AlertMsg alertMsg = new AlertMsg
{
Message = $"网口连接断开!",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
else
{
i = 0;
}
ConnectedStatus = iConnectFuture.Connected;
2024-12-16 16:44:02 +08:00
}
2025-01-18 15:08:04 +08:00
private void SessionCloseMethod(System.Object o, IoSessionEventArgs e)
{
logger.Info($"SessionClosed");
SocketConnect();
Task.Factory.StartNew(() =>
{
_fingerprintUtil.FingerDisconnect();
});
}
public void SocketDisConnect()
{
try
{
acceptor.SessionClosed -= new EventHandler<IoSessionEventArgs>(SessionCloseMethod);
iConnectFuture.Session.CloseNow();
}
catch(Exception ex)
{
logger.Info($"SocketDisConnect异常{ex.Message}");
}
}
2024-12-16 16:44:02 +08:00
public void SendMessage(MyBaseMessage baseMessage)
{
if (!ConnectedStatus)
{
SocketConnect();
}
2024-12-16 16:44:02 +08:00
iConnectFuture.Session.Write(baseMessage);
logger.Info($"SendMessage:{baseMessage.functionCode},delay:{baseMessage.delay},lockNo:{baseMessage.lockNo}");
2024-12-16 16:44:02 +08:00
}
2025-01-04 11:32:32 +08:00
public SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
public void SpeakAsync(string textinfo)
{
speechSynthesizer.Rate = 2;
speechSynthesizer.SpeakAsync(textinfo);
}
2024-12-16 16:44:02 +08:00
}
}