AsmXtProgramDal.cs 40 KB

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