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 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.Xml.Linq;
using TouchSocket.Core;
using TouchSocket.Sockets;
using TouchSocket.SerialPorts;
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
{///
/// 异步TCP客户端
///
public class AsyncTcpClient
{
public static SQLiteHelper SQLiteChartAdress = null; //定义数据库
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) =>
{
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 Chart_new = new Dictionary();//缓存函数
Chart_new = JsonConvert.DeserializeObject>(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 { { "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 { { "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 { { "State", 101 } },
"WorkOrder ='" + DAT + "'", null);
}
catch (Exception ex) { LogGing.LogGingDATA("[ERR='" + ex + "']=Exception"); MainWindowViewModel.ERR_c++; }
}
else if (SYSAPI == "SC827")
{
try
{
DAT = DAT.Substring(DAT.IndexOf("]") + 1);
DataTable dataTable = JsonConvert.DeserializeObject(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 Product_ = new Dictionary();//缓存函数
Product_.Add("State", 202);
Product_.Add("Dyelot", newRow.Field("Dyelot"));
Product_.Add("Step", newRow.Field("Step"));
Product_.Add("ProductCode", newRow.Field("ProductCode"));
Product_.Add("Amount", newRow.Field