CheckCardModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using DataServices;
  6. using System.Data;
  7. using AGV_WPF_Global;
  8. namespace AGV_WPF.Models
  9. {
  10. public class RoutePoint
  11. {
  12. public int currentPoint;
  13. public int nextPoint;
  14. public int route;
  15. }
  16. public class MissReadEventArgs:EventArgs
  17. {
  18. /// <summary>
  19. /// 漏读卡生产区
  20. /// </summary>
  21. public int workLine;
  22. /// <summary>
  23. /// 漏读卡AGV
  24. /// </summary>
  25. public int agvNum;
  26. /// <summary>
  27. /// 漏读卡路线
  28. /// </summary>
  29. public int route;
  30. /// <summary>
  31. /// 漏读站点
  32. /// </summary>
  33. public int stationNum;
  34. /// <summary>
  35. /// 记录信息,是漏读还是多读
  36. /// </summary>
  37. public string info;
  38. }
  39. public class CheckCardUnit
  40. {
  41. public CheckCardUnit(int agvNum,int workLine,DataBase data)
  42. {
  43. this.AgvNum = agvNum;
  44. this.workLine = workLine;
  45. this.data = data;
  46. ExcelDataBase exceldata = (ExcelDataBase)this.data;
  47. if (!exceldata.IsExistTable(exceldata.DataFile, "路线"))
  48. {
  49. exceldata.CreateTable(exceldata.DataFile, "路线");
  50. }
  51. }
  52. private int agvNum;
  53. public int AgvNum
  54. {
  55. get { return agvNum; }
  56. set { agvNum = value; }
  57. }
  58. private int workLine;
  59. public int WorkLine
  60. {
  61. get { return workLine; }
  62. set { workLine = value; }
  63. }
  64. public List<RoutePoint> missPoints = new List<RoutePoint>();//漏读站点列表
  65. public RoutePoint lastPoint = null;//上一站点
  66. public delegate void MissReadEventHandler(object o,MissReadEventArgs e);
  67. public event MissReadEventHandler MissReadEvent;
  68. private DataBase data;
  69. public DataBase Data
  70. {
  71. get { return data; }
  72. }
  73. protected void OnMissReadEvent(object o, MissReadEventArgs e)
  74. {
  75. if (MissReadEvent != null)
  76. {
  77. MissReadEvent(o, e);
  78. }
  79. }
  80. public void CheckPoint(RoutePoint currentPoint)
  81. {
  82. DataTable table = null;
  83. Console.ForegroundColor = ConsoleColor.Yellow;
  84. if (lastPoint == null)
  85. {
  86. Console.WriteLine("LastPoint为空");
  87. }
  88. if (lastPoint == null || (lastPoint != null && lastPoint.nextPoint == 0) || (lastPoint != null && lastPoint.route != currentPoint.route) || (currentPoint.currentPoint == lastPoint.currentPoint && currentPoint.route == lastPoint.route))//刚进行检测或者AGV已经跑完此条路线(无下一站点)或者当前线路跟上一线路不同时
  89. {
  90. lastPoint = currentPoint;
  91. #region 填充lastPoint.nextPoint操作
  92. string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
  93. if (data != null)
  94. table = (DataTable)data.Execute(str);
  95. else
  96. throw new Exception("数据库未实例化");
  97. if (table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
  98. {
  99. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  100. table = (DataTable)data.Execute(str);
  101. //有路线信息后,再找出当前点位置,默认只会找到一条记录
  102. if (table.Rows.Count > 0)
  103. {
  104. int temp;
  105. //得到当前点的序号
  106. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  107. return;
  108. //寻找下一序号对应站点
  109. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  110. table = (DataTable)data.Execute(str);
  111. //存在下一站点,默认只会找到一条记录
  112. if (table.Rows.Count > 0)
  113. {
  114. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  115. return;
  116. lastPoint.nextPoint = temp;
  117. }
  118. //不存在
  119. else
  120. {
  121. lastPoint.nextPoint = 0;
  122. }
  123. }
  124. }
  125. else//否则用通用AGV编号0去查找
  126. {
  127. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  128. table = (DataTable)data.Execute(str);
  129. if (table.Rows.Count > 0)
  130. {
  131. int temp;
  132. //得到当前点的序号
  133. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  134. return;
  135. //寻找下一序号对应站点
  136. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  137. table = (DataTable)data.Execute(str);
  138. //存在下一站点,默认只会找到一条记录
  139. if (table.Rows.Count > 0)
  140. {
  141. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  142. return;
  143. lastPoint.nextPoint = temp;
  144. }
  145. //不存在
  146. else
  147. {
  148. lastPoint.nextPoint = 0;
  149. }
  150. }
  151. return;
  152. }
  153. #endregion
  154. Console.WriteLine(string.Format("检测单元:AGV{0},路线{1},当前地标{2}", this.agvNum, lastPoint.route, lastPoint.currentPoint));
  155. }
  156. else if (lastPoint.route == currentPoint.route)
  157. {
  158. if (lastPoint.nextPoint != currentPoint.currentPoint)//上一站点的下一站点不等于当前站点,说明漏读卡了
  159. {
  160. string tempStr = string.Format(@"select * from [路线$] where [AGV编号]={0} and [路线编号]={1} and [生产区号]={2}", agvNum, currentPoint.route, workLine);
  161. string info="";
  162. if (data != null)
  163. {
  164. table = (DataTable)data.Execute(tempStr);
  165. if (table.Rows.Count > 0)//先查看是否有指定AGV路线
  166. {
  167. tempStr = string.Format(@"select * from [路线$] where [AGV编号]={0} and [路线编号]={1} and [站点编号]={2} and [生产区号]={3}", agvNum, currentPoint.route, currentPoint.currentPoint, workLine);
  168. table = (DataTable)data.Execute(tempStr);//再看当前点是否在路线中
  169. if (table.Rows.Count > 0)
  170. {
  171. info = "漏读";//在路线中则为漏读
  172. }
  173. else
  174. {
  175. info = "没登记";//否则为多读
  176. }
  177. }
  178. else//否则用通用AGV路线去查找
  179. {
  180. tempStr = string.Format(@"select * from [路线$] where [AGV编号]=0 and [路线编号]={0} and [生产区号]={1}", currentPoint.route, workLine);
  181. table = (DataTable)data.Execute(tempStr);
  182. if (table.Rows.Count > 0)//有通用路线
  183. {
  184. tempStr = string.Format(@"select * from [路线$] where [AGV编号]=0 and [路线编号]={0} and [站点编号]={1} and [生产区号]={2}", currentPoint.route, currentPoint.currentPoint, workLine);
  185. table = (DataTable)data.Execute(tempStr);//再看当前点是否在路线中
  186. if (table.Rows.Count > 0)
  187. {
  188. info = "漏读";//在路线中则为漏读
  189. }
  190. else
  191. {
  192. info = "没登记";//否则为多读
  193. }
  194. }
  195. else
  196. {
  197. info = "没登记";//否则为多读
  198. }
  199. }
  200. }
  201. else
  202. throw new Exception("数据库未实例化");
  203. RoutePoint missPoint = new RoutePoint();
  204. missPoint.route = lastPoint.route;
  205. missPoint.currentPoint = lastPoint.nextPoint;
  206. missPoint.nextPoint = 0;//注此处没给漏读卡,后一站点赋值,如果需要可从数据库中得到
  207. //missPoints.Add(missPoint);//添加漏读卡到漏读列表当中
  208. MissReadEventArgs e = new MissReadEventArgs();
  209. e.agvNum = this.agvNum;
  210. e.route = missPoint.route;
  211. e.stationNum = missPoint.currentPoint;
  212. e.info = info;
  213. /************************************************************************/
  214. /* 将漏卡写入Excel文件 */
  215. /************************************************************************/
  216. string date = string.Format(@"Link\{0}.xls", DateTime.Now.ToString("yyyy-MM-dd"));
  217. DataBase tempData = new ExcelDataBase(date);
  218. if (info.Contains("没登记"))//如果是多读则必须checked后能存入excle
  219. {
  220. if (GlobalPara.IsEnableExtraReadCheck)
  221. {
  222. 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"));
  223. }
  224. }
  225. else//其他的直接存
  226. {
  227. 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"));
  228. }
  229. OnMissReadEvent(this, e);
  230. lastPoint = currentPoint;//将lastPoint信息更新到当前点,以便下一次检测,随后填充lastPoint的nextPoint值
  231. #region 填充lastPoint.nextPoint操作
  232. string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
  233. if (data != null)
  234. table = (DataTable)data.Execute(str);
  235. else
  236. throw new Exception("数据库未实例化");
  237. if(table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
  238. {
  239. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  240. table = (DataTable)data.Execute(str);
  241. //有路线信息后,再找出当前点位置,默认只会找到一条记录
  242. if (table.Rows.Count > 0)
  243. {
  244. int temp;
  245. //得到当前点的序号
  246. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  247. return;
  248. //寻找下一序号对应站点
  249. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  250. table = (DataTable)data.Execute(str);
  251. //存在下一站点,默认只会找到一条记录
  252. if (table.Rows.Count > 0)
  253. {
  254. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  255. return;
  256. lastPoint.nextPoint = temp;
  257. }
  258. //不存在
  259. else
  260. {
  261. lastPoint.nextPoint = 0;
  262. }
  263. }
  264. }
  265. else//否则用通用AGV编号0去查找
  266. {
  267. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  268. table = (DataTable)data.Execute(str);
  269. if (table.Rows.Count > 0)
  270. {
  271. int temp;
  272. //得到当前点的序号
  273. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  274. return;
  275. //寻找下一序号对应站点
  276. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  277. table = (DataTable)data.Execute(str);
  278. //存在下一站点,默认只会找到一条记录
  279. if (table.Rows.Count > 0)
  280. {
  281. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  282. return;
  283. lastPoint.nextPoint = temp;
  284. }
  285. //不存在
  286. else
  287. {
  288. lastPoint.nextPoint = 0;
  289. }
  290. }
  291. return;
  292. }
  293. #endregion
  294. }
  295. else
  296. {
  297. lastPoint = currentPoint;
  298. Console.WriteLine(string.Format("检测正常"));
  299. #region 填充lastPoint.nextPoint操作
  300. string str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and [生产区号]=" + workLine.ToString();
  301. if (data != null)
  302. table = (DataTable)data.Execute(str);
  303. else
  304. throw new Exception("数据库未实例化");
  305. if (table.Rows.Count > 0)//先判断表中是否有该AGV路线信息,没有再用通用路线
  306. {
  307. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  308. table = (DataTable)data.Execute(str);
  309. //有路线信息后,再找出当前点位置,默认只会找到一条记录
  310. if (table.Rows.Count > 0)
  311. {
  312. int temp;
  313. //得到当前点的序号
  314. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  315. return;
  316. //寻找下一序号对应站点
  317. str = @"select * from [路线$] where [AGV编号]=" + agvNum.ToString() + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  318. table = (DataTable)data.Execute(str);
  319. //存在下一站点,默认只会找到一条记录
  320. if (table.Rows.Count > 0)
  321. {
  322. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  323. return;
  324. lastPoint.nextPoint = temp;
  325. }
  326. //不存在
  327. else
  328. {
  329. lastPoint.nextPoint = 0;
  330. }
  331. }
  332. }
  333. else//否则用通用AGV编号0去查找
  334. {
  335. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + "and " + "[站点编号]=" + lastPoint.currentPoint + " and [生产区号]=" + workLine.ToString();
  336. table = (DataTable)data.Execute(str);
  337. if (table.Rows.Count > 0)
  338. {
  339. int temp;
  340. //得到当前点的序号
  341. if (!int.TryParse(table.Rows[0]["站点序号"].ToString(), out temp))
  342. return;
  343. //寻找下一序号对应站点
  344. str = @"select * from [路线$] where [AGV编号]=0" + " and " + "[路线编号]=" + lastPoint.route.ToString() + " and " + "[站点序号]=" + (temp + 1).ToString() + " and [生产区号]=" + workLine.ToString();
  345. table = (DataTable)data.Execute(str);
  346. //存在下一站点,默认只会找到一条记录
  347. if (table.Rows.Count > 0)
  348. {
  349. if (!int.TryParse(table.Rows[0]["站点编号"].ToString(), out temp))
  350. return;
  351. lastPoint.nextPoint = temp;
  352. }
  353. //不存在
  354. else
  355. {
  356. lastPoint.nextPoint = 0;
  357. }
  358. }
  359. return;
  360. }
  361. #endregion
  362. }
  363. }
  364. Console.ResetColor();
  365. }
  366. }
  367. public class CheckCardSystem
  368. {
  369. public CheckCardSystem()
  370. {
  371. }
  372. public delegate void MissReadEventHandler(object o, MissReadEventArgs e);
  373. public event MissReadEventHandler MissReadEvent;
  374. protected void OnMissReadEvent(object o, MissReadEventArgs e)
  375. {
  376. if (MissReadEvent != null)
  377. {
  378. MissReadEvent(o, e);
  379. }
  380. }
  381. private List<CheckCardUnit> unitList = new List<CheckCardUnit>();
  382. public List<CheckCardUnit> UnitList
  383. {
  384. get { return unitList; }
  385. }
  386. public void Add(CheckCardUnit unit)
  387. {
  388. unitList.Add(unit);
  389. unit.MissReadEvent += new CheckCardUnit.MissReadEventHandler(OnMissReadEvent);
  390. }
  391. public void Remove(CheckCardUnit unit)
  392. {
  393. unit.MissReadEvent -= new CheckCardUnit.MissReadEventHandler(OnMissReadEvent);
  394. unitList.Remove(unit);
  395. }
  396. }
  397. }