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.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()
{
//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;
}
public bool Reset()
{
this.Status = ENozzleStatus.IDLE;
//this.FromType = TurnoverType.Unknown;
//this.FromFloor = 0;
//this.FromIndex = 0;
//this.ToType = TurnoverType.Unknown;
//this.ToFloor = 0;
//this.ToIndex = 0;
return Update();
}
}
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 and nozzleindex<=8";
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()
{
if (nozzles != null && nozzles.Count > 0)
{
return nozzles.Where(n => n.Status == ENozzleStatus.IDLE).First();
}
return null;
}
public static Nozzle GetToUnloadNozzle()
{
if (nozzles != null && nozzles.Count > 0)
{
return nozzles.Where(n => n.Status == ENozzleStatus.ToUnload).OrderBy(o=>o.ToType).First();
}
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();
}
}
}