diff --git a/Class/DataReceived.cs b/Class/DataReceived.cs index 5227e9e..5de1cf5 100644 --- a/Class/DataReceived.cs +++ b/Class/DataReceived.cs @@ -34,95 +34,126 @@ namespace SunlightAggregationTerminal.Class } public static string ServerIP = ""; + public static string ServerURL = ""; public static string ServerName = ""; public static string ServerPassword = ""; public static string ServerTerminal = ""; - public static void Received(string dat) { - try + if (dat.Length < 6) { - Person deserializedPerson = JsonSerializer.Deserialize(dat)!; - var data = JsonSerializer.Deserialize>(deserializedPerson.Dat); + string inf; + switch (dat) + { + case "999": + inf = "不明错误"; + break; + case "990": + inf = "拒接访问账户远程访问许可证过期"; + break; + case "980": + inf = "目标对象服务器不存在或已离线"; + break; + case "970": + inf = "目标对象服务器不存在或已离线"; + break; + default: + inf = dat; + break; + } - if (data != null) + MainThread.BeginInvokeOnMainThread(() => { - //处理服务器发送信息 - 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; + Application.Current!.Windows[0].Page!.DisplayAlertAsync("", inf, "是"); + }); + } + else + { - Transmit("{\"Command\":\"Info\"}"); - } + try + { + Person deserializedPerson = JsonSerializer.Deserialize(dat)!; + var data = JsonSerializer.Deserialize>(deserializedPerson.Dat); - if (data.TryGetValue("UserData", out var value_UserData)) + 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; - } - if (data.TryGetValue("SysData", out var value_SysData)) - { + Transmit("{\"Command\":\"Info\"}"); + } - } + if (data.TryGetValue("UserData", out var value_UserData)) + { - 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()!); + } + if (data.TryGetValue("SysData", out var value_SysData)) + { - string _Title = "", _Content = ""; - int _Type = 0; + } - if (dataNotification!.TryGetValue("Title", out var value_Notification_Title)) + if (data.TryGetValue("Info", out val) && val != null)//机台信息接受 { - _Title = value_Notification_Title.ToString() ?? ""; + DataModel.Info_data = JsonSerializer.Deserialize>>(val.ToString()!)!; + //发送消息 + WeakReferenceMessenger.Default.Send(new object()); } - if (dataNotification!.TryGetValue("Content", out var value_Notification_Content)) + if (data.TryGetValue("Query", out val) && val != null)//查询接受 { - _Content = value_Notification_Content.ToString() ?? ""; + QueryPage.Query_data(val.ToString()!); } - if (dataNotification!.TryGetValue("Type", out var value_Notification_Type)) + if (data.TryGetValue("QueryDetail", out val) && val != null)//查询接受 { - string temp = value_Notification_Type?.ToString() ?? ""; - // 转换 - _Type = temp switch - { - "System" => 2, - "Message" => 1, - _ => 0 - }; + 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; - Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" + - _Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');"); + 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(() => + } + catch (Exception ex) { - Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是"); - }); + MainThread.BeginInvokeOnMainThread(() => + { + Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是"); + }); + } } } diff --git a/Class/HttpServer.cs b/Class/HttpServer.cs index 8538a53..113069f 100644 --- a/Class/HttpServer.cs +++ b/Class/HttpServer.cs @@ -17,7 +17,10 @@ namespace SunlightAggregationTerminal.Class public static async void Configuration(string ip, string Name, string Password, string Terminal) { - DataReceived.ServerIP = ip; + string[] parts = ip.Split('/'); + + DataReceived.ServerIP = parts[0]; + DataReceived.ServerURL = parts[1]; DataReceived.ServerName = Name; DataReceived.ServerPassword = Password; DataReceived.ServerTerminal = Terminal; @@ -89,7 +92,7 @@ namespace SunlightAggregationTerminal.Class var request = new HttpRequest(); request.SetContent(new StringHttpContent(JsonSerializer.Serialize(dat), Encoding.UTF8, "application/scjson")) .InitHeaders() - .SetUrl("/H") + .SetUrl(DataReceived.ServerURL) .SetHost(httpclient.RemoteIPHost.Host) .AsPost(); diff --git a/Models/AppModels.cs b/Models/AppModels.cs index e62d7c4..0786a70 100644 --- a/Models/AppModels.cs +++ b/Models/AppModels.cs @@ -73,6 +73,8 @@ namespace SunlightAggregationTerminal.Models { App.GlobalData.LogNo = false; + // await WebSocketServer.WebSocketClient(); + //发送登录请求 if (App.GlobalData.LocalAreaNetworkMode) { diff --git a/WebSocketServer.cs b/WebSocketServer.cs new file mode 100644 index 0000000..2ab1c1b --- /dev/null +++ b/WebSocketServer.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TouchSocket.Core; +using TouchSocket.Http; +using TouchSocket.Http.WebSockets; +using TouchSocket.Sockets; + +namespace SunlightAggregationTerminal +{ + public class WebSocketServer + { + public static async Task WebSocketClient() + { + using var webSocket = new WebSocketClient(); + webSocket.Connected = (c, e) => + { + Console.WriteLine("Connected"); + return EasyTask.CompletedTask; + }; + + #region WebSocket客户端使用Received委托接收数据 + + webSocket.Received = (c, e) => + { + Console.WriteLine(e.DataFrame.ToText()); + return EasyTask.CompletedTask; + }; + + #endregion WebSocket客户端使用Received委托接收数据 + + webSocket.Closed = (c, e) => + { + Console.WriteLine("Closed"); + return EasyTask.CompletedTask; + }; + + await webSocket.SetupAsync(new TouchSocketConfig() + + .ConfigureContainer(a => + + { + + a.AddConsoleLogger(); + + }) + .ConfigurePlugins(a => + { + a.UseReconnection(options => + { + //设置在线状态轮询时间为5秒钟。 + options.PollingInterval = TimeSpan.FromSeconds(5); + + //使用websocket专门的心跳在线检测。基本逻辑如下: + //由于设置的PollingInterval为5秒,所以每5秒会检查一下WebSocketClient在线状态。 + //1.每隔pingInterval的时间,发送一次ping报文。 + //2.如果在activeTimeSpan(30秒)内,有数据收发,则也会认为在活。 + options.UseWebSocketCheckAction(activeTimeSpan: TimeSpan.FromSeconds(30), pingInterval: TimeSpan.FromSeconds(5)); + }); + }) + + + .SetRemoteIPHost("http://127.0.0.1:7792/ws")); + + + await webSocket.ConnectAsync(); + } + } +}