| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using System.Net.Sockets;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- namespace YG.Device
 
- {
 
-     public class DeviceSocket : DeviceList
 
-     {
 
-         public override event RunMessage Msg;
 
-         public override event RunState rState;
 
-         private Socket ClientSocket;
 
-         private string ip;
 
-         int port;
 
-         public DeviceSocket(string _ip, int _port)
 
-         {
 
-             ip = _ip;
 
-             port = _port;
 
-         }
 
-         private bool _ison = false;
 
-         public override bool IsOn
 
-         {
 
-             get { return _ison; }
 
-             set
 
-             {
 
-                 if (_ison != value)
 
-                 {
 
-                     if (rState != null)
 
-                     {
 
-                         rState.Invoke(this.DeviceName, value);
 
-                         _ison = value;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public override void DeviceInit()
 
-         {
 
-             this.DeviceName = "Socket";
 
-             base.DeviceInit();
 
-             IsOn = true;
 
-             //IsOn = false;
 
-             try
 
-             {
 
-                 ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
-                 ClientSocket.Connect(ip, port);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 YG.Log.Instance.WriteLogAdd($"53-->{ex.Message}");
 
-                 //IsOn = true;
 
-                 IsOn = false;
 
-             }
 
-             // byte[] bt = Encoding.UTF8.GetBytes("Hello");
 
-             // ClientSocket.Send(bt);
 
-         }
 
-         public override void DeviceState()
 
-         {
 
-             System.Timers.Timer timer = new System.Timers.Timer();
 
-             timer.Elapsed += Timer_Elapsed;
 
-             timer.Interval = 3000;
 
-             timer.Start();
 
-         }
 
-         private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 
-         {
 
-             try
 
-             {
 
-                 // if (ClientSocket != null && ClientSocket.Connected)
 
-                 {
 
-                     //    ClientSocket.Send(Encoding.UTF8.GetBytes("\r"));
 
-                     IsOn = true;
 
-                 }
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 YG.Log.Instance.WriteLogAdd($"85-->{ex.Message}");
 
-                 IsOn = false;
 
-                 //ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
-                 //ClientSocket.Connect("192.168.0.11", 1100);
 
-                 //if (!ClientSocket.Connected)
 
-                 {
 
-                     ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
-                     try
 
-                     {
 
-                         ClientSocket.Connect(ip, port);
 
-                     }
 
-                     catch
 
-                     {
 
-                         Log.Instance.WriteLogAdd($"{ip}-重新连接失败");
 
-                     }
 
-                 }
 
-             }
 
-             finally
 
-             { }
 
-         }
 
-         public void ReceiveSocket()
 
-         {
 
-             byte[] by = new byte[1024];
 
-             while (true)
 
-             {
 
-                 if (ClientSocket != null) /*&& IsOn*/
 
-                 {
 
-                     int leng = 0;
 
-                     try
 
-                     {
 
-                         leng = ClientSocket.Receive(by);
 
-                     }
 
-                     catch (Exception ex)
 
-                     {
 
-                         YG.Log.Instance.WriteLogAdd($"123-->{ex.Message}");
 
-                     }
 
-                     if (Msg != null)
 
-                     {
 
-                         if (leng > 0)
 
-                         {
 
-                             Msg.Invoke("Socket", SendValue, Encoding.UTF8.GetString(by));
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public override void DeviceStart()
 
-         {
 
-             Task.Factory.StartNew(() =>
 
-             {
 
-                 while (true)
 
-                 {
 
-                     byte[] by = new byte[1024];
 
-                     if (ClientSocket != null) /*&& IsOn*/
 
-                     {
 
-                         int leng = 0;
 
-                         try
 
-                         {
 
-                             leng = ClientSocket.Receive(by);
 
-                         }
 
-                         catch (Exception ex)
 
-                         {
 
-                             YG.Log.Instance.WriteLogAdd($"154-->{ex.Message}");
 
-                         }
 
-                         if (Msg != null)
 
-                         {
 
-                             if (leng > 0)
 
-                             {
 
-                                 Msg.Invoke("Socket", SendValue, Encoding.UTF8.GetString(by));
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             });
 
-             base.DeviceStart();
 
-         }
 
-         public override void DeviceSend(object ob)
 
-         {
 
-             try
 
-             {
 
-                 SendValue = ob.ToString();
 
-                 if (ClientSocket.Connected)
 
-                 {
 
-                     ClientSocket.Close();
 
-                     this.DeviceInit();
 
-                 }
 
-                 ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 this.DeviceInit();
 
-                 System.Threading.Thread.Sleep(1000);
 
-                 SendValue = ob.ToString();
 
-                 ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
 
-                 YG.Log.Instance.WriteLogAdd($"179-->{ex.Message}");
 
-             }
 
-             base.DeviceSend(ob);
 
-         }
 
-     }
 
- }
 
 
  |