DeviceSocket.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace YG.Device
  8. {
  9. public class DeviceSocket : DeviceList
  10. {
  11. public override event RunMessage Msg;
  12. public override event RunState rState;
  13. private Socket ClientSocket;
  14. private string ip;
  15. int port;
  16. public DeviceSocket(string _ip, int _port)
  17. {
  18. ip = _ip;
  19. port = _port;
  20. }
  21. private bool _ison = false;
  22. public override bool IsOn
  23. {
  24. get { return _ison; }
  25. set
  26. {
  27. if (_ison != value)
  28. {
  29. if (rState != null)
  30. {
  31. rState.Invoke(this.DeviceName, value);
  32. _ison = value;
  33. }
  34. }
  35. }
  36. }
  37. public override void DeviceInit()
  38. {
  39. this.DeviceName = "Socket";
  40. base.DeviceInit();
  41. IsOn = true;
  42. //IsOn = false;
  43. try
  44. {
  45. ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  46. ClientSocket.Connect(ip, port);
  47. }
  48. catch (Exception ex)
  49. {
  50. YG.Log.Instance.WriteLogAdd($"53-->{ex.Message}");
  51. //IsOn = true;
  52. IsOn = false;
  53. }
  54. // byte[] bt = Encoding.UTF8.GetBytes("Hello");
  55. // ClientSocket.Send(bt);
  56. }
  57. public override void DeviceState()
  58. {
  59. System.Timers.Timer timer = new System.Timers.Timer();
  60. timer.Elapsed += Timer_Elapsed;
  61. timer.Interval = 3000;
  62. timer.Start();
  63. }
  64. private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  65. {
  66. try
  67. {
  68. // if (ClientSocket != null && ClientSocket.Connected)
  69. {
  70. // ClientSocket.Send(Encoding.UTF8.GetBytes("\r"));
  71. IsOn = true;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. YG.Log.Instance.WriteLogAdd($"85-->{ex.Message}");
  77. IsOn = false;
  78. //ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  79. //ClientSocket.Connect("192.168.0.11", 1100);
  80. //if (!ClientSocket.Connected)
  81. {
  82. ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  83. try
  84. {
  85. ClientSocket.Connect(ip, port);
  86. }
  87. catch
  88. {
  89. Log.Instance.WriteLogAdd($"{ip}-重新连接失败");
  90. }
  91. }
  92. }
  93. finally
  94. { }
  95. }
  96. public void ReceiveSocket()
  97. {
  98. byte[] by = new byte[1024];
  99. while (true)
  100. {
  101. if (ClientSocket != null) /*&& IsOn*/
  102. {
  103. int leng = 0;
  104. try
  105. {
  106. leng = ClientSocket.Receive(by);
  107. }
  108. catch (Exception ex)
  109. {
  110. YG.Log.Instance.WriteLogAdd($"123-->{ex.Message}");
  111. }
  112. if (Msg != null)
  113. {
  114. if (leng > 0)
  115. {
  116. Msg.Invoke("Socket", SendValue, Encoding.UTF8.GetString(by));
  117. }
  118. }
  119. }
  120. }
  121. }
  122. public override void DeviceStart()
  123. {
  124. Task.Factory.StartNew(() =>
  125. {
  126. while (true)
  127. {
  128. byte[] by = new byte[1024];
  129. if (ClientSocket != null) /*&& IsOn*/
  130. {
  131. int leng = 0;
  132. try
  133. {
  134. leng = ClientSocket.Receive(by);
  135. }
  136. catch (Exception ex)
  137. {
  138. YG.Log.Instance.WriteLogAdd($"154-->{ex.Message}");
  139. }
  140. if (Msg != null)
  141. {
  142. if (leng > 0)
  143. {
  144. Msg.Invoke("Socket", SendValue, Encoding.UTF8.GetString(by));
  145. }
  146. }
  147. }
  148. }
  149. });
  150. base.DeviceStart();
  151. }
  152. public override void DeviceSend(object ob)
  153. {
  154. try
  155. {
  156. SendValue = ob.ToString();
  157. if (ClientSocket.Connected)
  158. {
  159. ClientSocket.Close();
  160. this.DeviceInit();
  161. }
  162. ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
  163. }
  164. catch (Exception ex)
  165. {
  166. this.DeviceInit();
  167. System.Threading.Thread.Sleep(1000);
  168. SendValue = ob.ToString();
  169. ClientSocket.Send(Encoding.ASCII.GetBytes(ob.ToString()));
  170. YG.Log.Instance.WriteLogAdd($"179-->{ex.Message}");
  171. }
  172. base.DeviceSend(ob);
  173. }
  174. }
  175. }