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.

771 lines
25 KiB
C#

3 months 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;
3 months ago
using System.Timers;
3 months ago
using System.Windows.Forms;
using System.Xml.Serialization;
using NewLife.Caching;
3 months ago
using NewLife.Collections;
2 months ago
using System.IO.Ports;
using NewLife.Log;
3 months ago
namespace NDSD_Screwdriver
{
public partial class MainForm : Form
{
/// <summary>
/// 输出端口操作
/// </summary>
private DOperate DOperateInfo;
3 months ago
/// <summary>
/// TCP服务器
/// </summary>
private TcpServer server = TcpServer.Instance;
/// <summary>
/// 前端列表集合
/// </summary>
private List<MonitorEntity> monitorEntities;
private List<RFIDConfigEntity> RFIDConfigEntitys;
private Random rand = new Random();
2 months ago
3 months ago
2 months ago
3 months ago
/// <summary>
/// 日志实例
/// </summary>
public List<RFIDLogsEntity> RFIDLogsEntitys;
/// <summary>
/// 日志数据库操作实例
/// </summary>
public IDbContext LogContext;
3 months ago
// Thread flashThread;
3 months ago
SerialPortFactory serialPort;
private CancellationTokenSource cancellationTokenSource;
3 months ago
3 months ago
public int textRFID = 0;
3 months ago
private ICache cache = Cache.Default;
3 months ago
int FlashIndex = 0;
2 months ago
// bool _flashflag = true;
2 months ago
private Dictionary<string, int> dictionary = new Dictionary<string, int>();
3 months ago
2 months ago
// private DengTool tool;
3 months ago
3 months ago
public MainForm()
{
3 months ago
3 months ago
InitializeComponent();
2 months ago
// tool = new DengTool();
// tool.Start();
3 months ago
FlashThreadTime();
3 months ago
var memorySetting = MemorySetting.Current;
3 months ago
SqlLiteTool.CreateTable(memorySetting.DB);
LogContext = SqlLiteTool.GetDb(memorySetting.DB);
3 months ago
RFIDLogsEntitys = LogContext.Query<RFIDLogsEntity>().OrderByDesc(a => a.CreateTime).Take(200).ToList();
LogDataGridView.AutoGenerateColumns = false;
LogDataGridView.DataSource = null;
LogDataGridView.DataSource = RFIDLogsEntitys;
2 months ago
DOperateInfo = new DOperate();
3 months ago
if (!server.ServerOpen(memorySetting.ServerIP, memorySetting.ServerPort))
3 months ago
{
MessageBox.Show("服务端打开失败!");
FormUtils.LogInsert(LogContext, "服务端打开失败");
}
3 months ago
try
{
2 months ago
3 months ago
serialPort = new SerialPortFactory(memorySetting.Com);
2 months ago
for (int w = 0; w < 2; w++)
{
for (int i = 0; i < 16; i++)
{
DOperateInfo.DClose(i);
Thread.Sleep(300);
}
}
3 months ago
3 months ago
}
catch (Exception e)
{
MessageBox.Show(e.Message);
FormUtils.LogInsert(LogContext, e.Message);
}
2 months ago
3 months ago
}
3 months 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, "服务端释放失败");
}
}
3 months ago
3 months ago
2 months ago
// tool.Stop();
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
}
3 months ago
timer.Close();
timer.Dispose();
3 months ago
}
#region 按钮
/// <summary>
/// 打开DO测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DOTest_Click(object sender, EventArgs e)
{
3 months ago
ScrewdriverTest screwdriverTest = new ScrewdriverTest(server, DOperateInfo);
3 months ago
screwdriverTest.Show();
}
/// <summary>
/// 初始化按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InitButton_Click(object sender, EventArgs e)
{
InitTable();
2 months ago
StartLongRunningTask();
3 months ago
}
/// <summary>
/// 初始化Table
/// </summary>
private void InitTable()
{
2 months ago
// _flashflag = true;
3 months ago
FlashIndex = 0;
textRFID = 0;
3 months ago
timer.Start();
3 months ago
var memorySetting = MemorySetting.Current;
3 months ago
//读取rfid数据
3 months ago
var ctx = SqlLiteTool.GetDb(memorySetting.DB);
3 months ago
var list = ctx.Query<RFIDConfigEntity>().Where(x => x.IsEnable == true).ToList();
var strs = list.Select(x => x.RfidNo).ToArray();
2 months ago
monitorEntities = new List<MonitorEntity>(list.Count);
int i1 = 0;
foreach (var en in list)
3 months ago
{
monitorEntities.Add(new MonitorEntity()
{
2 months ago
Green = Convert.ToInt32(en.Green),
Yellow = Convert.ToInt32(en.Yellow),
Red=Convert.ToInt32(en.Red),
RFIDState = 0,
RFIDValue = en.RfidNo,
RowIndex = i1,
3 months ago
RowEntitys = InitRows()
3 months ago
});
3 months ago
2 months ago
monitorEntities[i1].RowEntitys[1].Value = en.RfidNo;
i1++;
}
monitorEntities[0].RFIDState = 2;
FormUtils.LogInsert(LogContext, "RFID " + 0 + " 准备工作", monitorEntities[0].RFIDValue);
GetLog();
3 months ago
RefreshRoll();
2 months ago
3 months ago
}
/// <summary>
/// 打开设置界面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SettingButton_Click(object sender, EventArgs e)
{
FrmSetting frmSetting = new FrmSetting();
frmSetting.Show();
}
2 months ago
3 months ago
2 months ago
3 months ago
/// <summary>
/// 测试按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TestButton1_Click(object sender, EventArgs e)
{
2 months ago
//if (monitorEntities.Count == 0)
//{
// MessageBox.Show("请先初始化RFID列表");
//}
//else if (textRFID == monitorEntities.Count)
//{
// SetNowWorkRow("0000000");
// textRFID = 0;
// return;
//}
//SetNowWorkRow(monitorEntities[textRFID].RFIDValue);
//if (++textRFID > monitorEntities.Count)
//{
// textRFID = 0;
// return;
//}
3 months ago
2 months ago
LogContext.Delete<RFIDLogsEntity>(x=>x.ID>0);
GetLog();
3 months ago
}
#endregion
#region 功能性方法
/// <summary>
/// 根据读取到的RFID条码设置当前工作的行 并且更新state
/// </summary>
/// <param name="rfid">读取到的RFID</param>
private void SetNowWorkRow(string rfid)
{
3 months ago
var memorySetting = MemorySetting.Current;
3 months ago
//设置RFID状态
int flag = 0;
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
2 months ago
if (monitorEntities[i].RFIDValue == rfid && monitorEntities[i].RFIDState == 2)
3 months ago
{
2 months ago
monitorEntities[i].RFIDState = 1;
FormUtils.LogInsert(LogContext, "RFID " + i + " 开始工作", monitorEntities[i].RFIDValue);
3 months ago
GetLog();
2 months ago
// if (i - 1 >= 0)
// {
// string rfidValue = monitorEntities[i - 1].RFIDValue;
// var leastTime = RFIDLogsEntitys.First(x => x.RFIDId == rfidValue).CreateTime;
// double timeSpan = (DateTime.Now - leastTime).TotalSeconds;
// if (timeSpan >= memorySetting.AlarmTimeValue)
// {
// monitorEntities[i - 1].RFIDState = 4;
// FormUtils.LogInsert(LogContext, "RFID " + i + " 超时结束", monitorEntities[i].RFIDValue);
// }
// if (timeSpan < memorySetting.AlarmTimeValue)
// {
// monitorEntities[i - 1].RFIDState = 3;
// FormUtils.LogInsert(LogContext, "RFID " + (i - 1) + " 成功结束", monitorEntities[i].RFIDValue);
// }
// GetLog();
// }
// if (i + 1 < monitorEntities.Count)
// {
// FormUtils.LogInsert(LogContext, "RFID " + (i + 1) + " 准备工作", monitorEntities[i].RFIDValue);
// GetLog();
// monitorEntities[i + 1].RFIDState = 2;
// FlashIndex = i + 1;
// }
// else
// {
// timer.Stop();
// // _flashflag = false;
// }
3 months ago
flag = 1;
break;
}
}
if (flag == 0) //列表里没有已读的RFID
{
3 months ago
2 months ago
// for (int i = 0; i < monitorEntities.Count; i++)
// {
// if (monitorEntities[i].RFIDState == 1)
// {
// string rfidValue = monitorEntities[i - 1].RFIDValue;
// if (rfidValue != null)
// {
// var leastTime = RFIDLogsEntitys.First(x => x.RFIDId == rfidValue).CreateTime;
// double timeSpan = (DateTime.Now - leastTime).TotalSeconds;
// if (timeSpan >= memorySetting.AlarmTimeValue)
// {
// monitorEntities[i].RFIDState = 4;
// FormUtils.LogInsert(LogContext, "RFID " + i + " 超时结束", monitorEntities[i].RFIDValue);
// }
// if (timeSpan < memorySetting.AlarmTimeValue)
// {
// monitorEntities[i].RFIDState = 3;
// FormUtils.LogInsert(LogContext, "RFID " + (i - 1) + " 成功结束", monitorEntities[i].RFIDValue);
// }
// }
//
// }
// }
3 months ago
}
//刷新前端灯的状态
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
2 months ago
// if (monitorEntities[i].RFIDState == 1)
// {
// SetRowsLightState(i, 1);
// }
// else if (monitorEntities[i].RFIDState == 3)
// {
// SetRowsLightState(i, 0);
// }
// else if (monitorEntities[i].RFIDState == 4)
// {
// SetRowsLightState(i, 2);
// }
// else
// {
// SetRowsLightState(i, Brushes.Transparent);
// }
3 months ago
}
2 months ago
// if(_flashflag)
// timer.Stop();
//
// //刷新IO的状态
// // for (int i = 0; i < RFIDStatesEntities.Count; i++)
// // {
// //
// // if (RFIDStatesEntities[i].RFIDState == 1)
// // {
// // // DOperateInfo.DOpen(lightsEntities[i].Yellow);
// // tool.AddMq(new DOperateEntity()
// // {
// // port = lightsEntities[i].Yellow,
// // delay = 0
// // });
// //
// // }
// // else if (RFIDStatesEntities[i].RFIDState == 3)
// // {
// // tool.AddMq(new DOperateEntity()
// // {
// // port = lightsEntities[i].Green,
// // delay = 0
// // });
// //
// // tool.AddMq(new DOperateEntity()
// // {
// // port = lightsEntities[i].Yellow,
// // delay = -1
// // });
// //
// // }
// // else if (RFIDStatesEntities[i].RFIDState == 4)
// // {
// //
// // tool.AddMq(new DOperateEntity()
// // {
// // port = lightsEntities[i].Red,
// // delay = 0
// // });
// //
// // tool.AddMq(new DOperateEntity()
// // {
// // port = lightsEntities[i].Yellow,
// // delay = -1
// // });
// //
// // }
// // }
// if (_flashflag)
// timer.Start();
3 months ago
RefreshRoll();
}
/// <summary>
/// 初始化一行
/// </summary>
/// <returns></returns>
3 months ago
private List<RowEntity> InitRows()
3 months ago
{
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[]>();
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
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;
//工作中箭头指示
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
2 months ago
monitorEntities[i].RowEntitys[0].Value = monitorEntities[i].RFIDState == 1 ? "=>" : "";
3 months ago
}
}
3 months 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);
}
}
}
}
}
2 months ago
private QingTcpClient2 quClient2 = new QingTcpClient2();
private DateTime dateTime = DateTime.Now;
3 months ago
/// <summary>
/// RFID刷新任务
/// </summary>
private void StartLongRunningTask()
{
cancellationTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
2 months ago
3 months ago
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
//SetNowWorkRow("0000000");
// 这里是你的长期运行逻辑
2 months ago
try
{
var read = serialPort?.Read();
// var read = "";
if (read.Length == 16)
{
this.Invoke(() =>
{
SetNowWorkRow(read);
});
var monitorEntitiesCount = monitorEntities.Count;
var last = monitorEntities[FlashIndex].RFIDState;
if (FlashIndex < monitorEntitiesCount)
{
if (last != 100 && last==1)
{
quClient2.Enable();
dateTime = DateTime.Now;
if (FlashIndex < monitorEntitiesCount)
{
XTrace.WriteLine("a");
//状态修改
monitorEntities[FlashIndex].RFIDState = 1;
DOperateInfo.DOpen(monitorEntities[FlashIndex].Yellow);
timer.Start();
}
}
}
}
else
{
XTrace.WriteLine("aa");
var total = DateTime.Now - dateTime;
if (total.TotalSeconds > 2)
{
quClient2.Close();
dateTime = DateTime.Now;
}
}
Thread.Sleep(500); // 模拟一些长时间的工作
}
catch (Exception e)
{
Console.WriteLine(e);
}
3 months ago
}
}, cancellationTokenSource.Token);
}
3 months ago
2 months ago
System.Timers.Timer timer = new System.Timers.Timer(500); // 设置1秒间隔
3 months ago
/// <summary>
/// 等待工作指示灯闪烁进程
/// </summary>
3 months ago
private void FlashThreadTime()
3 months ago
{
3 months ago
int step = 0;
timer.Elapsed += (sender, e) =>
3 months ago
{
2 months ago
Set(FlashIndex);
RefreshRoll();
};
2 months ago
}
2 months ago
int step1 = 0;
public void Set(int id)
{
var monitorEntitiesCount = monitorEntities.Count;
if (id < monitorEntitiesCount)
3 months ago
{
2 months ago
/// 状态指示 0未操作 2准备操作 1正在操作 3已完成 4超时报警
int RFIDState = monitorEntities[id].RFIDState;
if (step1 == 0)
{
step1 = 1;
2 months ago
RFIDState = monitorEntities[id].RFIDState;
if (RFIDState == 2)
{
DOperateInfo.DClose(monitorEntities[id].Red);
Thread.Sleep(300);
DOperateInfo.DClose(monitorEntities[id].Green);
Thread.Sleep(300);
DOperateInfo.DTimeOpen(monitorEntities[id].Yellow, 1);
monitorEntities[id].RowEntitys[2].Color = Brushes.Yellow;
}
RFIDState = monitorEntities[id].RFIDState;
if (RFIDState == 1)
{
3 months ago
2 months ago
if ((id+1) < monitorEntitiesCount)
{
monitorEntities[id + 1].RFIDState = 2;
/// 状态指示 0未操作 2准备操作 1正在操作 3已完成 4超时报警
}
if (dictionary.ContainsKey(monitorEntities[id].RFIDValue))
{
var i = dictionary[monitorEntities[id].RFIDValue];
dictionary[monitorEntities[id].RFIDValue] = i + 1;
if (i >3)
{
monitorEntities[id].RFIDState = 3;
dictionary[monitorEntities[id].RFIDValue] = 3;
}
}
else
{
dictionary[monitorEntities[id].RFIDValue] = 1;
}
monitorEntities[id].RowEntitys[2].Color = Brushes.Yellow;
}
RFIDState = monitorEntities[id].RFIDState;
if (RFIDState == 3)
{
DOperateInfo.DClose(monitorEntities[id].Red);
Thread.Sleep(300);
DOperateInfo.DClose(monitorEntities[id].Yellow);
Thread.Sleep(300);
FormUtils.LogInsert(LogContext, "RFID {0} 发送绿灯", monitorEntities[id].RFIDValue);
XTrace.WriteLine("========================================================================");
DOperateInfo.DOpen(monitorEntities[id].Green);
Thread.Sleep(300);
DOperateInfo.DOpen(monitorEntities[id].Green);
quClient2.Close();
monitorEntities[id].RowEntitys[2].Color = Brushes.Green;
monitorEntities[id].RFIDState=100;
timer.Stop();
FlashIndex += 1;
timer.Start();
}
}
else if (step1 == 1)
{
step1=0;
if (RFIDState == 2)
{
monitorEntities[id].RowEntitys[2].Color = Brushes.Transparent;
}
}
}
3 months ago
}
2 months ago
private int step2 = 0;
3 months ago
/// <summary>
/// 获取Log
/// </summary>
private void GetLog()
{
RFIDLogsEntitys = LogContext.Query<RFIDLogsEntity>().OrderByDesc(a => a.CreateTime).Take(200).ToList();
LogDataGridView.DataSource = null;
LogDataGridView.DataSource = RFIDLogsEntitys;
}
3 months ago
/// <summary>
/// 获取准备行
/// </summary>
/// <returns>准备行index</returns>
private int GetReadyRowIndex()
{
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
2 months ago
if (monitorEntities[i].RFIDState == 2)
3 months ago
{
return i;
}
}
return -1;
}
/// <summary>
/// 获取正在工作的行
/// </summary>
/// <returns>工作行index</returns>
private int GetNowRowIndex()
{
2 months ago
for (int i = 0; i < monitorEntities.Count; i++)
3 months ago
{
2 months ago
if (monitorEntities[i].RFIDState == 1)
3 months ago
{
return i;
}
}
return -1;
}
2 months ago
3 months ago
#endregion
}
}