KnsProgramDal.cs 40 KB

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