using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;
using ICSharpCode.Core;
using Mesnac.Controls.Default;
namespace Mesnac.Action.ChemicalWeighing.Product.StorageBinViewDio
{
///
/// 料仓参数设置:弹窗初始化
///
public class SBVSetFromViewAction : ChemicalWeighingAction,IAction
{
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime); //必须调用
ICSharpCode.Core.LoggingService.Debug("料仓参数设置");
#region 料仓号获取
SpecialButton2 CurrentSB2 = runtime.Sender as SpecialButton2;
int _sbvNumber = CurrentSB2.SbvNumber;
if(_sbvNumber == 0)
{
ICSharpCode.Core.LoggingService.Warn(String.Format("请设置正确的料仓号!"));
return;
}
#endregion
#region 初始化料仓设置窗口
StorageBinViewSetForm frmSVBSForm = new StorageBinViewSetForm(_sbvNumber);
frmSVBSForm.Text = _sbvNumber + frmSVBSForm.Text;
#endregion
#region 从PLC读取已有参数并显示
#region 变量定义
int offset = 0; //偏移量
int[] dataValue = null;
RT_Parameter entity = new RT_Parameter();
string readEquipRunName = String.Empty;
System.Reflection.PropertyInfo[] pis = null;
string tableName = "FeederParameter" + _sbvNumber + "Data";
#endregion
#region 读取料仓参数的数据结构
List readerList = PlcSchemaHelper.Instance.GetCWSSPlcReader(tableName);
if (readerList == null || readerList.Count == 0)
{
ICSharpCode.Core.LoggingService.Warn(String.Format("设置料仓时读取PLC数据结构失败:PlcSchema.xml中没有对应WeighDataInfo的配置节!"));
return;
}
readEquipRunName = readerList[0].PlcFiledName;
#endregion
#region 获取PLC品牌名称,处理西门子与其他PLC差异
foreach (PlcRecipeReader reader in readerList)
{
string equipBrand = Mesnac.Equips.Factory.Instance.GetEquipBrandByRunName(reader.PlcFiledName);
reader.EquipBrand = equipBrand;
foreach (PlcRecipeReaderItem readerItem in reader.ItemList)
{
readerItem.EquipBrand = equipBrand;
}
}
#endregion
#region 读取料仓设置数据
if (!BasePlcHelper.Instance.PlcReadByRunName(readEquipRunName, out dataValue))
{
ICSharpCode.Core.LoggingService.Warn(String.Format("基础报表数据存盘失败:读取对应的PLC设备变量[{0}]数据失败!", readEquipRunName));
return;
}
else
{
ICSharpCode.Core.LoggingService.Debug(String.Format("基础报表数据存盘_读取PLC设备变量[{0}]成功,长度={1}", readEquipRunName, dataValue.Length));
}
#endregion
#region 整理读取到的数据
foreach (PlcRecipeReader reader in readerList)
{
pis = typeof(Entity.RT_Parameter).GetProperties();
foreach (PlcRecipeReaderItem item in reader.ItemList)
{
int[] value = new int[item.ValueLen];
Array.Copy(dataValue, offset, value, 0, value.Length);
item.SetValue = value;
offset += value.Length;
foreach (System.Reflection.PropertyInfo pi in pis)
{
if (pi.Name == item.DataFieldName)
{
object objValue = null;
try
{
objValue = Mesnac.Basic.DataProcessor.ChangeType(item.GetValue(), pi.PropertyType);
}
catch
{
objValue = item.GetValue();
}
pi.SetValue(entity, objValue, null);
}
}
}
}
#endregion
#region 显示在画面上
frmSVBSForm.numericUpDown_HighMax.Value = (decimal)entity.Big_MaxSpeed;
frmSVBSForm.numericUpDown_HighMin.Value = (decimal)entity.Big_MinSpeed;
frmSVBSForm.numericUpDown_BigF.Value = (decimal)entity.Big_F;
frmSVBSForm.numericUpDown_LowMax.Value = (decimal)entity.Small_MaxSpeed;
frmSVBSForm.numericUpDown_LowMin.Value = (decimal)entity.Small_MinSpeed;
frmSVBSForm.numericUpDown_SmallF.Value = (decimal)entity.Small_F;
#endregion
#endregion
#region 画面参数下传至PLC
DialogResult result = frmSVBSForm.ShowDialog();
if (result == DialogResult.OK)
{
//将最新值先更新到数据库表中
entity.Big_MaxSpeed = (int)frmSVBSForm.numericUpDown_HighMax.Value;
entity.Big_MinSpeed = (int)frmSVBSForm.numericUpDown_HighMin.Value;
entity.Big_F = (int)frmSVBSForm.numericUpDown_BigF.Value;
entity.Small_MaxSpeed = (int)frmSVBSForm.numericUpDown_LowMax.Value;
entity.Small_MinSpeed = (int)frmSVBSForm.numericUpDown_LowMin.Value;
entity.Small_F = (int)frmSVBSForm.numericUpDown_SmallF.Value;
//下传写入
ICSharpCode.Core.LoggingService.Debug("下传" + _sbvNumber + "号料仓参数");
try
{
List msgList = new List();
bool downresult = ChemicalWeighingPlc.PlcStorageBinParaSetHelper.DownloadStorageBinPara(msgList, tableName, entity, _sbvNumber);
if (downresult)
{
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_SBVSetFromViewAction_DownloadAction_msg1")); //下传料仓参数完毕!
#region 记录操作日志
//base.DBLog(msg1);
#endregion
MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_SBVSetFromViewAction_DownloadAction_msg2")); //下传料仓参数失败!
string msg2 = Mesnac.Basic.DataProcessor.CombineMsgList(msgList);
#region 记录操作日志
base.DBLog(msg2);
#endregion
MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("下传" + _sbvNumber + "号料仓参数异常:" + ex.Message, ex);
#region 记录操作日志
//base.DBLog(ex.Message);
#endregion
MessageBox.Show(ex.Message, Mesnac.Basic.LanguageHelper.ErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
}
}
}