EverLightStopMac.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Cksoft.Unity;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. namespace WebStatusToolkit
  11. {
  12. public class EverLightStopMac
  13. {
  14. private Socket currSocket = null;
  15. public bool StopMac(string macCode,string strIp)
  16. {
  17. try
  18. {
  19. int port= Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":port"]);
  20. int x = Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":x"]);
  21. int y = Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":y"]);
  22. bool init = InitSocket(macCode, strIp, port);
  23. if(!init)
  24. {
  25. return false;
  26. }
  27. byte[] datas = GetSendDatas(1, x, y);
  28. if (datas == null)
  29. {
  30. return false;
  31. }
  32. bool sendResult = Send(macCode, datas);
  33. Thread.Sleep(2000);
  34. if(!sendResult)
  35. {
  36. return false;
  37. }
  38. // result = ReceiveMsg(macCode);
  39. return true;
  40. }
  41. catch(Exception ex)
  42. {
  43. string logerrorinfo = "";
  44. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令异常:{ex.Message}", ref logerrorinfo);
  45. return false;
  46. }
  47. }
  48. public bool InitSocket(string macCode,string strIp, int port)
  49. {
  50. try
  51. {
  52. if (currSocket != null)
  53. {
  54. currSocket.Shutdown(SocketShutdown.Both);
  55. currSocket.Close();
  56. currSocket.Dispose();
  57. currSocket = null;
  58. }
  59. currSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  60. IPAddress ip = IPAddress.Parse(strIp);
  61. //连接到目标IP的哪个应用(端口号)
  62. IPEndPoint point = new IPEndPoint(ip, port);
  63. currSocket.Connect(point);
  64. currSocket.ReceiveTimeout = 1000 * 60;
  65. currSocket.SendTimeout = 1000 * 60;
  66. string logerrorinfo = "";
  67. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"与机台建立连接成功,机台ip({currSocket.RemoteEndPoint.ToString()}),客户端ip({currSocket.LocalEndPoint.ToString()})", ref logerrorinfo);
  68. return true;
  69. }
  70. catch(Exception ex)
  71. {
  72. string logerrorinfo = "";
  73. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"与机台建立连接异常:{ex.Message}", ref logerrorinfo);
  74. return false;
  75. }
  76. }
  77. int ReceiveMsg(string macCode)
  78. {
  79. //while (true)
  80. //{
  81. int result = 0;
  82. try
  83. {
  84. byte[] buffer = new byte[12];
  85. int n = currSocket.Receive(buffer);
  86. byte[] statusDatas = new byte[4];
  87. Array.Copy(buffer, 4, statusDatas, 0, 4);
  88. int mode = BitConverter.ToInt32(statusDatas, 0);
  89. statusDatas = new byte[4];
  90. Array.Copy(buffer, 8, statusDatas, 0, 4);
  91. result = BitConverter.ToInt32(statusDatas, 0);
  92. currSocket.Shutdown(SocketShutdown.Both);
  93. currSocket.Close();
  94. currSocket.Dispose();
  95. currSocket = null;
  96. string logerrorinfo = "";
  97. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"接收到机台停机结果:{result}", ref logerrorinfo);
  98. }
  99. catch (Exception ex)
  100. {
  101. string logerrorinfo = "";
  102. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"接收机台停机结果异常:{ex.Message}", ref logerrorinfo);
  103. }
  104. return result;
  105. //}
  106. }
  107. public bool Send(string macCode,byte[] buffer)
  108. {
  109. if (currSocket != null)
  110. {
  111. try
  112. {
  113. currSocket.Send(buffer);
  114. currSocket.Shutdown(SocketShutdown.Both);
  115. currSocket.Close();
  116. currSocket.Dispose();
  117. currSocket = null;
  118. string logerrorinfo = "";
  119. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令给机台成功", ref logerrorinfo);
  120. return true;
  121. }
  122. catch (Exception ex)
  123. {
  124. string logerrorinfo = "";
  125. WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令给机台异常:{ex.Message}", ref logerrorinfo);
  126. return false;
  127. }
  128. }
  129. return false;
  130. }
  131. private byte[] GetSendDatas(Int32 mode, Int32 x, Int32 y)
  132. {
  133. byte[] alldatas = BitConverter.GetBytes(16);
  134. byte[] forAll = (alldatas.Concat(BitConverter.GetBytes(mode)).Concat(BitConverter.GetBytes(x)).Concat(BitConverter.GetBytes(y))).ToArray();
  135. return forAll;
  136. }
  137. }
  138. }