FilesProgramDal.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using ZipFileHelper;
  13. namespace DllEapDal
  14. {
  15. public class FilesProgramDal
  16. {
  17. private IDatabase CurrDb = null;
  18. public FilesProgramDal(IDatabase db)
  19. {
  20. CurrDb = db;
  21. }
  22. public 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. public int SendOrder(Machine mac, int sval, int fval, ref string errorinfo)
  254. {
  255. try
  256. {
  257. string condition = $"";
  258. List<MacOrder> macorder = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  259. if (macorder.Count <= 0)
  260. {
  261. errorinfo = $"未找到机台【{mac.FCode}】对应的指令。";
  262. return -1;
  263. }
  264. condition = $" and a.preid={macorder[0].PreID} and a.sval={sval} and a.fval={fval}";
  265. List<OrderDetail> order = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  266. if (order.Count <= 0)
  267. {
  268. errorinfo = $"未找到机台【{mac.FCode}】对应的S2F15指令。";
  269. return -1;
  270. }
  271. condition = $" and a.preid={order[0].ID}";
  272. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  273. HsmsWeb accessmac = new HsmsWeb();
  274. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order[0], datas, ref errorinfo);
  275. if (rec == null)
  276. return -1;
  277. int result = int.Parse(rec.Datalists[0].FContent);
  278. if (result > 0)
  279. {
  280. errorinfo = $"设置机台ECID参数出错,错误代码为:{result}";
  281. return -1;
  282. }
  283. return 1;
  284. }
  285. catch (Exception ex)
  286. {
  287. errorinfo = ex.Message.ToString();
  288. return -1;
  289. }
  290. }
  291. /// <summary>
  292. /// 将机台设置为HIT模式
  293. /// </summary>
  294. /// <param name="mac"></param>
  295. /// <param name="errorinfo"></param>
  296. /// <returns></returns>
  297. private int SetHit(Machine mac, string rcmd, ref string errorinfo)
  298. {
  299. try
  300. {
  301. HsmsWeb accessmac = new HsmsWeb();
  302. //发送指令,将程序模式设置为hit模式
  303. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  304. List<OrderData> datas = GetS2F41DataForHit(order.PreID, rcmd, ref errorinfo);
  305. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  306. if (rec == null)
  307. return -1;
  308. int result = int.Parse(rec.Datalists[1].FContent);
  309. if (result == 5)
  310. return 1;
  311. if (result == 0)
  312. return 1;
  313. return -1;
  314. }
  315. catch (Exception ex)
  316. {
  317. errorinfo = ex.Message.ToString();
  318. return -1;
  319. }
  320. }
  321. /// <summary>
  322. /// 获取机台当前使用的程序文件,并将文件放到指定的目录
  323. /// </summary>
  324. /// <param name="mac"></param>
  325. /// <param name="errorinfo"></param>
  326. /// <returns></returns>
  327. public string ReadProgramFromMac(Machine mac, ProgramMst program, string desdir, ref string errorinfo)
  328. {
  329. try
  330. {
  331. HsmsWeb accessmac = new HsmsWeb();
  332. //发送指令,将程序模式设置为hit模式
  333. //OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  334. //List<OrderData> datas = GetS2F41DataForHit(program.FName, order.PreID, ref errorinfo);
  335. //OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  336. //if (rec == null)
  337. // return null;
  338. //int result = int.Parse(rec.Datalists[1].FContent);
  339. //if(result>0)
  340. //{
  341. // errorinfo = $"将机台设置为HIT模式出错,错误代码为:{result}";
  342. // return null;
  343. //}
  344. int result = SetHit(mac, "ENTER_HIT", ref errorinfo);
  345. if (result <= 0)
  346. {
  347. return null;
  348. }
  349. //发送取程序的指令
  350. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  351. List<OrderData> datas = GetS2F41Data(program.FName, order.PreID, ref errorinfo);
  352. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  353. if (rec == null)
  354. return null;
  355. result = int.Parse(rec.Datalists[1].FContent);
  356. if (result != 0 && result != 4)
  357. {
  358. errorinfo = $"下载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  359. return null;
  360. }
  361. if (result == 4)
  362. {
  363. //说明机台已经开始处理程序,休眠5秒钟,等待机台处理
  364. //或等待处理完成事件上来
  365. Thread.Sleep(1000 * 5);
  366. }
  367. order = ReadMachineOrderDetail(mac.ID, 7, 5, ref errorinfo);
  368. datas = GetS7F5Data(program.FName, order.PreID, ref errorinfo);
  369. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  370. if (rec == null)
  371. return null;
  372. datas = rec.Datalists;
  373. if (datas[0].FLen == 0)
  374. {
  375. errorinfo = "未能正确的获取程序文件。";
  376. return "";
  377. }
  378. result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo);
  379. if (result <= 0)
  380. return null;
  381. return program.FName;
  382. }
  383. catch (Exception ex)
  384. {
  385. errorinfo = ex.Message.ToString();
  386. return "";
  387. }
  388. }
  389. /// <summary>
  390. /// 获取程序参数
  391. /// </summary>
  392. /// <param name="mac"></param>
  393. /// <param name="program"></param>
  394. /// <param name="desdir"></param>
  395. /// <param name="errorinfo"></param>
  396. /// <returns></returns>
  397. public List<FileParams> GetProgramParams(string rarpath, Machine mac, ProgramMst program, string desdir, ref string errorinfo)
  398. {
  399. try
  400. {
  401. string programname = ReadProgramFromMac(mac, program, desdir, ref errorinfo);
  402. if (string.IsNullOrEmpty(programname))
  403. return null;
  404. string filepath = desdir + "\\" + programname;
  405. string tempdesdir = desdir + "\\temp";
  406. List<FileParams> filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, programname
  407. , tempdesdir, mac.MModeID, ref errorinfo);
  408. return filesdata;
  409. }
  410. catch (Exception ex)
  411. {
  412. errorinfo = ex.Message.ToString();
  413. return null;
  414. }
  415. }
  416. /// <summary>
  417. /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
  418. /// </summary>
  419. /// <param name="maccode"></param>
  420. /// <param name="partcode"></param>
  421. /// <param name="pcode"></param>
  422. /// <param name="errorinfo"></param>
  423. /// <returns></returns>
  424. public ProgramMst FindProgram(Machine mac, string partcode, string pcode, ref string errorinfo)
  425. {
  426. try
  427. {
  428. string condition = $" and b.ModelID={mac.MModeID} and b.ProcessCode='{pcode}'";
  429. List<ProgramRule> rules = CurrDb.FindListForCondition<ProgramRule>(condition, ref errorinfo).ToList();
  430. if (rules == null)
  431. return null;
  432. if (rules.Count <= 0)
  433. {
  434. errorinfo = "未找到对应的程序规则。";
  435. return null;
  436. }
  437. //先精确匹配产品编号
  438. List<ProgramRule> templist = rules.Where(t => t.PartCode == partcode).ToList();
  439. if (templist.Count > 0)
  440. return CurrDb.FindEntityFor<ProgramMst>(templist[0].ProgramMstID);
  441. //取模糊匹配的规则
  442. rules = rules.Where(t => t.PartCode.Contains("%")).ToList();
  443. if (rules.Count <= 0)
  444. {
  445. errorinfo = "未找到对应的程序规则。";
  446. return null;
  447. }
  448. //去掉规则里产品编号的“%”符号
  449. foreach (var item in rules)
  450. {
  451. item.PartCode = item.PartCode.Replace("%", "");
  452. }
  453. //排序规则列表,按产品编号长度从大到小排列
  454. rules = rules.OrderByDescending(t => t.PartCode.Length).ToList();
  455. foreach (var item in rules)
  456. {
  457. if (partcode.Contains(item.PartCode))
  458. {
  459. return CurrDb.FindEntityFor<ProgramMst>(item.ProgramMstID);
  460. }
  461. }
  462. errorinfo = "未找到对应的程序规则。";
  463. return null;
  464. }
  465. catch (Exception ex)
  466. {
  467. errorinfo = ex.Message.ToString();
  468. return null;
  469. }
  470. }
  471. /// <summary>
  472. /// 根据机台编号读取机台
  473. /// </summary>
  474. /// <param name="maccode"></param>
  475. /// <param name="errorinfo"></param>
  476. /// <returns></returns>
  477. public Machine ReadMachine(string maccode, ref string errorinfo)
  478. {
  479. try
  480. {
  481. string condition = $" and a.fcode='{maccode}'";
  482. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  483. if (macs == null)
  484. {
  485. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  486. return null;
  487. }
  488. if (macs.Count <= 0)
  489. {
  490. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  491. return null;
  492. }
  493. return macs[0];
  494. }
  495. catch (Exception ex)
  496. {
  497. errorinfo = ex.Message.ToString();
  498. return null;
  499. }
  500. }
  501. private void SendInfo(string maccode, string info)
  502. {
  503. try
  504. {
  505. MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  506. string temperrorinfo = "";
  507. senddal.SendS10F3(maccode, info, ref temperrorinfo);
  508. }
  509. catch
  510. {
  511. return;
  512. }
  513. }
  514. public string GetMacStatus(Machine mac, ref string errorinfo)
  515. {
  516. try
  517. {
  518. OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
  519. if (order == null)
  520. return "";
  521. string condition = $" and a.PreID={mac.MModeID} and b.FCode='{StandardCode.SVID_CurrentStatus}'";
  522. List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  523. if (mmsecs.Count <= 0)
  524. {
  525. errorinfo = $"未找到机台【{mac.FCode}】对应的状态参数定义。";
  526. return "";
  527. }
  528. List<OrderData> datas = new List<OrderData>();
  529. OrderData lentity = new OrderData();
  530. lentity.ID = 1;
  531. lentity.ParentID = 0;
  532. lentity.PreID = order.ID;
  533. lentity.FCode = "L";
  534. lentity.FContent = "";
  535. lentity.FLen = 1;
  536. lentity.FNum = 10;
  537. datas.Add(lentity);
  538. lentity = new OrderData();
  539. lentity.ID = 2;
  540. lentity.ParentID = 1;
  541. lentity.PreID = order.ID;
  542. lentity.FCode = mmsecs[0].DCode;
  543. lentity.FContent = mmsecs[0].FVal;
  544. lentity.FLen = 1;
  545. lentity.FNum = 10;
  546. datas.Add(lentity);
  547. HsmsWeb accessmac = new HsmsWeb();
  548. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  549. if (rec == null)
  550. return "";
  551. return rec.Datalists[1].FContent;
  552. }
  553. catch (Exception ex)
  554. {
  555. errorinfo = ex.Message.ToString();
  556. return "";
  557. }
  558. }
  559. public int DownloadProgram(Machine mac, string orgprogramname, ref string errorinfo)
  560. {
  561. try
  562. {
  563. orgprogramname = orgprogramname.Trim();
  564. #region
  565. #endregion
  566. //产生随机文件
  567. int rdcount = new Random(Guid.NewGuid().GetHashCode()).Next(1, 9999);
  568. string programid = rdcount.ToString("0000");
  569. int result = 0;
  570. if (mac.CallPMode <= 2)
  571. {
  572. //说明要下载程序
  573. MacProgramDal macprogramdal = new MacProgramDal(CurrDb);
  574. int orgtypeid = 1;
  575. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  576. if (mac.CallPMode == 1)
  577. {
  578. orgtypeid = 1;
  579. }
  580. else
  581. {
  582. orgtypeid = 2;
  583. }
  584. byte[] filedatas = macprogramdal.ReadMacProgram(mac, orgprogramname, orgtypeid, programdir, ref errorinfo);
  585. if (filedatas == null)
  586. {
  587. errorinfo = $"读取文件发生错误:{errorinfo}";
  588. return -30;
  589. }
  590. string rootdir = AppConfigurtaionServices.Configuration["RootDir"];
  591. string rootpath = $@"\\{mac.IPAddress}\{rootdir}\auto";
  592. //rootpath = $@"\\10.2.30.199\c\Disco\3000\Cver\000\dev\auto";
  593. //清空目录
  594. DirectoryInfo dirinfo = new DirectoryInfo(rootpath);
  595. if (dirinfo.Exists)
  596. {
  597. dirinfo.Delete(true);
  598. }
  599. //清空所有程序
  600. //string parentdir = $@"\\10.2.30.199\c\Disco\3000\Cver\000\dev";
  601. //parentdir = $@"\\{mac.IPAddress}\{rootdir}";
  602. //ClearProgram(rootpath);
  603. result = DownloadProgram(mac, programid, orgprogramname, filedatas, rootpath, ref errorinfo);
  604. if (result <= 0)
  605. {
  606. errorinfo = $"下载文件发生错误:{errorinfo}";
  607. return -20;
  608. }
  609. //修改文件内容
  610. string filecontent = $"{programid},{orgprogramname}";
  611. result = WriteFile(rootpath, filecontent, ref errorinfo);
  612. if (result <= 0)
  613. {
  614. errorinfo = $"修改列表文件发生错误:{errorinfo}";
  615. return result - 1;
  616. }
  617. }
  618. return 1;
  619. }
  620. catch (Exception ex)
  621. {
  622. errorinfo = "FilesProgramDal类函数DownloadProgram发生异常:" + ex.Message.ToString();
  623. return -1;
  624. }
  625. }
  626. //清空程序目录下所有程序引导
  627. private bool ClearProgram(string dir)
  628. {
  629. FileInfo files = new FileInfo($@"{dir}\dev.LST");
  630. if (files.Exists)
  631. {
  632. //清空内容
  633. StreamWriter sw = new StreamWriter(files.FullName, false, System.Text.Encoding.ASCII);
  634. sw.WriteLine("");
  635. sw.Close();
  636. }
  637. files = new FileInfo($@"{dir}\devid.LST");
  638. if (files.Exists)
  639. {
  640. //清空内容
  641. StreamWriter sw = new StreamWriter(files.FullName, false, System.Text.Encoding.ASCII);
  642. sw.WriteLine("");
  643. sw.Close();
  644. }
  645. DirectoryInfo dirinfo = new DirectoryInfo(dir);
  646. foreach (var item in dirinfo.GetDirectories())
  647. {
  648. return ClearProgram(item.FullName);
  649. }
  650. return true;
  651. }
  652. public int DownloadProgram(Machine mac, string programid, string programname, byte[] filedatas, string rootpath, ref string errorinfo)
  653. {
  654. try
  655. {
  656. byte[] filelen = new byte[4];
  657. Array.Copy(filedatas, 0, filelen, 0, 4);
  658. int dfdlen = GetFileLen(filelen);
  659. byte[] dfdfiles = new byte[dfdlen];
  660. Array.Copy(filedatas, 12, dfdfiles, 0, dfdlen);
  661. //替换程序名称
  662. dfdfiles = DiodesProgramFileHelper.ReplaceProgramNameForDfd(dfdfiles, programname, ref errorinfo);
  663. if (dfdfiles == null)
  664. {
  665. errorinfo = $"替换程序名称发生异常:{errorinfo}";
  666. return -100;
  667. }
  668. int result = UnityHelper.WriteFile(rootpath, $"{programid}.dfd", dfdfiles, ref errorinfo);
  669. if (result <= 0)
  670. {
  671. errorinfo = $"写入DFD文件发生异常:{errorinfo}";
  672. return -1;
  673. }
  674. Array.Copy(filedatas, 4, filelen, 0, 4);
  675. int clnlen = GetFileLen(filelen);
  676. byte[] clnfiles = new byte[clnlen];
  677. Array.Copy(filedatas, 12 + dfdlen, clnfiles, 0, clnlen);
  678. result = UnityHelper.WriteFile(rootpath, $"{programid}.cln", clnfiles, ref errorinfo);
  679. if (result <= 0)
  680. {
  681. errorinfo = $"写入CLN文件发生异常:{errorinfo}";
  682. return -1;
  683. }
  684. Array.Copy(filedatas, 8, filelen, 0, 4);
  685. int alulen = GetFileLen(filelen);
  686. byte[] alufiles = new byte[alulen];
  687. Array.Copy(filedatas, 12 + dfdlen + clnlen, alufiles, 0, alulen);
  688. result = UnityHelper.WriteFile(rootpath, $"{programid}.alu", alufiles, ref errorinfo);
  689. if (result <= 0)
  690. {
  691. errorinfo = $"写入ALU文件发生异常:{errorinfo}";
  692. return -1;
  693. }
  694. return 1;
  695. }
  696. catch (Exception ex)
  697. {
  698. errorinfo = $"机台编号={mac.FCode},程序ID={programid}, 程序名称={programname}, 文件长度={filedatas.Length},文件路径={rootpath} 下载文件发生异常:" + ex.Message.ToString();
  699. return -1;
  700. }
  701. }
  702. //private int WriteFile(string filedir, string filecontent, ref string errorinfo)
  703. //{
  704. // StreamWriter sw = null;
  705. // try
  706. // {
  707. // string filepath = filedir + "\\dev.LST";
  708. // if (!File.Exists(@filepath))
  709. // {
  710. // FileStream fs= File.Create(filepath);
  711. // fs.Close();
  712. // }
  713. // sw = new StreamWriter(filepath, false, System.Text.Encoding.ASCII);
  714. // sw.WriteLine(filecontent);
  715. // sw.Close();
  716. // filepath = filedir + "\\devid.LST";
  717. // if (!File.Exists(@filepath))
  718. // {
  719. // FileStream fs = File.Create(filepath);
  720. // fs.Close();
  721. // }
  722. // sw = new StreamWriter(filepath, false, System.Text.Encoding.ASCII);
  723. // sw.WriteLine(filecontent);
  724. // sw.Close();
  725. // return 1;
  726. // }
  727. // catch (Exception ex)
  728. // {
  729. // errorinfo = ex.Message;
  730. // return -1;
  731. // }
  732. // finally
  733. // {
  734. // if (sw != null)
  735. // sw.Close();
  736. // }
  737. //}
  738. private int WriteFile(string filedir, string filecontent, ref string errorinfo)
  739. {
  740. try
  741. {
  742. int result = UnityHelper.WriteFile(filedir, $"dev.LST", System.Text.Encoding.ASCII.GetBytes(filecontent), ref errorinfo);
  743. if (result <= 0)
  744. return -1;
  745. result = UnityHelper.WriteFile(filedir, $"devid.LST", System.Text.Encoding.ASCII.GetBytes(filecontent), ref errorinfo);
  746. if (result <= 0)
  747. return -1;
  748. return 1;
  749. }
  750. catch (Exception ex)
  751. {
  752. errorinfo = ex.Message;
  753. return -1;
  754. }
  755. }
  756. private int GetFileLen(byte[] filelen)
  757. {
  758. return BitConverter.ToInt32(filelen, 0);
  759. }
  760. public int DownloadProgram(string maccode, string orgprogramname, ref string errorinfo)
  761. {
  762. try
  763. {
  764. string condition = $" and a.fcode='{maccode}'";
  765. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  766. if (macs == null || macs.Count <= 0)
  767. {
  768. errorinfo = "未找到机台信息。";
  769. return -1;
  770. }
  771. return DownloadProgram(macs[0], orgprogramname, ref errorinfo);
  772. }
  773. catch (Exception ex)
  774. {
  775. errorinfo = $"机台编号={maccode},程序名称={orgprogramname} 发生异常:" + ex.Message.ToString();
  776. return -1;
  777. }
  778. }
  779. }
  780. }