refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于标记HIWIN机械臂控制器状态Bit定义的属性。
|
||||
/// </summary>
|
||||
public class BitTypeClassPropertyAttribute : Attribute
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public BitTypeClassPropertyAttribute(int bitIndex, string message)
|
||||
{
|
||||
BitIndex = bitIndex;
|
||||
Message = message;
|
||||
IsErrorBit = false;
|
||||
}
|
||||
|
||||
public BitTypeClassPropertyAttribute(int bitIndex, string message, bool isErrBit)
|
||||
: this(bitIndex, message)
|
||||
{
|
||||
|
||||
IsErrorBit = isErrBit;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态所在的位地址。
|
||||
/// </summary>
|
||||
public int BitIndex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态信息。
|
||||
/// </summary>
|
||||
public string Message { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前Bit是否表示错误。
|
||||
/// </summary>
|
||||
public bool IsErrorBit { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// /************************************************************************
|
||||
// * @file BaseRoutineWithDeviceLocker.cs
|
||||
// * @author Su Liang
|
||||
// * @date 2022/11/18
|
||||
// *
|
||||
// * @copyright © Sicentury Inc.
|
||||
// *
|
||||
// * @brief
|
||||
// *
|
||||
// * @details
|
||||
// *
|
||||
// *
|
||||
// * *****************************************************************************/
|
||||
|
||||
using System;
|
||||
using Aitex.Core.RT.Event;
|
||||
using Aitex.Core.RT.Routine;
|
||||
using Sicentury.Core;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core
|
||||
{
|
||||
public class BaseRoutineWithDeviceLocker : ModuleRoutine
|
||||
{
|
||||
private readonly DeviceLocker _lockerPump2;
|
||||
|
||||
public BaseRoutineWithDeviceLocker(string moduleName) :base(moduleName)
|
||||
{
|
||||
_lockerPump2 = DeviceLockerManager.Instance.GetLocker(DeviceLockerManager.LockerNames.Pump2);
|
||||
}
|
||||
|
||||
public void LockPump2(out string reason, int timeoutMs = 30 * 60 * 1000)
|
||||
{
|
||||
reason = "";
|
||||
EV.PostInfoLog(Module, $"[{Module}] is locking the Pump2");
|
||||
var ret = _lockerPump2?.TryLock(Module, out reason, timeoutMs);
|
||||
if (ret.HasValue == false || ret == DeviceLocker.Results.Ok ||
|
||||
ret == DeviceLocker.Results.IHaveLocker) // 不需要锁定、已锁定成功、或已占有该锁
|
||||
{
|
||||
EV.PostInfoLog(Module, $"[{Module}] locked the Pump2, Result: {ret}");
|
||||
return;
|
||||
}
|
||||
|
||||
// 无法锁定设备。
|
||||
EV.PostWarningLog(Module, $"[{Module}] was unable to lock Pump2, {reason}");
|
||||
throw new Exception($"unable to lock {_lockerPump2.LockerName} by {Module}, {reason}");
|
||||
}
|
||||
|
||||
public void UnlockPump2()
|
||||
{
|
||||
EV.PostInfoLog(Module, $"[{Module}] is unlocking the Pump2");
|
||||
|
||||
var reason = "";
|
||||
var ret = _lockerPump2?.TryUnlock(Module, out reason);
|
||||
if (ret.HasValue == false || ret == DeviceLocker.Results.Ok) // 不需要锁定、解锁成功
|
||||
{
|
||||
EV.PostInfoLog(Module, $"[{Module}] unlock the Pump2, Result: {ret}");
|
||||
return;
|
||||
}
|
||||
|
||||
EV.PostWarningLog(Module, $"[{Module}] was unable to unlock pump2, {reason}");
|
||||
}
|
||||
|
||||
public void ResetLocker()
|
||||
{
|
||||
_lockerPump2?.Reset();
|
||||
EV.PostInfoLog(Module, $"[{Module}] Pump2 Locker reset");
|
||||
}
|
||||
|
||||
public override void Abort()
|
||||
{
|
||||
CancelLockWaiting();
|
||||
//UnlockPump2();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消等待锁定Pump2。
|
||||
/// </summary>
|
||||
public void CancelLockWaiting()
|
||||
{
|
||||
_lockerPump2?.CancelLockWaiting();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
// /************************************************************************
|
||||
// * @file DeviceLockerManager.cs
|
||||
// * @author Su Liang
|
||||
// * @date 2022/11/18
|
||||
// *
|
||||
// * @copyright © Sicentury Inc.
|
||||
// *
|
||||
// * @brief 设备锁定控制类。
|
||||
// *
|
||||
// * @details 系统中的某些设备会被多个模块共同使用,例如Pump2被LoadLock和UnLoad共用,这类设备
|
||||
// * 无法同时被使用;为提高设备利用率,并解决设备冲突竞争问题,请使用该类对设备进行锁定。
|
||||
// *
|
||||
// * *****************************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Aitex.Core.Util;
|
||||
using MECF.Framework.RT.EquipmentLibrary.Core.Exceptions;
|
||||
using Sicentury.Core;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core
|
||||
{
|
||||
public class DeviceLockerManager : Singleton<DeviceLockerManager>
|
||||
{
|
||||
public enum LockerNames
|
||||
{
|
||||
Pump2
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, DeviceLocker> _lockerCollection;
|
||||
|
||||
public DeviceLockerManager()
|
||||
{
|
||||
_lockerCollection = new Dictionary<string, DeviceLocker>();
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_lockerCollection.Add(LockerNames.Pump2.ToString(), new DeviceLocker(LockerNames.Pump2.ToString(), 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定名称的锁。
|
||||
/// <para>通常一个锁对应一个设备,系统中可用的锁放在列表中,锁名称使用<see cref="LockerNames"/>枚举定义。</para>
|
||||
/// </summary>
|
||||
/// <param name="lockerNames"></param>
|
||||
/// <returns></returns>
|
||||
public DeviceLocker GetLocker(LockerNames lockerNames)
|
||||
{
|
||||
if (_lockerCollection.ContainsKey(lockerNames.ToString()) == false)
|
||||
return null;
|
||||
|
||||
return _lockerCollection[lockerNames.ToString()];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 锁定指定的设备锁。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="occupier">占用者名称,当前锁被谁占用。</param>
|
||||
/// <param name="reason">锁定失败后的错误信息。</param>
|
||||
/// <param name="ct">终止等待锁定。<see cref="CancellationToken"/></param>
|
||||
/// <param name="timeoutMillisecond">等待锁定的超时时间,超过此时间后如果还未成功锁定,则返回False</param>
|
||||
/// <returns>True:锁定成功;False:锁定失败</returns>
|
||||
public DeviceLocker.Results TryLock(string lockerName, string occupier, out string reason, int timeoutMillisecond = 50)
|
||||
{
|
||||
if (_lockerCollection.ContainsKey(lockerName) == false)
|
||||
throw new DeviceLockerNotFoundException(lockerName);
|
||||
|
||||
var locker = _lockerCollection[lockerName];
|
||||
return locker.TryLock(occupier, out reason, timeoutMillisecond);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 锁定指定的设备锁。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="occupier">占用者名称,当前锁被谁占用。</param>
|
||||
/// <param name="reason">锁定失败后的错误信息。</param>
|
||||
/// <param name="ct">终止等待锁定。<see cref="CancellationToken"/></param>
|
||||
/// <param name="timeoutMillisecond">等待锁定的超时时间,超过此时间后如果还未成功锁定,则返回False</param>
|
||||
/// <returns>True:锁定成功;False:锁定失败</returns>
|
||||
public DeviceLocker.Results TryLock(LockerNames lockerName, string occupier, out string reason, int timeoutMillisecond = 50)
|
||||
{
|
||||
return TryLock(lockerName.ToString(), occupier, out reason, timeoutMillisecond);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解除设备锁定。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="occupier">占用者名称,当前锁被谁解锁。</param>
|
||||
/// <para>注意:只能解锁被自己锁定的设备锁!</para>
|
||||
/// <param name="reason">解锁失败后的错误信息</param>
|
||||
/// <returns>True:解锁成功;False:解锁失败</returns>
|
||||
public DeviceLocker.Results TryUnLock(string lockerName, string occupier, out string reason)
|
||||
{
|
||||
if (_lockerCollection.ContainsKey(lockerName) == false)
|
||||
throw new DeviceLockerNotFoundException(lockerName);
|
||||
|
||||
var locker = _lockerCollection[lockerName];
|
||||
return locker.TryUnlock(occupier, out reason);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解除设备锁定。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="occupier">占用者名称,当前锁被谁解锁。</param>
|
||||
/// <para>注意:只能解锁被自己锁定的设备锁!</para>
|
||||
/// <param name="reason">解锁失败后的错误信息</param>
|
||||
/// <returns>True:解锁成功;False:解锁失败</returns>
|
||||
public DeviceLocker.Results TryUnLock(LockerNames lockerName, string occupier, out string reason)
|
||||
{
|
||||
return TryUnLock(lockerName.ToString(), occupier, out reason);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消等待可用的锁。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <exception cref="DeviceLockerNotFoundException"></exception>
|
||||
public void CancelLockWaiting(string lockerName)
|
||||
{
|
||||
if (_lockerCollection.ContainsKey(lockerName) == false)
|
||||
throw new DeviceLockerNotFoundException(lockerName);
|
||||
|
||||
var locker = _lockerCollection[lockerName];
|
||||
locker.CancelLockWaiting();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 复位设备锁。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="reason">复位失败原因</param>
|
||||
/// <returns>True:复位成功;False:复位失败</returns>
|
||||
public DeviceLocker.Results Reset(string lockerName, out string reason)
|
||||
{
|
||||
if (_lockerCollection.ContainsKey(lockerName) == false)
|
||||
throw new DeviceLockerNotFoundException(lockerName);
|
||||
|
||||
reason = "";
|
||||
var locker = _lockerCollection[lockerName];
|
||||
locker.Reset();
|
||||
return DeviceLocker.Results.Ok;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复位设备锁。
|
||||
/// </summary>
|
||||
/// <param name="lockerName">锁名称</param>
|
||||
/// <param name="reason">复位失败原因</param>
|
||||
/// <returns>True:复位成功;False:复位失败</returns>
|
||||
public DeviceLocker.Results Reset(LockerNames lockerName, out string reason)
|
||||
{
|
||||
return Reset(lockerName.ToString(), out reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// /************************************************************************
|
||||
// * @file DeviceLockerNotFoundException.cs
|
||||
// * @author Su Liang
|
||||
// * @date 2022/11/18
|
||||
// *
|
||||
// * @copyright © Sicentury Inc.
|
||||
// *
|
||||
// * @brief
|
||||
// *
|
||||
// * @details
|
||||
// *
|
||||
// *
|
||||
// * *****************************************************************************/
|
||||
|
||||
using System;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core.Exceptions
|
||||
{
|
||||
public class DeviceLockerNotFoundException : Exception
|
||||
{
|
||||
public DeviceLockerNotFoundException(string lockerName) : base(
|
||||
$"unable to find the device locker with name {lockerName}")
|
||||
{
|
||||
LockerName = lockerName;
|
||||
}
|
||||
|
||||
public string LockerName { get; }
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MECF.Framework.RT.EquipmentLibrary.Core.Attributes;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core.Extensions
|
||||
{
|
||||
public static class BitTypeClassExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回类型中以<see cref="BitTypeClassPropertyAttribute"/>标记的公共属性。
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private static List<PropertyInfo> GetProperties(IReflect type)
|
||||
{
|
||||
var piList = type
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(x => x.PropertyType == typeof(bool) &&
|
||||
x.GetCustomAttributes(true)
|
||||
.OfType<BitTypeClassPropertyAttribute>()
|
||||
.Any());
|
||||
|
||||
return piList.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将整数转换为包含Bit定义的类。
|
||||
/// <para>注意:</para>
|
||||
/// <para>目标类中和输入整数bit对应的属性需要用<see cref="BitTypeClassPropertyAttribute"/>标记。</para>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">包含Bit属性定义的类的类型。</typeparam>
|
||||
/// <param name="bitValueSource">待转换的整数。</param>
|
||||
/// <param name="bitLen">有效的位长度。</param>
|
||||
/// <returns></returns>
|
||||
public static T ToBitTypeClass<T>(this int bitValueSource, int bitLen)
|
||||
where T : new()
|
||||
{
|
||||
var obj = new T();
|
||||
|
||||
for (var i = 0; i < bitLen; i++)
|
||||
{
|
||||
var pi = typeof(T)
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.FirstOrDefault(x => x.PropertyType == typeof(bool) &&
|
||||
x.GetCustomAttributes(true)
|
||||
.OfType<BitTypeClassPropertyAttribute>()
|
||||
.Any(a => a.BitIndex == i));
|
||||
if (pi != null)
|
||||
{
|
||||
pi.SetValue(obj, (bitValueSource & (1 << i)) != 0 ? true : false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (T)obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将BitType对象中以<see cref="BidArgumentTypeAttribute"/>标记的属性合并整理为int。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">包含Bit属性定义的类的类型。</typeparam>
|
||||
/// <param name="obj">对象实例。</param>
|
||||
/// <returns></returns>
|
||||
public static int FromBitTypeClass<T>(this T obj)
|
||||
{
|
||||
var piList = GetProperties(typeof(T));
|
||||
if (!piList.Any())
|
||||
return -1;
|
||||
|
||||
var integValue = 0;
|
||||
|
||||
foreach (var pi in piList)
|
||||
{
|
||||
// 如果该属性没有用BitTypeClassPropertyAttribute标记,则忽略
|
||||
if (!(pi.GetCustomAttribute(typeof(BitTypeClassPropertyAttribute)) is BitTypeClassPropertyAttribute
|
||||
attr))
|
||||
continue;
|
||||
|
||||
// 如果该属性的返回值不是bool型,则忽略
|
||||
if (!(pi.GetValue(obj) is bool v))
|
||||
continue;
|
||||
|
||||
// 如果该属性为True,则将返回整数的指定Bit设置为1.
|
||||
if(v)
|
||||
integValue |= (1 << attr.BitIndex);
|
||||
}
|
||||
|
||||
return integValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从BitType类聚合错误信息。
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static List<string> AggregateErrorMessages<T>(this T obj)
|
||||
{
|
||||
var piList = GetProperties(typeof(T));
|
||||
var piForError = piList.Where(pi =>
|
||||
pi.GetCustomAttributes()
|
||||
.OfType<BitTypeClassPropertyAttribute>()
|
||||
.Any(a => a.IsErrorBit))
|
||||
.ToList();
|
||||
|
||||
|
||||
var errs = new List<string>();
|
||||
foreach (var pi in piForError)
|
||||
{
|
||||
if (!(pi.GetValue(obj) is bool vv))
|
||||
continue;
|
||||
|
||||
if (!vv)
|
||||
continue;
|
||||
|
||||
var attr = pi.GetCustomAttribute(
|
||||
typeof(BitTypeClassPropertyAttribute)) as BitTypeClassPropertyAttribute;
|
||||
|
||||
errs.Add(attr?.Message??$"Cannot get message from {nameof(BitTypeClassPropertyAttribute)} of property {pi.Name} of object {obj}");
|
||||
}
|
||||
|
||||
return errs;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.Core.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// 将设备返回值中Bit定义转换为C#对象。
|
||||
/// <para>预留扩展用,暂时未使用。</para>
|
||||
/// </summary>
|
||||
public interface IBitTypeClass
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user