Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/Aitex/Core/RT/IOCore/DOAccessor.cs

90 lines
2.5 KiB
C#
Raw Normal View History

2023-04-13 11:51:03 +08:00
using System.Threading;
using System.Threading.Tasks;
using Aitex.Core.RT.DataCollection.HighPerformance;
2023-04-13 11:51:03 +08:00
using Aitex.Core.RT.Event;
using Aitex.Core.Util;
using MECF.Framework.RT.Core.IoProviders;
namespace Aitex.Core.RT.IOCore
{
public class DOAccessor : IOAccessor<bool>
{
private Task _tPulseGen;
public DOAccessor(string name, int index, string address, bool isSimulator = false)
: base(name, index, address, IOType.DO, isSimulator)
2023-04-13 11:51:03 +08:00
{
}
/// <inheritdoc />
protected override void SetValue(int index, bool value)
{
if (IO.CanSetDO(Name, value, out var _))
{
Buffer[index] = value;
Singleton<IoProviderManager>.Instance.GetProvider(Provider)?.SetValue(this, value);
}
}
public bool SetValue(bool value, out string reason)
2023-04-13 11:51:03 +08:00
{
if (IO.CanSetDO(Name, value, out reason))
2023-04-13 11:51:03 +08:00
{
var provider = Singleton<IoProviderManager>.Instance.GetProvider(base.Provider);
2023-04-13 11:51:03 +08:00
if (provider != null && !provider.SetValue(this, value))
{
reason = "Write DO[" + Name + "] failed";
2023-04-13 11:51:03 +08:00
return false;
}
Buffer[Index] = value;
if (!IsSimulator)
DataRecorderManager.Instance.ImmediateCache(new CacheDiagnosisInfo("", Name, Type, Index,
value.ToString()));
return true;
2023-04-13 11:51:03 +08:00
}
return false;
}
public bool SetPulseValue(bool value, out string reason, bool holdValue, int delayMillisecond)
{
//Debug.Assert(_tPulseGen == null || _tPulseGen.IsCompleted, $"DO {name} The last pulse is not finished.");
if (IO.CanSetDO(Name, value, out reason))
2023-04-13 11:51:03 +08:00
{
if (_tPulseGen is { IsCompleted: false })
{
// reason = $"{name} is busy generating pulse";
return true;
}
_tPulseGen = Task.Run(() =>
{
Value = value;
if (!IsSimulator)
DataRecorderManager.Instance.ImmediateCache();
2023-04-13 11:51:03 +08:00
Thread.Sleep(delayMillisecond);
if (IO.CanSetDO(Name, holdValue, out var reason))
2023-04-13 11:51:03 +08:00
{
Value = holdValue;
}
else
EV.PostAlarmLog("", $"Unable to restore level of {Name}, {reason}");
});
return true;
}
return false;
}
public bool Check(bool value, out string reason)
{
return IO.CanSetDO(Name, value, out reason);
2023-04-13 11:51:03 +08:00
}
2023-04-13 11:51:03 +08:00
}
}