|
|
using HighWayIot.Log4net;
|
|
|
using HighWayIot.TouchSocket;
|
|
|
using Org.BouncyCastle.Utilities;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Data;
|
|
|
using System.Diagnostics;
|
|
|
using System.Drawing;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Forms;
|
|
|
using System.Xml;
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
namespace RFIDSocket
|
|
|
{
|
|
|
public partial class RFIDBinAudlt : Form
|
|
|
{
|
|
|
private TcpClientServer Client = TcpClientServer.Instance;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 日志实例
|
|
|
/// </summary>
|
|
|
private static LogHelper logHelper = LogHelper.Instance;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 前端列表实例
|
|
|
/// </summary>
|
|
|
private DataTable dt = new DataTable();
|
|
|
|
|
|
/// <summary>
|
|
|
/// xml读写实例
|
|
|
/// </summary>
|
|
|
XmlDocument xd = new XmlDocument();
|
|
|
|
|
|
/// <summary>
|
|
|
/// 运行时路径
|
|
|
/// </summary>
|
|
|
private string Path = System.Environment.CurrentDirectory;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 所有适配器IP
|
|
|
/// </summary>
|
|
|
private string[] ServiceIPs;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 格口编码集合
|
|
|
/// </summary>
|
|
|
private string[] BinCodes = new string[0];
|
|
|
|
|
|
/// <summary>
|
|
|
/// 格口编码返回结果字符串
|
|
|
/// </summary>
|
|
|
private StringBuilder BinCodesString = new StringBuilder();
|
|
|
|
|
|
/// <summary>
|
|
|
/// 应有格口数
|
|
|
/// </summary>
|
|
|
private int ServiceCount = -1;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 正常格口数
|
|
|
/// </summary>
|
|
|
private int NormalCount = 0;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 异常格口数
|
|
|
/// </summary>
|
|
|
private int ErrorCount = 0;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端端口
|
|
|
/// </summary>
|
|
|
string Port = string.Empty;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端地址
|
|
|
/// </summary>
|
|
|
string IP = string.Empty;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 上次接受数据的时间
|
|
|
/// </summary>
|
|
|
DateTime LastInfoTime = DateTime.Now;
|
|
|
|
|
|
public RFIDBinAudlt()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
Init();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 初始化方法
|
|
|
/// </summary>
|
|
|
private async void Init()
|
|
|
{
|
|
|
GetSetting();
|
|
|
dataGridView.AutoGenerateColumns = false;
|
|
|
foreach (DataGridViewColumn column in dataGridView.Columns)
|
|
|
{
|
|
|
dt.Columns.Add(column.HeaderText, typeof(string));
|
|
|
}
|
|
|
|
|
|
//Client.GetBinCode += AddBinCodeString;
|
|
|
Client.GetString += ConnectMessageString;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读取XML配置文件中的IP
|
|
|
/// </summary>
|
|
|
private void GetSetting()
|
|
|
{
|
|
|
xd.Load($"{Path}\\Configuration.xml");//加载xml文档
|
|
|
XmlNode rootNode = xd.SelectSingleNode("BinAudlt");//得到xml文档的根节点
|
|
|
XmlNodeList nodes = rootNode.SelectNodes("ServiceIpConfig");//获取根节点的子节点"ServiceIpConfig"
|
|
|
ServiceIPs = new string[nodes.Count];
|
|
|
for (int i = 0; i < nodes.Count; i++)
|
|
|
{
|
|
|
ServiceIPs[i] = nodes[i].InnerText;
|
|
|
}
|
|
|
XmlNode count = rootNode.SelectSingleNode("ServiceIpCount");
|
|
|
if (!int.TryParse(count.InnerText, out ServiceCount))
|
|
|
{
|
|
|
MessageBox.Show("XML配置文件中ServiceIpConfig的值有问题,请重新配置!");
|
|
|
}
|
|
|
XmlNode ServerIp = rootNode.SelectSingleNode("ConnectIp"); // 服务器IP
|
|
|
XmlNode ServerPort = rootNode.SelectSingleNode("ConnectPort"); // 服务器端口
|
|
|
IP = ServerIp.InnerText.Trim();
|
|
|
Port = ServerPort.InnerText.Trim();
|
|
|
IPText.Text = IP;
|
|
|
PortText.Text = Port;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 开始盘点
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private void AudltButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
BinCodes = new string[0];
|
|
|
BinCodesString.Clear();
|
|
|
|
|
|
MultiFuncLoading loadingfrm = new MultiFuncLoading(this);
|
|
|
SplashScreenManager loading = new SplashScreenManager(loadingfrm);
|
|
|
loading.ShowLoading();
|
|
|
loadingfrm.SetTxt("格口盘点中", $"共有{ServiceIPs.Length}个IP需要扫描", "Please Waitting...");
|
|
|
|
|
|
try
|
|
|
{
|
|
|
Task.Run(async () =>
|
|
|
{
|
|
|
int count = 0;
|
|
|
foreach (string s in ServiceIPs)
|
|
|
{
|
|
|
count++;
|
|
|
Client.Send($"connect {s}").GetAwaiter().GetResult();
|
|
|
loadingfrm.SetJD($"正在盘点{s} {count}/{ServiceIPs.Length}", $"正在盘点{s} {count}/{ServiceIPs.Length}");
|
|
|
do
|
|
|
{
|
|
|
await Task.Delay(500);
|
|
|
}
|
|
|
while (DateTime.Now - LastInfoTime <= TimeSpan.FromMilliseconds(3000));
|
|
|
}
|
|
|
}).GetAwaiter().GetResult();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show("加载发生错误,请检查连接情况" + ex);
|
|
|
return;
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
loading.CloseWaitForm();
|
|
|
}
|
|
|
string sss = BinCodesString.ToString();
|
|
|
|
|
|
logHelper.Info($"此次盘点的字符串为{sss}");
|
|
|
|
|
|
BinCodes = ClientStringAnalysis.GetInfoAnalyzer(sss);
|
|
|
|
|
|
int[] intArray = BinCodes
|
|
|
.Select(a => int.Parse(a)) // 转换为整型
|
|
|
.Distinct() // 去重
|
|
|
.OrderBy(x => x) // 排序
|
|
|
.ToArray(); // 转换为数组
|
|
|
|
|
|
NormalCount = 0;
|
|
|
ErrorCount = 0;
|
|
|
|
|
|
GridViewRefresh(ConvertToBoolArray(intArray, ServiceCount));
|
|
|
|
|
|
NormalCountLabel.Text = NormalCount.ToString();
|
|
|
ErrorCountLabel.Text = ErrorCount.ToString();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 字符串连接委托类
|
|
|
/// </summary>
|
|
|
/// <param name="message"></param>
|
|
|
private void ConnectMessageString(string message)
|
|
|
{
|
|
|
LastInfoTime = DateTime.Now;
|
|
|
BinCodesString.Append(message);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取BinCode委托方法
|
|
|
/// </summary>
|
|
|
private void AddBinCodeString(string[] binCodes)
|
|
|
{
|
|
|
///拼接BinCode
|
|
|
Task.Run(() =>
|
|
|
{
|
|
|
string[] temp = new string[binCodes.Length + BinCodes.Length];
|
|
|
Array.Copy(BinCodes, 0, temp, 0, BinCodes.Length);
|
|
|
Array.Copy(binCodes, 0, temp, BinCodes.Length, binCodes.Length);
|
|
|
BinCodes = temp;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// int数组转化为bool数组
|
|
|
/// </summary>
|
|
|
/// <param name="intArray"></param>
|
|
|
/// <param name="count"></param>
|
|
|
/// <returns></returns>
|
|
|
private bool[] ConvertToBoolArray(int[] intArray, int count)
|
|
|
{
|
|
|
// 初始化一个长度为count的bool数组,默认为false
|
|
|
bool[] result = new bool[count];
|
|
|
|
|
|
// 遍历intArray,将对应位置的值设置为true
|
|
|
foreach (int num in intArray)
|
|
|
{
|
|
|
if (num >= 1 && num <= count) // 确保num在合法范围内
|
|
|
{
|
|
|
result[num - 1] = true; // 将下标为num-1的值设置为true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 表格刷新
|
|
|
/// </summary>
|
|
|
/// <param name="states">bool状态列表</param>
|
|
|
private void GridViewRefresh(bool[] states)
|
|
|
{
|
|
|
if (ServiceCount < 0)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
//统计多少行
|
|
|
int lineCount = ServiceCount / 20;
|
|
|
//最后一行多少个数据
|
|
|
int lastCount = ServiceCount % 20;
|
|
|
if (lastCount > 0)
|
|
|
{
|
|
|
//最后不完整行
|
|
|
lineCount++;
|
|
|
}
|
|
|
//格口个数
|
|
|
int totalCount = 0;
|
|
|
dt.Rows.Clear();
|
|
|
//添加格口编号 添加状态
|
|
|
for (int i = 0; i < lineCount; i++)
|
|
|
{
|
|
|
//新建dt行
|
|
|
DataRow numdr = dt.NewRow();
|
|
|
DataRow statedr = dt.NewRow();
|
|
|
for (int j = 0; j < 20; j++)
|
|
|
{
|
|
|
if (totalCount >= ServiceCount)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
statedr[j] = states[totalCount] ? "正常" : "异常";
|
|
|
//计数
|
|
|
if (states[totalCount])
|
|
|
{
|
|
|
NormalCount++;
|
|
|
}
|
|
|
else if (!states[totalCount])
|
|
|
{
|
|
|
ErrorCount++;
|
|
|
}
|
|
|
numdr[j] = (++totalCount).ToString();
|
|
|
}
|
|
|
dt.Rows.Add(numdr);
|
|
|
dt.Rows.Add(statedr);
|
|
|
}
|
|
|
|
|
|
//添加DT表
|
|
|
dataGridView.DataSource = null;
|
|
|
dataGridView.DataSource = dt;
|
|
|
|
|
|
//设置背景颜色
|
|
|
for (int i = 0; i < lineCount * 2; i++)
|
|
|
{
|
|
|
if (i % 2 == 0)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
for (int j = 0; j < 20; j++)
|
|
|
{
|
|
|
if (int.TryParse(dataGridView.Rows[i - 1].Cells[j].Value.ToString(), out int head))
|
|
|
{
|
|
|
DataGridViewCell cell = dataGridView.Rows[i].Cells[j];
|
|
|
cell.Style.BackColor = states[head - 1] ? Color.Green : Color.Red;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 随机布尔数组生成
|
|
|
/// </summary>
|
|
|
/// <param name="length"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool[] GenerateRandomBoolArray(int length)
|
|
|
{
|
|
|
bool[] boolArray = new bool[length];
|
|
|
Random random = new Random();
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
{
|
|
|
boolArray[i] = random.Next(2) == 1;
|
|
|
}
|
|
|
|
|
|
return boolArray;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 连接开关
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private void MonitorOnOff_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
if (!Client.ClientState)
|
|
|
{
|
|
|
if (Client.ClientConnect(IP, Port).GetAwaiter().GetResult())
|
|
|
{
|
|
|
MonitorOnOff.Text = "断开客户端";
|
|
|
MonitorState.Text = "开";
|
|
|
MonitorState.BackColor = Color.Green;
|
|
|
MessageBox.Show("服务端连接成功!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("服务端连接失败!");
|
|
|
}
|
|
|
}
|
|
|
else if (Client.ClientState)
|
|
|
{
|
|
|
if (Client.ClientClose().GetAwaiter().GetResult())
|
|
|
{
|
|
|
MonitorOnOff.Text = "连接客户端";
|
|
|
MonitorState.Text = "关";
|
|
|
MonitorState.BackColor = Color.Transparent;
|
|
|
MessageBox.Show("服务端断开成功!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("服务端断开失败!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 设置服务端地址
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private void SetAddress_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
IP = IPText.Text.Trim();
|
|
|
Port = PortText.Text.Trim();
|
|
|
//ip保存
|
|
|
XmlNode rootNode = xd.SelectSingleNode("BinAudlt");//得到xml文档的根节点
|
|
|
XmlNode ServerIp = rootNode.SelectSingleNode("ConnectIp"); // 服务器IP
|
|
|
XmlNode ServerPort = rootNode.SelectSingleNode("ConnectPort"); // 服务器端口
|
|
|
ServerIp.InnerText = IP;
|
|
|
ServerPort.InnerText = Port;
|
|
|
xd.Save($"{Path}\\Configuration.xml");
|
|
|
MessageBox.Show("保存成功!");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show("服务端地址 XML文件保存失败!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 关闭窗口事件
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private void RFIDBinAudlt_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
{
|
|
|
if (Client.ClientState)
|
|
|
{
|
|
|
if (!Client.ClientClose().GetAwaiter().GetResult())
|
|
|
{
|
|
|
MessageBox.Show("服务端断开失败!请检查原因后重新关闭或重试");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|