123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DataServices;
- using System.Data;
- using AGV_WPF_Global;
- namespace AGV_WPF.Models
- {
- public class RoutePoint
- {
- public int currentPoint;
- public int nextPoint;
- public int route;
-
- }
- public class MissReadEventArgs:EventArgs
- {
- /// <summary>
- /// 漏读卡生产区
- /// </summary>
- public int workLine;
- /// <summary>
- /// 漏读卡AGV
- /// </summary>
- public int agvNum;
- /// <summary>
- /// 漏读卡路线
- /// </summary>
- public int route;
- /// <summary>
- /// 漏读站点
- /// </summary>
- public int stationNum;
- /// <summary>
- /// 记录信息,是漏读还是多读
- /// </summary>
- public string info;
- }
- public class CheckCardUnit
- {
- public CheckCardUnit(int agvNum,int workLine,DataBase data)
- {
- this.AgvNum = agvNum;
- this.workLine = workLine;
- this.data = data;
- ExcelDataBase exceldata = (ExcelDataBase)this.data;
- if (!exceldata.IsExistTable(exceldata.DataFile, "路线"))
- {
- exceldata.CreateTable(exceldata.DataFile, "路线");
- }
- }
- private int agvNum;
- public int AgvNum
- {
- get { return agvNum; }
- set { agvNum = value; }
- }
- private int workLine;
- public int WorkLine
- {
- get { return workLine; }
- set { workLine = value; }
- }
-
- public List<RoutePoint> missPoints = new List<RoutePoint>();//漏读站点列表
- public RoutePoint lastPoint = null;//上一站点
- public delegate void MissReadEventHandler(object o,MissReadEventArgs e);
- public event MissReadEventHandler MissReadEvent;
- private DataBase data;
- public DataBase Data
- {
- get { return data; }
-
- }
-
- protected void OnMissReadEvent(object o, MissReadEventArgs e)
- {
- if (MissReadEvent != null)
- {
- MissReadEvent(o, e);
- }
- }
- public void CheckPoint(RoutePoint currentPoint)
- {
- DataTable table = null;
- Console.ForegroundColor = ConsoleColor.Yellow;
- if (lastPoint == null)
- {
- Console.WriteLine("LastPoint为空");
- }
- if (lastPoint == null || (lastPoint != null && lastPoint.nextPoint == 0) || (lastPoint != null && lastPoint.route != currentPoint.route) || (currentPoint.currentPoint == lastPoint.currentPoint && currentPoint.route == lastPoint.route))//刚进行检测或者AGV已经跑完此条路线(无下一站点)或者当前线路跟上一线路不同时
- {
- lastPoint = currentPoint;
- #region 填充lastPoint.nextPoint操作
- string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
- if (data != null)
- table = (DataTable)data.Execute(str);
- else
- throw new Exception("数据库未实例化");
- if (table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
- {
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //有路线信息后,再找出当前点位置,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
- }
- }
- else//否则用通用AGV编号0去查找
- {
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
- }
- return;
- }
- #endregion
-
- Console.WriteLine(string.Format("检测单元:AGV{0},路线{1},当前地标{2}", this.agvNum, lastPoint.route, lastPoint.currentPoint));
- }
- else if (lastPoint.route == currentPoint.route)
- {
- if (lastPoint.nextPoint != currentPoint.currentPoint)//上一站点的下一站点不等于当前站点,说明漏读卡了
- {
- string tempStr = string.Format(@"select * from [路线$] where [AGV编号]={0} and [路线编号]={1} and [生产区号]={2}", agvNum, currentPoint.route, workLine);
-
-
- string info="";
- if (data != null)
- {
- table = (DataTable)data.Execute(tempStr);
- if (table.Rows.Count > 0)//先查看是否有指定AGV路线
- {
- tempStr = string.Format(@"select * from [路线$] where [AGV编号]={0} and [路线编号]={1} and [站点编号]={2} and [生产区号]={3}", agvNum, currentPoint.route, currentPoint.currentPoint, workLine);
- table = (DataTable)data.Execute(tempStr);//再看当前点是否在路线中
- if (table.Rows.Count > 0)
- {
- info = "漏读";//在路线中则为漏读
- }
- else
- {
- info = "没登记";//否则为多读
- }
- }
- else//否则用通用AGV路线去查找
- {
- tempStr = string.Format(@"select * from [路线$] where [AGV编号]=0 and [路线编号]={0} and [生产区号]={1}", currentPoint.route, workLine);
- table = (DataTable)data.Execute(tempStr);
- if (table.Rows.Count > 0)//有通用路线
- {
- tempStr = string.Format(@"select * from [路线$] where [AGV编号]=0 and [路线编号]={0} and [站点编号]={1} and [生产区号]={2}", currentPoint.route, currentPoint.currentPoint, workLine);
- table = (DataTable)data.Execute(tempStr);//再看当前点是否在路线中
- if (table.Rows.Count > 0)
- {
- info = "漏读";//在路线中则为漏读
- }
- else
- {
- info = "没登记";//否则为多读
- }
- }
- else
- {
- info = "没登记";//否则为多读
- }
- }
-
- }
- else
- throw new Exception("数据库未实例化");
- RoutePoint missPoint = new RoutePoint();
- missPoint.route = lastPoint.route;
- missPoint.currentPoint = lastPoint.nextPoint;
- missPoint.nextPoint = 0;//注此处没给漏读卡,后一站点赋值,如果需要可从数据库中得到
- //missPoints.Add(missPoint);//添加漏读卡到漏读列表当中
- MissReadEventArgs e = new MissReadEventArgs();
- e.agvNum = this.agvNum;
- e.route = missPoint.route;
- e.stationNum = missPoint.currentPoint;
- e.info = info;
- /************************************************************************/
- /* 将漏卡写入Excel文件 */
- /************************************************************************/
- string date = string.Format(@"Link\{0}.xls", DateTime.Now.ToString("yyyy-MM-dd"));
- DataBase tempData = new ExcelDataBase(date);
- if (info.Contains("没登记"))//如果是多读则必须checked后能存入excle
- {
- if (GlobalPara.IsEnableExtraReadCheck)
- {
- MissRecord.Insert(ref tempData, this.workLine, this.agvNum, currentPoint.route, lastPoint.currentPoint, lastPoint.nextPoint, currentPoint.currentPoint, info, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- }
- else//其他的直接存
- {
- MissRecord.Insert(ref tempData, this.workLine, this.agvNum, currentPoint.route, lastPoint.currentPoint, lastPoint.nextPoint, currentPoint.currentPoint, info, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- OnMissReadEvent(this, e);
- lastPoint = currentPoint;//将lastPoint信息更新到当前点,以便下一次检测,随后填充lastPoint的nextPoint值
- #region 填充lastPoint.nextPoint操作
- string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
- if (data != null)
- table = (DataTable)data.Execute(str);
- else
- throw new Exception("数据库未实例化");
- if(table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
- {
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //有路线信息后,再找出当前点位置,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
-
- }
- }
- else//否则用通用AGV编号0去查找
- {
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
- }
- return;
- }
- #endregion
- }
- else
- {
- lastPoint = currentPoint;
- Console.WriteLine(string.Format("检测正常"));
- #region 填充lastPoint.nextPoint操作
- string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
- if (data != null)
- table = (DataTable)data.Execute(str);
- else
- throw new Exception("数据库未实例化");
- if (table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
- {
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //有路线信息后,再找出当前点位置,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
- }
- }
- else//否则用通用AGV编号0去查找
- {
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- if (table.Rows.Count > 0)
- {
- int temp;
- //得到当前点的序号
- if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
- return;
- //寻找下一序号对应站点
- str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
- table = (DataTable)data.Execute(str);
- //存在下一站点,默认只会找到一条记录
- if (table.Rows.Count > 0)
- {
- if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
- return;
- lastPoint.nextPoint = temp;
- }
- //不存在
- else
- {
- lastPoint.nextPoint = 0;
- }
- }
- return;
- }
-
- #endregion
- }
- }
- Console.ResetColor();
- }
-
- }
- public class CheckCardSystem
- {
- public CheckCardSystem()
- {
- }
- public delegate void MissReadEventHandler(object o, MissReadEventArgs e);
- public event MissReadEventHandler MissReadEvent;
- protected void OnMissReadEvent(object o, MissReadEventArgs e)
- {
- if (MissReadEvent != null)
- {
- MissReadEvent(o, e);
- }
- }
- private List<CheckCardUnit> unitList = new List<CheckCardUnit>();
- public List<CheckCardUnit> UnitList
- {
- get { return unitList; }
-
- }
- public void Add(CheckCardUnit unit)
- {
- unitList.Add(unit);
- unit.MissReadEvent += new CheckCardUnit.MissReadEventHandler(OnMissReadEvent);
- }
- public void Remove(CheckCardUnit unit)
- {
- unit.MissReadEvent -= new CheckCardUnit.MissReadEventHandler(OnMissReadEvent);
- unitList.Remove(unit);
- }
-
-
- }
- }
|