You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
11 KiB
244 lines
11 KiB
using CommunityToolkit.Mvvm.Messaging;
|
|
using SunlightAggregationTerminal.Models;
|
|
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;
|
|
using TouchSocket.Core;
|
|
|
|
namespace SunlightAggregationTerminal.Class
|
|
{
|
|
public class DataReceived
|
|
{
|
|
public class Person
|
|
{
|
|
public required string User { get; set; }
|
|
public required string Password { get; set; }
|
|
public required string IP { get; set; }
|
|
public required string Terminal { get; set; }
|
|
public required string Dat { get; set; }
|
|
}
|
|
|
|
public static string GetLocalIpSimple()
|
|
{
|
|
try
|
|
{
|
|
// 获取当前设备上所有的网络接口
|
|
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
foreach (var networkInterface in networkInterfaces)
|
|
{
|
|
// 仅检查处于活动状态的网络接口
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MainThread.BeginInvokeOnMainThread(() =>
|
|
{
|
|
Application.Current!.Windows[0].Page!.DisplayAlertAsync("获取IP地址失败", ex.Message.ToString(), "是");
|
|
});
|
|
}
|
|
|
|
return ""; // 如果未找到有效IP则返回null
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (dat.Length < 6)
|
|
{
|
|
string inf;
|
|
switch (dat)
|
|
{
|
|
case "999":
|
|
inf = "不明错误";
|
|
break;
|
|
case "990":
|
|
inf = "拒接访问账户远程访问许可证过期";
|
|
break;
|
|
case "980":
|
|
inf = "目标对象服务器不存在或已离线";
|
|
break;
|
|
case "970":
|
|
inf = "目标对象服务器不存在或已离线";
|
|
break;
|
|
default:
|
|
inf = dat;
|
|
break;
|
|
}
|
|
|
|
// MainThread.BeginInvokeOnMainThread(() =>
|
|
// {
|
|
// Application.Current!.Windows[0].Page!.DisplayAlertAsync("", inf, "是");
|
|
// });
|
|
}
|
|
else
|
|
{
|
|
|
|
try
|
|
{
|
|
Person deserializedPerson = JsonSerializer.Deserialize<Person>(dat)!;
|
|
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(deserializedPerson.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)
|
|
{
|
|
DataReceived.Transmit("{\"Command\":\"Info\"}");
|
|
App.GlobalData.UserId = val.ToString() ?? "";
|
|
App.GlobalData.LogNo = true;
|
|
}
|
|
|
|
if (data.TryGetValue("UserData", out var value_UserData))
|
|
{
|
|
|
|
}
|
|
if (data.TryGetValue("SysData", out var value_SysData))
|
|
{
|
|
var tmp = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(value_SysData.ToString()!)!;
|
|
}
|
|
if (data.TryGetValue("Info", out val) && val != null)//机台信息接受
|
|
{
|
|
DataModel.Info_data = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!)!;
|
|
InfoPage.DyeMeasurementNumber = data.GetValue("DyeMeasurementNumber").ToString();
|
|
InfoPage.ChemicalNumber = data.GetValue("ChemicalNumber").ToString();
|
|
InfoPage.PowderNumber = data.GetValue("PowderNumber").ToString();
|
|
InfoPage.DyeDissolveNumber = data.GetValue("DyeDissolveNumber").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("InfoMachine", out val) && val != null)//
|
|
{
|
|
InfoDetail.Info_data(val.ToString()!, data.GetValue("Type").ToString()!);
|
|
}
|
|
if (data.TryGetValue("InfoMachineHistory", out val) && val != null)//
|
|
{
|
|
InfoDetail.Info_data(val.ToString()!, data.GetValue("Type").ToString()!);
|
|
}
|
|
if (data.TryGetValue("InfoMachinePipes", out val) && val != null)//
|
|
{
|
|
InfoDetail.Info_data(val.ToString()!, "00");
|
|
}
|
|
if (data.TryGetValue("Notification", out val) && val != null)//信息通知接受
|
|
{
|
|
var dataNotification = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!);
|
|
|
|
string _Title = "", _Content = "";
|
|
int _Type = 0, _id = 0;
|
|
|
|
foreach (var dataNotificationrow in dataNotification!)
|
|
{
|
|
//解析信息
|
|
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)
|
|
&& value_Notification_Title != null)
|
|
{
|
|
_Title = value_Notification_Title.ToString() ?? "";
|
|
}
|
|
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)
|
|
&& value_Notification_Type != null)
|
|
{
|
|
string temp = value_Notification_Type?.ToString() ?? "";
|
|
// 转换
|
|
_Type = temp switch
|
|
{
|
|
"System" => 2,
|
|
"Message" => 1,
|
|
_ => 0
|
|
};
|
|
}
|
|
if (_id != 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 + ");");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (data.TryGetValue("SETUSER", out val) && val != null)//信息通知接受
|
|
{
|
|
ProfilePage._data(data.GetValue("VALUE").ToString()!,data.GetValue("KEY").ToString()!);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|