退出软件断开与板子的连接
This commit is contained in:
		
							parent
							
								
									35270de250
								
							
						
					
					
						commit
						12c2cc4e22
					
				| 
						 | 
					@ -6,6 +6,7 @@ using DM_Weight.ViewModels;
 | 
				
			||||||
using DM_Weight.Views;
 | 
					using DM_Weight.Views;
 | 
				
			||||||
using log4net;
 | 
					using log4net;
 | 
				
			||||||
using Mina.Core.Future;
 | 
					using Mina.Core.Future;
 | 
				
			||||||
 | 
					using Mina.Core.Session;
 | 
				
			||||||
using Mina.Filter.Codec;
 | 
					using Mina.Filter.Codec;
 | 
				
			||||||
using Mina.Filter.Logging;
 | 
					using Mina.Filter.Logging;
 | 
				
			||||||
using Mina.Transport.Socket;
 | 
					using Mina.Transport.Socket;
 | 
				
			||||||
| 
						 | 
					@ -27,12 +28,12 @@ namespace DM_Weight.Port
 | 
				
			||||||
        private readonly ILog logger = LogManager.GetLogger(typeof(SocketHelper));
 | 
					        private readonly ILog logger = LogManager.GetLogger(typeof(SocketHelper));
 | 
				
			||||||
        AsyncSocketConnector acceptor = new AsyncSocketConnector();
 | 
					        AsyncSocketConnector acceptor = new AsyncSocketConnector();
 | 
				
			||||||
        IConnectFuture iConnectFuture;
 | 
					        IConnectFuture iConnectFuture;
 | 
				
			||||||
        public bool OpenStatus=false;
 | 
					        public bool OpenStatus = false;
 | 
				
			||||||
        public bool ConnectedStatus = false;
 | 
					        public bool ConnectedStatus = false;
 | 
				
			||||||
        //多线程退出标识
 | 
					        //多线程退出标识
 | 
				
			||||||
        public bool IsMultiThread = false;
 | 
					        public bool IsMultiThread = false;
 | 
				
			||||||
        string ip = ConfigurationManager.AppSettings["modbusIp"].ToString();
 | 
					        string ip = ConfigurationManager.AppSettings["modbusIp"].ToString();
 | 
				
			||||||
        int port =Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
 | 
					        int port = Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
 | 
				
			||||||
        IEventAggregator _eventAggregator;
 | 
					        IEventAggregator _eventAggregator;
 | 
				
			||||||
        public DateTime dateTime { get; set; } = DateTime.Now;
 | 
					        public DateTime dateTime { get; set; } = DateTime.Now;
 | 
				
			||||||
        FingerprintUtil _fingerprintUtil;
 | 
					        FingerprintUtil _fingerprintUtil;
 | 
				
			||||||
| 
						 | 
					@ -70,16 +71,17 @@ namespace DM_Weight.Port
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                //}    
 | 
					                //}    
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            acceptor.SessionClosed += (o, e) =>
 | 
					            acceptor.SessionClosed += new EventHandler<IoSessionEventArgs>(SessionCloseMethod);
 | 
				
			||||||
            {
 | 
					            //acceptor.SessionClosed += (o, e) =>
 | 
				
			||||||
                logger.Info("SessionClosed");
 | 
					            //{
 | 
				
			||||||
                //Task.Delay(50).Wait();
 | 
					            //    logger.Info("SessionClosed");
 | 
				
			||||||
                SocketConnect();
 | 
					            //    //Task.Delay(50).Wait();
 | 
				
			||||||
                Task.Factory.StartNew(() =>
 | 
					            //    SocketConnect();
 | 
				
			||||||
                {
 | 
					            //    Task.Factory.StartNew(() =>
 | 
				
			||||||
                    _fingerprintUtil.FingerDisconnect();
 | 
					            //    {
 | 
				
			||||||
                });
 | 
					            //        _fingerprintUtil.FingerDisconnect();
 | 
				
			||||||
            };
 | 
					            //    });
 | 
				
			||||||
 | 
					            //};
 | 
				
			||||||
            this.SocketConnect();
 | 
					            this.SocketConnect();
 | 
				
			||||||
            _fingerprintUtil = fingerprintUtil;
 | 
					            _fingerprintUtil = fingerprintUtil;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -94,7 +96,7 @@ namespace DM_Weight.Port
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (i <= 3)
 | 
					                if (i <= 3)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    Thread.Sleep(1950);
 | 
					                    Thread.Sleep(50);
 | 
				
			||||||
                    //没连上会再连两次
 | 
					                    //没连上会再连两次
 | 
				
			||||||
                    SocketConnect();
 | 
					                    SocketConnect();
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
| 
						 | 
					@ -115,7 +117,27 @@ namespace DM_Weight.Port
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            ConnectedStatus = iConnectFuture.Connected;
 | 
					            ConnectedStatus = iConnectFuture.Connected;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        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}");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        public void SendMessage(MyBaseMessage baseMessage)
 | 
					        public void SendMessage(MyBaseMessage baseMessage)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (!ConnectedStatus)
 | 
					            if (!ConnectedStatus)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -374,6 +374,7 @@ _exitCommand ??= new DelegateCommand(Exit);
 | 
				
			||||||
        void Exit()
 | 
					        void Exit()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            //_chkFunction.HIKLoginOut();
 | 
					            //_chkFunction.HIKLoginOut();
 | 
				
			||||||
 | 
					            _socketHelper.SocketDisConnect();
 | 
				
			||||||
            Process.GetCurrentProcess().Kill();
 | 
					            Process.GetCurrentProcess().Kill();
 | 
				
			||||||
            Environment.Exit(0);
 | 
					            Environment.Exit(0);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue