Files
CleanMachine/CleanMachineHMI/FrmUserControl/frmPlcItemConfig.cs
2022-05-10 15:21:13 +08:00

335 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CleanMachineHMI.Domain.Enums;
using CleanMachineHMI.frmChild;
using CleanMachineHMI.Utils;
using Krypton.Toolkit;
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;
using CleanMachine.Model;
using CleanMachine.Model.BAS;
namespace CleanMachineHMI.FrmUserControl
{
[FormType(EnumFormType.PlcItemConfig)]
public partial class frmPlcItemConfig : UserControl
{
public frmPlcItemConfig()
{
InitializeComponent();
}
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();
this.SetFormControlText();
{
kcmb_plcItemType.Items.Clear();
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemType.Name))
{
kcmb_plcItemType.Items.Add(item);
}
kcmb_plcItemDateType.Items.Clear();
foreach (string item in this.GetComBoboxItemArr(kcmb_plcItemDateType.Name))
{
kcmb_plcItemDateType.Items.Add(item);
}
}
kcmb_plcItemType.SelectedIndex = 0;
kcmb_plcItemDateType.SelectedIndex = 0;
RefreshFrmHost = new dlgRefreshFrmHost(RefreshPlcData);
RefreshPlcData();
foreach (Control item in tableLayoutPanel2.Controls)
{
if (item.Tag == null)
{
item.Enabled = Global.CheckRole(this.Name, item.Name);
//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;
//}
}
}
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
List<PlcItem> tmpMdList;
if (!string.IsNullOrEmpty(ktb_plcMsg.Text.Trim()))
{
tmpMdList = PlcOperationHelper.Instance. PlcItemMdList.FindAll(x => x.PlcItemCode.Contains(ktb_plcMsg.Text.Trim()) || x.PlcItemName.Contains(ktb_plcMsg.Text.Trim()));
}
else
{
tmpMdList = PlcOperationHelper.Instance.PlcItemMdList;
}
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)
{
tmpMdList = tmpMdList.FindAll(x => x.PlcItemDataType == (PlcItemTypeDesc) (kcmb_plcItemDateType.SelectedIndex - 1));
}
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);
foreach (PlcItem item in tmpMdList)
{
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";
}
if (item.PlcItemDataType == PlcItemTypeDesc.M)
{
dr["PLC变量数据类型"] = "M Type";
}
else if (item.PlcItemDataType == PlcItemTypeDesc.D)
{
dr["PLC变量数据类型"] = "D Type";
}
else if (item.PlcItemDataType == PlcItemTypeDesc.X)
{
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();
kryptonDataGridView1.SetDataViewColumnText(this.Name);
}
else
{
kryptonDataGridView1.DataSource = null;
}
}
catch (Exception ex)
{
KryptonMessageBox.Show(this.T("加载PLC变量数据界面发生异常", "LoadFrmPlcItemExp") + $":【{ex.Message}】!");
}
}
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变量数据行!");
KryptonMessageBox.Show(this.T("请选择要编辑的PLC变量数据行!", "SelectWantToEditRow"));
}
}
private void kbt_Delete_Click(object sender, EventArgs e)
{
try
{
if (kryptonDataGridView1.SelectedRows.Count > 0)
{
DialogResult dr = KryptonMessageBox.Show(this.T("确定要删除所选的PLC变量信息吗?", "DeleteInfo"), "Prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
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}'";
int row = LeadDbHelperSQLite.ExecuteSql(sql);
if (row > 0)
{
//KryptonMessageBox.Show("删除PLC变量数据成功!");
KryptonMessageBox.Show(this.T("删除PLC变量数据成功!", "DeleteSuccessInfo"));
RefreshFrmHost();
}
else
{
//KryptonMessageBox.Show("删除PLC变量数据失败!");
KryptonMessageBox.Show(this.T("删除PLC变量数据失败!", "DeleteFailInfo"));
}
}
}
else
{
//KryptonMessageBox.Show("请选择要删除的PLC变量数据行!");
KryptonMessageBox.Show(this.T("请选择要删除的PLC变量数据行!", "SelectWantToDeleteRow"));
}
}
catch (Exception ex)
{
KryptonMessageBox.Show(this.T("删除PLC变量数据发生异常", "DeletePlcItemDataExp") + $":【{ex.Message}】!");
}
}
}
}