You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.6 KiB
C#

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
{
/// <summary>
/// 空闲
/// </summary>
IDLE,
/// <summary>
/// 准备下料
/// </summary>
ToUnload
}
/// <summary>
/// 排料吸嘴实体类
/// </summary>
public class Nozzle
{
SqliteHelper db = new SqliteHelper();
public string SN { get; set; }
/// <summary>
/// 吸嘴索引
/// </summary>
public int NozzleIndex { get; set; }
/// <summary>
/// 吸嘴名称
/// </summary>
public string NozzleName { get; set; }
public TurnoverType FromType { get; set; }
public int FromFloor { get; set; }
/// <summary>
/// 周转信息(和中控保持一致)
/// </summary>
public int FromIndex { get; set; }
public TurnoverType ToType { get; set; }
public int ToFloor { get; set; }
/// <summary>
/// 周转信息(和中控保持一致)
/// </summary>
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;
}
/// <summary>
/// 重置吸嘴
/// </summary>
/// <returns></returns>
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();
}
/// <summary>
/// 真空吸操作
/// </summary>
public void VacSuction( EIoOperate op)
{
if(op== EIoOperate.Open)
{
Ops.On($"{NozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(GlobalVar.LoadNozzleOpenVacSuctionDelaytime);
}
else
{
Ops.Off($"{NozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(GlobalVar.LoadNozzleCloseVacSuctionDelaytime);
}
}
/// <summary>
/// 真空破操作
/// </summary>
public void VacBreak(EIoOperate op)
{
if (op == EIoOperate.Open)
{
Ops.On($"{NozzleIndex}号吸嘴真空破电磁阀");
}
else
{
Ops.Off($"{NozzleIndex}号吸嘴真空破电磁阀");
}
}
/// <summary>
/// 通过真空吸判断吸嘴是否有产品
/// </summary>
/// <returns></returns>
public bool HasProduct()
{
return Ops.IsOn($"{NozzleIndex}号吸嘴真空吸检测");
}
}
public static class NozzleManager
{
static SqliteHelper db = new SqliteHelper();
private static List<Nozzle> nozzles;
static NozzleManager() { nozzles = new List<Nozzle>(); }
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<Nozzle>(dataRow);
nozzle.Status = ENozzleStatus.IDLE;
if(nozzle!=null)
{
nozzles.Add(nozzle);
}
}
}
}
/// <summary>
/// 根据吸嘴的状态获取吸嘴
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public static List<Nozzle> GetNozzlesByStatus(ENozzleStatus status)
{
if(nozzles!=null && nozzles.Count>0)
{
return nozzles.Where(n => n.Status == status).OrderBy(n=>n.NozzleIndex).ToList();
}
return null;
}
/// <summary>
/// 获取空闲的吸嘴(索引从小大)
/// </summary>
/// <param name="turnoverToTray"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 保存吸嘴的状态
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 更新吸嘴的状态
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 通过吸嘴的索引获取吸嘴
/// </summary>
/// <param name="nozzleIndex"></param>
/// <returns></returns>
public static Nozzle GetNozzle(int nozzleIndex)
{
return nozzles.Where(n => n.NozzleIndex == nozzleIndex).FirstOrDefault();
}
/// <summary>
/// 获取所有的吸嘴
/// </summary>
/// <returns></returns>
public static List<Nozzle> GetAllNozzles()
{
return nozzles;
}
}
}