Files

341 lines
15 KiB
C#
Raw Permalink Normal View History

2021-06-05 14:17:30 +08:00
using CleanMachineHMI.Domain.Enums;
using CleanMachineHMI.frmChild;
using CleanMachineHMI.Utils;
2021-12-26 21:30:02 +08:00
using Krypton.Toolkit;
2021-06-05 14:17:30 +08:00
using LeadDataBaseUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2021-12-31 19:53:53 +08:00
using CleanMachine.Model;
2022-05-05 20:42:02 +08:00
using CleanMachine.Model.BAS;
2021-06-05 14:17:30 +08:00
namespace CleanMachineHMI.FrmUserControl
{
[FormType(EnumFormType.PlcItemConfig)]
public partial class frmPlcItemConfig : UserControl
{
public frmPlcItemConfig()
{
InitializeComponent();
}
2022-03-18 21:25:34 +08:00
2021-06-05 14:17:30 +08:00
public delegate void dlgRefreshFrmHost();
public static dlgRefreshFrmHost RefreshFrmHost;
private void txt_GotFocus(object sender, EventArgs e)
{
KeyboardAutoPopUp.ShowInputPanel();
}
private void txt_LostFocus(object sender, EventArgs e)
{
KeyboardAutoPopUp.HideInputPanel();
}
private void InitControlEvent()
{
ktb_plcMsg.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
ktb_plcMsg.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
ktb_plcItemModbusAddress.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
ktb_plcItemModbusAddress.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
ktb_plcItemPriAddress.GotFocus += new EventHandler(txt_GotFocus); //获取焦点前发生事件
ktb_plcItemPriAddress.LostFocus += new EventHandler(txt_LostFocus); //失去焦点后发生事件
}
private void frmPlcItemConfig_Load(object sender, EventArgs e)
{
InitControlEvent();
2022-03-18 19:06:26 +08:00
this.SetFormControlText();
var items = this.GetComBoboxItemArr(kcmb_plcItemType.Name);
if (items != null && items.Count > 0)
2021-06-05 14:17:30 +08:00
{
kcmb_plcItemType.Items.Clear();
foreach (string item in items)
2021-06-05 14:17:30 +08:00
{
2022-03-18 21:25:34 +08:00
kcmb_plcItemType.Items.Add(item);
2021-06-05 14:17:30 +08:00
}
}
items = this.GetComBoboxItemArr(kcmb_plcItemDateType.Name);
if (items != null && items.Count > 0)
{
2021-06-05 14:17:30 +08:00
kcmb_plcItemDateType.Items.Clear();
foreach (string item in items)
2021-06-05 14:17:30 +08:00
{
2022-03-18 21:25:34 +08:00
kcmb_plcItemDateType.Items.Add(item);
2021-06-05 14:17:30 +08:00
}
}
2021-06-05 14:17:30 +08:00
kcmb_plcItemType.SelectedIndex = 0;
kcmb_plcItemDateType.SelectedIndex = 0;
RefreshFrmHost = new dlgRefreshFrmHost(RefreshPlcData);
RefreshPlcData();
foreach (Control item in tableLayoutPanel2.Controls)
{
if (item.Tag == null)
{
2022-05-06 12:28:16 +08:00
item.Enabled = Global.CheckRole(this.Name, item.Name);
2022-05-06 10:36:42 +08:00
//DataRow[] drs = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{this.Name}' AND MENU_PID = '-1'");
//DataRow[] drs1 = GlobalHelper.RoleMenuPermission.Select($" MENU_URL = '{item.Name}' AND MENU_PID = '{drs[0]["MENU_ID"]}'");
//if (drs1.Length > 0)
//{
// item.Enabled = true;
//}
//else
//{
// item.Enabled = false;
//}
2021-06-05 14:17:30 +08:00
}
}
kryptonGroupBox1.Focus();
}
private void RefreshPlcData()
{
try
{
#region 已删除
//string sql = $@"
// SELECT
// PLC_ITEM_ID '序号',
// PLC_ITEM_CODE 'PLC变量编码',
// PLC_ITEM_NAME 'PLC变量名称',
// CASE PLC_ITEM_TYPE
// WHEN 0 THEN
// '写入变量'
// WHEN 1 THEN
// '读取变量'
// ELSE
// '可读可写'
// END 'PLC变量类型',
// CASE PLC_ITEM_DATA_TYPE
// WHEN 0 THEN
// 'M 布尔类型'
// WHEN 1 THEN
// 'D 整数类型'
// WHEN 2 THEN
// 'X 数据类型'
// WHEN 3 THEN
// 'Y 数据类型'
// ELSE
// '未知'
// END 'PLC变量数据类型',
// PLC_ITEM_PRI_ADDRESS 'PLC寄存器地址',
// PLC_ITEM_MODBUS_ADDRESS 'ModBus地址',
// PLC_ITEM_VALUE '当前值'
// FROM
// BAS_PLC_ITEM
// WHERE 1=1
// ";
//if (!string.IsNullOrEmpty(ktb_plcMsg.Text.Trim()))
//{
// sql += $" AND (PLC_ITEM_CODE LIKE '%{ktb_plcMsg.Text.Trim()}%' OR PLC_ITEM_NAME LIKE '%{ktb_plcMsg.Text.Trim()}%')";
//}
//if (kcmb_plcItemType.SelectedIndex != 0)
//{
// sql += $" AND PLC_ITEM_TYPE = '{kcmb_plcItemType.SelectedIndex -1}'";
//}
//if (kcmb_plcItemDateType.SelectedIndex != 0)
//{
// sql += $" AND PLC_ITEM_DATA_TYPE = '{kcmb_plcItemDateType.SelectedIndex - 1}'";
//}
//if (!string.IsNullOrEmpty(ktb_plcItemModbusAddress.Text.Trim()))
//{
// sql += $" AND PLC_ITEM_MODBUS_ADDRESS = '{ktb_plcItemModbusAddress.Text.Trim()}'";
//}
//if (!string.IsNullOrEmpty(ktb_plcItemPriAddress.Text.Trim()))
//{
// sql += $" AND PLC_ITEM_PRI_ADDRESS = '{ktb_plcItemPriAddress.Text.Trim()}'";
//}
//DataTable dt = LeadDbHelperSQLite.Query(sql).Tables[0];
#endregion
2021-12-31 19:53:53 +08:00
List<PlcItem> tmpMdList;
2021-06-05 14:17:30 +08:00
if (!string.IsNullOrEmpty(ktb_plcMsg.Text.Trim()))
{
2022-05-18 10:15:49 +08:00
tmpMdList = PlcOperationHelper.Instance. PlcItemMdList.Values.ToList().FindAll(x => x.PlcItemCode.Contains(ktb_plcMsg.Text.Trim()) || x.PlcItemName.Contains(ktb_plcMsg.Text.Trim()));
2021-06-05 14:17:30 +08:00
}
else
{
2022-05-18 10:15:49 +08:00
tmpMdList = PlcOperationHelper.Instance.PlcItemMdList.Values.ToList();
2021-06-05 14:17:30 +08:00
}
if (tmpMdList != null && tmpMdList.Count > 0)
{
if (kcmb_plcItemType.SelectedIndex != 0)
{
tmpMdList = tmpMdList.FindAll(x => x.PlcItemType == (kcmb_plcItemType.SelectedIndex - 1));
}
if (tmpMdList != null && tmpMdList.Count > 0)
{
if (kcmb_plcItemDateType.SelectedIndex != 0)
{
2022-05-10 15:21:13 +08:00
tmpMdList = tmpMdList.FindAll(x => x.PlcItemDataType == (PlcItemTypeDesc) (kcmb_plcItemDateType.SelectedIndex - 1));
2021-06-05 14:17:30 +08:00
}
if (tmpMdList != null && tmpMdList.Count > 0)
{
if (!string.IsNullOrEmpty(ktb_plcItemModbusAddress.Text.Trim()))
{
tmpMdList = tmpMdList.FindAll(x => x.PlcItemModbusAddress == ktb_plcItemModbusAddress.Text.Trim());
}
if (tmpMdList != null && tmpMdList.Count > 0)
{
if (!string.IsNullOrEmpty(ktb_plcItemPriAddress.Text.Trim()))
{
tmpMdList = tmpMdList.FindAll(x => x.PlcItemPriAddress == ktb_plcItemPriAddress.Text.Trim());
}
}
}
}
}
if (tmpMdList != null && tmpMdList.Count > 0)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("序号", Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("PLC变量编码", Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("PLC变量名称", Type.GetType("System.String"));
DataColumn dc8 = new DataColumn("当前值", Type.GetType("System.String"));
DataColumn dc4 = new DataColumn("PLC变量类型", Type.GetType("System.String"));
DataColumn dc5 = new DataColumn("PLC变量数据类型", Type.GetType("System.String"));
DataColumn dc6 = new DataColumn("PLC寄存器地址", Type.GetType("System.String"));
DataColumn dc7 = new DataColumn("ModBus地址", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc8);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
2021-12-31 19:53:53 +08:00
foreach (PlcItem item in tmpMdList)
2021-06-05 14:17:30 +08:00
{
DataRow dr = dt.NewRow();
dr["序号"] = item.PlcItemId;
dr["PLC变量编码"] = item.PlcItemCode;
dr["PLC变量名称"] = item.PlcItemName;
if (item.PlcItemType == 0)
{
dr["PLC变量类型"] = "Can Write";
}
else if (item.PlcItemType == 1)
{
dr["PLC变量类型"] = "Can Read";
}
else
{
dr["PLC变量类型"] = "Read and Write";
}
2022-05-10 15:21:13 +08:00
if (item.PlcItemDataType == PlcItemTypeDesc.M)
2021-06-05 14:17:30 +08:00
{
dr["PLC变量数据类型"] = "M Type";
}
2022-05-10 15:21:13 +08:00
else if (item.PlcItemDataType == PlcItemTypeDesc.D)
2021-06-05 14:17:30 +08:00
{
dr["PLC变量数据类型"] = "D Type";
}
2022-05-10 15:21:13 +08:00
else if (item.PlcItemDataType == PlcItemTypeDesc.X)
2021-06-05 14:17:30 +08:00
{
dr["PLC变量数据类型"] = "X Type";
}
else
{
dr["PLC变量数据类型"] = "Y Type";
}
dr["PLC寄存器地址"] = item.PlcItemPriAddress;
dr["ModBus地址"] = item.PlcItemModbusAddress;
dr["当前值"] = item.PlcItemValue;
dt.Rows.Add(dr);
}
kryptonDataGridView1.DataSource = dt;
kryptonDataGridView1.ClearSelection();
2022-03-19 12:46:23 +08:00
kryptonDataGridView1.SetDataViewColumnText(this.Name);
2021-06-05 14:17:30 +08:00
}
else
{
kryptonDataGridView1.DataSource = null;
}
}
catch (Exception ex)
{
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("加载PLC变量数据界面发生异常", "LoadFrmPlcItemExp") + $":【{ex.Message}】!");
2021-06-05 14:17:30 +08:00
}
}
private void kbt_Query_Click(object sender, EventArgs e)
{
RefreshPlcData();
}
private void kbt_Reset_Click(object sender, EventArgs e)
{
ktb_plcMsg.Text = "";
ktb_plcItemModbusAddress.Text = "";
ktb_plcItemPriAddress.Text = "";
kcmb_plcItemDateType.SelectedIndex = 0;
kcmb_plcItemType.SelectedIndex = 0;
}
private void kbt_Add_Click(object sender, EventArgs e)
{
frmAddPlcItem frm = new frmAddPlcItem();
frm.ShowDialog();
}
private void kbt_Edit_Click(object sender, EventArgs e)
{
if (kryptonDataGridView1.SelectedRows.Count > 0)
{
frmEditPlcItem frm = new frmEditPlcItem();
frm.PlcItemId = kryptonDataGridView1.SelectedRows[0].Cells[0].Value.ToString();
frm.ShowDialog();
}
else
{
//KryptonMessageBox.Show("请选择要编辑的PLC变量数据行!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请选择要编辑的PLC变量数据行!", "SelectWantToEditRow"));
2021-06-05 14:17:30 +08:00
}
}
private void kbt_Delete_Click(object sender, EventArgs e)
{
try
{
if (kryptonDataGridView1.SelectedRows.Count > 0)
{
2022-05-06 08:45:56 +08:00
DialogResult dr = KryptonMessageBox.Show(this.T("确定要删除所选的PLC变量信息吗?", "DeleteInfo"), "Prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
2021-06-05 14:17:30 +08:00
if (dr == DialogResult.OK)
{
string PlcItemId = kryptonDataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = $@"DELETE FROM BAS_PLC_ITEM WHERE PLC_ITEM_ID = '{PlcItemId}'";
2022-05-16 09:03:42 +08:00
int row = LeadDbHelperSQLite.Execute(sql);
2021-06-05 14:17:30 +08:00
if (row > 0)
{
//KryptonMessageBox.Show("删除PLC变量数据成功!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("删除PLC变量数据成功!", "DeleteSuccessInfo"));
2021-06-05 14:17:30 +08:00
RefreshFrmHost();
}
else
{
//KryptonMessageBox.Show("删除PLC变量数据失败!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("删除PLC变量数据失败!", "DeleteFailInfo"));
2021-06-05 14:17:30 +08:00
}
}
}
else
{
//KryptonMessageBox.Show("请选择要删除的PLC变量数据行!");
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("请选择要删除的PLC变量数据行!", "SelectWantToDeleteRow"));
2021-06-05 14:17:30 +08:00
}
}
catch (Exception ex)
{
2022-05-06 08:45:56 +08:00
KryptonMessageBox.Show(this.T("删除PLC变量数据发生异常", "DeletePlcItemDataExp") + $":【{ex.Message}】!");
2021-06-05 14:17:30 +08:00
}
}
}
}