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; using System.Windows; namespace SunlightCentralizedControlManagement_SCCM_.UserClass {/// /// 异步TCP客户端 /// 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") { try { 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(); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } } else if (SYSAPI == "SC810") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 111 } }, "WorkOrder ='" + DAT + "'", null); SQLiteHelpers.Close(); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } } else if (SYSAPI == "SC811") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 101 } }, "WorkOrder ='" + DAT + "'", null); SQLiteHelpers.Close(); }catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } } else if (SYSAPI == "SC830") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); Dictionary _new = new Dictionary();//缓存函数 _new = JsonConvert.DeserializeObject>(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 Chart_new = new Dictionary();//缓存函数 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(); } } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } } else if (SYSAPI == "SC831") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_TP = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } } else if (SYSAPI == "SC832") { try { MainWindowViewModel.MachineLOG = DAT.Substring(DAT.IndexOf("]") + 1); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//当前细节信息 else if (SYSAPI == "SC833") { try { MainWindowViewModel.MachineLOG = DAT.Substring(DAT.IndexOf("]") + 1); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//当前领料单信息 else if (SYSAPI == "SC851") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_d = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//数字开关表 else if (SYSAPI == "SC852") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_a = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//寄存器表 else if (SYSAPI == "SC853") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_m = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//缓存表 else if (SYSAPI == "SC854") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_ParameterSet = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//程序设置表 else if (SYSAPI == "SC855") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MainWindowViewModel.dt_SysSet = JsonConvert.DeserializeObject(DAT);//反序列化 } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//系统设置表 else if (SYSAPI == "SC910") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 109 } }, "WorkOrder ='" + DAT + "'", null); SQLiteHelpers.Close(); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//发布失败 else if (SYSAPI == "SC911") { try { DAT = DAT.Substring(DAT.IndexOf("]") + 1); SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.Update("WorkOrder", new Dictionary { { "State", 119 } }, "WorkOrder ='" + DAT + "'", null); SQLiteHelpers.Close(); } catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; } }//细节错误 else if (SYSAPI == "SC980") { DAT = DAT.Substring(DAT.IndexOf("]") + 1); MessageBox.Show(DAT,"SCCM", MessageBoxButton.OK, MessageBoxImage.Question); } 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; }*/ } } }