|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Person? deserializedPerson = JsonSerializer.Deserialize<Person>(mes);
|
|
|
|
|
|
|
|
|
|
var _user = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "State", "User='" +
|
|
|
|
|
deserializedPerson?.User + "' and Password ='" + deserializedPerson?.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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await tcpClient.SetupAsync(new TouchSocketConfig()
|
|
|
|
|
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
|
|
|
|
.SetRemoteIPHost(ip)
|
|
|
|
|
.SetKeepAliveValue(options => //心跳
|
|
|
|
|
{
|
|
|
|
|
options.AckInterval = 2000;
|
|
|
|
|
options.Interval = 30 * 1000;
|
|
|
|
|
})
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
{
|
|
|
|
|
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.PollingInterval = TimeSpan.FromSeconds(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.ConfigureContainer(a =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LogGing.ERRDATA(ex);
|
|
|
|
|
Console.WriteLine("Tcp:" + ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>>(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 + "' and Password ='" + person?.Password + "'");
|
|
|
|
|
|
|
|
|
|
data_.Add("Enterprise", _user?.Field<object>("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 == "Info")//回复info请求的信息
|
|
|
|
|
{
|
|
|
|
|
var t = ToList(MainWindowViewModel._machines).ToJsonString();
|
|
|
|
|
//格式化返回信息
|
|
|
|
|
data_.Add("Info", t);
|
|
|
|
|
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")//回复info请求的信息
|
|
|
|
|
{
|
|
|
|
|
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_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
person!.Dat = ret.ToString();
|
|
|
|
|
var jsdat = JsonSerializer.Serialize(person);
|
|
|
|
|
await tcpClient.SendAsync(jsdat);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LogGing.ERRDATA(ex);
|
|
|
|
|
Console.WriteLine(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|