Dad3650ProgramDal.cs 37 KB

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