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.
392 lines
20 KiB
392 lines
20 KiB
using SunlightAggregationManager.UserWindow;
|
|
using SunlightAggregationManager.ViewModel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using TouchSocket.Core;
|
|
using TouchSocket.Sockets;
|
|
using static SunlightAggregationManager.UserClass.AsyncTcpServer;
|
|
|
|
namespace SunlightAggregationManager.UserClass
|
|
{
|
|
public class AsyncTcpClient
|
|
{
|
|
public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();
|
|
public static async void Configuration(string ip, string Name, string user, string Password)
|
|
{
|
|
await TcpClient(tcpClient, "tcp://"+ip);
|
|
}
|
|
|
|
public static async void TcpTransmit(object Dat)
|
|
{
|
|
//发送字符串数据
|
|
try
|
|
{
|
|
await tcpClient.SendAsync(JsonSerializer.Serialize(Dat));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
}
|
|
}
|
|
|
|
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip)
|
|
{
|
|
|
|
// 自定义心跳首次延迟60秒开始,之后每30秒执行一次
|
|
Timer timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(30));
|
|
|
|
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
|
tcpClient.Connected = (client, e) =>
|
|
{
|
|
//设定显示状态
|
|
System.Data.DataRow targetRow =ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!;
|
|
if (targetRow != null)
|
|
{
|
|
targetRow.BeginEdit();
|
|
targetRow["Status"] = "Connected";
|
|
targetRow.EndEdit();
|
|
}
|
|
//发送本机信息到转发服务器
|
|
Dictionary<string, object> myDict = new Dictionary<string, object>();
|
|
myDict.Add("SERVEREnterprise", MainWindowViewModel.Enterprise);
|
|
myDict.Add("SERVERMachineName", MainWindowViewModel.MachineName);
|
|
myDict.Add("SERVERNAME", MainWindowViewModel.SERVERNAME);
|
|
myDict.Add("SERVERUSER", MainWindowViewModel.SERVERUSER);
|
|
myDict.Add("SERRVERPASWORD", MainWindowViewModel.SERRVERPASWORD);
|
|
myDict.Add("MachineVer", MainWindowViewModel.MachineVer);
|
|
TcpTransmit(myDict);
|
|
|
|
Console.WriteLine("Sunlight_Server_Connected");
|
|
return EasyTask.CompletedTask;
|
|
};//成功连接到服务器
|
|
tcpClient.Closing = (client, e) => {
|
|
System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!;
|
|
if (targetRow != null)
|
|
{
|
|
targetRow.BeginEdit();
|
|
targetRow["Status"] = "Disconnect";
|
|
targetRow.EndEdit();
|
|
}
|
|
Console.WriteLine("Sunlight_Server_Disconnect");
|
|
return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
|
|
tcpClient.Closed = (client, e) => {
|
|
System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!;
|
|
if (targetRow != null)
|
|
{
|
|
targetRow.BeginEdit();
|
|
targetRow["Status"] = "Disconnect";
|
|
targetRow.EndEdit();
|
|
}
|
|
Console.WriteLine("Sunlight_Server_Disconnect");
|
|
return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
|
|
#region Tcp客户端使用Received异步委托接收数据
|
|
tcpClient.Received = (client, e) =>
|
|
{
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
var mes = e.Memory.Span.ToString(Encoding.UTF8);
|
|
//信息处理暂停心跳定时
|
|
timer.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
try
|
|
{
|
|
Person? deserializedPerson = JsonSerializer.Deserialize<Person>(mes);
|
|
string password= AES.AesDecrypt(deserializedPerson?.Terminal!, deserializedPerson?.Password!);
|
|
|
|
var _user = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "State", "User='" +
|
|
deserializedPerson?.User + "' and Password ='" + password + "'");
|
|
if (_user != null)
|
|
{
|
|
string _dat = _user?.ToString() ?? "0";
|
|
if (_dat != "99")
|
|
{
|
|
//行为
|
|
Dictionary<string, object> myDict = new Dictionary<string, object>();
|
|
//myDict.Add("LinkID", client.Id);
|
|
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP);
|
|
myDict.Add("LastActionTime", DateTime.Now);
|
|
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown");
|
|
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict);
|
|
|
|
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]");
|
|
CommandC(deserializedPerson?.Dat ?? "NOT", deserializedPerson!);
|
|
}
|
|
else
|
|
{//停用账号关闭连接
|
|
client.SendAsync("Account Deactivated");
|
|
}
|
|
}
|
|
else
|
|
{ //无效账号关闭连接
|
|
client.SendAsync("Invalid Account");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{//错误
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
client.SendAsync("Target instruction parsing error manager will close connection");
|
|
}
|
|
finally
|
|
{//继续心跳定时
|
|
timer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
|
|
}
|
|
|
|
return EasyTask.CompletedTask;
|
|
};
|
|
#endregion
|
|
|
|
try
|
|
{
|
|
await tcpClient.SetupAsync(new TouchSocketConfig()
|
|
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
|
.SetRemoteIPHost(ip)
|
|
.ConfigurePlugins(a =>
|
|
{
|
|
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
|
|
{
|
|
options.PollingInterval = TimeSpan.FromSeconds(5);
|
|
});
|
|
|
|
})
|
|
.ConfigureContainer(a =>
|
|
{
|
|
|
|
}));
|
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGing.ERRDATA(ex);
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
}
|
|
}
|
|
private static void TimerCallback(object? state)
|
|
{
|
|
try
|
|
{
|
|
if (tcpClient.Online)
|
|
{
|
|
tcpClient.SendAsync("SUNLIGHT");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
LogGing.ERRDATA(ex);
|
|
}
|
|
}
|
|
|
|
public static async void CommandC(string dat, Person person)//
|
|
{
|
|
try
|
|
{
|
|
string Command = "", ret = "";
|
|
|
|
Dictionary<string, object> data_ = new Dictionary<string, object>();
|
|
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>( AES.AesDecrypt(person.Terminal, dat))!;
|
|
if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!;
|
|
if (Command == "Log")//回复请求的信息
|
|
{
|
|
DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" +
|
|
person?.User + "'");
|
|
|
|
data_.Add("Enterprise", MainWindowViewModel.Enterprise);
|
|
data_.Add("Department", _user?.Field<object>("Department") ?? "");
|
|
data_.Add("Groups", _user?.Field<object>("Groups") ?? "");
|
|
data_.Add("UserId", _user?.Field<object>("UserID") ?? "");
|
|
data_.Add("UserName", _user?.Field<object>("Name") ?? "");
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "SysData")//回复请求的信息
|
|
{
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "ChemicalStatistics")//回复请求的信息
|
|
{
|
|
data_.Add("ChemicalStatistics", Newtonsoft.Json.JsonConvert.SerializeObject(MainWindowViewModel.ChemicalStatistics));
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "Info")//回复info请求的信息
|
|
{
|
|
var t = ToList(MainWindowViewModel._machines).ToJsonString();
|
|
//格式化返回信息
|
|
data_.Add("Info", t);
|
|
data_.Add("DyeMeasurementNumber", MainWindowViewModel.DyeMeasurementNumber.ToString());
|
|
data_.Add("ChemicalNumber", MainWindowViewModel.ChemicalNumber.ToString());
|
|
data_.Add("PowderNumber", MainWindowViewModel.PowderNumber.ToString());
|
|
data_.Add("DyeDissolveNumber", MainWindowViewModel.DyeDissolveNumber.ToString());
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "Query")//回复Query请求的信息
|
|
{
|
|
string s_ = deserialized.GetValue("StartTime").ToString()!;
|
|
string e_ = deserialized.GetValue("EndTime").ToString()!;
|
|
string f_ = deserialized.GetValue("Field").ToString()!;
|
|
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[Dyelots] " + "WHERE CreationTime>'" + s_ + "'and CreationTime<'" + e_ + "'");
|
|
//格式化返回信息
|
|
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "QueryDyelot")//回复QueryDyelot请求的信息
|
|
{
|
|
string d_ = deserialized.GetValue("DyelotName").ToString()!;
|
|
string f_ = deserialized.GetValue("Field").ToString()!;
|
|
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[Dyelots] " + "WHERE Dyelot LIKE '" + d_ + "%'");
|
|
//格式化返回信息
|
|
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "QueryDetail")//回复QueryDetail请求的信息
|
|
{
|
|
string d_ = deserialized.GetValue("Dyelot").ToString()!;
|
|
string r_ = deserialized.GetValue("ReDye").ToString()!;
|
|
string f_ = deserialized.GetValue("Field").ToString()!;
|
|
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[DyelotsBulkedRecipe] " + "WHERE Dyelot='" + d_ + "'and ReDye='" + r_ + "'");
|
|
//格式化返回信息
|
|
data_.Add("QueryDetail", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "Notification")//回复Notification请求的信息
|
|
{
|
|
string s_; try { s_ = deserialized.GetValue("StartTime").ToString()!; } catch(Exception) { s_ = "0"; }
|
|
string e_ = deserialized.GetValue("EndTime").ToString()!;
|
|
|
|
var groups = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData,
|
|
"Groups", "User='" + person?.User + "'");
|
|
var department = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData,
|
|
"Department", "User='" + person?.User + "'");
|
|
|
|
DataTable resultTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("" +
|
|
"select * from Notification WHERE Groups='" + groups + "'and Department='" +
|
|
department + "' and Id>'" + s_ + "'", null)?.Tables[0] ?? new DataTable();
|
|
if(resultTable.Rows.Count==0) resultTable.Rows.Add();
|
|
|
|
//格式化返回信息
|
|
data_.Add("Notification", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "InfoMachine")//回复InfoMachine请求的信息
|
|
{
|
|
string t_ = deserialized.GetValue("Type").ToString()!;
|
|
string? m_ =null, f_ = "*";
|
|
|
|
if (deserialized.TryGetValue("MachinName", out var MachinName) && MachinName != null) m_ = MachinName.ToString();
|
|
if (deserialized.TryGetValue("Field", out var Field) && Field != null) f_ = Field.ToString();
|
|
|
|
if (t_ == "10")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[WaitDispenseDyestuff] WHERE DispenseMachine='" + m_ + "'");
|
|
data_.Add("Type", "10");
|
|
data_.Add("InfoMachine", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else if (t_ == "12")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[ShelfInfo] ");
|
|
data_.Add("Type", "12");
|
|
data_.Add("InfoMachine", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else if (t_ == "20")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[WaitChemical] WHERE DispenseMachine='" + m_ + "'");
|
|
data_.Add("Type", "20");
|
|
data_.Add("InfoMachine", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else
|
|
{
|
|
data_.Add("InfoMachine", "");
|
|
}
|
|
|
|
//格式化返回信息
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "InfoMachineHistory")//回复InfoMachineHistory请求的信息
|
|
{
|
|
string t_ = deserialized.GetValue("Type").ToString()!;
|
|
string? m_ = null, f_ = "*";
|
|
|
|
if (deserialized.TryGetValue("MachinName", out var MachinName) && MachinName != null) m_ = MachinName.ToString();
|
|
if (deserialized.TryGetValue("Field", out var Field) && Field != null) f_ = Field.ToString();
|
|
|
|
if (t_ == "80")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[DyeSolutionHistory] WHERE DispenseStartTime >'" + DateTime.Now.AddDays(-3)+
|
|
"' and DispenseMachine='" + m_ + "'");
|
|
data_.Add("Type", "80");
|
|
data_.Add("InfoMachineHistory", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else if (t_ == "82")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[inOutRecords] WHERE Createtime >'" + DateTime.Now.AddDays(-3)+"'");
|
|
data_.Add("Type", "82");
|
|
data_.Add("InfoMachineHistory", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else if (t_ == "90")
|
|
{
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[DyeSolutionHistory] WHERE DispenseStartTime >'" + DateTime.Now.AddDays(-3) +
|
|
"' and DispenseMachine='" + m_ + "'");
|
|
data_.Add("Type", "90");
|
|
data_.Add("InfoMachineHistory", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
}
|
|
else
|
|
{
|
|
data_.Add("InfoMachineHistory", "");
|
|
}
|
|
|
|
//格式化返回信息
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "InfoMachinePipes")//回复Notification请求的信息
|
|
{
|
|
string? m_ = null, f_ = "*";
|
|
|
|
if (deserialized.TryGetValue("MachinName", out var MachinName) && MachinName != null) m_ = MachinName.ToString();
|
|
if (deserialized.TryGetValue("Field", out var Field) && Field != null) f_ = Field.ToString();
|
|
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
|
f_ + " FROM [dbo].[Pipes] WHERE Dispenser='" + m_ + "'");
|
|
data_.Add("InfoMachinePipes", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
|
|
|
//格式化返回信息
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
if (Command == "SETUSER")//回复请求的信息
|
|
{
|
|
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData,
|
|
"User='" + deserialized.GetValue("USER").ToString()! + "'" ,
|
|
deserialized.GetValue("VALUE").ToString(), deserialized.GetValue("KEY").ToString()!);
|
|
data_.Add("USER", deserialized.GetValue("USER").ToString()!);
|
|
data_.Add("VALUE", deserialized.GetValue("VALUE").ToString()!);
|
|
data_.Add("KEY", deserialized.GetValue("KEY").ToString()!);
|
|
data_.Add("SETUSER", "OK");
|
|
//格式化返回信息
|
|
ret = JsonSerializer.Serialize(data_);
|
|
}
|
|
//加密回传
|
|
person!.Dat =AES.AesEncipher( person.Terminal, ret);
|
|
var jsdat = JsonSerializer.Serialize(person);
|
|
await tcpClient.SendAsync(jsdat);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGing.ERRDATA(ex);
|
|
Console.WriteLine(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|