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.

803 lines
25 KiB
C#

1 month ago
using Chloe;
using DNSD_DB;
using DNSD_DB.Entity;
using NDSD_Screwdriver.Tool;
using NDSD_TouchSocket;
using Newtonsoft.Json.Linq;
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;
1 month ago
using System.Timers;
1 month ago
using System.Windows.Forms;
using System.Xml.Serialization;
using NewLife.Caching;
1 month ago
namespace NDSD_Screwdriver
{
public partial class MainForm : Form
{
/// <summary>
/// 输出端口操作
/// </summary>
private DOperate DOperateInfo;
1 month ago
/// <summary>
/// TCP服务器
/// </summary>
private TcpServer server = TcpServer.Instance;
/// <summary>
/// 前端列表集合
/// </summary>
private List<MonitorEntity> monitorEntities;
private List<RFIDConfigEntity> RFIDConfigEntitys;
private Random rand = new Random();
/// <summary>
/// RFID状态列表
/// </summary>
public List<RFIDStatesEntity> RFIDStatesEntities;
/// <summary>
/// 报警灯配置实例
/// </summary>
public List<LightsEntity> lightsEntities;
/// <summary>
/// 日志实例
/// </summary>
public List<RFIDLogsEntity> RFIDLogsEntitys;
/// <summary>
/// 日志数据库操作实例
/// </summary>
public IDbContext LogContext;
1 month ago
// Thread flashThread;
1 month ago
SerialPortFactory serialPort;
private CancellationTokenSource cancellationTokenSource;
private CancellationTokenSource sendCancellationTokenSource;
1 month ago
public int textRFID = 0;
private Queue<DOperateEntity> _methodQueue;
private ICache cache = Cache.Default;
1 month ago
int FlashIndex = 0;
bool _flashflag = true;
1 month ago
public MainForm()
{
1 month ago
1 month ago
InitializeComponent();
_methodQueue = new Queue<DOperateEntity>();
1 month ago
FlashThreadTime();
1 month ago
var memorySetting = MemorySetting.Current;
1 month ago
SqlLiteTool.CreateTable(memorySetting.DB);
LogContext = SqlLiteTool.GetDb(memorySetting.DB);
1 month ago
RFIDLogsEntitys = LogContext.Query<RFIDLogsEntity>().OrderByDesc(a => a.CreateTime).Take(200).ToList();
LogDataGridView.AutoGenerateColumns = false;
LogDataGridView.DataSource = null;
LogDataGridView.DataSource = RFIDLogsEntitys;
DOperateInfo = new DOperate();
1 month ago
if (!server.ServerOpen(memorySetting.ServerIP, memorySetting.ServerPort))
1 month ago
{
MessageBox.Show("服务端打开失败!");
FormUtils.LogInsert(LogContext, "服务端打开失败");
}
1 month ago
try
{
1 month ago
serialPort = new SerialPortFactory(memorySetting.Com);
1 month ago
StartLongRunningTask();
SendRunningTask();
1 month ago
}
catch (Exception e)
{
MessageBox.Show(e.Message);
FormUtils.LogInsert(LogContext, e.Message);
}
1 month ago
}
1 month ago
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, "服务端释放失败");
}
}
1 month ago
if (sendCancellationTokenSource != null)
{
sendCancellationTokenSource.Cancel();
}
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
}
1 month ago
timer.Close();
timer.Dispose();
1 month ago
}
#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, DOperateInfo);
1 month ago
screwdriverTest.Show();
}
/// <summary>
/// 初始化按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InitButton_Click(object sender, EventArgs e)
{
InitTable();
}
/// <summary>
/// 初始化Table
/// </summary>
private void InitTable()
{
_flashflag = true;
1 month ago
FlashIndex = 0;
textRFID = 0;
1 month ago
timer.Start();
1 month ago
var memorySetting = MemorySetting.Current;
1 month ago
//读取rfid数据
1 month ago
var ctx = SqlLiteTool.GetDb(memorySetting.DB);
1 month ago
var list = ctx.Query<RFIDConfigEntity>().Where(x => x.IsEnable == true).ToList();
var strs = list.Select(x => x.RfidNo).ToArray();
RFIDStatesEntities = new List<RFIDStatesEntity>();
RFIDStatesEntities.Clear();
foreach (var a in strs)
{
RFIDStatesEntities.Add(new RFIDStatesEntity()
{
RFIDValue = a,
});
}
RFIDStatesEntities[0].RFIDState = 2;
FormUtils.LogInsert(LogContext, "RFID " + 0 + " 准备工作", RFIDStatesEntities[0].RFIDValue);
GetLog();
//读取报警灯数据
lightsEntities = new List<LightsEntity>();
lightsEntities.Clear();
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>();
monitorEntities.Clear();
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
monitorEntities.Add(new MonitorEntity()
{
RowIndex = i,
RowEntitys = InitRows(i)
});
}
//更新值
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
monitorEntities[i].RowEntitys[1].Value = RFIDStatesEntities[i].RFIDValue;
}
1 month ago
timer.Start();
1 month ago
RefreshRoll();
RefreshIO();
}
/// <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)
{
1 month ago
var memorySetting = MemorySetting.Current;
if (server.ServerRestart(memorySetting.ServerIP, memorySetting.ServerPort))
1 month ago
{
MessageBox.Show("服务器重启成功!");
FormUtils.LogInsert(LogContext, "服务器重启成功");
}
else
{
MessageBox.Show("服务器重启失败!请手动启动服务器。");
FormUtils.LogInsert(LogContext, "服务器重启失败");
}
}
/// <summary>
/// 服务端手动启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartServerButton_Click(object sender, EventArgs e)
{
1 month ago
var memorySetting = MemorySetting.Current;
if (!server.ServerOpen(memorySetting.ServerIP, memorySetting.ServerPort))
1 month ago
{
MessageBox.Show("服务端打开失败!");
FormUtils.LogInsert(LogContext, "服务端打开失败");
}
else
{
MessageBox.Show("服务器启动成功");
FormUtils.LogInsert(LogContext, "服务器启动成功");
}
}
/// <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("服务端关闭失败!");
FormUtils.LogInsert(LogContext, "服务端关闭失败");
}
if (!server.ServerDispose())
{
MessageBox.Show("服务端释放失败!");
FormUtils.LogInsert(LogContext, "服务端释放失败");
}
}
}
/// <summary>
/// 测试按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TestButton1_Click(object sender, EventArgs e)
{
if (RFIDStatesEntities.Count == 0)
{
MessageBox.Show("请先初始化RFID列表");
}
else if (textRFID == RFIDStatesEntities.Count)
{
SetNowWorkRow("0000000");
textRFID = 0;
return;
}
SetNowWorkRow(RFIDStatesEntities[textRFID].RFIDValue);
if (++textRFID > RFIDStatesEntities.Count)
{
textRFID = 0;
return;
}
}
#endregion
#region 功能性方法
/// <summary>
/// 根据读取到的RFID条码设置当前工作的行 并且更新state
/// </summary>
/// <param name="rfid">读取到的RFID</param>
private void SetNowWorkRow(string rfid)
{
1 month ago
var memorySetting = MemorySetting.Current;
1 month ago
//设置RFID状态
int flag = 0;
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDValue == rfid && RFIDStatesEntities[i].RFIDState == 2)
{
RFIDStatesEntities[i].RFIDState = 1;
FormUtils.LogInsert(LogContext, "RFID " + i + " 开始工作", RFIDStatesEntities[i].RFIDValue);
GetLog();
if (i - 1 >= 0)
{
string rfidValue = RFIDStatesEntities[i - 1].RFIDValue;
1 month ago
var leastTime = RFIDLogsEntitys.First(x => x.RFIDId == rfidValue).CreateTime;
1 month ago
double timeSpan = (DateTime.Now - leastTime).TotalSeconds;
1 month ago
if (timeSpan >= memorySetting.AlarmTimeValue)
1 month ago
{
RFIDStatesEntities[i - 1].RFIDState = 4;
FormUtils.LogInsert(LogContext, "RFID " + i + " 超时结束", RFIDStatesEntities[i].RFIDValue);
}
1 month ago
if (timeSpan < memorySetting.AlarmTimeValue)
1 month ago
{
RFIDStatesEntities[i - 1].RFIDState = 3;
FormUtils.LogInsert(LogContext, "RFID " + (i - 1) + " 成功结束", RFIDStatesEntities[i].RFIDValue);
}
GetLog();
}
if (i + 1 < RFIDStatesEntities.Count)
{
FormUtils.LogInsert(LogContext, "RFID " + (i + 1) + " 准备工作", RFIDStatesEntities[i].RFIDValue);
GetLog();
RFIDStatesEntities[i + 1].RFIDState = 2;
FlashIndex = i + 1;
}
else
{
1 month ago
timer.Stop();
_flashflag = false;
1 month ago
}
flag = 1;
break;
}
}
if (flag == 0) //列表里没有已读的RFID
{
1 month ago
1 month ago
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDState == 1)
{
string rfidValue = RFIDStatesEntities[i - 1].RFIDValue;
1 month ago
var leastTime = RFIDLogsEntitys.First(x => x.RFIDId == rfidValue).CreateTime;
1 month ago
double timeSpan = (DateTime.Now - leastTime).TotalSeconds;
1 month ago
if (timeSpan >= memorySetting.AlarmTimeValue)
1 month ago
{
RFIDStatesEntities[i].RFIDState = 4;
FormUtils.LogInsert(LogContext, "RFID " + i + " 超时结束", RFIDStatesEntities[i].RFIDValue);
}
1 month ago
if (timeSpan < memorySetting.AlarmTimeValue)
1 month ago
{
RFIDStatesEntities[i].RFIDState = 3;
FormUtils.LogInsert(LogContext, "RFID " + (i - 1) + " 成功结束", RFIDStatesEntities[i].RFIDValue);
}
}
}
}
//刷新前端灯的状态
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDState == 1)
{
SetRowsLightState(i, 1);
}
else if (RFIDStatesEntities[i].RFIDState == 3)
{
SetRowsLightState(i, 0);
}
else if (RFIDStatesEntities[i].RFIDState == 4)
{
SetRowsLightState(i, 2);
}
else
{
SetRowsLightState(i, Brushes.Transparent);
}
}
if(_flashflag)
timer.Stop();
Thread.Sleep(500);
1 month ago
//刷新IO的状态
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDState == 1)
{
// DOperateInfo.DOpen(lightsEntities[i].Yellow);
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Yellow,
delay = 0
});
1 month ago
}
else if (RFIDStatesEntities[i].RFIDState == 3)
{
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Green,
delay = 0
});
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Yellow,
delay = -1
});
1 month ago
}
else if (RFIDStatesEntities[i].RFIDState == 4)
{
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Red,
delay = 0
});
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Yellow,
delay = -1
});
1 month ago
}
}
if (_flashflag)
timer.Start();
1 month ago
RefreshRoll();
}
/// <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()
{
List<string[]> values = new List<string[]>();
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
values.Add(new string[]
{
"", "", ""
});
}
ScrewdriverMonitor.SetTableValue(values);
}
private void SetRowsLightState(int index, LightState lightState)
{
SetRowsLightState(index, FormUtils.EnumColorToBrush(lightState));
}
private void SetRowsLightState(int index, int lightState)
{
SetRowsLightState(index, (LightState)lightState);
}
/// <summary>
/// 设置指定行指示灯的状态以及工作的行的标识
/// </summary>
/// <param name="brush"></param>
private void SetRowsLightState(int index, Brush brush)
{
monitorEntities[index].RowEntitys[2].Color = brush;
//工作中箭头指示
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
monitorEntities[i].RowEntitys[0].Value = RFIDStatesEntities[i].RFIDState == 1 ? "=>" : "";
}
}
1 month ago
/// <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)
{
//SetNowWorkRow("0000000");
// 这里是你的长期运行逻辑
Thread.Sleep(1000); // 模拟一些长时间的工作
}
}, cancellationTokenSource.Token);
}
/// <summary>
/// RFID刷新任务
/// </summary>
private void SendRunningTask()
{
sendCancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
while (!sendCancellationTokenSource.Token.IsCancellationRequested)
{
if (!cache.ContainsKey("Test"))
{
while (_methodQueue.Count > 0)
{
var q = _methodQueue.Dequeue();
// -1 DClose > 0 DTimeOpen =0 Dopen
if (q.delay == -1)
{
DOperateInfo.DClose(q.port);
}
if (q.delay == 0)
{
DOperateInfo.DOpen(q.port);
}
if (q.delay > 0)
{
DOperateInfo.DTimeOpen(q.port, q.delay);
}
Thread.Sleep(210); // 模拟一些长时间的工作
}
}
else
{
_methodQueue.Clear();
}
Thread.Sleep(100); // 模拟一些长时间的工作
}
}, cancellationTokenSource.Token);
}
1 month ago
///// <summary>
///// 等待工作指示灯闪烁进程
///// </summary>
//private void FlashThread()
//{
// //等待闪烁线程
// flashThread = new Thread(() =>
// {
// while (true)
// {
// DOperate.DTimeOpen(lightsEntities[FlashIndex].Yellow, 1);
// monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Yellow;
// RefreshRoll();
// Thread.Sleep(1000);
// monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Transparent;
// RefreshRoll();
// Thread.Sleep(1000);
// }
// });
//}
System.Timers.Timer timer = new System.Timers.Timer(1000); // 设置1秒间隔
1 month ago
/// <summary>
/// 等待工作指示灯闪烁进程
/// </summary>
1 month ago
private void FlashThreadTime()
1 month ago
{
1 month ago
int step = 0;
timer.Elapsed += (sender, e) =>
1 month ago
{
1 month ago
if (step == 0)
1 month ago
{
1 month ago
step = 1;
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[FlashIndex].Yellow,
delay = 1
});
1 month ago
monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Yellow;
1 month ago
}
else if (step == 1)
{
step=0;
1 month ago
monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Transparent;
1 month ago
}
1 month ago
RefreshRoll();
};
////等待闪烁线程
//flashThread = new Thread(() =>
//{
// while (true)
// {
// DOperate.DTimeOpen(lightsEntities[FlashIndex].Yellow, 1);
// monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Yellow;
// RefreshRoll();
// Thread.Sleep(1000);
// monitorEntities[FlashIndex].RowEntitys[2].Color = Brushes.Transparent;
// RefreshRoll();
// Thread.Sleep(1000);
// }
//});
1 month ago
}
1 month ago
1 month ago
/// <summary>
/// 获取Log
/// </summary>
private void GetLog()
{
RFIDLogsEntitys = LogContext.Query<RFIDLogsEntity>().OrderByDesc(a => a.CreateTime).Take(200).ToList();
LogDataGridView.DataSource = null;
LogDataGridView.DataSource = RFIDLogsEntitys;
}
1 month ago
/// <summary>
/// 获取准备行
/// </summary>
/// <returns>准备行index</returns>
private int GetReadyRowIndex()
{
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDState == 2)
{
return i;
}
}
return -1;
}
/// <summary>
/// 获取正在工作的行
/// </summary>
/// <returns>工作行index</returns>
private int GetNowRowIndex()
{
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
if (RFIDStatesEntities[i].RFIDState == 1)
{
return i;
}
}
return -1;
}
private void RefreshIO()
{
for (int i = 0; i < RFIDStatesEntities.Count; i++)
{
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Yellow,
delay = -1
});
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Red,
delay = -1
});
_methodQueue.Enqueue(new DOperateEntity()
{
port = lightsEntities[i].Green,
delay = -1
});
1 month ago
}
}
#endregion
}
}