|
|
@ -33,6 +33,10 @@ namespace SunlightAggregationManager.UserClass |
|
|
|
|
|
|
|
|
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip) |
|
|
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.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
|
|
tcpClient.Connected = (client, e) => |
|
|
tcpClient.Connected = (client, e) => |
|
|
{ |
|
|
{ |
|
|
@ -82,6 +86,8 @@ namespace SunlightAggregationManager.UserClass |
|
|
{ |
|
|
{ |
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
|
var mes = e.Memory.Span.ToString(Encoding.UTF8); |
|
|
var mes = e.Memory.Span.ToString(Encoding.UTF8); |
|
|
|
|
|
//信息处理暂停心跳定时
|
|
|
|
|
|
timer.Change(Timeout.Infinite, Timeout.Infinite); |
|
|
|
|
|
|
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
@ -95,30 +101,34 @@ namespace SunlightAggregationManager.UserClass |
|
|
if (_dat != "99") |
|
|
if (_dat != "99") |
|
|
{ |
|
|
{ |
|
|
//行为
|
|
|
//行为
|
|
|
Dictionary<string, object> myDict = new Dictionary<string, object>(); |
|
|
Dictionary<string, object> myDict = new Dictionary<string, object>(); |
|
|
//myDict.Add("LinkID", client.Id);
|
|
|
//myDict.Add("LinkID", client.Id);
|
|
|
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP); |
|
|
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP); |
|
|
myDict.Add("LastActionTime", DateTime.Now); |
|
|
myDict.Add("LastActionTime", DateTime.Now); |
|
|
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown"); |
|
|
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown"); |
|
|
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict); |
|
|
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict); |
|
|
|
|
|
|
|
|
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]"); |
|
|
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]"); |
|
|
CommandC(deserializedPerson?.Dat ?? "NOT", deserializedPerson!); |
|
|
CommandC(deserializedPerson?.Dat ?? "NOT", deserializedPerson!); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{//停用账号关闭连接
|
|
|
{//停用账号关闭连接
|
|
|
client.SendAsync("Account Deactivated"); |
|
|
client.SendAsync("Account Deactivated"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ //无效账号关闭连接
|
|
|
{ //无效账号关闭连接
|
|
|
client.SendAsync("Invalid Account"); |
|
|
client.SendAsync("Invalid Account"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{//错误
|
|
|
{//错误
|
|
|
Console.WriteLine("Tcp:" + ex.ToString()); |
|
|
Console.WriteLine("Tcp:" + ex.ToString()); |
|
|
client.SendAsync("Target instruction parsing error manager will close connection"); |
|
|
client.SendAsync("Target instruction parsing error manager will close connection"); |
|
|
|
|
|
} |
|
|
|
|
|
finally |
|
|
|
|
|
{//继续心跳定时
|
|
|
|
|
|
timer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask; |
|
|
return EasyTask.CompletedTask; |
|
|
@ -129,17 +139,12 @@ namespace SunlightAggregationManager.UserClass |
|
|
{ |
|
|
{ |
|
|
await tcpClient.SetupAsync(new TouchSocketConfig() |
|
|
await tcpClient.SetupAsync(new TouchSocketConfig() |
|
|
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
|
|
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
|
|
.SetRemoteIPHost(ip) |
|
|
.SetRemoteIPHost(ip) |
|
|
.SetKeepAliveValue(options => //心跳
|
|
|
|
|
|
{ |
|
|
|
|
|
options.AckInterval = 2000; |
|
|
|
|
|
options.Interval = 30 * 1000; |
|
|
|
|
|
}) |
|
|
|
|
|
.ConfigurePlugins(a => |
|
|
.ConfigurePlugins(a => |
|
|
{ |
|
|
{ |
|
|
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options => |
|
|
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options => |
|
|
{ |
|
|
{ |
|
|
options.PollingInterval = TimeSpan.FromSeconds(3); |
|
|
options.PollingInterval = TimeSpan.FromSeconds(5); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
}) |
|
|
}) |
|
|
@ -149,6 +154,7 @@ namespace SunlightAggregationManager.UserClass |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
@ -156,73 +162,105 @@ namespace SunlightAggregationManager.UserClass |
|
|
Console.WriteLine("Tcp:" + ex.ToString()); |
|
|
Console.WriteLine("Tcp:" + ex.ToString()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private static void TimerCallback(object? state) |
|
|
public static async void CommandC(string dat, Person person)//
|
|
|
|
|
|
{ |
|
|
{ |
|
|
try { |
|
|
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()!; |
|
|
if (tcpClient.Online) |
|
|
string e_ = deserialized.GetValue("EndTime").ToString()!; |
|
|
{ |
|
|
string f_ = deserialized.GetValue("Field").ToString()!; |
|
|
tcpClient.SendAsync("SUNLIGHT"); |
|
|
|
|
|
} |
|
|
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请求的信息
|
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
string d_ = deserialized.GetValue("DyelotName").ToString()!; |
|
|
Console.WriteLine(ex.ToString()); |
|
|
string f_ = deserialized.GetValue("Field").ToString()!; |
|
|
LogGing.ERRDATA(ex); |
|
|
|
|
|
|
|
|
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请求的信息
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static async void CommandC(string dat, Person person)//
|
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
{ |
|
|
{ |
|
|
string d_ = deserialized.GetValue("Dyelot").ToString()!; |
|
|
string Command = "", ret = ""; |
|
|
string r_ = deserialized.GetValue("ReDye").ToString()!; |
|
|
|
|
|
string f_ = deserialized.GetValue("Field").ToString()!; |
|
|
Dictionary<string, object> data_ = new Dictionary<string, object>(); |
|
|
|
|
|
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(dat)!; |
|
|
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " + |
|
|
if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!; |
|
|
f_ + " FROM [dbo].[DyelotsBulkedRecipe] " + "WHERE Dyelot='" + d_ + "'and ReDye='" + r_ + "'"); |
|
|
if (Command == "Log")//回复请求的信息
|
|
|
//格式化返回信息
|
|
|
{ |
|
|
data_.Add("QueryDetail", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable)); |
|
|
DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" + |
|
|
ret = JsonSerializer.Serialize(data_); |
|
|
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")//回复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_ = deserialized.GetValue("StartTime").ToString()!; |
|
|
|
|
|
string e_ = deserialized.GetValue("EndTime").ToString()!; |
|
|
|
|
|
|
|
|
|
|
|
var groups = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, |
|
|
|
|
|
"Groups", "User='" + person?.User + "'"); |
|
|
|
|
|
|
|
|
|
|
|
var resultTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("" + |
|
|
|
|
|
"select * from Notification WHERE Groups='" + groups + "' and Time>'" + |
|
|
|
|
|
s_ + "' and Time<'" + e_ + "'", null)?.Tables[0] ?? new DataTable(); |
|
|
|
|
|
|
|
|
|
|
|
//格式化返回信息
|
|
|
|
|
|
data_.Add("Notification", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable)); |
|
|
|
|
|
ret = JsonSerializer.Serialize(data_); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
person!.Dat = ret.ToString(); |
|
|
person!.Dat = ret.ToString(); |
|
|
var jsdat = JsonSerializer.Serialize(person); |
|
|
var jsdat = JsonSerializer.Serialize(person); |
|
|
await tcpClient.SendAsync(jsdat); |
|
|
await tcpClient.SendAsync(jsdat); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
|