Bg8761ProgramDal.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 Bg8761ProgramDal
  16. {
  17. private IDatabase CurrDb = null;
  18. public Bg8761ProgramDal(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,byte[] filedatas, 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 = "";
  479. entity.OrgDatas = filedatas;
  480. entity.FLen = filedatas.Length;
  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. private void SendInfo(string maccode, string info)
  525. {
  526. try
  527. {
  528. MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  529. string temperrorinfo = "";
  530. senddal.SendS10F3(maccode, info, ref temperrorinfo);
  531. }
  532. catch
  533. {
  534. return;
  535. }
  536. }
  537. /// <summary>
  538. /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台
  539. /// </summary>
  540. /// <param name="maccode"></param>
  541. /// <param name="partcode"></param>
  542. /// <param name="pcode"></param>
  543. /// <param name="errorinfo"></param>
  544. /// <returns></returns>
  545. public int DownloadProgram(Machine mac,string programname, byte[] filedatas, ref string errorinfo)
  546. {
  547. try
  548. {
  549. //要修改程序名称
  550. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
  551. if (order == null)
  552. return -1;
  553. string condition = $" and a.preid={order.ID}";
  554. List<OrderData> datas = GetS7F1Data(programname, filedatas.Length, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  555. if (datas == null)
  556. return -1;
  557. HsmsWeb accessmac = new HsmsWeb();
  558. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  559. if (rec == null)
  560. return -1;
  561. int result = int.Parse(rec.Datalists[0].FContent);
  562. if (result > 1)
  563. {
  564. errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
  565. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  566. return -1;
  567. }
  568. if (result == 0)
  569. {
  570. order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
  571. datas = GetS7F3Data(programname,filedatas, order.PreID, ref errorinfo);
  572. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  573. if (rec == null)
  574. return -1;
  575. if (int.Parse(rec.Datalists[0].FContent) != 0)
  576. {
  577. errorinfo = "机台没有正确接受程序。";
  578. SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  579. return -1;
  580. }
  581. }
  582. return 1;
  583. }
  584. catch (Exception ex)
  585. {
  586. errorinfo = ex.Message.ToString();
  587. return -1;
  588. }
  589. }
  590. public int SelectProgram(Machine mac, string programname, ref string errorinfo)
  591. {
  592. try
  593. {
  594. MacOrderSendDal dal = new MacOrderSendDal(CurrDb);
  595. OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41SelProgram", ref errorinfo);
  596. if (order == null)
  597. return -1;
  598. string condition = $" and a.preid={order.ID}";
  599. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  600. if (datas == null)
  601. return -1;
  602. List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();//取得第一行
  603. tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t=>t.FNum).ToList();//取得第二层的2个节点
  604. tempdatas = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList();
  605. List<OrderData> tempdatasf = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t => t.FNum).ToList();
  606. tempdatasf[1].FContent = programname;
  607. tempdatasf[1].FLen = programname.Length;
  608. tempdatasf = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList();
  609. tempdatasf[1].FContent = "AutoSelect";
  610. tempdatasf[1].FLen = 10;
  611. HsmsWeb accessmac = new HsmsWeb();
  612. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  613. if (rec == null)
  614. return -1;
  615. if (int.Parse(rec.Datalists[1].FContent) != 0)
  616. {
  617. errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  618. return -1;
  619. }
  620. return 1;
  621. }
  622. catch (Exception ex)
  623. {
  624. errorinfo = ex.Message.ToString();
  625. return -1;
  626. }
  627. }
  628. public MacProgram SaveProgram(string maccode, string usercode, ref string errorinfo)
  629. {
  630. try
  631. {
  632. //读取机台信息
  633. string condition = $" and a.FCode='{maccode}'";
  634. Machine mac = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList()[0];
  635. //string programname = (string)imputds["programname"];
  636. ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
  637. string programname = comdal.GetMacProgramName(mac, ref errorinfo);
  638. if (string.IsNullOrEmpty(programname))
  639. {
  640. errorinfo = $"未找到机台【{maccode}】对应的程序。{errorinfo}";
  641. return null;
  642. }
  643. //从机台上读取文件并保存
  644. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  645. MacProgram macprogram = SaveProgramFromMac(mac, programname, programdir, ref errorinfo);
  646. if (macprogram == null)
  647. {
  648. return null;
  649. }
  650. return macprogram;
  651. }
  652. catch (Exception ex)
  653. {
  654. errorinfo = ex.Message.ToString();
  655. CurrDb.Rollback();
  656. return null;
  657. }
  658. }
  659. private List<OrderData> GetS2F13Data( int preid, ref string errorinfo)
  660. {
  661. List<OrderData> ldata = new List<OrderData>();
  662. OrderData lentity = new OrderData();
  663. lentity.ID = 1;
  664. lentity.ParentID = 0;
  665. lentity.PreID = preid;
  666. lentity.FCode = "L";
  667. lentity.FLen = 1;
  668. ldata.Add(lentity);
  669. OrderData entity = new OrderData();
  670. entity.ID = 2;
  671. entity.ParentID = 1;
  672. entity.FNum = 10;
  673. entity.PreID = preid;
  674. entity.FCode = "U2";
  675. entity.FContent = "4200";
  676. entity.FLen = 4;
  677. ldata.Add(entity);
  678. return ldata;
  679. }
  680. public string GetProgramName(Machine mac,ref string errorinfo)
  681. {
  682. try
  683. {
  684. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 13, ref errorinfo);
  685. if (order == null)
  686. return "";
  687. string condition = $" and a.preid={order.ID}";
  688. List<OrderData> datas = GetS2F13Data(order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  689. if (datas == null)
  690. return "";
  691. HsmsWeb accessmac = new HsmsWeb();
  692. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  693. if (rec == null)
  694. return "";
  695. string org = rec.Datalists[1].FContent;
  696. return org;
  697. }
  698. catch(Exception ex)
  699. {
  700. errorinfo = ex.Message.ToString();
  701. return "";
  702. }
  703. }
  704. //public string GetMacProgramName(Machine mac, ref string errorinfo)
  705. //{
  706. // try
  707. // {
  708. // OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
  709. // if (order == null)
  710. // return null;
  711. // //从机型参数中读取程序参数信息
  712. // string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
  713. // List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  714. // if (mmsecs == null)
  715. // return null;
  716. // if (mmsecs.Count <= 0)
  717. // {
  718. // errorinfo = "未找到此机台对应的程序参数说明。";
  719. // return null;
  720. // }
  721. // List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
  722. // if (datas == null)
  723. // return null;
  724. // HsmsWeb accessmac = new HsmsWeb();
  725. // OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  726. // if (rec == null)
  727. // return null;
  728. // string programname = rec.Datalists[1].FContent;
  729. // return programname;
  730. // }
  731. // catch (Exception ex)
  732. // {
  733. // errorinfo = ex.Message.ToString();
  734. // return "";
  735. // }
  736. //}
  737. public int CompareParams(Machine mac, string usercode, ref string errorinfo)
  738. {
  739. try
  740. {
  741. string programname = GetProgramName(mac, ref errorinfo);
  742. if (errorinfo != "")
  743. {
  744. return -1;
  745. }
  746. if (programname == "")
  747. {
  748. return -1;
  749. }
  750. //string programname = "BCP5316TA.BCX5316T3D";
  751. string condition = $" and a.FName='{programname}' and a.ProcessCode='{mac.PCode}' and a.ModelID={mac.MModeID}";
  752. List<ProgramMst> programmsts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
  753. if(programmsts.Count<=0)
  754. {
  755. errorinfo = $"未找到程序名称【{programname}】,制程代码【{mac.PCode}】,机型代码【{mac.MModeCode}】对应的信息。";
  756. return -1;
  757. }
  758. ProgramMst programmst = programmsts[0];
  759. ParamsComMst mst = null;
  760. //比较机台程序名称
  761. ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
  762. //比对程序参数
  763. mst = comdal.CompareParams(mac, programmst, ref errorinfo);
  764. if (mst == null)
  765. {
  766. errorinfo = $"未找到机台【{mac.FCode}】程序【{programmst.FName}】对应的比对主档。{errorinfo}";
  767. CurrDb.Rollback();
  768. return -1;
  769. }
  770. //插入比对记录
  771. mst = comdal.IUParamsComMst(mst, usercode, ref errorinfo);
  772. if (mst == null)
  773. {
  774. errorinfo = $"插入比对结果发生错误:{errorinfo}";
  775. CurrDb.Rollback();
  776. return -1;
  777. }
  778. CurrDb.Commit();
  779. //如果参数不对,就发送停机指令
  780. if (mst.IsPass <= 0)
  781. {
  782. MacOrderSendDal macorderdal = new MacOrderSendDal(CurrDb);
  783. int result = macorderdal.SendS10F3(mac.FCode, "Parameter comparison failed", ref errorinfo);
  784. if (result <= 0)
  785. {
  786. //errorinfo = $"发送消息失败:{errorinfo}";
  787. //return -1;
  788. //写入日志
  789. string logerrorinfo = "";
  790. WriteLog.WriteLogStr(mac.FCode, "", DateTime.Now, $"给机台发送消息失败:{errorinfo}", ref logerrorinfo);
  791. errorinfo = "";
  792. }
  793. result = macorderdal.SendStopMac(mac.FCode, ref errorinfo);
  794. if (result <= 0)
  795. {
  796. errorinfo = $"发送停机指令发生错误,错误信息为:{errorinfo}";
  797. return -1;
  798. }
  799. }
  800. return 1;
  801. }
  802. catch (Exception ex)
  803. {
  804. errorinfo = ex.Message.ToString();
  805. return -1;
  806. }
  807. }
  808. public int DownloadProgram(Machine mac, string orgprogramname, ref string errorinfo)
  809. {
  810. try
  811. {
  812. orgprogramname = orgprogramname.Trim();
  813. int result = 0;
  814. string programname = orgprogramname;
  815. if (mac.CallPMode <= 2)
  816. {
  817. //说明要下载程序
  818. MacProgramDal macprogramdal = new MacProgramDal(CurrDb);
  819. byte[] filedatas = null;
  820. if (mac.CallPMode == 1)
  821. {
  822. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  823. filedatas = macprogramdal.ReadMacProgram(mac.FCode, orgprogramname, programdir, ref errorinfo);
  824. if (result <= 0)
  825. {
  826. return -1;
  827. }
  828. }
  829. else
  830. {
  831. string programdir = AppConfigurtaionServices.Configuration["MacModelProgramDir"];
  832. filedatas = macprogramdal.ReadMacModelProgram(mac.FCode, orgprogramname, programdir, ref errorinfo);
  833. if (result <= 0)
  834. {
  835. return -1;
  836. }
  837. }
  838. result = DownloadProgram(mac, orgprogramname,filedatas, ref errorinfo);
  839. if (result <= 0)
  840. return -20;
  841. }
  842. //提示下载程序成功
  843. MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  844. string temperrorinfo = "";
  845. senddal.SendS10F3(mac.FCode, $"Recipe download success [{orgprogramname}]", ref temperrorinfo);
  846. //选择程序
  847. //result = tempdal.Go_Remote(mac, ref errorinfo);
  848. //if (result <= 0)
  849. // return -1;
  850. result = SelectProgram(mac, orgprogramname, ref errorinfo);
  851. if (result <= 0)
  852. return -1;
  853. //result = tempdal.Start(mac, ref errorinfo);
  854. //if (result <= 0)
  855. // return -1;
  856. //result = tempdal.Go_Local(mac, ref errorinfo);
  857. if (result <= 0)
  858. return -1;
  859. return 1;
  860. }
  861. catch (Exception ex)
  862. {
  863. errorinfo = ex.Message.ToString();
  864. return -1;
  865. }
  866. }
  867. }
  868. }