using HML;
using JinYuan.CommunicationLib.Enum;
using JinYuan.ControlCenters;
using JinYuan.Helper;
using JinYuan.MES.Models;
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using Newtonsoft.Json;
using PLCCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LargeSquareOne
{
public partial class FrmDataMonitor : Form
{
private System.Threading.Timer m_RefreshTimer;
///
/// 上料条码电池数据显示
///
private BindingList listTA = new BindingList();
///
/// 下料电池数据绑定委托
///
///
private BindingList listTB = new BindingList();
public FrmDataMonitor()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.dgvFeedingData.AutoGenerateColumns = false;
this.dgvFeedingData.DataSource = listTA;
this.dgvFeedingData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
this.dgvFeedingData.DoubleBufferedDataGirdView(true);
this.dgvBlankingData.AutoGenerateColumns = false;
this.dgvBlankingData.DataSource = listTB;
this.dgvBlankingData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
this.dgvBlankingData.DoubleBufferedDataGirdView(true);
this.lVConfigRange.DoubleBufferedListView(true);
//this.lst_Info.DoubleBufferedListView(true);
//this.lis_Info.DoubleBufferedListBox(true);
btStop.Enabled = false;
//m_RefreshTimer = new System.Threading.Timer(TimerWork_Elapsed, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
//StartTimer(m_RefreshTimer);//开启刷新
this.FormClosing += (sender, e) =>
{
//this.updateTimer.Stop();
};
}
private void FrmDataMonitor_Load(object sender, EventArgs e)
{
btSatrt.Enabled = false;
dgvFeedingData.ClearSelection();
//dgvBlankingData.ClearSelection();
SetCombOrg();
IsNoOrg = true;
}
private string CurrentTime
{
get { return DateTime.Now.ToString("HH:mm:ss"); }
}
///
/// 从数据库获取产品型号并绑定到下拉框中
///
///
bool IsModelInit = false;
public void SetCombOrg()
{
try
{
txtMaterialCode.Text = "";
CommonMethods.mesConfig.MaterialCode = "";
var productList = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetProdTypeSql());
if (!IsModelInit)
{
DropDownListPlusItemCollection list = new DropDownListPlusItemCollection();
for (int i = 0; i < productList.Count; i++)
{
list.Add(new DropDownListPlusItem() { Text = productList[i].ModelName, Tag = productList[i].MaterialCode });
}
this.cmbModelType.BindItemsSource(list);
IsModelInit = true;
}
if (productList != null && productList.Count > 0)
{
CommonMethods.mesConfig.ModelName = "";
CommonMethods.mesConfig.MaterialCode = "";
DropDownListPlusItem gg = cmbModelType.SelectedItem;
if (gg != null)
{
CommonMethods.mesConfig.ModelName = gg.Text.ToString();
CommonMethods.mesConfig.MaterialCode = gg.Tag.ToString();
txtMaterialCode.Text = CommonMethods.mesConfig.MaterialCode;
}
}
}
catch (Exception ex)
{
//LogHelper.Instance.WriteEx("获取数据库型号异常", ex);
MessageBox.Show($"获取数据库型号异常:{ex}");
}
}
public bool IsNoOrg = false;
public bool IsSetParam = false;
private void cmbModelType_SelectedItemsChanged(object sender, EventArgs e)
{
if (IsNoOrg && CommonMethods.IsLoginOk)
{
btSatrt.Enabled = true;
IsSetParam = false;
SetCombOrg();
GetLocalParaData();
}
else
{
this.cmbModelType.BindItemsSource(null);
IsModelInit = false;
SetCombOrg();
new FrmMsgBoxOutWithAck(2, "当前未登录,请先登录再操作!", "提示").ShowDialog();
}
}
public void GetLocalParaData()
{
if (string.IsNullOrEmpty(CommonMethods.mesConfig.ModelName))
{
new FrmMsgBoxOutWithAck(2, "产品型号不能为空!", "型号选择").Show();
return;
}
try
{
var list = CommonMethods.db.QuerySqlList(CommonMethods.dBSQL.GetParamSql(CommonMethods.mesConfig.ModelName));
if (list != null && list.Count > 0)
{
CommonMethods.paramlists = list;
IsSetParam = false;
GetParam();
}
}
catch (Exception ex)
{
LoggerHelp.WriteEX(ex);
}
}
///
/// 刷新参数
///
public void GetParam()
{
try
{
if (!IsSetParam && CommonMethods.paramlists != null)
{
System.Windows.Forms.ListViewItem listItem = null;
lVConfigRange.Items.Clear();
int j = 0;
foreach (var ProductPara in CommonMethods.paramlists) //遍历list集合
{
string strRemark = ProductPara.Remark;
string strName = ProductPara.ParaName;
listItem = new System.Windows.Forms.ListViewItem(CommonMethods.paramlists[j].Remark);
listItem.SubItems.Add("0");
listItem.SubItems.Add("0");
listItem.SubItems.Add("0");
lVConfigRange.Items.Add(listItem);
lVConfigRange.Items[j].SubItems[1].Text = ProductPara.ParaValueMin;
lVConfigRange.Items[j].SubItems[2].Text = ProductPara.ParaValueMax;
lVConfigRange.Items[j].SubItems[3].Text = ProductPara.Uint;
switch (strName)
{
//case "intoShellPressure1SettingA07": //入壳压力1
// CommonMethods.parRange.PrePressure1Max = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax);
// break;
//case "intoShellPressure2SettingA07": //入壳压力2
// CommonMethods.parRange.PrePressure2Max = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax);
// break;
//case "intoShellPressure3SettingA07": //入壳压力3
// CommonMethods.parRange.PrePressure3Max = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax);
// break;
//default:
// break;
}
j++;
}
IsSetParam = true;
}
}
catch (Exception ex)
{
LoggerHelp.WriteEX("获取参数失败", ex);
}
}
///
/// 加载MES参数
///
public bool GetMesParaData(ref string strErr)
{
try
{
bool isChangeParam = false;
var nowDate = DateTime.Now;
string filePath = $@"{CommonMethods.strMesLogspath}\参数设定值请求\变更记录\{nowDate.Year}\{nowDate.Month}";
string fileName = $@"{nowDate.Day}.csv";
string fileTitle = "时间,机台,MES账户,更改参数名称,更改前参数值,更改后参数值";
string fileContent = string.Empty;
DataTable dt = new DataTable();
dt.Columns.Add("MES账户");
dt.Columns.Add("更改参数名称");
dt.Columns.Add("更改前参数值");
dt.Columns.Add("更改后参数值");
string resJson = "";
var result = CommonMethods.hbgMes.GetParamSetRequest(CommonMethods.mesConfig.ParamSetpointRequestMesUrl, CommonMethods.mesConfig.equipNum, CommonMethods.mesConfig.siteCode,
CommonMethods.mesConfig.lineCode, CommonMethods.mesConfig.MaterialCode, CommonMethods.mesConfig.mesUserName, ref resJson);
var resultData = JsonConvert.DeserializeObject(resJson);
if (!resultData.success)
{
//获取参数未成功 81022652
strErr = "获取MES参数不成功!";
return false;
}
List NewParamlists = CommonMethods.paramlists;
for (int i = 0; i < resultData.total; i++)
{
List rows = resultData.rows;
for (int j = 0; j < rows.Count; j++)
{
var tagList = rows[j].tagList;
for (int k = 0; k < NewParamlists.Count; k++)
{
for (int n = 0; n < rows[j].tagList.Count; n++)
{
if (NewParamlists[k].ParaName == tagList[n].tagCode)
{
string strMin = "0";
if (String.IsNullOrEmpty(tagList[n].minValue.Trim()) || tagList[n].minValue.Trim() == "null")
strMin = "0";
else
strMin = tagList[n].minValue.Trim();
if (NewParamlists[k].ParaValueMin != strMin)
{
fileContent += $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss")},{CommonMethods.mesConfig.equipNum}," +
$"{CommonMethods.mesConfig.mesUserName},{tagList[n].tagDescription}最小值,{NewParamlists[k].ParaValueMin},{strMin}\n";
dt.Rows.Add(CommonMethods.mesConfig.mesUserName, $"{tagList[n].tagDescription}最小值", NewParamlists[k].ParaValueMin, strMin);
NewParamlists[k].ParaValueMin = strMin;
isChangeParam = true;
}
string strMax = "0";
if (String.IsNullOrEmpty(tagList[n].maxValue.Trim()) || tagList[n].maxValue.Trim() == "null")
strMax = "0";
else
strMax = tagList[n].maxValue.Trim();
if (NewParamlists[k].ParaValueMax != tagList[n].maxValue)
{
fileContent += $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss")},{CommonMethods.mesConfig.equipNum}," +
$"{CommonMethods.mesConfig.mesUserName},{tagList[n].tagDescription}最大值,{NewParamlists[k].ParaValueMax},{strMax}\n";
dt.Rows.Add(CommonMethods.mesConfig.mesUserName, $"{tagList[n].tagDescription}最大值", NewParamlists[k].ParaValueMax, strMax);
NewParamlists[k].ParaValueMax = strMax;
isChangeParam = true;
}
}
}
}
if (isChangeParam)
{
CustomMessageBox customMessageBox = new CustomMessageBox(dt, $"当前MES下发参数与设备本地参数不一致,请确认是否使用MES下发参数");
if (customMessageBox.ShowDialog() == DialogResult.OK)
{
// 写csv文件
fileContent = fileContent.Substring(0, fileContent.Length - 1);
CSVHelper