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;
namespace RFIDSocket
{
public partial class RFIDBinAudlt : Form
{
TcpClientServer TCPClient = TcpClientServer.Instance;
DataTable dt = new DataTable();
string Path = System.Environment.CurrentDirectory;
string[] ServiceIPs;
int ServiceCount = -1;
int NormalCount = 0;
int ErrorCount = 0;
public RFIDBinAudlt()
{
InitializeComponent();
Init();
}
private void Init()
{
GetSetting();
dataGridView.AutoGenerateColumns = false;
foreach (DataGridViewColumn column in dataGridView.Columns)
{
dt.Columns.Add(column.HeaderText, typeof(string));
}
}
///
/// 读取XML配置文件中的IP
///
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的值有问题,请重新配置!");
}
}
///
/// 开始盘点
///
///
///
private void AudltButton_Click(object sender, EventArgs e)
{
//bool res = TCPClient.ClientConnect("127.0.0.1", "7789").GetAwaiter().GetResult();
//if (res)
//{
// MessageBox.Show("我操牛逼");
//}
NormalCount = 0;
ErrorCount = 0;
GridViewRefresh(GenerateRandomBoolArray(ServiceCount));
NormalCountLabel.Text = NormalCount.ToString();
ErrorCountLabel.Text = ErrorCount.ToString();
}
///
/// 表格刷新
///
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;
}
}
}
}
///
/// 随机布尔数组生成
///
///
///
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;
}
}
}