Device.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using WCS.Core;
  6. using WCS.Entity;
  7. public static class Device
  8. {
  9. #region WCS_DEVICE扩展数据
  10. private static ConcurrentDictionary<string, object> DeviceValues = new ConcurrentDictionary<string, object>();
  11. public static void AddFlag(this WCS_DEVICE source, DF flag)
  12. {
  13. var df = source.Get<DF>("DeviceFlag");
  14. df = df | flag;
  15. source.Set("DeviceFlag", df);
  16. }
  17. public static bool Is(this WCS_DEVICE source, DF flag)
  18. {
  19. var df = source.Get<DF>("DeviceFlag");
  20. return (df & flag) == flag;
  21. }
  22. public static void Set<T>(this WCS_DEVICE source, string key, T value)
  23. {
  24. DeviceValues[source.CODE + key] = value;
  25. }
  26. public static T Get<T>(this WCS_DEVICE source, string key)
  27. {
  28. if (!DeviceValues.ContainsKey(source.CODE + key))
  29. return default(T);
  30. return (T)DeviceValues[source.CODE + key];
  31. }
  32. public static short Code(this WCS_DEVICE source)
  33. {
  34. return short.Parse(source.CODE);
  35. }
  36. public static string Tunnel(this WCS_DEVICE source)
  37. {
  38. return source.Get<string>("Tunnel");
  39. }
  40. public static int TunnelNum(this WCS_DEVICE source)
  41. {
  42. return int.Parse(source.Tunnel().Last().ToString());
  43. }
  44. public static int Floor(this WCS_DEVICE source)
  45. {
  46. return source.Get<int>("Floor");
  47. }
  48. public static WCS_DEVICE SC(this WCS_DEVICE source)
  49. {
  50. return source.Get<WCS_DEVICE>("SC");
  51. }
  52. public static WCS_DEVICE RGV(this WCS_DEVICE source)
  53. {
  54. return source.Get<WCS_DEVICE>("RGV");
  55. }
  56. public static LocInfo LocInfo(this WCS_DEVICE source)
  57. {
  58. return source.Get<LocInfo>("LocInfo");
  59. }
  60. public static bool WakeupOn(this WCS_DEVICE source, int sec, string key)
  61. {
  62. var str = "WakeupOn" + key;
  63. var last = source.Get<DateTime>(str);
  64. if ((DateTime.Now - last).TotalMilliseconds > sec)
  65. {
  66. source.Set(str, DateTime.Now);
  67. return true;
  68. }
  69. else
  70. {
  71. Ltc.Log("OnSleep");
  72. return false;
  73. }
  74. }
  75. public static bool WakeupOn(this WCS_DEVICE source, int sec)
  76. {
  77. return source.WakeupOn(sec, "");
  78. }
  79. #endregion WCS_DEVICE扩展数据
  80. #region 静态方法
  81. public static WCS_DEVICE[] Where(Func<WCS_DEVICE, bool> func)
  82. {
  83. var arr = LogicHandler.AllObjects.OfType<WCS_DEVICE>().Where(func).ToArray();
  84. return arr;
  85. }
  86. public static WCS_DEVICE Find(string code)
  87. {
  88. return Where(v => v.CODE == code).Single();
  89. }
  90. public static WCS_DEVICE[] Find(params string[] codes)
  91. {
  92. return Where(v => codes.Contains(v.CODE)).ToArray();
  93. }
  94. public static void Set<T>(string key, T value, params string[] devices)
  95. {
  96. var arr = LogicHandler.AllObjects.OfType<WCS_DEVICE>().Where(v => devices.Contains(v.CODE)).ToArray();
  97. Parallel.ForEach(arr, v =>
  98. {
  99. v.Set(key, value);
  100. });
  101. }
  102. public static void Set<T>(string key, T value, Func<WCS_DEVICE, bool> func)
  103. {
  104. var arr = LogicHandler.AllObjects.OfType<WCS_DEVICE>().Where(func).ToArray();
  105. Parallel.ForEach(arr, v =>
  106. {
  107. v.Set(key, value);
  108. });
  109. }
  110. public static void AddFlag(DF flag, params string[] devices)
  111. {
  112. var arr = LogicHandler.AllObjects.OfType<WCS_DEVICE>().Where(v => devices.Contains(v.CODE)).ToArray();
  113. Parallel.ForEach(arr, v =>
  114. {
  115. v.AddFlag(flag);
  116. });
  117. }
  118. public static void AddFlag(DF flag, Action<WCS_DEVICE> callbck, params string[] devices)
  119. {
  120. var arr = LogicHandler.AllObjects.OfType<WCS_DEVICE>().Where(v => devices.Contains(v.CODE)).ToArray();
  121. Parallel.ForEach(arr, v =>
  122. {
  123. v.AddFlag(flag);
  124. callbck?.Invoke(v);
  125. });
  126. }
  127. #endregion 静态方法
  128. }
  129. /// <summary>
  130. /// 设备配置
  131. /// </summary>
  132. [Flags]
  133. public enum DF
  134. {
  135. 无 = 0,
  136. SRM = 1 << 0,
  137. SRM二级品取货 = 1 << 1,
  138. SRM涂布取货 = 1 << 2,
  139. SRM月台放货 = 1 << 3,
  140. 一楼RGV放货 = 1 << 4,
  141. 月台 = 1 << 5,
  142. 涂布RGV = 1 << 6,
  143. BOPPRGV = 1 << 7,
  144. 涂布RGV取货设备组 = 1 << 8,
  145. 涂布RGV放货设备组 = 1 << 9,
  146. 涂布出库RGV取货站台 = 1 << 10,
  147. 涂布入库RGV取货站台 = 1 << 11,
  148. SRM涂布放货 = 1 << 12,
  149. 涂布RGV取货站台 = 1 << 13,
  150. BOPPRGV取货设备组 = 1 << 14,
  151. BOPPRGV放货设备组 = 1 << 15,
  152. SRMBOPP取货 = 1 << 16,
  153. }