using DNSD_DB;
using NDSD_Screwdriver.Entity;
using NDSD_TouchSocket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NDSD_Screwdriver
{
public partial class MainForm : Form
{
///
/// 输出端口操作
///
private DOperate DOperate;
///
/// TCP服务器
///
private TcpServer server = TcpServer.Instance;
///
/// 前端列表集合
///
private List monitorEntities;
private Random rand = new Random();
/////
///// 判断是否是第一次初始化
/////
//private int flag = 0;
///
/// 判断现在操作哪一行
///
public int NowRowIndex = 0;
///
/// RFID列表
///
public string[] RFIDs = new string[]
{
"1111222233334444",
"2222222233334444",
"3333222233334444",
"4444222233334444",
"5555222233334444"
};
public MainForm()
{
InitializeComponent();
DOperate = new DOperate();
if (!server.ServerOpen("192.168.0.101", "6001"))
{
MessageBox.Show("服务端打开失败!");
}
}
///
/// 打开DO测试
///
///
///
private void DOTest_Click(object sender, EventArgs e)
{
ScrewdriverTest screwdriverTest = new ScrewdriverTest(server, DOperate);
screwdriverTest.Show();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (server != null)
{
if (!server.ServerStop())
{
MessageBox.Show("服务端关闭失败!");
}
if (!server.ServerDispose())
{
MessageBox.Show("服务端释放失败!");
}
}
}
///
/// 列表回调函数
///
///
///
///
///
///
///
private void ScrewdriverMonitor_OnDrawCellTextEvent(Graphics g, int rowIndex, int colIndex, RectangleF rectangle, string value, StringFormat sf)
{
foreach (MonitorEntity entity in monitorEntities)
{
if (rowIndex == entity.RowIndex)
{
foreach (RowEntity rowEneiey in entity.RowEntitys)
{
if (colIndex == rowEneiey.ColumnIndex)
{
g.FillRectangle(rowEneiey.Color, rectangle);
g.DrawString(rowEneiey.Value, this.ScrewdriverMonitor.Font, Brushes.Black, rectangle, sf);
}
}
}
}
}
///
/// 测试按钮
///
///
///
private void TestButton1_Click(object sender, EventArgs e)
{
if(RFIDs.Length == 0)
{
MessageBox.Show("没有值,请先设置。");
}
SetNowRowsLightState(FormUtils.TestRamColor());
RefreshRoll();
if (++NowRowIndex >= RFIDs.Length)
{
NowRowIndex = 0;
}
}
private void InitButton_Click(object sender, EventArgs e)
{
NowRowIndex = 0;
InitMonitirIntity();
SetRFIDValue(RFIDs);
}
///
/// 刷新列表
///
private void RefreshRoll()
{
for (int i = 0; i < RFIDs.Length; i++)
{
ScrewdriverMonitor.AddRowTop(new string[]{
"",
"",
""
});
}
}
///
/// 初始化整个列表
///
private void InitMonitirIntity()
{
monitorEntities = new List();
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities.Add(new MonitorEntity()
{
RowIndex = i,
RowEntitys = InitRows(i)
});
}
RefreshRoll();
}
///
/// 初始化一行
///
/// 当前的初始化行数
///
private List InitRows(int rowNo)
{
List rowEntities = new List();
for (int i = 0; i < 3; i++)
{
rowEntities.Add(new RowEntity()
{
ColumnIndex = i,
});
}
return rowEntities;
}
///
/// 初始化RFID数据
///
/// RFID列表
private void SetRFIDValue(string[] rFIDvalue)
{
for (int i = 0; i < rFIDvalue.Length; i++)
{
monitorEntities[i].RowEntitys[1].Value = rFIDvalue[i];
}
}
///
/// 设置当前行指示灯的状态以及工作的行
///
///
private void SetNowRowsLightState(LightState lightState)
{
monitorEntities[NowRowIndex].RowEntitys[2].Color = FormUtils.EnumColorToBrush(lightState);
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities[NowRowIndex].RowEntitys[0].Value = i == NowRowIndex ? "=>" : "";
}
}
private void SetNowRowsLightState(Brush brush)
{
monitorEntities[NowRowIndex].RowEntitys[2].Color = brush;
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities[i].RowEntitys[0].Value = i == NowRowIndex ? "=>" : "";
}
}
}
}