Ad830PlusProgramDal.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllEapEntity.Rms;
  5. using DllHsms;
  6. using DllHsmsWeb;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using ZipFileHelper;
  12. namespace DllEapDal
  13. {
  14. public class Ad830PlusProgramDal
  15. {
  16. private IDatabase CurrDb = null;
  17. public Ad830PlusProgramDal(IDatabase db)
  18. {
  19. CurrDb = db;
  20. }
  21. private OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, ref string errorinfo)
  22. {
  23. //读取机台信息
  24. string condition = $" and a.macid={macid}";
  25. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  26. if (macs == null)
  27. return null;
  28. if (macs.Count <= 0)
  29. {
  30. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  31. return null;
  32. }
  33. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
  34. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  35. if (details == null)
  36. return null;
  37. if (details.Count <= 0)
  38. {
  39. errorinfo = "未找到您要的指令。";
  40. return null;
  41. }
  42. return details[0];
  43. }
  44. /// <summary>
  45. /// 获取请求机台程序的指令数据
  46. /// </summary>
  47. /// <param name="programname"></param>
  48. /// <param name="preid"></param>
  49. /// <param name="errorinfo"></param>
  50. /// <returns></returns>
  51. private List<OrderData> GetS2F41Data(string programname, int preid, ref string errorinfo)
  52. {
  53. string rcmd = "PP_PREP";
  54. List<OrderData> ldata = new List<OrderData>();
  55. OrderData lentity = new OrderData();
  56. lentity.ID = 1;
  57. lentity.ParentID = 0;
  58. lentity.PreID = preid;
  59. lentity.FCode = "L";
  60. lentity.FLen = 2;
  61. ldata.Add(lentity);
  62. OrderData entity = new OrderData();
  63. entity.ID = 2;
  64. entity.ParentID = 1;
  65. entity.FNum = 10;
  66. entity.PreID = preid;
  67. entity.FCode = "A";
  68. entity.FContent = rcmd;
  69. entity.FLen = rcmd.Length;
  70. ldata.Add(entity);
  71. entity = new OrderData();//添加L节点
  72. entity.ID = 3;
  73. entity.ParentID = 1;
  74. entity.FNum = 20;
  75. entity.PreID = preid;
  76. entity.FCode = "L";
  77. entity.FLen = 1;
  78. ldata.Add(entity);
  79. entity = new OrderData();//添加L节点
  80. entity.ID = 4;
  81. entity.ParentID = 3;
  82. entity.FNum = 10;
  83. entity.PreID = preid;
  84. entity.FCode = "L";
  85. entity.FLen = 2;
  86. ldata.Add(entity);
  87. string cpname = "PPID";
  88. entity = new OrderData();
  89. entity.ID = 5;
  90. entity.ParentID = 4;
  91. entity.FNum = 10;
  92. entity.PreID = preid;
  93. entity.FCode = "A";
  94. entity.FContent = cpname;
  95. entity.FLen = cpname.Length;
  96. ldata.Add(entity);
  97. entity = new OrderData();
  98. entity.ID = 6;
  99. entity.ParentID = 4;
  100. entity.FNum = 20;
  101. entity.PreID = preid;
  102. entity.FCode = "A";
  103. entity.FContent = programname;
  104. entity.FLen = programname.Length;
  105. ldata.Add(entity);
  106. return ldata;
  107. }
  108. /// <summary>
  109. /// DP机台装载程序指令数据
  110. /// </summary>
  111. /// <param name="programname">要装载的程序名称</param>
  112. /// <param name="preid"></param>
  113. /// <param name="errorinfo"></param>
  114. /// <returns></returns>
  115. private List<OrderData> GetS2F41DataForPPSelect(string programname, int preid, ref string errorinfo)
  116. {
  117. string rcmd = "PP_SELECT";
  118. List<OrderData> ldata = new List<OrderData>();
  119. OrderData lentity = new OrderData();
  120. lentity.ID = 1;
  121. lentity.ParentID = 0;
  122. lentity.PreID = preid;
  123. lentity.FCode = "L";
  124. lentity.FLen = 2;
  125. ldata.Add(lentity);
  126. OrderData entity = new OrderData();
  127. entity.ID = 2;
  128. entity.ParentID = 1;
  129. entity.FNum = 10;
  130. entity.PreID = preid;
  131. entity.FCode = "A";
  132. entity.FContent = rcmd;
  133. entity.FLen = rcmd.Length;
  134. ldata.Add(entity);
  135. entity = new OrderData();//添加L节点
  136. entity.ID = 3;
  137. entity.ParentID = 1;
  138. entity.FNum = 20;
  139. entity.PreID = preid;
  140. entity.FCode = "L";
  141. entity.FLen = 1;
  142. ldata.Add(entity);
  143. entity = new OrderData();//添加L节点
  144. entity.ID = 4;
  145. entity.ParentID = 3;
  146. entity.FNum = 10;
  147. entity.PreID = preid;
  148. entity.FCode = "L";
  149. entity.FLen = 2;
  150. ldata.Add(entity);
  151. string cpname = "PPID";
  152. entity = new OrderData();
  153. entity.ID = 5;
  154. entity.ParentID = 4;
  155. entity.FNum = 10;
  156. entity.PreID = preid;
  157. entity.FCode = "A";
  158. entity.FContent = cpname;
  159. entity.FLen = cpname.Length;
  160. ldata.Add(entity);
  161. entity = new OrderData();
  162. entity.ID = 6;
  163. entity.ParentID = 4;
  164. entity.FNum = 20;
  165. entity.PreID = preid;
  166. entity.FCode = "A";
  167. entity.FContent = programname;
  168. entity.FLen = programname.Length;
  169. ldata.Add(entity);
  170. return ldata;
  171. }
  172. private List<OrderData> GetS2F41DataForHit(int preid,string rcmd, ref string errorinfo)
  173. {
  174. //string rcmd = "ENTER_HIT";
  175. List<OrderData> ldata = new List<OrderData>();
  176. OrderData lentity = new OrderData();
  177. lentity.ID = 1;
  178. lentity.ParentID = 0;
  179. lentity.PreID = preid;
  180. lentity.FCode = "L";
  181. lentity.FLen = 2;
  182. ldata.Add(lentity);
  183. OrderData entity = new OrderData();
  184. entity.ID = 2;
  185. entity.ParentID = 1;
  186. entity.FNum = 10;
  187. entity.PreID = preid;
  188. entity.FCode = "A";
  189. entity.FContent = rcmd;
  190. entity.FLen = rcmd.Length;
  191. ldata.Add(entity);
  192. entity = new OrderData();//添加L节点
  193. entity.ID = 3;
  194. entity.ParentID = 1;
  195. entity.FNum = 20;
  196. entity.PreID = preid;
  197. entity.FCode = "L";
  198. entity.FLen = 0;
  199. ldata.Add(entity);
  200. return ldata;
  201. }
  202. private List<OrderData> GetS7F5Data(string programname, int preid, ref string errorinfo)
  203. {
  204. List<OrderData> ldata = new List<OrderData>();
  205. OrderData lentity = new OrderData();
  206. lentity.ID = 1;
  207. lentity.ParentID = 0;
  208. lentity.PreID = preid;
  209. lentity.FCode = "A";
  210. lentity.FContent = programname;
  211. lentity.FLen = programname.Length;
  212. ldata.Add(lentity);
  213. return ldata;
  214. }
  215. /// <summary>
  216. /// 将机台设置为1086参数
  217. /// </summary>
  218. /// <param name="mac"></param>
  219. /// <param name="errorinfo"></param>
  220. /// <returns></returns>
  221. private int SetEc1086(Machine mac, ref string errorinfo)
  222. {
  223. try
  224. {
  225. HsmsWeb accessmac = new HsmsWeb();
  226. //发送指令,将程序模式设置为hit模式
  227. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  228. List<OrderData> datas = GetS2F41DataForHit(order.PreID, "ENTER_HIT", ref errorinfo);
  229. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  230. if (rec == null)
  231. return -1;
  232. int result = int.Parse(rec.Datalists[1].FContent);
  233. if (result > 0)
  234. {
  235. errorinfo = $"将机台设置为HIT模式出错,错误代码为:{result}";
  236. return -1;
  237. }
  238. return 1;
  239. }
  240. catch (Exception ex)
  241. {
  242. errorinfo = ex.Message.ToString();
  243. return -1;
  244. }
  245. }
  246. /// <summary>
  247. /// 发送指令
  248. /// </summary>
  249. /// <param name="mac"></param>
  250. /// <param name="errorinfo"></param>
  251. /// <returns></returns>
  252. /// <summary>
  253. /// 将机台设置为HIT模式
  254. /// </summary>
  255. /// <param name="mac"></param>
  256. /// <param name="errorinfo"></param>
  257. /// <returns></returns>
  258. private int SetHit(Machine mac,string rcmd,ref string errorinfo)
  259. {
  260. try
  261. {
  262. HsmsWeb accessmac = new HsmsWeb();
  263. //发送指令,将程序模式设置为hit模式
  264. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  265. List<OrderData> datas = GetS2F41DataForHit(order.PreID, rcmd, ref errorinfo);
  266. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  267. if (rec == null)
  268. return -1;
  269. int result = int.Parse(rec.Datalists[1].FContent);
  270. if (result == 5)
  271. return 1;
  272. if (result == 0)
  273. return 1;
  274. errorinfo = $"设置ENTER_HIT模式失败,返回代码为{rec.Datalists[1].FContent}";
  275. return -1;
  276. }
  277. catch(Exception ex)
  278. {
  279. errorinfo = ex.Message.ToString();
  280. return -1;
  281. }
  282. }
  283. /// <summary>
  284. /// 获取机台当前使用的程序文件,并将文件放到指定的目录 2019-09-04修改,使用下面的函数
  285. /// </summary>
  286. /// <param name="mac"></param>
  287. /// <param name="errorinfo"></param>
  288. /// <returns></returns>
  289. //public string ReadProgramFromMac(Machine mac,ProgramMst program,string desdir,ref string errorinfo)
  290. //{
  291. // try
  292. // {
  293. // HsmsWeb accessmac = new HsmsWeb();
  294. // //发送指令,将程序模式设置为hit模式
  295. // //OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  296. // //List<OrderData> datas = GetS2F41DataForHit(program.FName, order.PreID, ref errorinfo);
  297. // //OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  298. // //if (rec == null)
  299. // // return null;
  300. // //int result = int.Parse(rec.Datalists[1].FContent);
  301. // //if(result>0)
  302. // //{
  303. // // errorinfo = $"将机台设置为HIT模式出错,错误代码为:{result}";
  304. // // return null;
  305. // //}
  306. // int result = SetHit(mac, "ENTER_HIT", ref errorinfo);
  307. // if(result<=0)
  308. // {
  309. // return null;
  310. // }
  311. // //发送取程序的指令
  312. // OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  313. // List<OrderData> datas = GetS2F41Data(program.FName, order.PreID, ref errorinfo);
  314. // OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  315. // if (rec == null)
  316. // return null;
  317. // result = int.Parse(rec.Datalists[1].FContent);
  318. // if (result != 0&& result != 4)
  319. // {
  320. // errorinfo = $"下载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  321. // return null;
  322. // }
  323. // if(result==4)
  324. // {
  325. // //说明机台已经开始处理程序,休眠5秒钟,等待机台处理
  326. // //或等待处理完成事件上来
  327. // Thread.Sleep(1000 * 5);
  328. // }
  329. // order = ReadMachineOrderDetail(mac.ID, 7, 5, ref errorinfo);
  330. // datas = GetS7F5Data(program.FName, order.PreID, ref errorinfo);
  331. // rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  332. // if (rec == null)
  333. // return null;
  334. // datas = rec.Datalists;
  335. // if(datas[0].FLen==0)
  336. // {
  337. // errorinfo = "未能正确的获取程序文件。";
  338. // return "";
  339. // }
  340. // result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo);
  341. // if (result <= 0)
  342. // return null;
  343. // return program.FName;
  344. // }
  345. // catch(Exception ex)
  346. // {
  347. // errorinfo = ex.Message.ToString();
  348. // return "";
  349. // }
  350. //}
  351. public MacProgram SaveProgramFromMac(Machine mac, string programname, string desdir, ref string errorinfo)
  352. {
  353. try
  354. {
  355. HsmsWeb accessmac = new HsmsWeb();
  356. int result = SetHit(mac, "ENTER_HIT", ref errorinfo);
  357. if (result <= 0)
  358. {
  359. return null;
  360. }
  361. //发送取程序的指令
  362. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  363. List<OrderData> datas = GetS2F41Data(programname, order.PreID, ref errorinfo);
  364. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  365. if (rec == null)
  366. return null;
  367. result = int.Parse(rec.Datalists[1].FContent);
  368. if (result != 0 && result != 4)
  369. {
  370. errorinfo = $"下载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  371. return null;
  372. }
  373. if (result == 4)
  374. {
  375. //说明机台已经开始处理程序,休眠5秒钟,等待机台处理
  376. //或等待处理完成事件上来
  377. Thread.Sleep(1000 * 5);
  378. }
  379. order = ReadMachineOrderDetail(mac.ID, 7, 5, ref errorinfo);
  380. datas = GetS7F5Data(programname, order.PreID, ref errorinfo);
  381. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  382. if (rec == null)
  383. return null;
  384. datas = rec.Datalists;
  385. if (datas[0].FLen == 0)
  386. {
  387. errorinfo = "未能正确的获取程序文件。";
  388. return null;
  389. }
  390. //result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo);
  391. //if (result <= 0)
  392. // return null;
  393. MacProgramDal dal = new MacProgramDal(CurrDb);
  394. MacProgram macprogram=dal.IMacProgram(mac, programname, desdir, datas[2].OrgDatas, ref errorinfo);
  395. return macprogram;
  396. }
  397. catch (Exception ex)
  398. {
  399. errorinfo = ex.Message.ToString();
  400. return null;
  401. }
  402. }
  403. /// <summary>
  404. /// 获取程序参数
  405. /// </summary>
  406. /// <param name="mac"></param>
  407. /// <param name="program"></param>
  408. /// <param name="desdir"></param>
  409. /// <param name="errorinfo"></param>
  410. /// <returns></returns>
  411. public List<FileParams> GetProgramParams(string rarpath,Machine mac, ProgramMst program, string desdir
  412. ,string programdir,MacProgram macprogram, ref string errorinfo)
  413. {
  414. try
  415. {
  416. // string programname = ReadProgramFromMac(db,mac, program, desdir, ref errorinfo);
  417. // if (string.IsNullOrEmpty(programname))
  418. // return null;
  419. // string filepath = desdir + "\\" + programname;
  420. // string tempdesdir = desdir + "\\temp";
  421. // List<FileParams> filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, programname
  422. //, tempdesdir, mac.MModeID, ref errorinfo);
  423. // return filesdata;
  424. string filepath = $"{programdir}\\{mac.FCode}\\{macprogram.ProgramName}\\{macprogram.ProgramName}_{macprogram.Version}";
  425. string tempdesdir = desdir + "\\temp";
  426. List<FileParams> filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, macprogram.ProgramName
  427. , tempdesdir, mac.MModeID, ref errorinfo);
  428. return filesdata;
  429. }
  430. catch (Exception ex)
  431. {
  432. errorinfo = ex.Message.ToString();
  433. return null;
  434. }
  435. }
  436. /// <summary>
  437. /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
  438. /// </summary>
  439. /// <param name="maccode"></param>
  440. /// <param name="partcode"></param>
  441. /// <param name="pcode"></param>
  442. /// <param name="errorinfo"></param>
  443. /// <returns></returns>
  444. /// <summary>
  445. /// 构造S7F3数据
  446. /// </summary>
  447. /// <param name="programname"></param>
  448. /// <param name="filestr"></param>
  449. /// <param name="errorinfo"></param>
  450. /// <returns></returns>
  451. private List<OrderData> GetS7F3Data(string programname, int filelen, string filepath, int preid, ref string errorinfo)
  452. {
  453. List<OrderData> ldata = new List<OrderData>();
  454. OrderData lentity = new OrderData();
  455. lentity.ID = 1;
  456. lentity.ParentID = 0;
  457. lentity.PreID = preid;
  458. lentity.FCode = "L";
  459. lentity.FLen = 2;
  460. ldata.Add(lentity);
  461. OrderData entity = new OrderData();
  462. entity.ID = 2;
  463. entity.ParentID = 1;
  464. entity.FNum = 10;
  465. entity.PreID = preid;
  466. entity.FCode = "A";
  467. entity.FContent = programname;
  468. entity.FLen = programname.Length;
  469. entity.Remark = "";
  470. ldata.Add(entity);
  471. entity = new OrderData();
  472. entity.ID = 2;
  473. entity.ParentID = 1;
  474. entity.FNum = 20;
  475. entity.PreID = preid;
  476. entity.FCode = "B";
  477. entity.FContent = filepath;
  478. entity.OrgDatas = null;
  479. entity.FLen = filelen;
  480. //entity.FLen = programname.Length;
  481. ldata.Add(entity);
  482. return ldata;
  483. }
  484. /// <summary>
  485. /// 构造S7F1数据
  486. /// </summary>
  487. /// <param name="programname"></param>
  488. /// <param name="filestr"></param>
  489. /// <param name="errorinfo"></param>
  490. /// <returns></returns>
  491. private List<OrderData> GetS7F1Data(string programname, int len, int preid, ref string errorinfo)
  492. {
  493. List<OrderData> ldata = new List<OrderData>();
  494. OrderData lentity = new OrderData();
  495. lentity.ID = 1;
  496. lentity.ParentID = 0;
  497. lentity.PreID = preid;
  498. lentity.FCode = "L";
  499. lentity.FLen = 2;
  500. ldata.Add(lentity);
  501. OrderData entity = new OrderData();
  502. entity.ID = 2;
  503. entity.ParentID = 1;
  504. entity.FNum = 10;
  505. entity.PreID = preid;
  506. entity.FCode = "A";
  507. entity.FContent = programname;
  508. entity.FLen = programname.Length;
  509. ldata.Add(entity);
  510. entity = new OrderData();
  511. entity.ID = 2;
  512. entity.ParentID = 1;
  513. entity.FNum = 20;
  514. entity.PreID = preid;
  515. entity.FCode = "U4";
  516. entity.FLen = 1;
  517. entity.FContent = len.ToString();
  518. //entity.OrgDatas = filedatas;
  519. //entity.FLen = programname.Length;
  520. ldata.Add(entity);
  521. return ldata;
  522. }
  523. /// <summary>
  524. /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台
  525. /// </summary>
  526. /// <param name="maccode"></param>
  527. /// <param name="partcode"></param>
  528. /// <param name="pcode"></param>
  529. /// <param name="errorinfo"></param>
  530. /// <returns></returns>
  531. public int DownloadProgram(Machine mac,string programname, int filelen, string filepath, ref string errorinfo)
  532. {
  533. try
  534. {
  535. //要修改程序名称
  536. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
  537. if (order == null)
  538. return -1;
  539. string condition = $" and a.preid={order.ID}";
  540. List<OrderData> datas = GetS7F1Data(programname, filelen, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  541. if (datas == null)
  542. return -1;
  543. HsmsWeb accessmac = new HsmsWeb();
  544. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  545. if (rec == null)
  546. return -1;
  547. int result = int.Parse(rec.Datalists[0].FContent);
  548. if (result > 1)
  549. {
  550. errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
  551. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  552. return -1;
  553. }
  554. if (result == 0)
  555. {
  556. //产生文件
  557. ShareFileDal filedal = new ShareFileDal(CurrDb);
  558. ShareFile fileentity = filedal.MadeShareFile(filepath, ref errorinfo);
  559. if (fileentity == null)
  560. return -1;
  561. order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
  562. datas = GetS7F3Data(programname, filelen, fileentity.FilePath, order.PreID, ref errorinfo);
  563. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  564. if (rec == null)
  565. return -1;
  566. if (int.Parse(rec.Datalists[0].FContent) != 0)
  567. {
  568. errorinfo = "机台没有正确接受程序。";
  569. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  570. return -1;
  571. }
  572. }
  573. return 1;
  574. }
  575. catch (Exception ex)
  576. {
  577. errorinfo = ex.Message.ToString();
  578. return -1;
  579. }
  580. }
  581. private void SendInfo(string maccode, string info)
  582. {
  583. try
  584. {
  585. MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  586. string temperrorinfo = "";
  587. senddal.SendS10F3(maccode, info, ref temperrorinfo);
  588. }
  589. catch
  590. {
  591. return;
  592. }
  593. }
  594. public int SelectProgram(Machine mac, string programname, ref string errorinfo)
  595. {
  596. try
  597. {
  598. MacOrderSendDal dal = new MacOrderSendDal(CurrDb);
  599. OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41SelProgram", ref errorinfo);
  600. if (order == null)
  601. return -1;
  602. string condition = $" and a.preid={order.ID}";
  603. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  604. if (datas == null)
  605. return -1;
  606. List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();//取得第一行
  607. tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t=>t.FNum).ToList();//取得第二层的2个节点
  608. tempdatas = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList();
  609. tempdatas = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList();
  610. tempdatas[1].FContent = programname;
  611. tempdatas[1].FLen = programname.Length;
  612. HsmsWeb accessmac = new HsmsWeb();
  613. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  614. if (rec == null)
  615. {
  616. return -1;
  617. }
  618. if (int.Parse(rec.Datalists[1].FContent) != 0)
  619. {
  620. errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  621. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  622. return -1;
  623. }
  624. return 1;
  625. }
  626. catch (Exception ex)
  627. {
  628. errorinfo = ex.Message.ToString();
  629. return -1;
  630. }
  631. }
  632. public MacProgram SaveProgram(string maccode, string usercode, ref string errorinfo)
  633. {
  634. try
  635. {
  636. //读取机台信息
  637. string condition = $" and a.FCode='{maccode}'";
  638. Machine mac = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList()[0];
  639. //string programname = (string)imputds["programname"];
  640. ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
  641. string programname = comdal.GetMacProgramName(mac, ref errorinfo);
  642. if (string.IsNullOrEmpty(programname))
  643. {
  644. errorinfo = $"未找到机台【{maccode}】对应的程序。{errorinfo}";
  645. return null;
  646. }
  647. //从机台上读取文件并保存
  648. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  649. MacProgram macprogram = SaveProgramFromMac(mac, programname, programdir, ref errorinfo);
  650. if (macprogram == null)
  651. {
  652. return null;
  653. }
  654. return macprogram;
  655. }
  656. catch (Exception ex)
  657. {
  658. errorinfo = ex.Message.ToString();
  659. CurrDb.Rollback();
  660. return null;
  661. }
  662. }
  663. private List<OrderData> GetS2F13Data( int preid, ref string errorinfo)
  664. {
  665. List<OrderData> ldata = new List<OrderData>();
  666. OrderData lentity = new OrderData();
  667. lentity.ID = 1;
  668. lentity.ParentID = 0;
  669. lentity.PreID = preid;
  670. lentity.FCode = "L";
  671. lentity.FLen = 1;
  672. ldata.Add(lentity);
  673. OrderData entity = new OrderData();
  674. entity.ID = 2;
  675. entity.ParentID = 1;
  676. entity.FNum = 10;
  677. entity.PreID = preid;
  678. entity.FCode = "U2";
  679. entity.FContent = "4200";
  680. entity.FLen = 4;
  681. ldata.Add(entity);
  682. return ldata;
  683. }
  684. private List<OrderData> GetS1F3Data(int preid,MMSecDetail mmsecs, ref string errorinfo)
  685. {
  686. List<OrderData> ldata = new List<OrderData>();
  687. OrderData lentity = new OrderData();
  688. lentity.ID = 1;
  689. lentity.ParentID = 0;
  690. lentity.PreID = preid;
  691. lentity.FCode = "L";
  692. lentity.FLen = 1;
  693. ldata.Add(lentity);
  694. OrderData entity = new OrderData();
  695. entity.ID = 2;
  696. entity.ParentID = 1;
  697. entity.FNum = 10;
  698. entity.PreID = preid;
  699. entity.FCode = mmsecs.DCode;
  700. entity.FContent = mmsecs.FVal;
  701. entity.FLen = 1;
  702. ldata.Add(entity);
  703. return ldata;
  704. }
  705. public string GetProgramName(Machine mac,ref string errorinfo)
  706. {
  707. try
  708. {
  709. OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
  710. if (order == null)
  711. return "";
  712. string condition = $" and b.FCode='{StandardCode.SVID_ProgramName}' and a.PreID={mac.MModeID}";
  713. MMSecDetail mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList()[0];
  714. List<OrderData> datas = GetS1F3Data(order.ID, mmsecs, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  715. if (datas == null)
  716. return "";
  717. HsmsWeb accessmac = new HsmsWeb();
  718. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  719. if (rec == null)
  720. return "";
  721. string org = rec.Datalists[1].FContent;
  722. return org;
  723. }
  724. catch(Exception ex)
  725. {
  726. errorinfo = ex.Message.ToString();
  727. return "";
  728. }
  729. }
  730. //public string GetMacProgramName(Machine mac, ref string errorinfo)
  731. //{
  732. // try
  733. // {
  734. // OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
  735. // if (order == null)
  736. // return null;
  737. // //从机型参数中读取程序参数信息
  738. // string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
  739. // List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  740. // if (mmsecs == null)
  741. // return null;
  742. // if (mmsecs.Count <= 0)
  743. // {
  744. // errorinfo = "未找到此机台对应的程序参数说明。";
  745. // return null;
  746. // }
  747. // List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
  748. // if (datas == null)
  749. // return null;
  750. // HsmsWeb accessmac = new HsmsWeb();
  751. // OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  752. // if (rec == null)
  753. // return null;
  754. // string programname = rec.Datalists[1].FContent;
  755. // return programname;
  756. // }
  757. // catch (Exception ex)
  758. // {
  759. // errorinfo = ex.Message.ToString();
  760. // return "";
  761. // }
  762. //}
  763. public int CompareParams(Machine mac, string usercode, ref string errorinfo)
  764. {
  765. try
  766. {
  767. string programname = GetProgramName(mac, ref errorinfo);
  768. if (errorinfo != "")
  769. {
  770. errorinfo = $"获取程序名称出错:{errorinfo}";
  771. return -1;
  772. }
  773. if (programname == "")
  774. {
  775. errorinfo = $"获取程序名称出错:{errorinfo}";
  776. return -1;
  777. }
  778. //string programname = "BCP5316TA.BCX5316T3D";
  779. string condition = $" and a.FName='{programname}' and a.ProcessCode='{mac.PCode}' and a.ModelID={mac.MModeID}";
  780. List<ProgramMst> programmsts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
  781. if(programmsts.Count<=0)
  782. {
  783. errorinfo = $"未找到程序名称【{programname}】,制程代码【{mac.PCode}】,机型代码【{mac.MModeCode}】对应的信息。";
  784. return -1;
  785. }
  786. ProgramMst programmst = programmsts[0];
  787. ParamsComMst mst = null;
  788. //比较机台程序名称
  789. ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
  790. //比对程序参数
  791. mst = comdal.CompareParams(mac, programmst, ref errorinfo);
  792. if (mst == null)
  793. {
  794. errorinfo = $"未找到机台【{mac.FCode}】程序【{programmst.FName}】对应的比对主档。{errorinfo}";
  795. return -1;
  796. }
  797. //插入比对记录
  798. mst = comdal.IUParamsComMst(mst, usercode, ref errorinfo);
  799. if (mst == null)
  800. {
  801. errorinfo = $"插入比对结果发生错误:{errorinfo}";
  802. return -1;
  803. }
  804. //如果参数不对,就发送停机指令
  805. if (mst.IsPass <= 0)
  806. {
  807. //MacOrderSendDal macorderdal = new MacOrderSendDal(CurrDb);
  808. //int result = macorderdal.SendS10F3(mac.FCode, "Parameter comparison failed", ref errorinfo);
  809. //if (result <= 0)
  810. //{
  811. // //errorinfo = $"发送消息失败:{errorinfo}";
  812. // //return -1;
  813. // //写入日志
  814. // string logerrorinfo = "";
  815. // WriteLog.WriteLogStr(mac.FCode, "", DateTime.Now, $"给机台发送消息失败:{errorinfo}", ref logerrorinfo);
  816. // errorinfo = "";
  817. //}
  818. //result = macorderdal.SendStopMac(mac.FCode, ref errorinfo);
  819. //if (result <= 0)
  820. //{
  821. // errorinfo = $"发送停机指令发生错误,错误信息为:{errorinfo}";
  822. // return -1;
  823. //}
  824. return -1;
  825. }
  826. return 1;
  827. }
  828. catch (Exception ex)
  829. {
  830. errorinfo = ex.Message.ToString();
  831. return -1;
  832. }
  833. }
  834. }
  835. }