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