You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

449 lines
22 KiB

using DyeingComputer.UserClass;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ScottPlot;
using ScottPlot.Colormaps;
using SkiaSharp;
using SunlightCentralizedControlManagement_SCCM_.View;
using SunlightCentralizedControlManagement_SCCM_.ViewModel;
using SunlightCentralizedControlManagement_SCCM_.WindowsView;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
using static SunlightCentralizedControlManagement_SCCM_.View.MachinesView;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using TcpClient = TouchSocket.Sockets.TcpClient;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{/// <summary>
/// 异步TCP客户端
/// </summary>
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) =>
{
try
{
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "'").First();
drEmployee.BeginEdit();
drEmployee["State"] = "801";
drEmployee.EndEdit();
drEmployee.AcceptChanges();
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_OK");
}
catch (Exception ex)
{
LogGing.ERRDATA(ex); MainWindowViewModel.ERR_c++;
}
return EasyTask.CompletedTask;
};//成功连接到服务器
tcpClient.Closing = (client, e) =>
{
try
{
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "'").First();
drEmployee.BeginEdit();
drEmployee["State"] = "800";
drEmployee["WorkOrder"] = "------";
drEmployee["Dyelot"] = "";
drEmployee["Temperature"] = 0.0;
drEmployee["WaterLevel"] = "----";
drEmployee["Process"] = "";
drEmployee["Step"] = "";
drEmployee["Message"] = "";
drEmployee["SYSKEY"] = "";
drEmployee["WORK_RUN"] = "-1";
drEmployee["ERR"] = false;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_STOP");
}
catch (Exception ex)
{
LogGing.ERRDATA(ex); MainWindowViewModel.ERR_c++;
}
return EasyTask.CompletedTask;
};//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) =>
{
try
{
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "'").First();
drEmployee.BeginEdit();
drEmployee["State"] = "800";
drEmployee["WorkOrder"] = "------";
drEmployee["Dyelot"] = "";
drEmployee["Temperature"] = 0.0;
drEmployee["WaterLevel"] = "----";
drEmployee["Process"] = "";
drEmployee["Step"] = "";
drEmployee["Message"] = "";
drEmployee["SYSKEY"] = "";
drEmployee["WORK_RUN"] = "-1";
drEmployee["ERR"] = false;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
LogGing.LogGingDATA("[IP='" + client.IP + "' AND port='" + client.Port + "']=Link_INTERRUPT");
}
catch (Exception ex)
{
LogGing.ERRDATA(ex); MainWindowViewModel.ERR_c++;
}
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<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
Chart_new = JsonConvert.DeserializeObject<Dictionary<string, object>>(DAT);//反序列化
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "'").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();
}
catch (Exception ex)
{
LogGing.ERRDATA(ex); MainWindowViewModel.ERR_c++;
}
}
else if (SYSAPI == "SC810")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 111 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex)
{
LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++;
}
}
else if (SYSAPI == "SC811")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 113 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC812")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 101 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC822")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(DAT).Copy();//反序列化
Application.Current.Dispatcher.Invoke(() =>
{
UserWorkOrder userWorkOrder = new UserWorkOrder();
userWorkOrder.WorkOrderTable = dataTable;
userWorkOrder.Show();
});
}
catch (Exception)
{
Application.Current.Dispatcher.Invoke(() =>
{
UserInf userInf = new UserInf();
userInf.Inf_DAT = "没有等待中的工艺";
userInf.Show();
userInf.Activate();
});
// LogGing.LogGingDATA("[ERR='" + ex + "']=Exception");
MainWindowViewModel.ERR_c++;
}
}
else if (SYSAPI == "SC827")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
foreach (DataRow sourceRow in dataTable.Rows)
{
// 创建新行(基于目标表结构)
DataRow newRow = MainWindowViewModel.Dyelot_CALL.NewRow();
// 复制源表中存在的列数据
foreach (DataColumn column in dataTable.Columns)
{
if (MainWindowViewModel.Dyelot_CALL.Columns.Contains(column.ColumnName))
{
newRow[column.ColumnName] = sourceRow[column.ColumnName];
}
}
MainWindowViewModel.Dyelot_CALL.Rows.Add(newRow);
Dictionary<string, object> Product_ = new Dictionary<string, object>();//缓存函数
Product_.Add("State", 202);
Product_.Add("Dyelot", newRow.Field<string>("Dyelot"));
Product_.Add("Step", newRow.Field<int>("Step"));
Product_.Add("ProductCode", newRow.Field<string>("ProductCode"));
Product_.Add("Amount", newRow.Field<object>("Amount"));
Product_.Add("DispenseEndTime", newRow.Field<object>("DispenseEndTime"));
newRow.BeginEdit();
newRow["State"] = 202;
newRow.EndEdit();
MainWindowViewModel.SQLiteHelpers.InsertData("DyelotHistory", MainWindowViewModel.SQLiteHelpers.ToDictionary(newRow));// 执行插入
MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString
{
ID = Convert.ToInt16(MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines,
"ID", "Name='" + newRow.Field<string>("Machine") + "'")),
DAT = "SC828" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines,
"SYSKEY", "Name='" + newRow.Field<string>("Machine") + "'") + Product_.ToJsonString()
});
}
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//获取呼叫领料单
else if (SYSAPI == "SC830")
{
try
{
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 + "'").First();
drEmployee.BeginEdit();
drEmployee["ERR"] = _new.GetValue("ERR");
drEmployee["LOCK"] = _new.GetValue("LOCK");
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["UserButton"] = _new.GetValue("UserButton");
if (_new.GetValue("UserInfoStart").ToString() != "900")
{ drEmployee["UserInfoStart"] = _new.GetValue("UserInfoStart"); }
drEmployee["UserInfo"] = _new.GetValue("UserInfo");
drEmployee["WORK_RUN"] = _new.GetValue("WORK_RUN");
drEmployee["RUN_STEPID"] = _new.GetValue("RUN_STEPID");
drEmployee["CALL"] = _new.GetValue("CALL");
drEmployee.EndEdit();
drEmployee.AcceptChanges();
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", drEmployee["Name"]);
Chart_new.Add("Time", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
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"));
MainWindowViewModel.SQLiteChartAdress.InsertData("Chart", Chart_new);// 执行插入
}
}
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<DataTable>(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
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MonitorView.Product_DAT = JsonConvert.DeserializeObject<DataTable>(DAT).Copy();//反序列化
MonitorView.Product_bool = true;
}
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<DataTable>(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<DataTable>(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<DataTable>(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<DataTable>(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<DataTable>(DAT);//反序列化
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//系统设置表
else if (SYSAPI == "SC859")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MachinesView.LOG_dataTable = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC910")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 109 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//发布失败
else if (SYSAPI == "SC911")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
MainWindowViewModel.SQLiteHelpers.Update("WorkOrder", new Dictionary<string, object> { { "State", 119 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}//细节错误
else if (SYSAPI == "SC921")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
}
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*100)
.SetMinBufferSize(1024*1024)
.SetRemoteIPHost(ip + ":" + port)
.ConfigurePlugins(a =>
{
//使用Polling轮询连接插件
a.UseTcpReconnection()
.UsePolling(TimeSpan.FromSeconds(5));
})
.ConfigureContainer(a =>
{
}
));
await tcpClient.TryConnectAsync();
}
}
}