DeviceExtentions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using DBHelper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using WCS.Core;
  6. using WCS.Entity;
  7. using WCS.Entity.Protocol;
  8. using WCS.Entity.Protocol.Station;
  9. namespace WCS.Service
  10. {
  11. public static class DeviceExtentions
  12. {
  13. public static IBCR80 BCR(this WCS_DEVICE source)
  14. {
  15. var dev = Device.Find("BCR" + source.CODE);
  16. return dev.PROTOCOLS.First().Data<IBCR80>();
  17. }
  18. public static short PalletType(this WCS_DEVICE source)
  19. {
  20. return (short)source.Get<int>("PalletType");
  21. }
  22. public static short ProductLine(this WCS_DEVICE source)
  23. {
  24. return (short)source.Get<int>("ProductLine");
  25. }
  26. public static short WorkShop(this WCS_DEVICE source)
  27. {
  28. return (short)source.Get<int>("WorkShop");
  29. }
  30. /// <summary>
  31. /// 获取从source到endaddr的路径中的所有位置
  32. /// </summary>
  33. /// <param name="source">起始位置</param>
  34. /// <param name="endAddr">目标位置</param>
  35. /// <param name="condition">路径筛选条件</param>
  36. /// <returns></returns>
  37. public static List<WCS_DEVICE> GetPath(this WCS_DEVICE source, string endAddr, Func<List<WCS_DEVICE>, bool> condition = null)
  38. {
  39. var q = source.PATHS.Where(v => v.START == source && v.END.CODE.Contains(endAddr.ToUpper()))
  40. .Select(v => v.PATH.Split('-').Select(v => Device.Find(v)).ToList());
  41. if (condition != null)
  42. q = q.Where(condition);
  43. return q.FirstOrDefault();
  44. }
  45. public static WCS_DEVICE GetNext(this WCS_DEVICE source, string endAddr)
  46. {
  47. return source.GetNext(endAddr, null);
  48. }
  49. /// <summary>
  50. /// 获取从source到endaddr的路径中的下一个位置
  51. /// </summary>
  52. /// <param name="source">起始位置</param>
  53. /// <param name="endAddr">目标位置</param>
  54. /// <param name="condition">路径筛选条件</param>
  55. /// <returns></returns>
  56. public static WCS_DEVICE GetNext(this WCS_DEVICE source, string endAddr, Func<List<WCS_DEVICE>, bool> condition)
  57. {
  58. var path = source.GetPath(endAddr, condition);
  59. if (Ltc.Do(path, v => v == null || v.Count == 0))
  60. return null;
  61. if (source.IsConv())
  62. {
  63. var obj = path.FirstOrDefault(v => !v.IsConv());
  64. if (obj == null)
  65. {
  66. return path.Last();
  67. }
  68. //else if (obj.IsSC() || obj.IsTunnel())
  69. //{
  70. // var lastConv = path.TakeWhile(v => v.IsConv()).LastOrDefault();
  71. // return lastConv;
  72. //}
  73. else if (obj.IsRGV())
  74. {
  75. var target = path.Skip(path.IndexOf(obj) + 1).FirstOrDefault();
  76. return target;
  77. }
  78. else
  79. return null;
  80. }
  81. else
  82. {
  83. return path.First();
  84. }
  85. }
  86. public static bool IsRGV(this WCS_DEVICE source)
  87. {
  88. return source.PROTOCOLS.Any(v => v.DB.PROTOCOL == typeof(IRGV520).AssemblyQualifiedName);
  89. }
  90. public static bool IsConv(this WCS_DEVICE source)
  91. {
  92. return source.PROTOCOLS.Any(v => v.DB.PROTOCOL == typeof(IStation521).AssemblyQualifiedName);
  93. }
  94. /// <summary>
  95. /// 是否堆垛机
  96. /// </summary>
  97. /// <param name="source"></param>
  98. /// <returns></returns>
  99. public static bool IsSC(this WCS_DEVICE source)
  100. {
  101. return source.PROTOCOLS.Any(v => v.DB.PROTOCOL == typeof(ISRM520).AssemblyQualifiedName);
  102. }
  103. //public static bool IsRobot(this WCS_DEVICE source)
  104. //{
  105. // return source.PROTOCOLS.Any(v => v.DB.PROTOCOL == typeof(IRobot).AssemblyQualifiedName);
  106. //}
  107. //public static bool IsRobotStation(this WCS_DEVICE source)
  108. //{
  109. // return source.PROTOCOLS.Any(v => v.DB.PROTOCOL == typeof(IRobotStation).AssemblyQualifiedName);
  110. //}
  111. public static bool IsTunnel(this WCS_DEVICE source)
  112. {
  113. return source.PROTOCOLS.Count == 0 && source.CODE.StartsWith("T");
  114. }
  115. }
  116. public static class TaskExtentions
  117. {
  118. public static string AddrCurrent(this WCS_TASK source, DB db)
  119. {
  120. var q = db.Default.Set<WCS_Station520>().Where(v => v.ISLAST && v.Tasknum == source.ID).Select(v => new { type = 1, v.DEVICE.CODE });
  121. q = q.Union(db.Default.Set<WCS_RGV521>().Where(v => v.ISLAST && (v.TaskID_1 == source.ID || v.TaskID_2 == source.ID)).Select(v => new { type = 2, v.DEVICE.CODE }));
  122. //q = q.Union(db.Default.Set<WCS_SC521>().Where(v => v.ISLAST && v.TaskID == source.ID).Select(v => new { type = 2, v.DEVICE.CODE }));
  123. //q = q.Union(db.Default.Set<WCS_Robot>().Where(v => v.ISLAST && v.TaskID == source.ID).Select(v => new { type = 2, v.DEVICE.CODE }));
  124. var res = q.OrderBy(v => v.type).FirstOrDefault();
  125. if (res == null)
  126. return "";
  127. return res.CODE;
  128. }
  129. }
  130. }