diff --git a/Class/DataReceived.cs b/Class/DataReceived.cs index 5de1cf5..9757108 100644 --- a/Class/DataReceived.cs +++ b/Class/DataReceived.cs @@ -2,6 +2,7 @@ using SunlightAggregationTerminal.Models; using System; using System.Collections.Generic; +using System.Data; using System.Net; using System.Net.Sockets; using System.Text; @@ -116,33 +117,49 @@ namespace SunlightAggregationTerminal.Class } if (data.TryGetValue("Notification", out val) && val != null)//信息通知接受 { - var dataNotification = JsonSerializer.Deserialize>(val.ToString()!); + var dataNotification = JsonSerializer.Deserialize>>(val.ToString()!); string _Title = "", _Content = ""; - int _Type = 0; + int _Type = 0 ,_id = 0; - if (dataNotification!.TryGetValue("Title", out var value_Notification_Title)) + foreach (var dataNotificationrow in dataNotification!) { - _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 + //解析信息 + if (dataNotificationrow!.TryGetValue("id", out var value_Notification_id)) + { + _id = (int)value_Notification_id; + } + if (dataNotificationrow!.TryGetValue("Title", out var value_Notification_Title)) { - "System" => 2, - "Message" => 1, - _ => 0 - }; + _Title = value_Notification_Title.ToString() ?? ""; + } + if (dataNotificationrow!.TryGetValue("Content", out var value_Notification_Content)) + { + _Content = value_Notification_Content.ToString() ?? ""; + } + if (dataNotificationrow!.TryGetValue("Type", out var value_Notification_Type)) + { + string temp = value_Notification_Type?.ToString() ?? ""; + // 转换 + _Type = temp switch + { + "System" => 2, + "Message" => 1, + _ => 0 + }; + } + //记录信息到数据库 + DataSet? Notificationdata = AppModels.Select("select * from Notification where Id = " + _id); + if (Notificationdata != null) + { + if (Notificationdata.Tables[0].Rows.Count == 0) + { + AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type,Id)VALUES('" + + _Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + + "','" + _Type + "'," + _id + ");"); + } + } } - - Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" + - _Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');"); } } diff --git a/Class/HttpServer.cs b/Class/HttpServer.cs index 5f2674b..56127ce 100644 --- a/Class/HttpServer.cs +++ b/Class/HttpServer.cs @@ -40,6 +40,8 @@ namespace SunlightAggregationTerminal.Class else { await HttpClient(DataReceived.ServerIP); + + HttpTransmit("{\"Command\":\"Log\"}"); } } @@ -53,30 +55,26 @@ namespace SunlightAggregationTerminal.Class #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(); + //MainThread.BeginInvokeOnMainThread(() => + // { + // Application.Current!.Windows[0].Page!.DisplayAlertAsync("", "客户端断开连接", "是"); + // }); + // await e.InvokeNext(); }); - }); - /*config.ConfigurePlugins(a => - { + //自动重连 a.UseReconnection(options => { options.PollingInterval = TimeSpan.FromSeconds(3); }); - });*/ + //处理连接 + a.AddTcpConnectedPlugin(async (c, e) => + { + await e.InvokeNext(); + }); + }); #endregion if (httpclient.Online)//是否重置客户端 @@ -93,6 +91,10 @@ namespace SunlightAggregationTerminal.Class /***发送信息处理***/ public static async void HttpTransmit(string Dat) { + if(!httpclient.Online) //检查连接 + { + await httpclient.ConnectAsync(); + } var dat = new DataReceived.Person() { User = DataReceived.ServerName, @@ -119,6 +121,7 @@ namespace SunlightAggregationTerminal.Class // GetBodyAsync 同样需要响应取消信号,如果这里超过剩余时间也会抛出异常 var result = await response.GetBodyAsync(); DataReceived.Received(result); + //await httpclient.CloseAsync();//关闭客户端 } } catch (OperationCanceledException) @@ -126,8 +129,9 @@ namespace SunlightAggregationTerminal.Class // 专门处理超时或主动取消的情况 MainThread.BeginInvokeOnMainThread(() => { - Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", "网络请求超时,请检查网络或服务器状态。", "是"); + Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", "网络请求超时,请检查网络网络连接", "是"); }); + await httpclient.CloseAsync();//关闭客户端 } catch (Exception ex) { @@ -136,9 +140,7 @@ namespace SunlightAggregationTerminal.Class Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", ex.Message.ToString(), "是"); }); } - } - - + } } diff --git a/Class/SQLiteConfig.cs b/Class/SQLiteConfig.cs index ef3bd81..a02bf30 100644 --- a/Class/SQLiteConfig.cs +++ b/Class/SQLiteConfig.cs @@ -215,7 +215,13 @@ namespace SunlightAggregationTerminal.Class { string sql = "ALTER TABLE Notification ADD COLUMN Type INT;"; SQLitedata.ExecuteNonQuery(sql, null); - } + } + // 检查 id + if (!existingColumns.Contains("Id")) + { + string sql = "ALTER TABLE Notification ADD COLUMN Id INTEGER PRIMARY KEY;"; + SQLitedata.ExecuteNonQuery(sql, null); + } } } private static void CheckAndRepairTableSchemaSERVER() @@ -302,7 +308,7 @@ namespace SunlightAggregationTerminal.Class // 建表语句 string createSql = @" CREATE TABLE Notification ( - Id INTEGER PRIMARY KEY AUTOINCREMENT, + Id INTEGER PRIMARY KEY, Title TEXT, Content TEXT, Time TEXT, diff --git a/Models/AppModels.cs b/Models/AppModels.cs index 0786a70..3a9fef6 100644 --- a/Models/AppModels.cs +++ b/Models/AppModels.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Text; +using TouchSocket.Core; namespace SunlightAggregationTerminal.Models { @@ -11,7 +12,8 @@ namespace SunlightAggregationTerminal.Models { public static SqliteHelper sqliteHelper = SQLiteConfig.Config(Path.Combine(FileSystem.AppDataDirectory, "MyAppData.db"))!; //数据库连接路径(获取各平台的应用数据目录) - + private Timer timer; + private static int TimerNotifications = -1440; public AppModels() { if(sqliteHelper != null) @@ -43,7 +45,24 @@ namespace SunlightAggregationTerminal.Models } } } - } + } + + //启动通知轮询延迟120s启动周期600s + timer = new Timer(TimerNotification, null, TimeSpan.FromSeconds(120), TimeSpan.FromSeconds(300)); + } + + //轮询通知的定时任务 + static void TimerNotification(object? state) + { + if (App.GlobalData.LogNo)//已经登录 + { + Dictionary NotificationDictionary = new Dictionary(); + NotificationDictionary.Add("Command", "Notification"); + NotificationDictionary.Add("StartTime", DateTime.Now.AddMinutes(TimerNotifications).ToString("yyyy-MM-dd HH:mm:ss.fff")); + NotificationDictionary.Add("EndTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); + DataReceived.Transmit(NotificationDictionary.ToJsonString()); + TimerNotifications = -6; + } } public static void Updata(string sql) @@ -115,6 +134,8 @@ namespace SunlightAggregationTerminal.Models } } + + public static string GetAppUniqueId() { const string key = "sunlight_app_unique_id";