| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using Prism.Mvvm;
- using SKMC.Api.Common;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- namespace SKMC.Api.Device.Material.Tray
- {
- /// <summary>
- /// 基础物料盘, 可放m*n个物料
- /// </summary>
- public class Tray : BindableBase
- {
- protected static ILogger log = LogFactory.Get();
- /// <summary>
- /// index:
- /// 0 1 2 3 4 5
- /// 6 7 8 9 ...
- /// </summary>
- public ObservableCollection<TraySlot> Slots { get; set; } = new ObservableCollection<TraySlot>();
- public long Id { get; set; }
- /// <summary>
- /// 类型编号
- /// </summary>
- public string Code { get; set; }
- /// <summary>
- /// 料盘类型 0:主盘 1:NG盘 2:OK盘
- /// </summary>
- public byte TrayType { get; set; } = 0;
- /// <summary>
- /// 料盘码
- /// </summary>
- public string SerialNo { get; set; }
- /// <summary>
- /// 产品码
- /// </summary>
- public string ProductNo { get; set; }
- /// <summary>
- /// 料盘参数对象
- /// </summary>
- public TrayConfig TrayConfiger { get; set; }
- // 开始进料时刻
- public long OnLoadMillis { get; set; }
- // 开始准备时刻
- public long OnReadyMillis { get; set; }
- // 完全离开时刻
- public long OnExitMillis { get; set; }
- /// <summary>
- /// X 方向的穴位坐标正负 1:+ -1:-
- /// </summary>
- public short DirectX { get; set; } = 1;
- /// <summary>
- /// X 方向的穴位坐标正负 1:+ -1:-
- /// </summary>
- public short DirectY { get; set; } = 1;
- /// <summary>
- /// 生成Matters的状态字符串
- /// </summary>
- /// <returns></returns>
- public string ToMap()
- {
- StringBuilder builder = new StringBuilder();
- foreach (var mat in Slots)
- {
- builder.Append(Convert.ToString(mat.Result));
- }
- return builder.ToString();
- }
- /// <summary>
- /// 解析状态字符串并恢复到Matters集合中
- /// </summary>
- /// <param name="map"></param>
- public void FromMap(string map, int rows, int cols)
- {
- //Matters.Clear();
- //for (int i = 0; i < map.Length; i++)
- //{
- // Matter matter = new Matter();
- // matter.TrayCols = cols;
- // matter.Set(i);
- // string s = Convert.ToString(map[i]);
- // matter.Result = Convert.ToByte(s);
- // Matters.Add(matter);
- //}
- //Num = map.Length;
- Counts();
- }
- public int Row { get; set; }
- public int Col { get; set; }
- private int _num;
- // 总数
- public int Num
- {
- get { return _num; }
- set { _num = value; RaisePropertyChanged(); }
- }
- private int _numOK;
- public int NumOK
- {
- get { return _numOK; }
- set
- {
- _numOK = value; RaisePropertyChanged();
- }
- }
- private int _numNG;
- // NG数
- public int NumNG
- {
- get { return _numNG; }
- set
- {
- _numNG = value; RaisePropertyChanged();
- }
- }
- private int _unLocated;
- // 剩余空位(未定位)
- public int Unlocated
- {
- get { return _unLocated; }
- set { _unLocated = value; RaisePropertyChanged(); }
- }
- public Tray(TrayConfig trayConfiger, byte trayType = 0, byte siteStatus = (byte)MatterStatus.NULL)
- {
- Id = BitConverter.ToInt64(System.Guid.NewGuid().ToByteArray(), 0);
- TrayType = trayType;
- Num = trayConfiger.Number;
- Row = trayConfiger.Row;
- Col = trayConfiger.Col;
- if (trayConfiger == null)
- {
- throw new ArgumentException("tray model is null");
- }
- for (int i = 0; i < trayConfiger.Row; i++)
- {
- for (int j = 0; j < trayConfiger.Col; j++)
- {
- TraySlot matter = new TraySlot
- {
- Index = j + i * trayConfiger.Col,
- ColX = j,
- RowY = i,
- IntervalX = trayConfiger.XPitch * j,
- IntervalY = trayConfiger.YPitch * i,
- Result = siteStatus
- };
- if (trayConfiger.SiteSwitchs != null && trayConfiger.SiteSwitchs.Count > matter.Index)
- {
- matter.IsEnable = trayConfiger.SiteSwitchs[matter.Index];
- }
- Slots.Add(matter);
- }
- }
- Counts();
- }
- // 注入一个Slot数据
- public void Inject(TraySlot slot)
- {
- //Matter thisMatter = Matters.Where(m => m.ColX == matter.ColX & m.RowY == matter.RowY).FirstOrDefault();
- //if (thisMatter != null)
- //{
- // if (matter.Code != null) thisMatter.Code = matter.Code;
- // if (matter.Result > 0) thisMatter.Result = matter.Result;
- // if (matter.ResultMsg != null) thisMatter.ResultMsg = matter.ResultMsg;
- // if (matter.FixsetX > 0) thisMatter.FixsetX = matter.FixsetX;
- // if (matter.FixsetY > 0) thisMatter.FixsetY = matter.FixsetY;
- // if (matter.FixsetR > 0) thisMatter.FixsetR = matter.FixsetR;
- //}
- foreach (var thisMat in Slots)
- {
- if (thisMat != null && thisMat.ColX == slot.ColX && thisMat.RowY == slot.RowY)
- {
- thisMat.Code = slot.Code;
- thisMat.Result = slot.Result;
- thisMat.Located = slot.Located;
- }
- }
- }
- /// <summary>
- /// 注入一个Slot集合数据
- /// </summary>
- /// <param name="slots"></param>
- public void Injects(List<TraySlot> slots)
- {
- foreach (var matter in slots)
- {
- Inject(matter);
- }
- }
- public int Count(MatterStatus status)
- {
- int counter = 0;
- foreach (var matter in Slots)
- {
- if (matter.Result == (byte)status) counter++;
- }
- return counter;
- }
- // 统计当前OK, NG数, 空位数
- public void Counts()
- {
- _numNG = 0;
- _numOK = 0;
- _unLocated = 0;
- foreach (var matter in Slots)
- {
- if (matter.Result == (byte)MatterStatus.OK) _numOK++;
- if (matter.Result == (byte)MatterStatus.NG) _numNG++;
- if (matter.Located == (byte)LocateStatus.TODO) _unLocated++;
- }
- NumOK = _numOK;
- NumNG = _numNG;
- Unlocated = _unLocated;
- }
- public void UpdateResults(MatterStatus status)
- {
- foreach (var matter in Slots)
- {
- matter.Result = (byte)status;
- }
- Counts();
- }
- public void Updates(Func<TraySlot, bool> cnd, Action<TraySlot> action)
- {
- foreach (var matter in Slots)
- {
- if (cnd.Invoke(matter)) action.Invoke(matter);
- }
- }
- public List<TraySlot> FindSlots(MatterStatus status)
- {
- List<TraySlot> results = new List<TraySlot>();
- foreach (var matter in Slots)
- {
- if (matter.Result == (byte)status) results.Add(matter);
- }
- return results;
- }
- public TraySlot FindNext(MatterStatus status)
- {
- foreach (var matter in Slots)
- {
- if (matter.Result == (byte)status) return matter;
- }
- return null;
- }
- public TraySlot FindNext(Func<TraySlot, bool> cnd)
- {
- foreach (var matter in Slots)
- {
- if (cnd.Invoke(matter)) return matter;
- }
- return null;
- }
- public void FindNext(ref TraySlot matter)
- {
- if (matter == null) return;
- if (matter.Index == Slots.Count() - 1)
- {
- matter = null;
- return;
- }
- matter = Slots[matter.Index + 1];
- }
- public override string ToString()
- {
- return $"id: {Id}, code: {SerialNo}";
- }
- public void PrintMatters()
- {
- log.Debug($"tray: {this}");
- foreach (var matter in Slots)
- {
- log.Debug(matter.ToString());
- }
- }
- }
- }
|