Dfd6341ProgramDal.cs 33 KB

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