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.

510 lines
21 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using CompressorXN.Untils;
using CompressorXN_Model;
using CompressorXN_Model.Enums;
using CompressorXN_Model.ViewModel.Response;
using CompressorXN_Service;
using DevExpress.XtraEditors.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Windows.Forms;
namespace CompressorXN
{
public partial class FrmAgreement : Form
{
private static readonly AgreementConfigService _agreementConfigService = new AgreementConfigService();
private static string _agreementName = string.Empty;
public FrmAgreement(string agreementName = null)
{
InitializeComponent();
_agreementName = agreementName;
Loading.ShowWaitForm();
BindDropdownHelper.BindBps(cmb_Bps);
BindDropdownHelper.BindMsgFormat(cmb_MsgFormat);
BindDropdownHelper.BindFrameType(cmb_FrameType);
//锁定控件
LockControl();
BindListBox();
//绑定CellPainting事件处理程序
gridView_FuZhu.RowPostPaint += gridView_Assist_RowPostPaint;
//gridView_FuZhu.DataBindingComplete += (s, e) =>
//{
// for (int i = 0; i < gridView_FuZhu.Rows.Count; i++)
// {
// gridView_FuZhu.Rows[i].HeaderCell.Value = $"Byte{i}";
// }
//};
Loading.CloseWaitForm();
}
private void FrmAgreement_Load(object sender, EventArgs e)
{
T_Agreement_Config config = listBox.SelectedItem as T_Agreement_Config;
if (config == null)
{
string agreementName = this.listBox.SelectedValue?.ToString();
BindRightData(agreementName);
}
}
/// <summary>
/// 绑定ListBox
/// </summary>
private void BindListBox()
{
List<T_Agreement_Config> list = _agreementConfigService.QueryAgreements();
this.listBox.DataSource = list;
this.listBox.DisplayMember = "AgreementName"; // 显示内容 数据的属性
this.listBox.ValueMember = "AgreementName"; // 项的值 数据的属性
this.listBox.SelectedValue = _agreementName;
}
/// <summary>
/// ListBox选项change事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
T_Agreement_Config config = listBox.SelectedItem as T_Agreement_Config;
if (config != null)
{
BindRightData(config.AgreementName);
}
}
/// <summary>
/// 绑定右侧数据
/// </summary>
/// <param name="agreementName"></param>
private void BindRightData(string agreementName = "template")
{
AgreementVM agreementVM = _agreementConfigService.QueryAgreementByName(agreementName);
if (agreementVM != null)
{
txt_AgreementName.Text = agreementVM.AgreementConfig.AgreementName;
txt_SendPeriod.Text = agreementVM.AgreementConfig.SendPeriod.ToString();
txt_SendId.Text = agreementVM.AgreementConfig.SendId;
txt_FrameLen.Text = agreementVM.AgreementConfig.FrameLen.ToString();
txt_RecivePeriod.Text = agreementVM.AgreementConfig.RecivePeriod.ToString();
txt_ReciveId.Text = agreementVM.AgreementConfig.ReciveId;
txt_SignalVal.Text = agreementVM.AgreementConfig.ReciveSignalVal;
cmb_Bps.SelectedItem = new ListItem { Value = agreementVM.AgreementConfig.Bps.ToString(), Text = $"{agreementVM.AgreementConfig.Bps / 1000.0}k" };
cmb_MsgFormat.SelectedItem = new ListItem { Value = agreementVM.AgreementConfig.MsgFormat, Text = agreementVM.AgreementConfig.MsgFormat };
string frameTypeText = agreementVM.AgreementConfig.FrameType == "EXTEND" ? "扩展帧" : "标准帧";
cmb_FrameType.SelectedItem = new ListItem { Value = agreementVM.AgreementConfig.FrameType, Text = frameTypeText };
BindFuZhuGridView(agreementVM.AgreementHelpRuleList);
BindDetailRuleGridView(agreementVM.AgreementDetailRuleList);
BindAdditionalGridView(agreementVM.AgreementAdditionalRuleList);
}
}
/// <summary>
/// 绑定发送转速报文数据源
/// </summary>
private void BindDetailRuleGridView(List<T_Agreement_Detail_Rule> list)
{
//取消自动生成列
GridView_SendSpeed.AutoGenerateColumns = false;
GridView_Recive.AutoGenerateColumns = false;
var sends = list.Where(m => m.MsgType == "send").ToList();
var recives = list.Where(m => m.MsgType == "recive").ToList();
GridView_SendSpeed.DataSource = sends;
GridView_SendSpeed.RowTemplate.Height = (GridView_SendSpeed.Height - GridView_SendSpeed.ColumnHeadersHeight) / GridView_SendSpeed.Rows.Count;
GridView_Recive.DataSource = recives;
}
/// <summary>
/// 绑定辅助位数据源
/// </summary>
private void BindFuZhuGridView(List<T_Agreement_Help_Rule> list)
{
//取消自动生成列
gridView_FuZhu.AutoGenerateColumns = false;
gridView_FuZhu.DataSource = list;
//gridView_FuZhu.RowTemplate.Height = (gridView_FuZhu.Height - gridView_FuZhu.ColumnHeadersHeight) / gridView_FuZhu.Rows.Count;
}
/// <summary>
/// 绑定附加报文
/// </summary>
/// <param name="list"></param>
private void BindAdditionalGridView(List<T_Agreement_Additional_Rule> list)
{
//取消自动生成列
GridView_Additional.AutoGenerateColumns = false;
GridView_Additional.DataSource = list;
GridView_Additional.RowTemplate.Height = (GridView_Additional.Height - GridView_Additional.ColumnHeadersHeight) / GridView_Additional.Rows.Count;
}
/// <summary>
/// 设置辅助位GridView纵向标题
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView_Assist_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var grid = sender as DataGridView;
var rowidx = $"Byte{e.RowIndex}";
var centerFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
var headerBuunds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
e.Graphics.DrawString(rowidx, new Font("微软雅黑", 12), SystemBrushes.ControlText, headerBuunds, centerFormat);
}
/// <summary>
/// 点击解锁按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Unlock_Click(object sender, EventArgs e)
{
FrmUnlock frmUnlock = new FrmUnlock();
DialogResult dialogResult = frmUnlock.ShowDialog();
if (dialogResult == DialogResult.OK)
{
LockControl(true);
}
}
public delegate void SenFormName(EventParaVM eventParaVM);
public event SenFormName sendFormNameEvent;
/// <summary>
/// 点击确定按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Confirm_Click(object sender, EventArgs e)
{
Loading.ShowWaitForm();
Save();
Loading.CloseWaitForm();
if (!string.IsNullOrEmpty(_agreementName) && sendFormNameEvent != null)
{
EventParaVM eventParaVM = new EventParaVM
{
formNameEnum = FormNameEnum.
};
sendFormNameEvent(eventParaVM);
}
}
/// <summary>
/// 保存协议配置
/// </summary>
private void Save()
{
AgreementVM agreementVM = new AgreementVM
{
AgreementConfig = new T_Agreement_Config(),
AgreementDetailRuleList = new List<T_Agreement_Detail_Rule>(),
AgreementAdditionalRuleList = new List<T_Agreement_Additional_Rule>(),
AgreementHelpRuleList = new List<T_Agreement_Help_Rule>()
};
#region 配置
string agreementName = txt_AgreementName.Text;
if (string.IsNullOrEmpty(agreementName))
{
new FrmDialog("请输入协议名称!").ShowDialog();
return;
}
//校验协议名称不能重复
string bps = cmb_Bps.SelectedValue.ToString();
bool parseBps = int.TryParse(bps, out int bpsVal);
if (!parseBps)
{
new FrmDialog("波特率格式错误!").ShowDialog();
return;
}
//发送周期
string sendPeriod = txt_SendPeriod.Text;
bool parseSendPeriod = int.TryParse(sendPeriod, out int sendPeriodVal);
if (!parseSendPeriod)
{
new FrmDialog("发送周期格式错误!").ShowDialog();
return;
}
//发送转速ID
string sendId = txt_SendId.Text;
if (!IsHexNumber(sendId))
{
new FrmDialog("发送转速ID格式错误").ShowDialog();
return;
}
//发送帧长度
string frameLen = txt_FrameLen.Text;
bool parseFrameLen = int.TryParse(frameLen, out int frameLenVal);
if (!parseFrameLen)
{
new FrmDialog("发送帧长度格式错误!").ShowDialog();
return;
}
//接收周期
string recivePeriod = txt_RecivePeriod.Text;
bool parseRecivePeriod = int.TryParse(recivePeriod, out int recivePeriodVal);
if (!parseRecivePeriod)
{
new FrmDialog("接收周期格式错误!").ShowDialog();
return;
}
//接收转速ID
string reciveId = txt_ReciveId.Text;
if (!IsHexNumber(reciveId))
{
new FrmDialog("接收转速ID格式错误").ShowDialog();
return;
}
string msgFormat = cmb_MsgFormat.SelectedValue.ToString();
string frameType = cmb_FrameType.SelectedValue.ToString();
agreementVM.AgreementConfig.AgreementName = agreementName;
agreementVM.AgreementConfig.Bps = bpsVal;
agreementVM.AgreementConfig.MsgFormat = msgFormat;
agreementVM.AgreementConfig.FrameType = frameType;
agreementVM.AgreementConfig.SendPeriod = sendPeriodVal;
agreementVM.AgreementConfig.SendId = sendId;
agreementVM.AgreementConfig.FrameLen = frameLenVal;
agreementVM.AgreementConfig.ReciveId = reciveId;
agreementVM.AgreementConfig.RecivePeriod = recivePeriodVal;
agreementVM.AgreementConfig.ReciveSignalVal=txt_SignalVal.Text;
#endregion
#region 发送明细
DataGridViewRow sendDataRow = GridView_SendSpeed.Rows[0];
string msgName = sendDataRow.Cells["MsgName"].Value.ToString();
string startBit = sendDataRow.Cells["StartBit"].Value.ToString();
bool parseStartBit = int.TryParse(startBit, out int startBitVal);
if (!parseStartBit)
{
new FrmDialog("发送转速报文起始位格式错误!").ShowDialog();
return;
}
string len = sendDataRow.Cells["Len"].Value.ToString();
bool parseLen = int.TryParse(len, out int lenVal);
if (!parseLen)
{
new FrmDialog("发送转速报文长度格式错误!").ShowDialog();
return;
}
string precision = sendDataRow.Cells["Precision"].Value.ToString();
bool parsePrecision = decimal.TryParse(precision, out decimal precisionVal);
if (!parsePrecision)
{
new FrmDialog("发送转速报文精度格式错误!").ShowDialog();
return;
}
string offset = sendDataRow.Cells["Offset"].Value.ToString();
bool parseOffset = decimal.TryParse(offset, out decimal offsetVal);
if (!parseOffset)
{
new FrmDialog("发送转速报文偏移量格式错误!").ShowDialog();
return;
}
T_Agreement_Detail_Rule sendDetailRule = new T_Agreement_Detail_Rule
{
AgreementName = agreementName,
MsgType = "send",
MsgName = msgName,
StartBit = startBitVal,
Len = lenVal,
Precision = precisionVal,
Offset = offsetVal
};
agreementVM.AgreementDetailRuleList.Add(sendDetailRule);
#endregion
#region 接收明细
DataGridViewRow reciveDataRow = GridView_Recive.Rows[0];
string reciveMsgName = reciveDataRow.Cells["ReciveMsgName"].Value.ToString();
string reciveStartBit = reciveDataRow.Cells["ReciveStartBit"].Value.ToString();
bool parseReciveStartBit = int.TryParse(reciveStartBit, out int reciveStartBitVal);
if (!parseReciveStartBit)
{
new FrmDialog("接收报文起始位格式错误!").ShowDialog();
return;
}
string reciveLen = reciveDataRow.Cells["ReciveLen"].Value.ToString();
bool parseReciveLen = int.TryParse(reciveLen, out int reciveLenVal);
if (!parseReciveLen)
{
new FrmDialog("接收报文长度格式错误!").ShowDialog();
return;
}
string recivePrecision = reciveDataRow.Cells["RecivePrecision"].Value.ToString();
bool parseRecivePrecision = decimal.TryParse(recivePrecision, out decimal recivePrecisionVal);
if (!parseRecivePrecision)
{
new FrmDialog("接收报文精度格式错误!").ShowDialog();
return;
}
string reciveOffset = reciveDataRow.Cells["ReciveOffset"].Value.ToString();
bool parseReciveOffset = decimal.TryParse(reciveOffset, out decimal reciveOffsetVal);
if (!parseReciveOffset)
{
new FrmDialog("接收报文偏移量格式错误!").ShowDialog();
return;
}
T_Agreement_Detail_Rule reciveDetailRule = new T_Agreement_Detail_Rule
{
AgreementName = agreementName,
MsgType = "recive",
MsgName = reciveMsgName,
StartBit = reciveStartBitVal,
Len = reciveLenVal,
Precision = recivePrecisionVal,
Offset = reciveOffsetVal
};
agreementVM.AgreementDetailRuleList.Add(reciveDetailRule);
#endregion
#region 附加明细
int additionalPeriodVal = 100;
string additionalContent = string.Empty;
DataGridViewRow additionalDataRow = GridView_Additional.Rows[0];
string additionalSendId = additionalDataRow.Cells["AdditionalSendId"].Value?.ToString();
if (!string.IsNullOrEmpty(additionalSendId))
{
string additionalPeriod = additionalDataRow.Cells["AdditionalPeriod"].Value.ToString();
bool parseAdditionalPeriod = int.TryParse(additionalPeriod, out additionalPeriodVal);
if (!parseAdditionalPeriod)
{
new FrmDialog("附加报文周期格式错误!").ShowDialog();
return;
}
if (!IsHexNumber(additionalSendId))
{
new FrmDialog("附加报文ID格式错误").ShowDialog();
return;
}
additionalContent = additionalDataRow.Cells["AdditionalContent"].Value.ToString();
if (string.IsNullOrEmpty(additionalContent))
{
new FrmDialog("附加报文内容不能为空!").ShowDialog();
return;
}
}
T_Agreement_Additional_Rule additionalRule = new T_Agreement_Additional_Rule
{
AgreementName = agreementName,
AdditionalPeriod = additionalPeriodVal,
AdditionalSendId = additionalSendId,
AdditionalContent = additionalContent
};
agreementVM.AgreementAdditionalRuleList.Add(additionalRule);
#endregion
#region 辅助位
foreach (DataGridViewRow row in gridView_FuZhu.Rows)
{
string bit7 = row.Cells["Bit7"].Value.ToString();
bool.TryParse(bit7, out bool bit7Val);
string bit6 = row.Cells["Bit6"].Value.ToString();
bool.TryParse(bit6, out bool bit6Val);
string bit5 = row.Cells["Bit5"].Value.ToString();
bool.TryParse(bit5, out bool bit5Val);
string bit4 = row.Cells["Bit4"].Value.ToString();
bool.TryParse(bit4, out bool bit4Val);
string bit3 = row.Cells["Bit3"].Value.ToString();
bool.TryParse(bit3, out bool bit3Val);
string bit2 = row.Cells["Bit2"].Value.ToString();
bool.TryParse(bit2, out bool bit2Val);
string bit1 = row.Cells["Bit1"].Value.ToString();
bool.TryParse(bit1, out bool bit1Val);
string bit0 = row.Cells["Bit0"].Value.ToString();
bool.TryParse(bit0, out bool bit0Val);
string sortIndex = row.Cells["SortIndex"].Value.ToString();
int.TryParse(sortIndex, out int sortIndexVal);
T_Agreement_Help_Rule helpRule = new T_Agreement_Help_Rule
{
AgreementName = agreementName,
Bit7 = bit7Val,
Bit6 = bit6Val,
Bit5 = bit5Val,
Bit4 = bit4Val,
Bit3 = bit3Val,
Bit2 = bit2Val,
Bit1 = bit1Val,
Bit0 = bit0Val,
SortIndex = sortIndexVal
};
agreementVM.AgreementHelpRuleList.Add(helpRule);
}
//辅助位至少64条不够64补到64条
int count = agreementVM.AgreementHelpRuleList.Count;
while (count < 64)
{
agreementVM.AgreementHelpRuleList.Add(new T_Agreement_Help_Rule()
{
AgreementName = agreementName,
SortIndex = count++
});
}
#endregion
bool saveResult = _agreementConfigService.SaveAgreement(agreementVM, agreementName);
if (saveResult)
{
new FrmDialog("操作成功!").ShowDialog();
}
else
{
new FrmDialog("操作失败!").ShowDialog();
}
}
/// <summary>
/// 锁定控件
/// </summary>
/// <param name="enabled"></param>
private void LockControl(bool enabled = false)
{
groupBox1.Enabled = enabled;
groupBox2.Enabled = enabled;
groupBox3.Enabled = enabled;
groupBox4.Enabled = enabled;
groupBox5.Enabled = enabled;
groupBox6.Enabled = enabled;
}
/// <summary>
/// 判断是否是十六进制字符串
/// </summary>
/// <param name="value"></param>
/// <returns>true是 false不是</returns>
public static bool IsHexNumber(string value)
{
// 正则表达式匹配16进制数
return Regex.IsMatch(value, @"^\s*[A-Fa-f0-9]+\s*$");
}
}
}