Dad3350ProgramDal.cs 45 KB

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