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; namespace SunlightCentralizedControlManagement_SCCM_.UserClass {/// /// 异步TCP客户端 /// public class AsyncTcpClient { 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(); 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.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); 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.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); 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 Chart_new = new Dictionary();//缓存函数 Chart_new = JsonConvert.DeserializeObject>(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 == "SC830") { DAT = DAT.Substring(DAT.IndexOf("]") + 1); Dictionary Chart_new = new Dictionary();//缓存函数 Chart_new = JsonConvert.DeserializeObject>(DAT);//反序列化 DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First(); drEmployee.BeginEdit(); drEmployee["Message"] = Chart_new.GetValue("Status").ToString(); drEmployee["WorkOrder"] = Chart_new.GetValue("WorkNumder").ToString(); drEmployee["Temperature"] = Chart_new.GetValue("MTT").ToString(); drEmployee.EndEdit(); drEmployee.AcceptChanges(); drEmployee.ClearErrors(); } else if (SYSAPI == "SC831") { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MonitorView.TechnologicalProcess = JsonConvert.DeserializeObject(DAT);//反序列化 } else if (SYSAPI == "SC832") { MonitorView.Sys_log = DAT.Substring(DAT.IndexOf("]") + 1); }//当前细节信息 else if (SYSAPI == "SC851") { DAT = DAT.Substring(DAT.IndexOf("]") + 1); DataTable obj = JsonConvert.DeserializeObject(DAT);//反序列化 // client.SendAsync("SC851" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.dt_d.ToJsonString());//数字开关信息 }//数字开关表 // tcpClient.Logger.Info($"客户端接收到信息:{mes}"); 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(10)) .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; }*/ } } }