using ConnectionUtil;
|
using LT.Utils.Json;
|
using Newtonsoft.Json;
|
using SuperSocketServer.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading;
|
|
namespace OPCConnection
|
{
|
public class OPCClient : SCADAApi
|
{
|
private Socket ClientSocket = null;
|
|
/// <summary>
|
/// 连接超时事件ms
|
/// </summary>
|
public int _ConnectTimeout = 20000;
|
private int Size = 0x989680;
|
private Dictionary<int, string> paraMPairs = new Dictionary<int, string>();
|
|
|
/// <summary>
|
/// IP连接Scada服务器
|
/// </summary>
|
/// <param name="IP">MES服务器IP</param>
|
/// <param name="Port"> MES服务器Port口</param>
|
/// <param name="LocalIP">本地连接IP</param>
|
/// <param name="LocalPort">本地连接端口</param>
|
/// <returns></returns>
|
public bool Connect(string IP, int Port, string LocalIP, int LocalPort)
|
{
|
try
|
{
|
this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
IPAddress address = IPAddress.Parse(IP);
|
IPEndPoint point = new IPEndPoint(IPAddress.Parse(LocalIP), LocalPort);
|
IPEndPoint remoteEP = new IPEndPoint(address, Port);
|
this.ClientSocket.Connect(remoteEP);
|
return true;
|
}
|
catch (Exception)
|
{
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 连接Scada管理服务器
|
/// </summary>
|
/// <param name="IP"> Scada服务器IP</param>
|
/// <param name="Port">ScadPort口</param>
|
/// <param name="LocalPort">本地连接端口</param>
|
/// <param name="errMsg"></param>
|
/// <param name="socket"></param>
|
/// <returns></returns>
|
public bool Connect2(string IP, int Port, int LocalPort, out string errMsg, Socket socket = null)
|
{
|
try
|
{
|
errMsg = string.Empty;
|
if (!this.TcpPing(IP, Port, out errMsg))
|
{
|
throw new Exception(errMsg);
|
}
|
if (socket != null)
|
{
|
this.ClientSocket = socket;
|
}
|
else
|
{
|
this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
}
|
IPAddress address = IPAddress.Parse(IP);
|
this.ClientSocket.Connect(address, Port);
|
if (!this.DoLogin("admin", "LT123456", out string str))
|
{
|
throw new Exception(str);
|
}
|
return true;
|
}
|
catch (Exception exception)
|
{
|
errMsg = "连接SCADA服务失败:" + exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 连接Scada管理服务器
|
/// </summary>
|
/// <param name="IP">ScadaManagerIP</param>
|
/// <param name="Port">ScadaManagerPort口</param>
|
/// <param name="LocalPort">本地连接端口</param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool ConnectManager(string IP, int Port, int LocalPort, out string errMsg)
|
{
|
try
|
{
|
errMsg = string.Empty;
|
if (!this.TcpPing(IP, Port, out errMsg))
|
{
|
throw new Exception(errMsg);
|
}
|
this.ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
IPAddress address = IPAddress.Parse(IP);
|
this.ClientSocket.Connect(address, Port);
|
return true;
|
}
|
catch (Exception exception)
|
{
|
errMsg = "连接SCADA服务失败:" + exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 断开服务器连接
|
/// </summary>
|
public void DisConnect()
|
{
|
try
|
{
|
if (this.ClientSocket != null)
|
{
|
//this.ClientSocket.Shutdown(SocketShutdown.Both);
|
this.ClientSocket.Close();
|
}
|
}
|
catch (Exception exception)
|
{
|
throw new Exception("断开SCADA服务器失败:" + exception.Message);
|
}
|
}
|
|
/// <summary>
|
/// 释放资源
|
/// </summary>
|
public void Dispose()
|
{
|
if (this.ClientSocket != null)
|
{
|
this.ClientSocket.Dispose();
|
}
|
}
|
|
/// <summary>
|
/// 批量写增加参数
|
/// </summary>
|
/// <param name="id"></param>
|
/// <param name="par"></param>
|
/// <returns></returns>
|
public bool DoAddParWriteMultiple(int id, string par)
|
{
|
if (this.paraMPairs.ContainsKey(id))
|
{
|
return false;
|
}
|
this.paraMPairs.Add(id, par);
|
return true;
|
}
|
|
/// <summary>
|
/// 换型
|
/// </summary>
|
/// <param name="_ProductLineName"></param>
|
/// <param name="_ProductName"></param>
|
/// <param name="errMsg"></param>
|
/// <param name="_WorkStationName"></param>
|
/// <returns></returns>
|
public bool DoDoChangeModel(string _ProductLineName, string _ProductName, out string errMsg, string _WorkStationName = null)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 7,
|
ProductLineName = _ProductLineName,
|
ProductName = _ProductName,
|
WorkStationName = _WorkStationName
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 批量读值
|
/// </summary>
|
/// <param name="_Id"></param>
|
/// <returns></returns>
|
public string[,] DoFctReadMultiple(int[] _Id)
|
{
|
string[,] strArray2;
|
try
|
{
|
var type = new
|
{
|
command = 2,
|
comefrom = 1,
|
paraM = _Id
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
if (this.ResquestResult(resquestStr, out Result result, out _))
|
{
|
Dictionary<string, string> dictionary = JsonUtil.ToObject<Dictionary<string, string>>(result.result.ToString());
|
string[,] strArray = new string[dictionary.Count, 2];
|
int num = 0;
|
foreach (KeyValuePair<string, string> pair in dictionary)
|
{
|
strArray[num, 0] = pair.Key;
|
strArray[num, 1] = pair.Value;
|
num++;
|
}
|
return strArray;
|
}
|
strArray2 = null;
|
}
|
catch (Exception exception)
|
{
|
throw new Exception(exception.Message);
|
}
|
return strArray2;
|
}
|
|
/// <summary>
|
/// 单一读值
|
/// </summary>
|
/// <param name="_name"></param>
|
/// <param name="result"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool DoFctReadSingle(string _name, out string result, out string errMsg)
|
{
|
result = string.Empty;
|
try
|
{
|
var type = new
|
{
|
command = 0x31,
|
id = _name,
|
comefrom = 1
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
if (this.ResquestResult(resquestStr, out Result result2, out errMsg))
|
{
|
result = result2.result?.ToString();
|
return true;
|
}
|
return false;
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
finally
|
{
|
GC.Collect();
|
}
|
}
|
|
/// <summary>
|
/// 单一写值
|
/// </summary>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool DoFctWriteMultiple(out string errMsg)
|
{
|
bool flag2;
|
errMsg = string.Empty;
|
try
|
{
|
if (this.ToFctWriteMultiple(this.paraMPairs, out errMsg))
|
{
|
return true;
|
}
|
flag2 = false;
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
flag2 = false;
|
}
|
finally
|
{
|
this.InitParWriteMultiple();
|
}
|
return flag2;
|
}
|
|
/// <summary>
|
/// 写单一值
|
/// </summary>
|
/// <param name="_name"></param>
|
/// <param name="_value"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool DoFctWriteSingle(string _name, string _value, out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 5,
|
id = _name,
|
value = _value
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// mqtt发布消息
|
/// </summary>
|
/// <param name="topic">主题</param>
|
/// <param name="_value">值</param>
|
/// <param name="errMsg">错误信息></param>
|
/// <returns></returns>
|
public bool DoMqttPublish(string topic, string _value, out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 12,
|
Tag = topic,
|
value = _value
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="_user"></param>
|
/// <param name="_password"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool DoLogin(string _user, string _password, out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 11,
|
user = _user,
|
password = _password
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 重启DataServer服务器
|
/// </summary>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool DoReSETDataServer(out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 10
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 获取动态IP
|
/// </summary>
|
/// <returns></returns>
|
public string getLocalIP()
|
{
|
string str2 = null;
|
IPHostEntry hostByName = Dns.GetHostByName(Dns.GetHostName());
|
int length = hostByName.AddressList.Length;
|
for (int i = 0; i < length; i++)
|
{
|
IPAddress address = hostByName.AddressList[i];
|
str2 = Convert.ToString(address);
|
if (str2.Substring(0, 5) == "10.10")
|
{
|
return str2;
|
}
|
}
|
return str2;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="_paraMPairs"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public short[,] GetLowerComputerStatus(short[] _paraMPairs, out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 9,
|
paraM = _paraMPairs
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
if (this.ResquestResult(resquestStr, out Result result, out errMsg))
|
{
|
Dictionary<short, short> dictionary = JsonUtil.ToObject<Dictionary<short, short>>(result.result.ToString());
|
short[,] numArray = new short[dictionary.Count, 2];
|
int num = 0;
|
foreach (KeyValuePair<short, short> pair in dictionary)
|
{
|
numArray[num, 0] = pair.Key;
|
numArray[num, 1] = pair.Value;
|
num++;
|
}
|
return numArray;
|
}
|
return null;
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return null;
|
}
|
}
|
|
/// <summary>
|
/// 初始化批量写参数
|
/// </summary>
|
public void InitParWriteMultiple()
|
{
|
this.paraMPairs.Clear();
|
}
|
|
/// <summary>
|
/// 检查与LTSCADA是否断开连接了
|
/// </summary>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
public bool IsConnected(out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 6
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="Pars"></param>
|
/// <returns></returns>
|
private static string ParsToString<T>(T Pars) =>
|
null;
|
|
/// <summary>
|
/// 返回请求信息
|
/// </summary>
|
/// <param name="ResquestStr"></param>
|
/// <param name="JResult"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
private bool ResquestResult(string ResquestStr, out Result JResult, out string errMsg)
|
{
|
errMsg = string.Empty;
|
JResult = null;
|
try
|
{
|
JResult = JsonConvert.DeserializeObject<Result>(ResquestStr);
|
if (!JResult.success)
|
{
|
errMsg = "请求失败:" + JResult.message;
|
return false;
|
}
|
return true;
|
}
|
catch (Exception exception)
|
{
|
errMsg = JResult?.message + " --- " + exception.Message;
|
return false;
|
}
|
}
|
|
|
/// <summary>
|
/// 接收数据超时时间
|
/// </summary>
|
public uint TimeRecOut { set; get; } = 20000;
|
|
/// <summary>
|
/// 线程锁
|
/// </summary>
|
private readonly object objLock = new object();
|
|
/// <summary>
|
/// 发送请求信息
|
/// </summary>
|
/// <param name="Cmd"></param>
|
/// <returns></returns>
|
private string SendSocketMessage(string Cmd)
|
{
|
string str2;
|
try
|
{
|
string str = "";
|
byte[] bytes = Encoding.UTF8.GetBytes(Cmd + "\r\n");
|
lock (objLock)
|
{
|
int available = this.ClientSocket.Available;
|
if (available > 0)
|
{
|
byte[] buffer2 = new byte[available];
|
this.ClientSocket.Receive(buffer2, 0, available, SocketFlags.None);
|
}
|
this.ClientSocket.Send(bytes);
|
bool flag = false;
|
int tickCount = Environment.TickCount;
|
int size = this.ClientSocket.Available;
|
bool flag2 = false;
|
while (((size < this.Size) && !flag) && !(flag2 = str.Contains("\r\n")))
|
{
|
Thread.Sleep(2);
|
size = this.ClientSocket.Available;
|
if ((size > this.Size) || (str.Length > this.Size))
|
{
|
throw new Exception($"接收数据超过设定值{this.Size}BYTE!");
|
}
|
flag = (Environment.TickCount - tickCount) > TimeRecOut;
|
if (flag)
|
{
|
if (size > 0)
|
{
|
byte[] buffer3 = new byte[size];
|
this.ClientSocket.Receive(buffer3, 0, size, SocketFlags.None);
|
}
|
throw new Exception("接收数据超时!");
|
}
|
if (!flag && (size > 0))
|
{
|
byte[] buffer4 = new byte[size];
|
this.ClientSocket.Receive(buffer4, 0, size, SocketFlags.None);
|
str = str + Encoding.UTF8.GetString(buffer4);
|
}
|
}
|
}
|
str2 = str;
|
}
|
catch (Exception exception)
|
{
|
throw new Exception(exception.Message);
|
}
|
return str2;
|
}
|
|
/// <summary>
|
/// TcpPing
|
/// </summary>
|
/// <param name="Host"></param>
|
/// <param name="Port"></param>
|
/// <param name="errMsg"></param>
|
/// <returns></returns>
|
private bool TcpPing(string Host, int Port, out string errMsg)
|
{
|
bool flag = false;
|
errMsg = string.Empty;
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
try
|
{
|
bool flag2 = socket.BeginConnect(Host, Port, null, null).AsyncWaitHandle.WaitOne(this._ConnectTimeout, true);
|
if (!flag2)
|
{
|
errMsg = "errTCPConnectionFailed";
|
}
|
flag = flag2;
|
}
|
catch
|
{
|
errMsg = "errTCPConnectionFailed";
|
flag = false;
|
}
|
socket.Close();
|
return flag;
|
}
|
|
/// <summary>
|
/// 批量写
|
/// </summary>
|
/// <param name="_paraMPairs">参数列表</param>
|
/// <param name="errMsg">错误信息</param>
|
/// <returns>处理结果</returns>
|
public bool ToFctWriteMultiple(Dictionary<int, string> _paraMPairs, out string errMsg)
|
{
|
try
|
{
|
var type = new
|
{
|
command = 15,
|
paraM = _paraMPairs
|
};
|
string resquestStr = this.SendSocketMessage(JsonExtensions.ToJson(type, false, false, false));
|
return this.ResquestResult(resquestStr, out _, out errMsg);
|
}
|
catch (Exception exception)
|
{
|
errMsg = exception.Message;
|
return false;
|
}
|
}
|
|
public bool Connect(object config, out string errMsg)
|
{
|
errMsg = null;
|
return false;
|
}
|
}
|
}
|