diff --git a/Class/DataReceived.cs b/Class/DataReceived.cs new file mode 100644 index 0000000..a4f2617 --- /dev/null +++ b/Class/DataReceived.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Text; +using SunlightAggregationTerminal.Models; +using System.Text.Json; +using CommunityToolkit.Mvvm.Messaging; + +namespace SunlightAggregationTerminal.Class +{ + public class DataReceived + { + public static void Received(string dat) + { + try + { + var data = JsonSerializer.Deserialize>(dat); + + 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; + + HttpServer.HttpTransmit("{\"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>>(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>(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(), "是"); + }); + } + } + + public static void Transmit(string Dat) + { + if (App.GlobalData.LocalAreaNetworkMode) + { TcpServer.TcpTransmit(Dat); } + else { HttpServer.HttpTransmit(Dat); } + } + } +} diff --git a/Class/HttpServer.cs b/Class/HttpServer.cs new file mode 100644 index 0000000..07e23b1 --- /dev/null +++ b/Class/HttpServer.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Text.Json; +using TouchSocket.Core; +using TouchSocket.Http; +using TouchSocket.Http.WebSockets; +using TouchSocket.Sockets; +using static SunlightAggregationTerminal.Class.TcpServer; + +namespace SunlightAggregationTerminal.Class +{ + public class HttpServer + { + public static TouchSocket.Http.HttpClient httpclient = new TouchSocket.Http.HttpClient(); + + public static async void Configuration(string ip, string Name, string Password, string Terminal) + { + ServerIP = ip; + ServerName = Name; + ServerPassword = Password; + ServerTerminal = Terminal; + + await HttpClient("127.0.0.1:7791"); + } + + public static async Task HttpClient(string url) + { + var config = new TouchSocketConfig(); + #region Http设置远程服务器地址 + config.SetRemoteIPHost("http://" + url); + #endregion + + #region Http客户端获取断线通知 + config.ConfigurePlugins(a => + { + a.AddTcpClosedPlugin(async (c, e) => + { + // 专门处理超时或主动取消的情况 + MainThread.BeginInvokeOnMainThread(() => + { + Application.Current!.Windows[0].Page!.DisplayAlertAsync("", "客户端断开连接", "是"); + }); + await e.InvokeNext(); + }); + }); config.ConfigurePlugins(a => + { + a.AddTcpConnectedPlugin(async (c, e) => + { + HttpTransmit("{\"Command\":\"Log\"}"); + await e.InvokeNext(); + }); + }); + config.ConfigurePlugins(a => + { + a.UseReconnection(options => + { + options.PollingInterval = TimeSpan.FromSeconds(3); + }); + }); + #endregion + + //配置config + await httpclient.SetupAsync(config); + //连接 + await httpclient.ConnectAsync(); + } + + /***发送信息处理***/ + public static async void HttpTransmit(string Dat) + { + var dat = new Person() + { + User = ServerName, + Password = ServerPassword, + IP = GetLocalIpSimple(), + Terminal = ServerTerminal, + Dat = Dat + }; + + var t = JsonSerializer.Serialize(dat); + var p = httpclient.RemoteIPHost.Host; + + var request = new HttpRequest(); + request.SetContent(new StringHttpContent(t, Encoding.UTF8, "application/scjson")) + .InitHeaders() + .SetUrl("/api/user-report") + .SetHost(p) + .AsPost(); + + // 创建一个超时操作,10秒后取消 + using var cts = new CancellationTokenSource(1000 * 15); + try + { + using (var responseResult = await httpclient.RequestAsync(request, cts.Token)) + { + var response = responseResult.Response; + // GetBodyAsync 同样需要响应取消信号,如果这里超过剩余时间也会抛出异常 + var result = await response.GetBodyAsync(); + DataReceived.Received(result); + } + } + catch (OperationCanceledException) + { + // 专门处理超时或主动取消的情况 + MainThread.BeginInvokeOnMainThread(() => + { + Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", "网络请求超时,请检查网络或服务器状态。", "是"); + }); + } + catch (Exception ex) + { + MainThread.BeginInvokeOnMainThread(() => + { + Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", ex.Message.ToString(), "是"); + }); + } + } + + + + + } +} diff --git a/InfoPage.xaml.cs b/InfoPage.xaml.cs index e17686c..873c2e9 100644 --- a/InfoPage.xaml.cs +++ b/InfoPage.xaml.cs @@ -37,7 +37,7 @@ public partial class InfoPage : ContentPage // 更新刷新时间 _lastRefreshTime = DateTime.Now; - TcpServer.TcpTransmit("{\"Command\":\"Info\"}"); + DataReceived.Transmit("{\"Command\":\"Info\"}"); } catch (Exception) { diff --git a/Models/AppModels.cs b/Models/AppModels.cs index 516021c..6b60d8c 100644 --- a/Models/AppModels.cs +++ b/Models/AppModels.cs @@ -77,9 +77,20 @@ namespace SunlightAggregationTerminal.Models App.GlobalData.LogNo = false; //发送登录请求 - TcpServer.Configuration(App.GlobalData.ServerID, + if (App.GlobalData.LocalAreaNetworkMode) + { + //tcp + TcpServer.Configuration(App.GlobalData.ServerID, + App.GlobalData.User, App.GlobalData.UserPassword, + Models.AppModels.GetAppUniqueId()); + } + else + { + //http + HttpServer.Configuration(App.GlobalData.ServerID, App.GlobalData.User, App.GlobalData.UserPassword, Models.AppModels.GetAppUniqueId()); + } // 创建一个 15 秒的延时任务 Task delayTask = Task.Delay(15000); diff --git a/QueryDetail.xaml.cs b/QueryDetail.xaml.cs index 36835be..261a44c 100644 --- a/QueryDetail.xaml.cs +++ b/QueryDetail.xaml.cs @@ -71,7 +71,7 @@ public partial class QueryDetail : ContentPage LoadingIndicator.IsVisible = true; QueryDetailDyelotDetailsItems.Clear(); - TcpServer.TcpTransmit(dat); + DataReceived.Transmit(dat); // 创建一个 秒的延时任务 Task delayTask = Task.Delay(10000); diff --git a/QueryPage.xaml.cs b/QueryPage.xaml.cs index 645e996..cd98b3b 100644 --- a/QueryPage.xaml.cs +++ b/QueryPage.xaml.cs @@ -71,8 +71,8 @@ public partial class QueryPage : ContentPage { LoadingIndicator.IsVisible = true; DyelotItems.Clear(); - - TcpServer.TcpTransmit(dat); + + DataReceived.Transmit(dat); // 创建一个 15 秒的延时任务 Task delayTask = Task.Delay(15000); diff --git a/SunlightAggregationTerminal.csproj b/SunlightAggregationTerminal.csproj index 52264d6..a0e3671 100644 --- a/SunlightAggregationTerminal.csproj +++ b/SunlightAggregationTerminal.csproj @@ -104,7 +104,8 @@ - + +