using Rs.DataAccess; using Rs.Framework; using Rs.MotionPlat.Flow; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Rs.MotionPlat.Commom { public enum ENozzleStatus { /// /// 空闲 /// IDLE, /// /// 准备下料 /// ToUnload } /// /// 排料吸嘴实体类 /// public class Nozzle { SqliteHelper db = new SqliteHelper(); public string SN { get; set; } /// /// 吸嘴索引 /// public int NozzleIndex { get; set; } /// /// 吸嘴名称 /// public string NozzleName { get; set; } public TurnoverType FromType { get; set; } public int FromFloor { get; set; } /// /// 周转信息(和中控保持一致) /// public int FromIndex { get; set; } public TurnoverType ToType { get; set; } public int ToFloor { get; set; } /// /// 周转信息(和中控保持一致) /// public int ToIndex { get; set; } public string TurnoverGUID { get; set; } public ENozzleStatus Status { get; set; } = ENozzleStatus.IDLE; public bool Update() { Task.Run(() => { SqliteHelper db = new SqliteHelper(); string val = Status == ENozzleStatus.IDLE ? "0" : "1"; string updateSql = $"update TrayStatus set status={val} where trayname='DischargeNozzle' and slotindex={NozzleIndex}"; db.ExecuteNonQuery(updateSql); }); //string updateSql = $"update nozzle set fromtype='{FromType}',fromfloor={FromFloor},fromindex={FromIndex},ToType='{ToType}',tofloor={ToFloor},ToIndex={ToIndex},Status='{Status}' where NozzleIndex={NozzleIndex}"; //string updateSql = $"update nozzle set Status='{Status}' where NozzleIndex={NozzleIndex}"; //return db.ExecuteNonQuery(updateSql) > 0; return true; } public void ResetStatus() { Status=ENozzleStatus.IDLE; FromIndex = 0; FromType = TurnoverType.Unknown; ToIndex = 0; ToType = TurnoverType.Unknown; } /// /// 重置吸嘴 /// /// public bool Reset() { this.Status = ENozzleStatus.IDLE; this.SN = ""; //this.FromType = TurnoverType.Unknown; //this.FromFloor = 0; //this.FromIndex = 0; //this.ToType = TurnoverType.Unknown; //this.ToFloor = 0; //this.ToIndex = 0; return Update(); } /// /// 真空吸操作 /// public void VacSuction( EIoOperate op) { if(op== EIoOperate.Open) { Ops.On($"{NozzleIndex}号吸嘴真空吸电磁阀"); Thread.Sleep(GlobalVar.LoadNozzleOpenVacSuctionDelaytime); } else { Ops.Off($"{NozzleIndex}号吸嘴真空吸电磁阀"); Thread.Sleep(GlobalVar.LoadNozzleCloseVacSuctionDelaytime); } } /// /// 真空破操作 /// public void VacBreak(EIoOperate op) { if (op == EIoOperate.Open) { Ops.On($"{NozzleIndex}号吸嘴真空破电磁阀"); } else { Ops.Off($"{NozzleIndex}号吸嘴真空破电磁阀"); } } /// /// 通过真空吸判断吸嘴是否有产品 /// /// public bool HasProduct() { return Ops.IsOn($"{NozzleIndex}号吸嘴真空吸检测"); } } public static class NozzleManager { static SqliteHelper db = new SqliteHelper(); private static List nozzles; static NozzleManager() { nozzles = new List(); } public static void Init() { nozzles.Clear(); string querySql = "select * from nozzle where enable=1"; DataTable dt = db.GetDataTable(querySql); if(dt!=null&&dt.Rows.Count>0) { foreach (DataRow dataRow in dt.Rows) { Nozzle nozzle = EntityHelper.ToEntity(dataRow); nozzle.Status = ENozzleStatus.IDLE; if(nozzle!=null) { nozzles.Add(nozzle); } } } } /// /// 根据吸嘴的状态获取吸嘴 /// /// /// public static List GetNozzlesByStatus(ENozzleStatus status) { if(nozzles!=null && nozzles.Count>0) { return nozzles.Where(n => n.Status == status).OrderBy(n=>n.NozzleIndex).ToList(); } return null; } /// /// 获取空闲的吸嘴(索引从小大) /// /// /// public static Nozzle GetIdelNozzle(bool disableNozzle1=false) { if (nozzles != null && nozzles.Count > 0) { if(disableNozzle1) { if(nozzles.Where(n => n.Status == ENozzleStatus.IDLE && n.NozzleIndex != 1).Count()>0) { return nozzles.Where(n => n.Status == ENozzleStatus.IDLE && n.NozzleIndex != 1).First(); } } else { if(nozzles.Where(n => n.Status == ENozzleStatus.IDLE).Count()>0) { return nozzles.Where(n => n.Status == ENozzleStatus.IDLE).First(); } } } return null; } public static Nozzle GetToUnloadNozzle() { if (nozzles != null && nozzles.Count > 0) { var nozzlelist = nozzles.Where(n => n.Status == ENozzleStatus.ToUnload); if (nozzlelist != null && nozzlelist.Count() > 0) { return nozzles.Where(n => n.Status == ENozzleStatus.ToUnload).OrderBy(o => o.ToType).First(); } else return null; } return null; } /// /// 保存吸嘴的状态 /// /// public static bool SaveAll() { string updateSql = ""; if(nozzles==null) return false; foreach (Nozzle nozzle in nozzles) { updateSql += $"update nozzle set " + $"status='{nozzle.Status.ToString()}'," + $" where NozzleIndex={nozzle.NozzleIndex}"; } return db.ExecuteNonQuery(updateSql) > 0; } /// /// 更新吸嘴的状态 /// /// public static bool UpdateNozzleStatus(int nozzleIndex) { string updateSql = ""; if (nozzles == null) return false; foreach (Nozzle nozzle in nozzles.Where(n=>n.NozzleIndex==nozzleIndex).ToList()) { updateSql += $"update nozzle set " + $"status='{nozzle.Status.ToString()}'," + $" where NozzleIndex={nozzle.NozzleIndex}"; } return db.ExecuteNonQuery(updateSql) > 0; } /// /// 通过吸嘴的索引获取吸嘴 /// /// /// public static Nozzle GetNozzle(int nozzleIndex) { return nozzles.Where(n => n.NozzleIndex == nozzleIndex).FirstOrDefault(); } /// /// 获取所有的吸嘴 /// /// public static List GetAllNozzles() { return nozzles; } } }