Browse Source

信息接受逻辑与页面显示

master
sc 5 days ago
parent
commit
2d6f2b9113
  1. 107
      Class/TcpServer.cs
  2. 5
      Models/DataSource.cs
  3. 9
      View/LogPage.xaml.cs

107
Class/TcpServer.cs

@ -33,17 +33,17 @@ namespace SunlightAggregationTerminal.Class
ServerTerminal = Terminal; ServerTerminal = Terminal;
_= TcpClient(tcpClient, ServerIP); _ = TcpClient(tcpClient, ServerIP);
} }
public static async void TcpTransmit(string Dat) public static async void TcpTransmit(string Dat)
{ {
var dat = new Person() var dat = new Person()
{ {
User = ServerName , User = ServerName,
Password = ServerPassword, Password = ServerPassword,
IP = ServerIP, IP = ServerIP,
Terminal = ServerTerminal, Terminal = ServerTerminal,
Dat = Dat Dat = Dat
}; };
@ -55,7 +55,7 @@ namespace SunlightAggregationTerminal.Class
} }
catch (Exception) catch (Exception)
{ {
await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "连接未启用", "是"); await Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", "请求异常", "是");
} }
} }
@ -69,33 +69,66 @@ namespace SunlightAggregationTerminal.Class
#region Tcp客户端使用Received异步委托接收数据 #region Tcp客户端使用Received异步委托接收数据
tcpClient.Received = (client, e) => tcpClient.Received = (client, e) =>
{ {
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。 //从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
var mes = e.Memory.Span.ToString(Encoding.UTF8); var mes = e.Memory.Span.ToString(Encoding.UTF8);
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(mes);
if (data != null) try
{ {
//处理服务器发送信息 var data = JsonSerializer.Deserialize<Dictionary<string, object>>(mes);
if (data.TryGetValue("Enterprise", out var value_Enterprise))
{ if (data != null)
if (value_Enterprise != null) { App.GlobalData.Enterprise = value_Enterprise.ToString() ?? ""; }
}
if (data.TryGetValue("Department", out var value_Department))
{
if (value_Department != null) { App.GlobalData.Department = value_Department.ToString() ?? ""; }
}
if (data.TryGetValue("Groups", out var value_Groups))
{
if (value_Groups != null) { App.GlobalData.Groups = value_Groups.ToString() ?? ""; }
}
if (data.TryGetValue("UserId", out var value_UserId))
{
if (value_Groups != null) { App.GlobalData.UserId = value_UserId.ToString() ?? ""; }
}
if (data.TryGetValue("UserName", out var value_UserName))
{ {
if (value_Groups != null) { App.GlobalData.Groups = value_UserName.ToString() ?? ""; } //处理服务器发送信息
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("UserId", out val) && val != null) App.GlobalData.UserId = val.ToString() ?? "";
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? "";
if (data.TryGetValue("UserData", out var value_UserData))
{
}
if (data.TryGetValue("SysData", out var value_SysData))
{
}
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) { }
return EasyTask.CompletedTask; return EasyTask.CompletedTask;
}; };
@ -104,17 +137,7 @@ namespace SunlightAggregationTerminal.Class
try try
{ {
await tcpClient.SetupAsync(new TouchSocketConfig() await tcpClient.SetupAsync(new TouchSocketConfig()
.SetRemoteIPHost(ip) .SetRemoteIPHost(ip));
.ConfigurePlugins(a =>
{
a.UseReconnection<TcpClient>(options =>
{
options.PollingInterval = TimeSpan.FromSeconds(5);
});
})
.ConfigureContainer(a =>
{
}));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
} catch (Exception) } catch (Exception)
{ {

5
Models/DataSource.cs

@ -26,12 +26,13 @@ namespace SunlightAggregationTerminal.Models
public static class DataService public static class DataService
{ {
public static DataSet? data = AppModels.Select("select * from Notification Where Time > '" + DateTime.Now.AddDays(-30) + "'");
// 直接返回 List // 直接返回 List
public static List<NotificationItem> _NotificationData = new List<NotificationItem>(); public static List<NotificationItem> _NotificationData = new List<NotificationItem>();
public static List<NotificationItem> GetAllItems() public static List<NotificationItem> GetAllItems()
{ {
DataSet? data = AppModels.Select("select * from Notification Where Time > '" +
DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");
_NotificationData.Clear(); _NotificationData.Clear();
if (data != null) if (data != null)

9
View/LogPage.xaml.cs

@ -117,7 +117,12 @@ public partial class LogPage : ContentPage
await DisplayAlertAsync("未登录", "服务器不存在或拒绝访问", "是"); await DisplayAlertAsync("未登录", "服务器不存在或拒绝访问", "是");
} }
LoadingIndicator.IsVisible = false; LoadingIndicator.IsVisible = false;
}
await Navigation.PopModalAsync();
}
} }
Loading…
Cancel
Save