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.
397 lines
12 KiB
C#
397 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using Mesnac.Equips.BaseInfo;
|
|
using System.Reflection;
|
|
using Mesnac.Equips.SetConfig;
|
|
|
|
namespace Mesnac.Equips
|
|
{
|
|
/// <summary>
|
|
/// 设备工厂
|
|
/// </summary>
|
|
public class Factory
|
|
{
|
|
#region 单例模式
|
|
private static Factory _this;
|
|
public static Factory Instance
|
|
{
|
|
get
|
|
{
|
|
if (null == _this)
|
|
_this = new Factory();
|
|
return _this;
|
|
}
|
|
}
|
|
private Factory()
|
|
{
|
|
Mesnac.Gui.Workbench.WorkbenchSingleton.Workbench.ShutDown += new EventHandler(Workbench_ShutDown);
|
|
this.AllEquips = new Dictionary<string, BaseEquip>();
|
|
}
|
|
|
|
private void Workbench_ShutDown(object sender, EventArgs e)
|
|
{
|
|
AllClose();
|
|
}
|
|
#endregion
|
|
|
|
#region 属性
|
|
/// <summary>
|
|
/// 读取信息返回参数
|
|
/// </summary>
|
|
public ReadEventArgs readEventArgs = new ReadEventArgs();
|
|
/// <summary>
|
|
/// 项目名称
|
|
/// </summary>
|
|
public string ProjectWizardName { get; set; }
|
|
|
|
|
|
public Dictionary<string, BaseEquip> AllEquips { get; set; }
|
|
|
|
private string _configFile = string.Empty;
|
|
public string ConfigFile
|
|
{
|
|
get
|
|
{
|
|
return _configFile;
|
|
}
|
|
set
|
|
{
|
|
if (_configFile != value)
|
|
{
|
|
if (dataform != null && (!dataform.IsDisposed))
|
|
{
|
|
dataform.Close();
|
|
dataform.Dispose();
|
|
}
|
|
_configFile = value;
|
|
if (string.IsNullOrWhiteSpace(this._configFile) || (!File.Exists(this._configFile)))
|
|
{
|
|
//throw new Exception("配置文件不正确!");
|
|
}
|
|
ReadConfig();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 函数实现
|
|
/// <summary>
|
|
/// 获取单个设备
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public BaseEquip Get(string key)
|
|
{
|
|
BaseEquip Result = null;
|
|
AllEquips.TryGetValue(key, out Result);
|
|
return Result;
|
|
}
|
|
/// <summary>
|
|
/// 打开所有设备
|
|
/// </summary>
|
|
public void AllOpen()
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.Open();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 启动数据读取
|
|
/// </summary>
|
|
public void AllReadStart()
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.Start();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 停止数据读取
|
|
/// </summary>
|
|
public void AllReadStop()
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.Stop();
|
|
equip.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 关闭设备
|
|
/// </summary>
|
|
public void AllClose()
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置读取事件
|
|
/// </summary>
|
|
/// <param name="readEvent"></param>
|
|
public void SetAllReadDataEvent(BaseEquip.ReadDataHandler readEvent)
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.EquipReadData += readEvent;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置最小读取频率设备的读取事件
|
|
/// </summary>
|
|
/// <param name="readEvent"></param>
|
|
public void SetMinHZReadDataEvent(BaseEquip.ReadDataHandler readEvent)
|
|
{
|
|
#region 查找读取频率最小的设备
|
|
|
|
BaseEquip minRZEquip = null;
|
|
foreach (BaseEquip equip in Mesnac.Equips.Factory.Instance.AllEquips.Values)
|
|
{
|
|
if (minRZEquip == null)
|
|
{
|
|
minRZEquip = equip;
|
|
}
|
|
else
|
|
{
|
|
if (equip.Main.ReadHz < minRZEquip.Main.ReadHz)
|
|
{
|
|
minRZEquip = equip;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//订阅读取频率最小设备的读取事件
|
|
if (minRZEquip != null)
|
|
{
|
|
minRZEquip.EquipReadData += readEvent;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置异常事件
|
|
/// </summary>
|
|
/// <param name="exceptEvent"></param>
|
|
public void SetAllExceptEvent(BaseEquip.ExceptHandler exceptEvent)
|
|
{
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
equip.OnExcept += exceptEvent;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 设置
|
|
#region 打开数据设置界面
|
|
|
|
DataConfigForm dataform = null;
|
|
public void ManageEquip(Mesnac.PlugIn.Workbench.IWorkbench workbench)
|
|
{
|
|
string projectName = this.ProjectWizardName;
|
|
if (dataform == null || dataform.IsDisposed)
|
|
{
|
|
dataform = new DataConfigForm(_configFile, projectName);
|
|
}
|
|
dataform.InitializeConfigInfo();
|
|
workbench.ShowView(dataform);
|
|
}
|
|
public void EditEquipData(string projectName)
|
|
{
|
|
if (dataform == null || dataform.IsDisposed)
|
|
{
|
|
dataform = new DataConfigForm(_configFile, projectName);
|
|
}
|
|
dataform.InitializeConfigInfo();
|
|
dataform.ShowDialog();
|
|
}
|
|
#endregion
|
|
#region 读取配置
|
|
public event EventHandler OnConfigChanged;
|
|
public void ReadConfig()
|
|
{
|
|
string projectName = this.ProjectWizardName;
|
|
if (this.AllEquips == null)
|
|
{
|
|
this.AllEquips = new Dictionary<string, BaseEquip>();
|
|
}
|
|
this.AllReadStop();
|
|
this.AllEquips.Clear();
|
|
//扩展配置设备
|
|
ConfigEquipFactory configEquipFactory = new ConfigEquipFactory(_configFile, projectName);
|
|
this.AllConfigEquip = configEquipFactory.GetAllConfigEquip();
|
|
this.AllEquips = configEquipFactory.GetBaseEquip();
|
|
|
|
this.readEventArgs.Data.Clear();
|
|
foreach (BaseEquip equip in this.AllEquips.Values)
|
|
{
|
|
foreach (Group group in equip.Group.Values)
|
|
{
|
|
foreach (Data data in group.Data.Values)
|
|
{
|
|
if (!this.readEventArgs.Data.ContainsKey(data.KeyName))
|
|
{
|
|
this.readEventArgs.Data.Add(data.KeyName, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (OnConfigChanged != null)
|
|
{
|
|
OnConfigChanged(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
#endregion
|
|
#region 保存配置
|
|
public void SaveConfig()
|
|
{
|
|
if (dataform == null)
|
|
{
|
|
return;
|
|
}
|
|
dataform.SaveConfig();
|
|
ReadConfig();
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
#region 读取数据
|
|
public void Read()
|
|
{
|
|
foreach (BaseEquip equip in this.AllEquips.Values)
|
|
{
|
|
equip.Read();
|
|
}
|
|
}
|
|
public void ReadAll()
|
|
{
|
|
foreach (BaseEquip equip in this.AllEquips.Values)
|
|
{
|
|
equip.ReadAll();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 扩展配置设备
|
|
|
|
/// <summary>
|
|
/// 所有的配置设备
|
|
/// </summary>
|
|
public Dictionary<string, ConfigEquip> AllConfigEquip { get; set; }
|
|
|
|
#endregion
|
|
|
|
public object getDataAfterMethod(object[] dataBefore, string dataKey)
|
|
{
|
|
Data data = FindDataByKey(dataKey);
|
|
if (data == null)
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
|
|
return Mesnac.Maths.Factory.Instance.Execute(FormatMethod(data), dataBefore);
|
|
}
|
|
}
|
|
|
|
public Data dd = new Data();
|
|
private string FormatMethod(Data fdata)
|
|
{
|
|
string Result = fdata.Method;
|
|
string[] parameters = Mesnac.Maths.Factory.Instance.GetParameters(Mesnac.Maths.Factory.Instance.GetMethodParameter(fdata.Method));
|
|
foreach (string ss in parameters)
|
|
{
|
|
double d = 0;
|
|
if (double.TryParse(ss, out d))
|
|
{
|
|
continue;
|
|
}
|
|
bool isFind = false;
|
|
object obj = null;
|
|
Data data = null;
|
|
if (Factory.Instance.readEventArgs.Data.TryGetValue(ss, out obj))
|
|
{
|
|
isFind = true;
|
|
}
|
|
if (!isFind)
|
|
{
|
|
string key = fdata.Group.Equip.Name + "." + ss;
|
|
if (Instance.readEventArgs.Data.TryGetValue(key, out obj))
|
|
{
|
|
isFind = true;
|
|
}
|
|
}
|
|
if (!isFind)
|
|
{
|
|
string key = fdata.Group.Equip.Name + "." + fdata.Group.Name + "." + ss;
|
|
if (Instance.readEventArgs.Data.TryGetValue(key, out obj))
|
|
{
|
|
isFind = true;
|
|
}
|
|
}
|
|
if (!isFind)
|
|
{
|
|
foreach (BaseEquip equip in Instance.AllEquips.Values)
|
|
{
|
|
foreach (Group group in equip.Group.Values)
|
|
{
|
|
isFind = group.Data.TryGetValue(ss, out data);
|
|
if (isFind)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if (isFind)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (isFind && data != null)
|
|
{
|
|
obj = data.Value;
|
|
}
|
|
if (isFind && obj != null)
|
|
{
|
|
Result = Result.Replace(ss, obj.ToString());
|
|
}
|
|
}
|
|
return Result;
|
|
}
|
|
private Data FindDataByKey(string key)
|
|
{
|
|
Data da = new Data();
|
|
bool isfand = false;
|
|
foreach (BaseEquip equip in AllEquips.Values)
|
|
{
|
|
foreach (Group group in equip.Group.Values)
|
|
{
|
|
foreach (Data data in group.Data.Values)
|
|
{
|
|
if (data.RunName == key)
|
|
{
|
|
isfand = true;
|
|
da = data;
|
|
dd = data;
|
|
break;
|
|
}
|
|
}
|
|
if (isfand)
|
|
{
|
|
break;
|
|
}
|
|
if (isfand)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return da;
|
|
}
|
|
}
|
|
}
|