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.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/ChemicalWeighingAction.cs

279 lines
9.4 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Data;
using System.IO;
using ICSharpCode.Core;
using Mesnac.Basic;
using Mesnac.Equips;
using Mesnac.Codd.Session;
using Mesnac.Action.Base;
using Mesnac.Controls.Base;
namespace Mesnac.Action.ChemicalWeighing
{
/// <summary>
/// 小料称量系统业务基类
/// </summary>
public class ChemicalWeighingAction : DatabaseAction
{
private static bool _isFirstRun = true; //是否首次运行
#region RunIni
/// <summary>
/// 初始化
/// </summary>
/// <param name="runtime"></param>
private void Ini(RuntimeParameter runtime)
{
if (_isFirstRun)
{
Global.PublicVar.Instance.ProjectWizardName = runtime.ProjectWizardName;
_isFirstRun = false;
}
}
/// <summary>
/// 接口方法
/// </summary>
/// <param name="sender"></param>
public new void RunIni(RuntimeParameter runtime)
{
this.Ini(runtime);
base.RunIni(runtime); //必须调用
}
#endregion
#region 日志方法
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="operateType">操作类别</param>
/// <param name="caption">日志标题</param>
/// <param name="content">日志内容</param>
public void DBLog(string operateType, string caption, string content)
{
BaseDataHelper.DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, operateType, caption, content);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="content">日志内容</param>
public void DBLog(string content)
{
BaseDataHelper.DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, this.FormText, this.FunctionText, content);
}
#endregion
#region 辅助方法
#region 刷新自定义设备变量
/// <summary>
/// 异步刷新自定义设备变量
/// </summary>
public void RefreshCustomEquip()
{
try
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ReadEquip));
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ChemicalWeighingAction>.Error("异步刷新自定义设备变量失败:" + ex.Message);
}
}
private void ReadEquip(Object stateInfo)
{
try
{
string equipName = base.GetConfigValue("Customer.EquipName", "FeedingCustomer"); //获取PLC设备名称
if (Mesnac.Equips.Factory.Instance.AllEquips.ContainsKey(equipName))
{
Mesnac.Equips.Factory.Instance.AllEquips[equipName].Read();
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ChemicalWeighingAction>.Error("刷新自定义设备变量失败:" + ex.Message);
}
}
#endregion
#region 获取界面数据库控件值
/// <summary>
/// 获取配方界面字段值
/// </summary>
/// <param name="key">界面字段的业务标识</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回string类型的配方界面字段值</returns>
public string GetValueByMCKey(string key, string defaultValue)
{
string result = defaultValue;
DbMCControl control = null;
List<DbMCControl> controls = base.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, key);
foreach (DbMCControl c in controls)
{
if (c.BaseControl.DbOptionType == DbOptionTypes.Modify ||
c.BaseControl.DbOptionType == DbOptionTypes.QueryAndModify)
{
control = c;
break;
}
}
if (control != null)
{
if (control.BaseControl is CheckBox)
{
if ((control.BaseControl as CheckBox).Checked)
{
return "1";
}
else
{
return "0";
}
}
else
{
object value = control.BaseControl.MCValue;
if (value != null)
{
return value.ToString();
}
}
}
return defaultValue;
}
/// <summary>
/// 获取配方界面字段值
/// </summary>
/// <param name="key">界面字段的业务标识</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回int类型的配方界面字段值</returns>
public int GetValueByMCKey(string key, int defaultValue)
{
int result = defaultValue;
string strResult = GetValueByMCKey(key, String.Empty);
if (int.TryParse(strResult, out result))
{
return result;
}
return defaultValue;
}
/// <summary>
/// 获取配方界面字段值
/// </summary>
/// <param name="key">界面字段的业务标识</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回float格式的配方界面字段值</returns>
public float GetValueByMCKey(string key, float defaultValue)
{
float result = defaultValue;
string strResult = GetValueByMCKey(key, String.Empty);
if (float.TryParse(strResult, out result))
{
return result;
}
return defaultValue;
}
/// <summary>
/// 获取界面字段值
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public decimal GetValueByMCKey(string key, decimal defaultValue)
{
decimal result = defaultValue;
string strResult = GetValueByMCKey(key, String.Empty);
if (decimal.TryParse(strResult, out result))
{
return result;
}
return defaultValue;
}
/// <summary>
/// 获取配方界面字段值
/// </summary>
/// <param name="key">界面字段的业务标识</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回DateTime格式的配方界面字段值</returns>
public DateTime GetValueByMCKey(string key, DateTime defaultValue)
{
DateTime result = defaultValue;
string strResult = GetValueByMCKey(key, String.Empty);
if (DateTime.TryParse(strResult, out result))
{
return result;
}
return defaultValue;
}
#endregion
#region 设置界面数据库控件值
/// <summary>
/// 设置界面数据库控件值
/// </summary>
/// <param name="key">界面字段的业务标识</param>
/// <param name="value">要设置的值</param>
/// <returns>返回字符串格式的配方界面字段值</returns>
public void SetValueByMCKey(string key, object value)
{
DbMCControl control = null;
List<DbMCControl> controls = base.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, key);
foreach (DbMCControl c in controls)
{
if (c.BaseControl.DbOptionType == DbOptionTypes.Modify ||
c.BaseControl.DbOptionType == DbOptionTypes.QueryAndModify)
{
control = c;
break;
}
}
if (control != null)
{
control.BaseControl.MCValue = value;
}
}
#endregion
#region 设置GridViewRow背景色
public static void SetBackColor(DataGridView grid, string columnName, object filterValue, System.Drawing.Color c)
{
lock (String.Empty)
{
try
{
if (grid == null)
{
ICSharpCode.Core.LoggingService.Warn("设置GridViewRow背景色失败网格控件为null");
return;
}
foreach (DataGridViewRow row in grid.Rows)
{
if (filterValue.Equals(row.Cells[columnName].Value))
{
row.DefaultCellStyle.BackColor = c;
}
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ChemicalWeighingAction>.Error("设置GridViewRow背景色失败" + ex.Message);
}
}
}
#endregion
#endregion
}
}