diff --git a/Class/DataReceived.cs b/Class/DataReceived.cs
index 9757108..d402f68 100644
--- a/Class/DataReceived.cs
+++ b/Class/DataReceived.cs
@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
+using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
@@ -23,15 +24,40 @@ namespace SunlightAggregationTerminal.Class
public static string GetLocalIpSimple()
{
- var host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (var ip in host.AddressList)
+ try
{
- if (ip.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ip))
+ // 获取当前设备上所有的网络接口
+ var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
+
+ foreach (var networkInterface in networkInterfaces)
{
- return ip.ToString();
+ // 仅检查处于活动状态的网络接口
+ if (networkInterface.OperationalStatus != OperationalStatus.Up)
+ continue;
+
+ // 获取该接口下的所有单播 IP 地址
+ var addresses = networkInterface.GetIPProperties().UnicastAddresses;
+
+ foreach (var address in addresses)
+ {
+ // 筛选条件:必须是 IPv4、非回环地址(127.x.x.x),且通常属于局域网网段
+ if (address.Address.AddressFamily == AddressFamily.InterNetwork &&
+ !IPAddress.IsLoopback(address.Address))
+ {
+ return address.Address.ToString();
+ }
+ }
}
}
- return "No network connection";
+ catch (Exception ex)
+ {
+ MainThread.BeginInvokeOnMainThread(() =>
+ {
+ Application.Current!.Windows[0].Page!.DisplayAlertAsync("获取IP地址失败", ex.Message.ToString(), "是");
+ });
+ }
+
+ return null; // 如果未找到有效IP则返回null
}
public static string ServerIP = "";
@@ -64,10 +90,10 @@ namespace SunlightAggregationTerminal.Class
break;
}
- MainThread.BeginInvokeOnMainThread(() =>
- {
- Application.Current!.Windows[0].Page!.DisplayAlertAsync("", inf, "是");
- });
+ // MainThread.BeginInvokeOnMainThread(() =>
+ // {
+ // Application.Current!.Windows[0].Page!.DisplayAlertAsync("", inf, "是");
+ // });
}
else
{
@@ -86,10 +112,9 @@ namespace SunlightAggregationTerminal.Class
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? "";
if (data.TryGetValue("UserId", out val) && val != null)
{
+ DataReceived.Transmit("{\"Command\":\"Info\"}");
App.GlobalData.UserId = val.ToString() ?? "";
App.GlobalData.LogNo = true;
-
- Transmit("{\"Command\":\"Info\"}");
}
if (data.TryGetValue("UserData", out var value_UserData))
@@ -125,19 +150,23 @@ namespace SunlightAggregationTerminal.Class
foreach (var dataNotificationrow in dataNotification!)
{
//解析信息
- if (dataNotificationrow!.TryGetValue("id", out var value_Notification_id))
+ if (dataNotificationrow!.TryGetValue("id", out var value_Notification_id)
+ && value_Notification_id != null)
{
_id = (int)value_Notification_id;
}
- if (dataNotificationrow!.TryGetValue("Title", out var value_Notification_Title))
+ if (dataNotificationrow!.TryGetValue("Title", out var value_Notification_Title)
+ && value_Notification_Title != null)
{
_Title = value_Notification_Title.ToString() ?? "";
}
- if (dataNotificationrow!.TryGetValue("Content", out var value_Notification_Content))
+ if (dataNotificationrow!.TryGetValue("Content", out var value_Notification_Content)
+ && value_Notification_Content != null)
{
_Content = value_Notification_Content.ToString() ?? "";
}
- if (dataNotificationrow!.TryGetValue("Type", out var value_Notification_Type))
+ if (dataNotificationrow!.TryGetValue("Type", out var value_Notification_Type)
+ && value_Notification_Type != null)
{
string temp = value_Notification_Type?.ToString() ?? "";
// 转换
@@ -148,19 +177,24 @@ namespace SunlightAggregationTerminal.Class
_ => 0
};
}
- //记录信息到数据库
- DataSet? Notificationdata = AppModels.Select("select * from Notification where Id = " + _id);
- if (Notificationdata != null)
+ if (_id != 0)
{
- if (Notificationdata.Tables[0].Rows.Count == 0)
+ //记录信息到数据库
+ DataSet? Notificationdata = AppModels.Select("select * from Notification where Id = " + _id);
+ if (Notificationdata != null)
{
- 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 + ");");
+ 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 + ");");
+ }
}
}
}
}
+
+
}
}
diff --git a/Class/HttpServer.cs b/Class/HttpServer.cs
index 56127ce..534f487 100644
--- a/Class/HttpServer.cs
+++ b/Class/HttpServer.cs
@@ -91,10 +91,12 @@ namespace SunlightAggregationTerminal.Class
/***发送信息处理***/
public static async void HttpTransmit(string Dat)
{
- if(!httpclient.Online) //检查连接
+ if (!httpclient.Online) //检查连接
{
- await httpclient.ConnectAsync();
+ await httpclient.ConnectAsync();
}
+ Dispose();
+
var dat = new DataReceived.Person()
{
User = DataReceived.ServerName,
@@ -121,7 +123,6 @@ namespace SunlightAggregationTerminal.Class
// GetBodyAsync 同样需要响应取消信号,如果这里超过剩余时间也会抛出异常
var result = await response.GetBodyAsync();
DataReceived.Received(result);
- //await httpclient.CloseAsync();//关闭客户端
}
}
catch (OperationCanceledException)
@@ -131,7 +132,6 @@ namespace SunlightAggregationTerminal.Class
{
Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", "网络请求超时,请检查网络网络连接", "是");
});
- await httpclient.CloseAsync();//关闭客户端
}
catch (Exception ex)
{
@@ -140,8 +140,37 @@ namespace SunlightAggregationTerminal.Class
Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", ex.Message.ToString(), "是");
});
}
- }
-
+ finally
+ {
+ Start();
+ }
+ }
+
+ //倒计时5秒关闭http客户端
+ private static Timer? _timer;
+ private static bool _isDisposed = false;
+ private static void Start()
+ {
+ // 延迟30秒后执行一次,不自动循环
+ _timer = new Timer(Httpout, null, TimeSpan.FromSeconds(5), Timeout.InfiniteTimeSpan);
+ }
+ private static async void Dispose()
+ {
+ if (!_isDisposed)
+ {
+ _isDisposed = true;
+ _timer?.Dispose(); // 释放定时器资源
+ _timer = null;
+ }
+ }
+ private static async void Httpout(object? state)
+ {
+ if (httpclient.Online)
+ {
+ await httpclient.CloseAsync();
+ }
+ Dispose();
+ }
}
}
diff --git a/Models/AppModels.cs b/Models/AppModels.cs
index 3a9fef6..60de25d 100644
--- a/Models/AppModels.cs
+++ b/Models/AppModels.cs
@@ -61,7 +61,7 @@ namespace SunlightAggregationTerminal.Models
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;
+ TimerNotifications = -5;
}
}
diff --git a/Models/DataSource.cs b/Models/DataSource.cs
index 91381f9..4a780ca 100644
--- a/Models/DataSource.cs
+++ b/Models/DataSource.cs
@@ -76,6 +76,7 @@ namespace SunlightAggregationTerminal.Models
public string? DispenseStartTime { get; set; }
public string? DispenseEndTime { get; set; }
public string? DispenseTime { get; set; }
+ public string? UsageTime { get; set; }
public double? DispenseGrams { get; set; }
public string DispenseGramsDisplay => DispenseGrams?.ToString("F2") ?? "";
public int? DispenseResult { get; set; }
diff --git a/QueryDetail.xaml b/QueryDetail.xaml
index f572372..19d1a38 100644
--- a/QueryDetail.xaml
+++ b/QueryDetail.xaml
@@ -56,9 +56,9 @@
FontSize="22" TextColor="Black"/>
-
-
@@ -67,6 +67,12 @@
+
+
+
+