Tray.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using Prism.Mvvm;
  2. using SKMC.Api.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Text;
  8. namespace SKMC.Api.Device.Material.Tray
  9. {
  10. /// <summary>
  11. /// 基础物料盘, 可放m*n个物料
  12. /// </summary>
  13. public class Tray : BindableBase
  14. {
  15. protected static ILogger log = LogFactory.Get();
  16. /// <summary>
  17. /// index:
  18. /// 0 1 2 3 4 5
  19. /// 6 7 8 9 ...
  20. /// </summary>
  21. public ObservableCollection<TraySlot> Slots { get; set; } = new ObservableCollection<TraySlot>();
  22. public long Id { get; set; }
  23. /// <summary>
  24. /// 类型编号
  25. /// </summary>
  26. public string Code { get; set; }
  27. /// <summary>
  28. /// 料盘类型 0:主盘 1:NG盘 2:OK盘
  29. /// </summary>
  30. public byte TrayType { get; set; } = 0;
  31. /// <summary>
  32. /// 料盘码
  33. /// </summary>
  34. public string SerialNo { get; set; }
  35. /// <summary>
  36. /// 产品码
  37. /// </summary>
  38. public string ProductNo { get; set; }
  39. /// <summary>
  40. /// 料盘参数对象
  41. /// </summary>
  42. public TrayConfig TrayConfiger { get; set; }
  43. // 开始进料时刻
  44. public long OnLoadMillis { get; set; }
  45. // 开始准备时刻
  46. public long OnReadyMillis { get; set; }
  47. // 完全离开时刻
  48. public long OnExitMillis { get; set; }
  49. /// <summary>
  50. /// X 方向的穴位坐标正负 1:+ -1:-
  51. /// </summary>
  52. public short DirectX { get; set; } = 1;
  53. /// <summary>
  54. /// X 方向的穴位坐标正负 1:+ -1:-
  55. /// </summary>
  56. public short DirectY { get; set; } = 1;
  57. /// <summary>
  58. /// 生成Matters的状态字符串
  59. /// </summary>
  60. /// <returns></returns>
  61. public string ToMap()
  62. {
  63. StringBuilder builder = new StringBuilder();
  64. foreach (var mat in Slots)
  65. {
  66. builder.Append(Convert.ToString(mat.Result));
  67. }
  68. return builder.ToString();
  69. }
  70. /// <summary>
  71. /// 解析状态字符串并恢复到Matters集合中
  72. /// </summary>
  73. /// <param name="map"></param>
  74. public void FromMap(string map, int rows, int cols)
  75. {
  76. //Matters.Clear();
  77. //for (int i = 0; i < map.Length; i++)
  78. //{
  79. // Matter matter = new Matter();
  80. // matter.TrayCols = cols;
  81. // matter.Set(i);
  82. // string s = Convert.ToString(map[i]);
  83. // matter.Result = Convert.ToByte(s);
  84. // Matters.Add(matter);
  85. //}
  86. //Num = map.Length;
  87. Counts();
  88. }
  89. public int Row { get; set; }
  90. public int Col { get; set; }
  91. private int _num;
  92. // 总数
  93. public int Num
  94. {
  95. get { return _num; }
  96. set { _num = value; RaisePropertyChanged(); }
  97. }
  98. private int _numOK;
  99. public int NumOK
  100. {
  101. get { return _numOK; }
  102. set
  103. {
  104. _numOK = value; RaisePropertyChanged();
  105. }
  106. }
  107. private int _numNG;
  108. // NG数
  109. public int NumNG
  110. {
  111. get { return _numNG; }
  112. set
  113. {
  114. _numNG = value; RaisePropertyChanged();
  115. }
  116. }
  117. private int _unLocated;
  118. // 剩余空位(未定位)
  119. public int Unlocated
  120. {
  121. get { return _unLocated; }
  122. set { _unLocated = value; RaisePropertyChanged(); }
  123. }
  124. public Tray(TrayConfig trayConfiger, byte trayType = 0, byte siteStatus = (byte)MatterStatus.NULL)
  125. {
  126. Id = BitConverter.ToInt64(System.Guid.NewGuid().ToByteArray(), 0);
  127. TrayType = trayType;
  128. Num = trayConfiger.Number;
  129. Row = trayConfiger.Row;
  130. Col = trayConfiger.Col;
  131. if (trayConfiger == null)
  132. {
  133. throw new ArgumentException("tray model is null");
  134. }
  135. for (int i = 0; i < trayConfiger.Row; i++)
  136. {
  137. for (int j = 0; j < trayConfiger.Col; j++)
  138. {
  139. TraySlot matter = new TraySlot
  140. {
  141. Index = j + i * trayConfiger.Col,
  142. ColX = j,
  143. RowY = i,
  144. IntervalX = trayConfiger.XPitch * j,
  145. IntervalY = trayConfiger.YPitch * i,
  146. Result = siteStatus
  147. };
  148. if (trayConfiger.SiteSwitchs != null && trayConfiger.SiteSwitchs.Count > matter.Index)
  149. {
  150. matter.IsEnable = trayConfiger.SiteSwitchs[matter.Index];
  151. }
  152. Slots.Add(matter);
  153. }
  154. }
  155. Counts();
  156. }
  157. // 注入一个Slot数据
  158. public void Inject(TraySlot slot)
  159. {
  160. //Matter thisMatter = Matters.Where(m => m.ColX == matter.ColX & m.RowY == matter.RowY).FirstOrDefault();
  161. //if (thisMatter != null)
  162. //{
  163. // if (matter.Code != null) thisMatter.Code = matter.Code;
  164. // if (matter.Result > 0) thisMatter.Result = matter.Result;
  165. // if (matter.ResultMsg != null) thisMatter.ResultMsg = matter.ResultMsg;
  166. // if (matter.FixsetX > 0) thisMatter.FixsetX = matter.FixsetX;
  167. // if (matter.FixsetY > 0) thisMatter.FixsetY = matter.FixsetY;
  168. // if (matter.FixsetR > 0) thisMatter.FixsetR = matter.FixsetR;
  169. //}
  170. foreach (var thisMat in Slots)
  171. {
  172. if (thisMat != null && thisMat.ColX == slot.ColX && thisMat.RowY == slot.RowY)
  173. {
  174. thisMat.Code = slot.Code;
  175. thisMat.Result = slot.Result;
  176. thisMat.Located = slot.Located;
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// 注入一个Slot集合数据
  182. /// </summary>
  183. /// <param name="slots"></param>
  184. public void Injects(List<TraySlot> slots)
  185. {
  186. foreach (var matter in slots)
  187. {
  188. Inject(matter);
  189. }
  190. }
  191. public int Count(MatterStatus status)
  192. {
  193. int counter = 0;
  194. foreach (var matter in Slots)
  195. {
  196. if (matter.Result == (byte)status) counter++;
  197. }
  198. return counter;
  199. }
  200. // 统计当前OK, NG数, 空位数
  201. public void Counts()
  202. {
  203. _numNG = 0;
  204. _numOK = 0;
  205. _unLocated = 0;
  206. foreach (var matter in Slots)
  207. {
  208. if (matter.Result == (byte)MatterStatus.OK) _numOK++;
  209. if (matter.Result == (byte)MatterStatus.NG) _numNG++;
  210. if (matter.Located == (byte)LocateStatus.TODO) _unLocated++;
  211. }
  212. NumOK = _numOK;
  213. NumNG = _numNG;
  214. Unlocated = _unLocated;
  215. }
  216. public void UpdateResults(MatterStatus status)
  217. {
  218. foreach (var matter in Slots)
  219. {
  220. matter.Result = (byte)status;
  221. }
  222. Counts();
  223. }
  224. public void Updates(Func<TraySlot, bool> cnd, Action<TraySlot> action)
  225. {
  226. foreach (var matter in Slots)
  227. {
  228. if (cnd.Invoke(matter)) action.Invoke(matter);
  229. }
  230. }
  231. public List<TraySlot> FindSlots(MatterStatus status)
  232. {
  233. List<TraySlot> results = new List<TraySlot>();
  234. foreach (var matter in Slots)
  235. {
  236. if (matter.Result == (byte)status) results.Add(matter);
  237. }
  238. return results;
  239. }
  240. public TraySlot FindNext(MatterStatus status)
  241. {
  242. foreach (var matter in Slots)
  243. {
  244. if (matter.Result == (byte)status) return matter;
  245. }
  246. return null;
  247. }
  248. public TraySlot FindNext(Func<TraySlot, bool> cnd)
  249. {
  250. foreach (var matter in Slots)
  251. {
  252. if (cnd.Invoke(matter)) return matter;
  253. }
  254. return null;
  255. }
  256. public void FindNext(ref TraySlot matter)
  257. {
  258. if (matter == null) return;
  259. if (matter.Index == Slots.Count() - 1)
  260. {
  261. matter = null;
  262. return;
  263. }
  264. matter = Slots[matter.Index + 1];
  265. }
  266. public override string ToString()
  267. {
  268. return $"id: {Id}, code: {SerialNo}";
  269. }
  270. public void PrintMatters()
  271. {
  272. log.Debug($"tray: {this}");
  273. foreach (var matter in Slots)
  274. {
  275. log.Debug(matter.ToString());
  276. }
  277. }
  278. }
  279. }