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.

178 lines
5.3 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.Tasks;
namespace Rs.MotionPlat.Commom
{
public enum ENozzleStatus
{
/// <summary>
/// 空闲
/// </summary>
IDLE,
/// <summary>
/// 准备下料
/// </summary>
ToUnload
}
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()
{
//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<Nozzle> nozzles;
static NozzleManager() { nozzles = new List<Nozzle>(); }
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<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;
}
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;
}
/// <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;
}
public static Nozzle GetNozzle(int nozzleIndex)
{
return nozzles.Where(n => n.NozzleIndex == nozzleIndex).FirstOrDefault();
}
}
}