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.

437 lines
12 KiB
C#

using DNSD_DB;
using NDSD_Screwdriver.Entity;
using NDSD_Screwdriver.Tool;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace NDSD_Screwdriver
{
public partial class MainForm : Form
{
/// <summary>
/// 输出端口操作
/// </summary>
private DOperate DOperate;
/// <summary>
/// TCP服务器
/// </summary>
private TcpServer server = TcpServer.Instance;
/// <summary>
/// 前端列表集合
/// </summary>
private List<MonitorEntity> monitorEntities;
private List<RfidSetting> rfidSettings;
private Random rand = new Random();
///// <summary>
///// 判断是否是第一次初始化
///// </summary>
//private int flag = 0;
/// <summary>
/// 判断现在操作哪一行
/// </summary>
public int NowRowIndex = 0;
/// <summary>
/// 准备工作的行号
/// </summary>
public int ReadyRowIndex = 0;
/// <summary>
/// RFID列表
/// </summary>
public string[] RFIDs;
public List<LightsEntity> lightsEntities;
Thread thread;
bool flag = false;
SerialPortFactory serialPort;
private CancellationTokenSource cancellationTokenSource;
private int textindex = 0;
public MainForm()
{
InitializeComponent();
DOperate = new DOperate();
if (!server.ServerOpen(MemorySetting.ServerIP, MemorySetting.ServerPort))
{
MessageBox.Show("服务端打开失败!");
}
FlashThread();
serialPort = new SerialPortFactory();
StartLongRunningTask();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (server != null)
{
if (!server.ServerStop())
{
MessageBox.Show("服务端关闭失败!");
}
if (!server.ServerDispose())
{
MessageBox.Show("服务端释放失败!");
}
}
}
#region 按钮
/// <summary>
/// 打开DO测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DOTest_Click(object sender, EventArgs e)
{
//string str = serialPort.Read();
ScrewdriverTest screwdriverTest = new ScrewdriverTest(server, DOperate);
screwdriverTest.Show();
}
/// <summary>
/// 测试按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TestButton1_Click(object sender, EventArgs e)
{
if (RFIDs.Length == 0)
{
MessageBox.Show("没有值,请先设置。");
}
SetNowWorkRow(RFIDs[textindex]);
SetNowRowsLightState(1);
if (flag && thread.ThreadState == ThreadState.Suspended)
{
thread.Resume();
}
RefreshRoll();
if (++textindex >= RFIDs.Length)
{
textindex = 0;
}
}
/// <summary>
/// 初始化按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InitButton_Click(object sender, EventArgs e)
{
NowRowIndex = 0;
//读取rfid数据
var ctx = SqlLiteTool.GetDb(AppTool.GetDb());
var list = ctx.Query<RfidSetting>().Where(x => x.IsEnable == true).ToList();
RFIDs = list.Select(x => x.RfidNo).ToArray();
//读取报警灯数据
lightsEntities = new List<LightsEntity>();
for (int i = 0; i < list.Count; i++)
{
lightsEntities.Add(new LightsEntity()
{
Green = Convert.ToInt32(list[i].Green),
Yellow = Convert.ToInt32(list[i].Yellow),
Red = Convert.ToInt32(list[i].Red),
});
}
//初始化列表框架
monitorEntities = new List<MonitorEntity>();
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities.Add(new MonitorEntity()
{
RowIndex = i,
RowEntitys = InitRows(i)
});
}
if (flag)
{
thread.Suspend();
}
//更新值
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities[i].RowEntitys[1].Value = RFIDs[i];
}
RefreshRoll();
}
/// <summary>
/// 打开设置界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SettingButton_Click(object sender, EventArgs e)
{
FrmSetting frmSetting = new FrmSetting();
frmSetting.Show();
}
/// <summary>
/// 服务端重启按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServerRestartButton_Click(object sender, EventArgs e)
{
if (server.ServerRestart(MemorySetting.ServerIP, MemorySetting.ServerPort))
{
MessageBox.Show("服务器重启成功!");
}
else
{
MessageBox.Show("服务器重启失败!请手动启动服务器。");
}
}
/// <summary>
/// 服务端手动启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartServerButton_Click(object sender, EventArgs e)
{
if (!server.ServerOpen(MemorySetting.ServerIP, MemorySetting.ServerPort))
{
MessageBox.Show("服务端打开失败!");
}
else
{
MessageBox.Show("服务器启动成功");
}
}
/// <summary>
/// 服务端手动停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ServerStopButton_Click(object sender, EventArgs e)
{
if (server != null)
{
if (!server.ServerStop())
{
MessageBox.Show("服务端关闭失败!");
}
if (!server.ServerDispose())
{
MessageBox.Show("服务端释放失败!");
}
}
}
#endregion
#region 功能性方法
/// <summary>
/// 初始化一行
/// </summary>
/// <param name="rowNo">当前的初始化行数</param>
/// <returns></returns>
private List<RowEntity> InitRows(int rowNo)
{
List<RowEntity> rowEntities = new List<RowEntity>();
for (int i = 0; i < 3; i++)
{
rowEntities.Add(new RowEntity()
{
ColumnIndex = i,
});
}
return rowEntities;
}
/// <summary>
/// 刷新列表
/// </summary>
private void RefreshRoll()
{
for (int i = 0; i < RFIDs.Length; i++)
{
ScrewdriverMonitor.AddRowTop(new string[]{
"",
"",
""
});
}
}
/// <summary>
/// 设置当前行指示灯的状态以及工作的行的标识 输入枚举类
/// </summary>
/// <param name="lightState"></param>
private void SetNowRowsLightState(LightState lightState)
{
SetNowRowsLightState(FormUtils.EnumColorToBrush(lightState));
}
private void SetNowRowsLightState(int lightState)
{
SetNowRowsLightState((LightState)lightState);
}
/// <summary>
/// 设置当前行指示灯的状态以及工作的行的标识 输入Brush
/// </summary>
/// <param name="brush"></param>
private void SetNowRowsLightState(Brush brush)
{
if (monitorEntities.Count == 0)
{
return;
}
monitorEntities[NowRowIndex].RowEntitys[2].Color = brush;
SetNextRowsFlash();
for (int i = 0; i < RFIDs.Length; i++)
{
monitorEntities[i].RowEntitys[0].Value = i == NowRowIndex ? "=>" : "";
}
}
/// <summary>
/// 设置待工作灯闪烁
/// </summary>
private void SetNextRowsFlash()
{
ReadyRowIndex = NowRowIndex + 1;
if (ReadyRowIndex >= RFIDs.Length)
{
ReadyRowIndex = 0;
thread.Suspend();
return;
}
if (!flag)
{
thread.Start();
flag = true;
}
}
/// <summary>
/// 根据读取到的RFID条码设置当前工作的行
/// </summary>
/// <param name="rfid">读取到的RFID</param>
private void SetNowWorkRow(string rfid)
{
for (int i = 0; i < RFIDs.Length; i++)
{
if (monitorEntities[i].RowEntitys[1].Value == rfid)
{
NowRowIndex = i;
return;
}
}
}
/// <summary>
/// 根据IO口控制灯的开关
/// </summary>
private void LightControl()
{
}
/// <summary>
/// 列表回调函数
/// </summary>
/// <param name="g"></param>
/// <param name="rowIndex"></param>
/// <param name="colIndex"></param>
/// <param name="rectangle"></param>
/// <param name="value"></param>
/// <param name="sf"></param>
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);
}
}
}
}
}
/// <summary>
/// RFID刷新任务
/// </summary>
private void StartLongRunningTask()
{
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
// 这里是你的长期运行逻辑
Thread.Sleep(1000); // 模拟一些长时间的工作
}
}, cancellationTokenSource.Token);
}
/// <summary>
/// 等待工作指示灯闪烁进程
/// </summary>
private void FlashThread()
{
//等待闪烁线程
thread = new Thread(() =>
{
while (true)
{
DOperate.DTimeOpen(1, 1);
monitorEntities[ReadyRowIndex].RowEntitys[2].Color = Brushes.Yellow;
RefreshRoll();
Thread.Sleep(1000);
monitorEntities[ReadyRowIndex].RowEntitys[2].Color = Brushes.Transparent;
RefreshRoll();
Thread.Sleep(1000);
}
});
}
#endregion
}
}