123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using Cksoft.Unity;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- namespace WebStatusToolkit
- {
- public class EverLightStopMac
- {
- private Socket currSocket = null;
- public bool StopMac(string macCode,string strIp)
- {
- try
- {
- int port= Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":port"]);
- int x = Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":x"]);
- int y = Convert.ToInt32(AppConfigurtaionServices.Configuration["grid" + ":y"]);
- bool init = InitSocket(macCode, strIp, port);
- if(!init)
- {
- return false;
- }
- byte[] datas = GetSendDatas(1, x, y);
- if (datas == null)
- {
- return false;
- }
- bool sendResult = Send(macCode, datas);
- Thread.Sleep(2000);
- if(!sendResult)
- {
- return false;
- }
- // result = ReceiveMsg(macCode);
- return true;
- }
- catch(Exception ex)
- {
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令异常:{ex.Message}", ref logerrorinfo);
- return false;
- }
- }
- public bool InitSocket(string macCode,string strIp, int port)
- {
- try
- {
- if (currSocket != null)
- {
- currSocket.Shutdown(SocketShutdown.Both);
- currSocket.Close();
- currSocket.Dispose();
- currSocket = null;
- }
- currSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress ip = IPAddress.Parse(strIp);
- //连接到目标IP的哪个应用(端口号)
- IPEndPoint point = new IPEndPoint(ip, port);
- currSocket.Connect(point);
- currSocket.ReceiveTimeout = 1000 * 60;
- currSocket.SendTimeout = 1000 * 60;
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"与机台建立连接成功,机台ip({currSocket.RemoteEndPoint.ToString()}),客户端ip({currSocket.LocalEndPoint.ToString()})", ref logerrorinfo);
- return true;
- }
- catch(Exception ex)
- {
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"与机台建立连接异常:{ex.Message}", ref logerrorinfo);
- return false;
- }
- }
- int ReceiveMsg(string macCode)
- {
- //while (true)
- //{
- int result = 0;
- try
- {
- byte[] buffer = new byte[12];
- int n = currSocket.Receive(buffer);
- byte[] statusDatas = new byte[4];
- Array.Copy(buffer, 4, statusDatas, 0, 4);
- int mode = BitConverter.ToInt32(statusDatas, 0);
- statusDatas = new byte[4];
- Array.Copy(buffer, 8, statusDatas, 0, 4);
- result = BitConverter.ToInt32(statusDatas, 0);
- currSocket.Shutdown(SocketShutdown.Both);
- currSocket.Close();
- currSocket.Dispose();
- currSocket = null;
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"接收到机台停机结果:{result}", ref logerrorinfo);
- }
- catch (Exception ex)
- {
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"接收机台停机结果异常:{ex.Message}", ref logerrorinfo);
- }
- return result;
- //}
- }
- public bool Send(string macCode,byte[] buffer)
- {
- if (currSocket != null)
- {
- try
- {
- currSocket.Send(buffer);
- currSocket.Shutdown(SocketShutdown.Both);
- currSocket.Close();
- currSocket.Dispose();
- currSocket = null;
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令给机台成功", ref logerrorinfo);
- return true;
- }
- catch (Exception ex)
- {
- string logerrorinfo = "";
- WriteLog.WriteLogStr(macCode, "", DateTime.Now, $"发送停机指令给机台异常:{ex.Message}", ref logerrorinfo);
- return false;
- }
- }
- return false;
- }
- private byte[] GetSendDatas(Int32 mode, Int32 x, Int32 y)
- {
- byte[] alldatas = BitConverter.GetBytes(16);
- byte[] forAll = (alldatas.Concat(BitConverter.GetBytes(mode)).Concat(BitConverter.GetBytes(x)).Concat(BitConverter.GetBytes(y))).ToArray();
- return forAll;
- }
- }
- }
|