OthersProgramDal.cs 39 KB

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