BgProgramDal.cs 37 KB

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