Merge branch 'master' of http://www.scserverddns.top:13000/sc/SunlightAggregationTerminal
This commit is contained in:
commit
a485d1cb58
5 files changed
+73
-148
No files matched your search
+43
-8
@@ -1,14 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using SunlightAggregationTerminal.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
|
||||
namespace SunlightAggregationTerminal.Class
|
||||
{
|
||||
public class DataReceived
|
||||
{
|
||||
public class Person
|
||||
{
|
||||
public required string User { get; set; }
|
||||
public required string Password { get; set; }
|
||||
public required string IP { get; set; }
|
||||
public required string Terminal { get; set; }
|
||||
public required string Dat { get; set; }
|
||||
}
|
||||
|
||||
public static string GetLocalIpSimple()
|
||||
{
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
foreach (var ip in host.AddressList)
|
||||
{
|
||||
if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
|
||||
{
|
||||
return ip.ToString();
|
||||
}
|
||||
}
|
||||
return "No network connection";
|
||||
}
|
||||
|
||||
public static string ServerIP = "";
|
||||
public static string ServerName = "";
|
||||
public static string ServerPassword = "";
|
||||
public static string ServerTerminal = "";
|
||||
|
||||
|
||||
public static void Received(string dat)
|
||||
{
|
||||
try
|
||||
@@ -27,7 +57,7 @@ namespace SunlightAggregationTerminal.Class
|
||||
App.GlobalData.UserId = val.ToString() ?? "";
|
||||
App.GlobalData.LogNo = true;
|
||||
|
||||
HttpServer.HttpTransmit("{\"Command\":\"Info\"}");
|
||||
Transmit("{\"Command\":\"Info\"}");
|
||||
}
|
||||
|
||||
if (data.TryGetValue("UserData", out var value_UserData))
|
||||
@@ -95,11 +125,16 @@ namespace SunlightAggregationTerminal.Class
|
||||
}
|
||||
}
|
||||
|
||||
public static void Transmit(string Dat)
|
||||
public static void Transmit(string Dat)
|
||||
{
|
||||
if (App.GlobalData.LocalAreaNetworkMode)
|
||||
{ TcpServer.TcpTransmit(Dat); }
|
||||
else { HttpServer.HttpTransmit(Dat); }
|
||||
{
|
||||
TcpServer.TcpTransmit(Dat);
|
||||
}
|
||||
else
|
||||
{
|
||||
HttpServer.HttpTransmit(Dat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-10
@@ -17,12 +17,12 @@ namespace SunlightAggregationTerminal.Class
|
||||
|
||||
public static async void Configuration(string ip, string Name, string Password, string Terminal)
|
||||
{
|
||||
ServerIP = ip;
|
||||
ServerName = Name;
|
||||
ServerPassword = Password;
|
||||
ServerTerminal = Terminal;
|
||||
DataReceived.ServerIP = ip;
|
||||
DataReceived.ServerName = Name;
|
||||
DataReceived.ServerPassword = Password;
|
||||
DataReceived.ServerTerminal = Terminal;
|
||||
|
||||
await HttpClient("127.0.0.1:7791");
|
||||
await HttpClient(DataReceived.ServerIP);
|
||||
}
|
||||
|
||||
public static async Task HttpClient(string url)
|
||||
@@ -70,12 +70,12 @@ namespace SunlightAggregationTerminal.Class
|
||||
/***发送信息处理***/
|
||||
public static async void HttpTransmit(string Dat)
|
||||
{
|
||||
var dat = new Person()
|
||||
var dat = new DataReceived.Person()
|
||||
{
|
||||
User = ServerName,
|
||||
Password = ServerPassword,
|
||||
IP = GetLocalIpSimple(),
|
||||
Terminal = ServerTerminal,
|
||||
User = DataReceived.ServerName,
|
||||
Password = DataReceived.ServerPassword,
|
||||
IP = DataReceived.GetLocalIpSimple(),
|
||||
Terminal = DataReceived.ServerTerminal,
|
||||
Dat = Dat
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace SunlightAggregationTerminal.Class
|
||||
{
|
||||
public class SecureJsonService
|
||||
{
|
||||
private static readonly byte[] _aesKey = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
|
||||
private static readonly byte[] _aesKey = Encoding.UTF8.GetBytes("1234567890");
|
||||
|
||||
/// <summary>
|
||||
/// 调用一:发送端处理
|
||||
|
||||
+17
-123
@@ -14,57 +14,29 @@ using TouchSocket.Sockets;
|
||||
namespace SunlightAggregationTerminal.Class
|
||||
{
|
||||
public class TcpServer
|
||||
{
|
||||
public class Person
|
||||
{
|
||||
public required string User { get; set; }
|
||||
public required string Password { get; set; }
|
||||
public required string IP { get; set; }
|
||||
public required string Terminal { get; set; }
|
||||
public required string Dat { get; set; }
|
||||
}
|
||||
|
||||
public static string GetLocalIpSimple()
|
||||
{
|
||||
var host = Dns.GetHostEntry(Dns.GetHostName());
|
||||
foreach (var ip in host.AddressList)
|
||||
{
|
||||
if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
|
||||
{
|
||||
return ip.ToString();
|
||||
}
|
||||
}
|
||||
return "No network connection";
|
||||
}
|
||||
|
||||
public static string ServerIP = "";
|
||||
public static string ServerName = "";
|
||||
public static string ServerPassword = "";
|
||||
public static string ServerTerminal = "";
|
||||
|
||||
|
||||
{
|
||||
public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();
|
||||
|
||||
public static async void Configuration(string ip, string Name, string Password, string Terminal)
|
||||
{
|
||||
ServerIP = ip;
|
||||
ServerName = Name;
|
||||
ServerPassword = Password;
|
||||
ServerTerminal = Terminal;
|
||||
DataReceived.ServerIP = ip;
|
||||
DataReceived.ServerName = Name;
|
||||
DataReceived.ServerPassword = Password;
|
||||
DataReceived.ServerTerminal = Terminal;
|
||||
|
||||
await TcpClient(tcpClient, ServerIP);
|
||||
await TcpClient(tcpClient, DataReceived.ServerIP);
|
||||
}
|
||||
|
||||
// 引入发送锁,彻底解决连续发送导致的并发断连问题
|
||||
private static readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
|
||||
public static async void TcpTransmit(string Dat)
|
||||
{
|
||||
var dat = new Person()
|
||||
var dat = new DataReceived.Person()
|
||||
{
|
||||
User = ServerName,
|
||||
Password = ServerPassword,
|
||||
IP = GetLocalIpSimple(),
|
||||
Terminal = ServerTerminal,
|
||||
User = DataReceived.ServerName,
|
||||
Password = DataReceived.ServerPassword,
|
||||
IP = DataReceived.GetLocalIpSimple(),
|
||||
Terminal = DataReceived.ServerTerminal,
|
||||
Dat = Dat
|
||||
};
|
||||
|
||||
@@ -92,9 +64,11 @@ namespace SunlightAggregationTerminal.Class
|
||||
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip)
|
||||
{
|
||||
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
||||
tcpClient.Connected = (client, e) => {
|
||||
tcpClient.Connected = (client, e) =>
|
||||
{
|
||||
TcpServer.TcpTransmit("{\"Command\":\"Log\"}");
|
||||
return EasyTask.CompletedTask; };//成功连接到服务器
|
||||
return EasyTask.CompletedTask;
|
||||
};//成功连接到服务器
|
||||
tcpClient.Closing = (client, e) => {return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
|
||||
tcpClient.Closed = (client, e) => {return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
|
||||
#region Tcp客户端使用Received异步委托接收数据
|
||||
@@ -103,88 +77,8 @@ namespace SunlightAggregationTerminal.Class
|
||||
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
||||
var mes = e.Memory.Span.ToString(Encoding.UTF8);
|
||||
|
||||
try
|
||||
{
|
||||
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(mes);
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
//处理服务器发送信息
|
||||
if (data.TryGetValue("Enterprise", out var val) && val != null) App.GlobalData.Enterprise = val.ToString() ?? "";
|
||||
if (data.TryGetValue("Department", out val) && val != null) App.GlobalData.Department = val.ToString() ?? "";
|
||||
if (data.TryGetValue("Groups", out val) && val != null) App.GlobalData.Groups = val.ToString() ?? "";
|
||||
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? "";
|
||||
if (data.TryGetValue("UserId", out val) && val != null)
|
||||
{
|
||||
App.GlobalData.UserId = val.ToString() ?? "";
|
||||
App.GlobalData.LogNo = true;
|
||||
|
||||
TcpServer.TcpTransmit("{\"Command\":\"Info\"}");
|
||||
}
|
||||
|
||||
if (data.TryGetValue("UserData", out var value_UserData))
|
||||
{
|
||||
|
||||
}
|
||||
if (data.TryGetValue("SysData", out var value_SysData))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (data.TryGetValue("Info", out val) && val != null)//机台信息接受
|
||||
{
|
||||
DataModel.Info_data = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!)!;
|
||||
//发送消息
|
||||
WeakReferenceMessenger.Default.Send(new object());
|
||||
}
|
||||
if (data.TryGetValue("Query", out val) && val != null)//查询接受
|
||||
{
|
||||
QueryPage.Query_data(val.ToString()!);
|
||||
}
|
||||
if (data.TryGetValue("QueryDetail", out val) && val != null)//查询接受
|
||||
{
|
||||
QueryDetail.Query_data(val.ToString()!);
|
||||
}
|
||||
if (data.TryGetValue("Notification", out val) && val != null)//信息通知接受
|
||||
{
|
||||
var dataNotification = JsonSerializer.Deserialize<Dictionary<string, object>>(val.ToString()!);
|
||||
|
||||
string _Title = "", _Content = "";
|
||||
int _Type = 0;
|
||||
|
||||
if (dataNotification!.TryGetValue("Title", out var value_Notification_Title))
|
||||
{
|
||||
_Title = value_Notification_Title.ToString() ?? "";
|
||||
}
|
||||
if (dataNotification!.TryGetValue("Content", out var value_Notification_Content))
|
||||
{
|
||||
_Content = value_Notification_Content.ToString() ?? "";
|
||||
}
|
||||
if (dataNotification!.TryGetValue("Type", out var value_Notification_Type))
|
||||
{
|
||||
string temp = value_Notification_Type?.ToString() ?? "";
|
||||
// 转换
|
||||
_Type = temp switch
|
||||
{
|
||||
"System" => 2,
|
||||
"Message" => 1,
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" +
|
||||
_Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex) {
|
||||
MainThread.BeginInvokeOnMainThread(() =>
|
||||
{
|
||||
Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是");
|
||||
});
|
||||
}
|
||||
|
||||
DataReceived.Received(mes);
|
||||
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
#endregion
|
||||
|
||||
Reference in new issue
Block a user