临时保存

添加仿真内容
修改旧的仿真调用
This commit is contained in:
hanqiangqiang
2026-07-07 14:58:23 +08:00
parent d930588fc2
commit 9ccbb49746
10 changed files with 952 additions and 25 deletions
@@ -0,0 +1,80 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorAI_AORandom : BaseDevice, IDevice
{
protected List<AI_AORandomData> AI_AOList = new List<AI_AORandomData>();
private readonly Random _rnd = new Random();
public SimulatorAI_AORandom(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
{
InitializeIValve(node);
}
/// <summary>
/// 根据XML配置创建MFC控制对象
/// </summary>
/// <param name="node"></param>
protected virtual void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("Random");
foreach (XmlElement xmlElement in valveNodes)
{
AI_AORandomData randomData = new AI_AORandomData()
{
Type = xmlElement.GetAttribute("type"),
Name = xmlElement.GetAttribute("ioName"),
BaseValue = Convert.ToSingle(xmlElement.GetAttribute("baseValue"))
};
AI_AOList.Add(randomData);
}
}
protected override void HandleMonitor()
{
Random();
}
protected virtual void Random()
{
foreach (var ai_ao in AI_AOList)
{
if (ai_ao.Type == "AI")
IO.AI[$"{Module}.{ai_ao.Name}"].Value = ai_ao.BaseValue + (float)_rnd.NextDouble();
else if (ai_ao.Type == "AO")
IO.AO[$"{Module}.{ai_ao.Name}"].Value = ai_ao.BaseValue + (float)_rnd.NextDouble();
}
}
public bool Initialize()
{
return true;
}
public void Reset()
{
}
public void Terminate()
{
}
}
public class AI_AORandomData
{
public string Type { get; set; }
public string Name { get; set; }
public float BaseValue { get; set; }
}
}
@@ -0,0 +1,188 @@
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorDeviceManager
{
private readonly Dictionary<string, IDevice> _nameDevice = new Dictionary<string, IDevice>();
private readonly object _lockerDevice = new object();
private readonly R_TRIG _trigExceed500 = new R_TRIG();
private readonly Dictionary<Type, List<IDevice>> _typeDevice = new Dictionary<Type, List<IDevice>>();
private readonly ConcurrentBag<IDevice> _optionDevice = new ConcurrentBag<IDevice>();
private PeriodicJob _thread;
/// <summary>
/// 设置或返回是否以异步方式初始化当前管理器中的设备。
/// </summary>
public bool DisableAsyncInitialize { get; set; }
/// <summary>
/// 返回当前设备管理器映射的XML配置文件节点。
/// </summary>
protected XmlElement DeviceModelNodes { get; private set; }
/// <summary>
/// 向设备管理器的设备字典中添加一个设备实例。
/// </summary>
/// <param name="device"></param>
protected virtual void QueueDevice(IDevice device)
{
QueueDevice($"{device.Module}.{device.Name}", device);
}
/// <summary>
/// 向设备管理器的设备字典中添加一个设备实例,并自定义该设备在字典中的Key。
/// </summary>
/// <param name="key"></param>
/// <param name="device"></param>
protected void QueueDevice(string key, IDevice device)
{
lock (_lockerDevice)
{
Debug.Assert(!_nameDevice.ContainsKey(key), "DeviceModel config file contains duplicated name " + key);
_nameDevice.Add(key, device);
}
}
public void Initialize(string modelFile, string type, ModuleName mod, string ioModule = "")
{
if (!File.Exists(modelFile))
{
throw new ApplicationException($"did not find the device model file {modelFile} ");
}
var xmlDocument = new XmlDocument();
try
{
xmlDocument.Load(modelFile);
if (!(xmlDocument.SelectSingleNode("DeviceModelDefine") is XmlElement xmlElement))
{
throw new ApplicationException($"device mode file {modelFile} is not valid");
}
var attribute = xmlElement.GetAttribute("type");
if (attribute != type)
{
throw new ApplicationException($"the type {attribute} in device mode file {modelFile} is inaccordance with the system type {type}");
}
DeviceModelNodes = xmlElement;
foreach (XmlNode childNode in xmlElement.ChildNodes)
{
if (childNode.NodeType == XmlNodeType.Comment || !(childNode is XmlElement xmlElement2))
{
continue;
}
var text = xmlElement2.GetAttribute("assembly");
if (string.IsNullOrEmpty(text))
{
text = "MECF.Framework.RT.EquipmentLibrary";
}
var text2 = xmlElement2.Name.Substring(0, xmlElement2.Name.Length - 1);
var text3 = xmlElement2.GetAttribute("classType");
if (string.IsNullOrEmpty(text3))
{
text3 = "Aitex.Core.RT.Device.Unit." + text2;
}
var assembly = Assembly.Load(text);
var type2 = assembly.GetType(text3);
if (type2 == null)
{
continue;
}
foreach (var childNode2 in xmlElement2.ChildNodes)
{
if (!(childNode2 is XmlElement xmlElement3))
{
LOG.Write("Device Model File contains non element node, " + (childNode2 as XmlNode).Value);
continue;
}
var device = Activator.CreateInstance(type2, mod.ToString(), xmlElement3, ioModule) as IDevice;
if (xmlElement3.HasAttribute("option") && Convert.ToBoolean(xmlElement3.Attributes["option"].Value))
{
_optionDevice.Add(device);
continue;
}
QueueDevice(device);
DATA.Subscribe(device, $"{device.Module}.{text2}.{device.Name}");
if (!_typeDevice.ContainsKey(type2))
{
_typeDevice[type2] = new List<IDevice>();
}
_typeDevice[type2].Add(device);
if (DisableAsyncInitialize)
{
InitDevice(device);
continue;
}
var task = Task.Run(delegate
{
InitDevice(device);
});
}
}
_thread = new PeriodicJob(200, Monitor, "SimulatorSystem", true);
}
catch (Exception ex)
{
LOG.Write(ex);
}
}
private void InitDevice(IDevice device)
{
try
{
if (!device.Initialize())
{
LOG.Write(device.Name + " initialize failed.");
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
}
/// <summary>
/// 后台扫面线程调用的方法。
/// </summary>
public bool Monitor()
{
lock (_lockerDevice)
{
foreach (var value in _nameDevice.Values)
{
try
{
PerformanceMonitor.MonitorRun(() =>
{
value.Monitor();
return true;
}, 500, _trigExceed500, out _, $"{value.Module}.{value.Name} monitor");
}
catch (Exception ex)
{
LOG.Write(ex, $"{ModuleName.Diagnosis} Monitor {value.Name} Exception");
}
}
}
return true;
}
}
}
@@ -0,0 +1,70 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorIoAO_AI : BaseDevice, IDevice
{
protected Dictionary<string, string> AO_AI = new Dictionary<string, string>();
public SimulatorIoAO_AI(string module, XmlElement node, string ioModule) : base(module, node, ioModule)
{
InitializeIValve(node);
}
/// <summary>
/// 根据XML配置创建气阀控制对象
/// </summary>
/// <param name="node"></param>
protected virtual void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("AO_AI");
foreach (XmlElement xmlElement in valveNodes)
{
string aiName = xmlElement.GetAttribute("ai");
string aoName = xmlElement.GetAttribute("ao");
if (!AO_AI.ContainsKey(aiName) && !AO_AI.ContainsKey(aoName))
AO_AI.Add(aiName, aoName);
var isDefaultOpen = xmlElement.GetAttribute("aoDefault");
if (!String.IsNullOrWhiteSpace(isDefaultOpen))
IO.AO[$"{Module}.{aoName}"].Value = Convert.ToSingle(isDefaultOpen);
}
}
protected override void HandleMonitor()
{
DoValueToDI();
}
protected void DoValueToDI()
{
foreach (var ai_ao in AO_AI)
{
IO.AI[$"{Module}.{ai_ao.Key}"].Value = IO.AO[$"{Module}.{ai_ao.Value}"].Value;
}
}
public bool Initialize()
{
return true;
}
public void Reset()
{
}
public void Terminate()
{
}
}
}
@@ -0,0 +1,39 @@
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorIoDO_DI : SimulatorIoValve
{
public SimulatorIoDO_DI(string module, XmlElement node, string ioModule) : base(module, node, ioModule)
{
}
/// <summary>
/// 根据XML配置创建气阀控制对象
/// </summary>
/// <param name="node"></param>
protected override void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("DO_DI");
foreach (XmlElement xmlElement in valveNodes)
{
string diName = xmlElement.GetAttribute("di");
string doName = xmlElement.GetAttribute("do");
if (!DI_DOList.ContainsKey(diName) && !DI_DOList.ContainsKey(doName))
DI_DOList.Add(diName, doName);
var isDefaultOpen = xmlElement.GetAttribute("doDefault");
if (!String.IsNullOrWhiteSpace(isDefaultOpen))
IO.DO[$"{Module}.{doName}"].Value = Convert.ToBoolean(isDefaultOpen);
}
}
}
}
@@ -0,0 +1,73 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorIoDefault : BaseDevice, IDevice
{
protected Dictionary<string, object> IO_ValueList = new Dictionary<string, object>();
private readonly Random _rnd = new Random();
public SimulatorIoDefault(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
{
InitializeIValve(node);
}
/// <summary>
/// 根据XML配置创建IO对象初始默认值
/// </summary>
/// <param name="node"></param>
protected virtual void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("IODefault");
foreach (XmlElement xmlElement in valveNodes)
{
string type = xmlElement.GetAttribute("type");
string ioName = xmlElement.GetAttribute("ioName");
string value = xmlElement.GetAttribute("default");
switch (type)
{
case "DI":
IO.DI[$"{Module}.{ioName}"].Value = Convert.ToBoolean(value);
break;
case "DO":
IO.DO[$"{Module}.{ioName}"].Value = Convert.ToBoolean(value);
break;
case "AI":
IO.AI[$"{Module}.{ioName}"].Value = Convert.ToSingle(value);
break;
case "AO":
IO.AO[$"{Module}.{ioName}"].Value = Convert.ToSingle(value);
break;
}
}
}
public bool Initialize()
{
return true;
}
public void Reset()
{
}
public void Terminate()
{
}
}
}
@@ -0,0 +1,92 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorMFC : BaseDevice, IDevice
{
protected Dictionary<string, string> AI_AOList = new Dictionary<string, string>();
private readonly Random _rnd = new Random();
public SimulatorMFC(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
{
InitializeIValve(node);
}
/// <summary>
/// 根据XML配置创建MFC控制对象
/// </summary>
/// <param name="node"></param>
protected virtual void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("IoMfc");
foreach (XmlElement xmlElement in valveNodes)
{
string aiFlow = xmlElement.GetAttribute("aiFlow");
string aoFlow = xmlElement.GetAttribute("aoFlow");
if (!AI_AOList.ContainsKey(aiFlow) && !AI_AOList.ContainsKey(aoFlow))
AI_AOList.Add(aiFlow, aoFlow);
}
}
protected override void HandleMonitor()
{
Ramp();
}
protected virtual void Ramp()
{
foreach (var ai_ao in AI_AOList)
{
float setpoint = 0;
float result = 0;
float current = 0;
if (IO.AI[$"{Module}.{ai_ao.Key}"] != null)
{
current = IO.AI[$"{Module}.{ai_ao.Key}"].Value;
setpoint = IO.AO[$"{Module}.{ai_ao.Value}"].Value;
}
if (current >= setpoint)
{
result = current - 50;
if (result < setpoint)
result = setpoint;
}
if (current < setpoint)
{
result = current + 50;
if (result > setpoint)
result = setpoint;
}
result = result + (float)_rnd.NextDouble();
IO.AI[$"{Module}.{ai_ao.Key}"].Value = result;
}
}
public bool Initialize()
{
return true;
}
public void Reset()
{
}
public void Terminate()
{
}
}
}
@@ -0,0 +1,40 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorPressure : SimulatorMFC
{
public SimulatorPressure(string module, XmlElement node, string ioModule) : base(module, node, ioModule)
{
}
/// <summary>
/// 根据XML配置创建PC控制对象
/// </summary>
/// <param name="node"></param>
protected override void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("IoPressureMeter");
foreach (XmlElement xmlElement in valveNodes)
{
string aiValue = xmlElement.GetAttribute("aiValue");
string aoValue = xmlElement.GetAttribute("aoValue");
if (!String.IsNullOrWhiteSpace(aoValue))
if (!AI_AOList.ContainsKey(aiValue) && !AI_AOList.ContainsKey(aoValue))
AI_AOList.Add(aiValue, aoValue);
}
}
}
}
@@ -0,0 +1,69 @@
using Aitex.Core.RT.Device;
using Aitex.Core.RT.IOCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MECF.Framework.Simulator.Core
{
public class SimulatorIoValve : BaseDevice, IDevice
{
protected Dictionary<string, string> DI_DOList = new Dictionary<string, string>();
public SimulatorIoValve(string module, XmlElement node, string ioModule) : base(module, node, ioModule)
{
InitializeIValve(node);
}
/// <summary>
/// 根据XML配置创建气阀控制对象
/// </summary>
/// <param name="node"></param>
protected virtual void InitializeIValve(XmlElement node)
{
XmlNodeList valveNodes = node.GetElementsByTagName("IoValve");
foreach (XmlElement xmlElement in valveNodes)
{
string diName = xmlElement.GetAttribute("diOpen");
string doName = xmlElement.GetAttribute("doOpen");
if (!DI_DOList.ContainsKey(diName) && !DI_DOList.ContainsKey(doName))
DI_DOList.Add(diName, doName);
var isDefaultOpen = xmlElement.GetAttribute("isDefaultOpen");
if (!String.IsNullOrWhiteSpace(isDefaultOpen))
IO.DO[$"{Module}.{doName}"].Value = Convert.ToBoolean(isDefaultOpen);
}
}
protected override void HandleMonitor()
{
DoValueToDI();
}
protected void DoValueToDI()
{
foreach (var di_do in DI_DOList)
{
IO.DI[$"{Module}.{di_do.Key}"].Value = IO.DO[$"{Module}.{di_do.Value}"].Value;
}
}
public bool Initialize()
{
return true;
}
public void Reset()
{
}
public void Terminate()
{
}
}
}
+9 -1
View File
@@ -62,6 +62,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AI_AORandom\SimulatorAI_AORandom.cs" />
<Compile Include="Aligners\HPA48\HPA48Simulator.cs" />
<Compile Include="Aligners\HPA48\HPA48View.xaml.cs">
<DependentUpon>HPA48View.xaml</DependentUpon>
@@ -75,6 +76,13 @@
<Compile Include="CarrierIDReader\HoneywellHf800\HoneywellBarcodeView.xaml.cs">
<DependentUpon>HoneywellBarcodeView.xaml</DependentUpon>
</Compile>
<Compile Include="Device\SimulatorDeviceManager.cs" />
<Compile Include="IoAO_AI\SimulatorIoAO_AI.cs" />
<Compile Include="IoDefault\SimulatorIoDefault.cs" />
<Compile Include="IoDO_DI\SimulatorIoDO_DI.cs" />
<Compile Include="IoMFC\SimulatorMFC.cs" />
<Compile Include="IoPressure\SimulatorPressure.cs" />
<Compile Include="IoValves\SimulatorIoValve.cs" />
<Compile Include="LoadPorts\SG\SGLoadPortSimulator.cs" />
<Compile Include="LoadPorts\SG\SGLoadPortView.xaml.cs">
<DependentUpon>SGLoadPortView.xaml</DependentUpon>
@@ -575,7 +583,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties robots_4hiratar4_4iosimulatoritems_1json__JsonSchema="https://jenkins-x.io/schemas/jx-schema.json" vce_4brooksvce_4iosimulatoritems_1json__JsonSchema="http://json.schemastore.org/apibuilder.json" />
<UserProperties vce_4brooksvce_4iosimulatoritems_1json__JsonSchema="http://json.schemastore.org/apibuilder.json" robots_4hiratar4_4iosimulatoritems_1json__JsonSchema="https://jenkins-x.io/schemas/jx-schema.json" />
</VisualStudio>
</ProjectExtensions>
</Project>