Administrator 2 months ago
parent
commit
70dabd0ca6
  1. 78
      Class/DataReceived.cs
  2. 41
      Class/HttpServer.cs
  3. 2
      Models/AppModels.cs
  4. 1
      Models/DataSource.cs
  5. 10
      QueryDetail.xaml
  6. 11
      QueryDetail.xaml.cs
  7. 1
      QueryPage.xaml.cs

78
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 + ");");
}
}
}
}
}
}
}

41
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();
}
}
}

2
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;
}
}

1
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; }

10
QueryDetail.xaml

@ -56,9 +56,9 @@
FontSize="22" TextColor="Black"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="开始时间: " FontAttributes="Bold"
<Label Text="呼叫时间: " FontAttributes="Bold"
FontSize="22" TextColor="Black"/>
<Label Text="{Binding DispenseStartTime}" FontAttributes="Bold"
<Label Text="{Binding CallTime}" FontAttributes="Bold"
FontSize="22" TextColor="Black"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
@ -67,6 +67,12 @@
<Label Text="{Binding DispenseEndTime}" FontAttributes="Bold"
FontSize="22" TextColor="Black"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="用时: " FontAttributes="Bold"
FontSize="22" TextColor="Black"/>
<Label Text="{Binding UsageTime}" FontAttributes="Bold"
FontSize="22" TextColor="Black"/>
</HorizontalStackLayout>
<BoxView HeightRequest="3" Color="#CCCCCC" />
<HorizontalStackLayout>
<Label Text="{Binding ProductName}" FontAttributes="Bold"

11
QueryDetail.xaml.cs

@ -25,12 +25,14 @@ public partial class QueryDetail : ContentPage
Dictionary<string, object> QueryDictionary = new Dictionary<string, object>();
QueryDictionary.Add("Command", "QueryDetail");
QueryDictionary.Add("Field", "[Dyelot],[ReDye],[StepNumber],[ProductCode],[ProductName]," +
"[ProductType],[Grams],[Created],[DispenseStartTime],[DispenseEndTime],[DispenseTime]," +
"[DispenseGrams],[DispenseResult],[Process]");
/* QueryDictionary.Add("Field", "[Dyelot],[ReDye],[StepNumber],[ProductCode],[ProductName]," +
"[ProductType],[Grams],[Created],[DispenseStartTime],[DispenseEndTime],[DispenseTime]," +
"[DispenseGrams],[DispenseResult],[Process]");*/
QueryDictionary.Add("Field", "*");
QueryDictionary.Add("Dyelot", item.Dyelot?.ToString() ?? "");
QueryDictionary.Add("ReDye", item.ReDye?.ToString()??"");
Query_Command(QueryDictionary.ToJsonString());
Query_Command(QueryDictionary.ToJsonString());
}
private void Data_()
@ -104,6 +106,7 @@ public partial class QueryDetail : ContentPage
//添加入显示信息
foreach (var item in data)
{
if (item.DispenseEndTime != null) item.DispenseEndTime = item.DispenseEndTime.Replace("T", " ");
QueryDetailDyelotDetailsItems.Add(item);
}

1
QueryPage.xaml.cs

@ -65,6 +65,7 @@ public partial class QueryPage : ContentPage
{
foreach (var item in data)
{
if(item.CreationTime != null) item.CreationTime = item.CreationTime.Replace("T", " ");
DyelotItems.Add(item);
}

Loading…
Cancel
Save