You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
7.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HighWayIot.TouchSocket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using TouchSocket.Sockets;
namespace RFIDSocket
{
public partial class RFIDBinAudlt : Form
{
private TcpClientServer TCPClient = TcpClientServer.Instance;
private DataTable dt = new DataTable();
private string Path = System.Environment.CurrentDirectory;
private string[] ServiceIPs;
private int ServiceCount = -1;
private int NormalCount = 0;
private int ErrorCount = 0;
string Port = string.Empty;
string IP = string.Empty;
public RFIDBinAudlt()
{
InitializeComponent();
Init();
}
private void Init()
{
GetSetting();
dataGridView.AutoGenerateColumns = false;
foreach (DataGridViewColumn column in dataGridView.Columns)
{
dt.Columns.Add(column.HeaderText, typeof(string));
}
}
/// <summary>
/// 读取XML配置文件中的IP
/// </summary>
private void GetSetting()
{
XmlDocument xd = new XmlDocument();
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;
Port = ServerPort.InnerText;
}
/// <summary>
/// 开始盘点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AudltButton_Click(object sender, EventArgs e)
{
NormalCount = 0;
ErrorCount = 0;
GridViewRefresh(GenerateRandomBoolArray(ServiceCount));
NormalCountLabel.Text = NormalCount.ToString();
ErrorCountLabel.Text = ErrorCount.ToString();
}
/// <summary>
/// 表格刷新
/// </summary>
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 (!TCPClient.ClientState)
{
if (TCPClient.ClientConnect(IP, Port).GetAwaiter().GetResult())
{
MessageBox.Show("服务端连接成功!");
}
else
{
MessageBox.Show("服务端连接失败!");
}
}
else if (TCPClient.ClientState)
{
if (TCPClient.ClientClose())
{
MessageBox.Show("服务端断开成功!");
}
else
{
MessageBox.Show("服务端断开失败!");
}
}
}
/// <summary>
/// 设置服务端地址
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetAddress_Click(object sender, EventArgs e)
{
}
#region 鼠标拖拽
private bool isDragging = false; // 用于检测是否正在拖动
private Point lastLocation; // 保存上次鼠标位置
private void RFIDBinAudlt_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastLocation = e.Location; // 记录鼠标按下时的位置
}
}
/// <summary>
/// 全局拖拽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RFIDBinAudlt_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Top = this.Top + (e.Y - lastLocation.Y);
this.Left = this.Left + (e.X - lastLocation.X);
}
}
private void RFIDBinAudlt_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
#endregion
}
}