|
|
|
using SunlightCentralizedControlManagement_SCCM_.UserClass;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Data;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using TouchSocket.Core;
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
using SunlightCentralizedControlManagement_SCCM_.ViewModel;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using ScottPlot.Colormaps;
|
|
|
|
using static System.Windows.Forms.AxHost;
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
|
|
|
|
using SunlightCentralizedControlManagement_SCCM_.View;
|
|
|
|
using TcpClient = TouchSocket.Sockets.TcpClient;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using System.Xml.Linq;
|
|
|
|
using ScottPlot;
|
|
|
|
using System.Collections;
|
|
|
|
using DyeingComputer.UserClass;
|
|
|
|
|
|
|
|
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
|
|
|
|
{/// <summary>
|
|
|
|
/// 异步TCP客户端
|
|
|
|
/// </summary>
|
|
|
|
public class AsyncTcpClient
|
|
|
|
{
|
|
|
|
private static SQLiteHelper SQLiteHelpers = null; //定义数据库
|
|
|
|
private static readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\SCCM.db"; //数据库路径
|
|
|
|
private static readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db"; //数据库路径
|
|
|
|
public static async Task TcpClient(TcpClient tcpClient, string ip, string port)
|
|
|
|
{
|
|
|
|
//TcpClient tcpClient = new TcpClient();
|
|
|
|
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
|
|
|
tcpClient.Connected = (client, e) =>
|
|
|
|
{
|
|
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
|
|
|
|
drEmployee.BeginEdit();
|
|
|
|
drEmployee["State"] = "801";
|
|
|
|
drEmployee.EndEdit();
|
|
|
|
drEmployee.AcceptChanges();
|
|
|
|
drEmployee.ClearErrors();
|
|
|
|
|
|
|
|
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_OK");
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};//成功连接到服务器
|
|
|
|
tcpClient.Closing = (client, e) =>
|
|
|
|
{
|
|
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
|
|
|
|
drEmployee.BeginEdit();
|
|
|
|
drEmployee["State"] = "899";
|
|
|
|
drEmployee["WorkOrder"] = "------";
|
|
|
|
drEmployee["Dyelot"] = "";
|
|
|
|
drEmployee["Temperature"] = "---.-";
|
|
|
|
drEmployee["WaterLevel"] = "----";
|
|
|
|
drEmployee["Process"] = "";
|
|
|
|
drEmployee["Step"] = "";
|
|
|
|
drEmployee["Message"] = "";
|
|
|
|
drEmployee.EndEdit();
|
|
|
|
drEmployee.AcceptChanges();
|
|
|
|
drEmployee.ClearErrors();
|
|
|
|
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_STOP");
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};//即将从服务器断开连接。此处仅主动断开才有效。
|
|
|
|
tcpClient.Closed = (client, e) =>
|
|
|
|
{
|
|
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
|
|
|
|
drEmployee.BeginEdit();
|
|
|
|
drEmployee["State"] = "899";
|
|
|
|
drEmployee["WorkOrder"] = "------";
|
|
|
|
drEmployee["Dyelot"] = "";
|
|
|
|
drEmployee["Temperature"] = "---.-";
|
|
|
|
drEmployee["WaterLevel"] = "----";
|
|
|
|
drEmployee["Process"] = "";
|
|
|
|
drEmployee["Step"] = "";
|
|
|
|
drEmployee["Message"] = "";
|
|
|
|
drEmployee.EndEdit();
|
|
|
|
drEmployee.AcceptChanges();
|
|
|
|
drEmployee.ClearErrors();
|
|
|
|
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_INTERRUPT");
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};//从服务器断开连接,当连接不成功时不会触发。
|
|
|
|
tcpClient.Received = (client, e) =>
|
|
|
|
{
|
|
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
|
|
string SYSAPI = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(0, 5);
|
|
|
|
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
|
|
|
if (SYSAPI == "SC800")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
|
|
|
|
Chart_new = JsonConvert.DeserializeObject<Dictionary<string, object>>(DAT);//反序列化
|
|
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
|
|
|
|
drEmployee.BeginEdit();
|
|
|
|
drEmployee["SYSKEY"] = Chart_new.GetValue("SYSKEY").ToString();
|
|
|
|
drEmployee["NAME"] = Chart_new.GetValue("MACHINE").ToString();
|
|
|
|
drEmployee["Groups"] = Chart_new.GetValue("GROUP").ToString();
|
|
|
|
drEmployee["State"] = "802";
|
|
|
|
drEmployee.EndEdit();
|
|
|
|
drEmployee.AcceptChanges();
|
|
|
|
drEmployee.ClearErrors();
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC810")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
|
|
|
|
"WorkOrder ='" + DAT + "'", null);
|
|
|
|
SQLiteHelpers.Close();
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC811")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
|
|
|
|
"WorkOrder ='" + DAT + "'", null);
|
|
|
|
SQLiteHelpers.Close();
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC830")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
Dictionary<string, object> _new = new Dictionary<string, object>();//缓存函数
|
|
|
|
_new = JsonConvert.DeserializeObject<Dictionary<string, object>>(DAT);//反序列化
|
|
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First();
|
|
|
|
drEmployee.BeginEdit();
|
|
|
|
drEmployee["Message"] = _new.GetValue("Status");
|
|
|
|
drEmployee["WorkOrder"] = _new.GetValue("WorkNumder");
|
|
|
|
drEmployee["Temperature"] = _new.GetValue("MTT");
|
|
|
|
drEmployee["WaterLevel"] = _new.GetValue("MTL");
|
|
|
|
drEmployee["Process"] = _new.GetValue("Process");
|
|
|
|
drEmployee["Step"] = _new.GetValue("Step");
|
|
|
|
drEmployee.EndEdit();
|
|
|
|
drEmployee.AcceptChanges();
|
|
|
|
drEmployee.ClearErrors();
|
|
|
|
|
|
|
|
if (_new.GetValue("Status").ToString() != "----------")
|
|
|
|
{
|
|
|
|
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
|
|
|
|
Chart_new.Add("WorkOrder", _new.GetValue("WorkNumder"));
|
|
|
|
Chart_new.Add("Machine", _new.GetValue("Machine"));
|
|
|
|
Chart_new.Add("Time", _new.GetValue("Time"));
|
|
|
|
Chart_new.Add("MST", _new.GetValue("MST"));
|
|
|
|
Chart_new.Add("MTT", _new.GetValue("MTT"));
|
|
|
|
Chart_new.Add("MTL", _new.GetValue("MTL"));
|
|
|
|
Chart_new.Add("MTH", _new.GetValue("MTH"));
|
|
|
|
Chart_new.Add("MUT", _new.GetValue("MUT"));
|
|
|
|
Chart_new.Add("STTA", _new.GetValue("STTA"));
|
|
|
|
Chart_new.Add("STLA", _new.GetValue("STLA"));
|
|
|
|
Chart_new.Add("STTB", _new.GetValue("STTB"));
|
|
|
|
Chart_new.Add("STLB", _new.GetValue("STLB"));
|
|
|
|
Chart_new.Add("STTC", _new.GetValue("STTC"));
|
|
|
|
Chart_new.Add("STLC", _new.GetValue("STLC"));
|
|
|
|
|
|
|
|
SQLiteHelpers = new SQLiteHelper(ChartAdress); //数据库连接路径
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
SQLiteHelpers.InsertData("Chart", Chart_new);// 执行插入
|
|
|
|
SQLiteHelpers.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC831")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_TP = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC832")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.MachineLOG = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
|
|
|
|
}//当前细节信息
|
|
|
|
else if (SYSAPI == "SC833")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.MachineLOG = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
|
|
|
|
}//当前领料单信息
|
|
|
|
else if (SYSAPI == "SC851")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_d = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}//数字开关表
|
|
|
|
else if (SYSAPI == "SC852")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_a = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}//寄存器表
|
|
|
|
else if (SYSAPI == "SC853")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_m = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}//缓存表
|
|
|
|
else if (SYSAPI == "SC854")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_ParameterSet = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}//程序设置表
|
|
|
|
else if (SYSAPI == "SC855")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
MainWindowViewModel.dt_SysSet = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
}//系统设置表
|
|
|
|
else if (SYSAPI == "SC910")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 109 } },
|
|
|
|
"WorkOrder ='" + DAT + "'", null);
|
|
|
|
SQLiteHelpers.Close();
|
|
|
|
}//发布失败
|
|
|
|
else if (SYSAPI == "SC911")
|
|
|
|
{
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
|
|
|
|
SQLiteHelpers.Open(); //打开数据库
|
|
|
|
SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 119 } },
|
|
|
|
"WorkOrder ='" + DAT + "'", null);
|
|
|
|
SQLiteHelpers.Close();
|
|
|
|
}//细节错误
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};
|
|
|
|
|
|
|
|
//载入配置
|
|
|
|
await tcpClient.SetupAsync(new TouchSocketConfig()
|
|
|
|
.SetMaxBufferSize(1024*1024)
|
|
|
|
.SetMinBufferSize(1024*64)
|
|
|
|
.SetRemoteIPHost(ip + ":" + port)
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
{
|
|
|
|
a.UseCheckClear()
|
|
|
|
.SetCheckClearType(CheckClearType.All)
|
|
|
|
.SetTick(TimeSpan.FromSeconds(30))
|
|
|
|
.SetOnClose((c, t) =>
|
|
|
|
{
|
|
|
|
c.TryShutdown();
|
|
|
|
});
|
|
|
|
a.UseTcpReconnection();//触发型重连
|
|
|
|
})
|
|
|
|
.ConfigureContainer(a =>
|
|
|
|
{
|
|
|
|
// a.AddConsoleLogger();//添加一个日志注入
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Result result = await
|
|
|
|
_ = tcpClient.TryConnectAsync();
|
|
|
|
// return result.IsSuccess;
|
|
|
|
/* try
|
|
|
|
{
|
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|