AsmProgramDal.cs 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  1. using Cksoft.Data;
  2. using Cksoft.Unity;
  3. using DllEapEntity;
  4. using DllEapEntity.Rms;
  5. using DllHsms;
  6. using DllHsmsWeb;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. namespace DllEapDal
  14. {
  15. /// <summary>
  16. /// 参数比对数据访问层
  17. /// </summary>
  18. public class AsmProgramDal
  19. {
  20. private IDatabase CurrDb = null;
  21. public AsmProgramDal(IDatabase db)
  22. {
  23. CurrDb = db;
  24. }
  25. public ParamsComMst IUParamsComMst(ParamsComMst mst,string usercode, ref string errorinfo)
  26. {
  27. try
  28. {
  29. int result = CurrDb.InsertFor(mst, usercode);
  30. if (result < 0)
  31. {
  32. return null;
  33. }
  34. object objid = CurrDb.FindObject("select @@IDENTITY");
  35. if (objid.ToString() == "")
  36. {
  37. return null;
  38. }
  39. int id = int.Parse(objid.ToString());
  40. foreach (var item in mst.CurrDetails)
  41. {
  42. item.PreID = id;
  43. item.EntityStatusID = 1;
  44. item.ID = 0;
  45. }
  46. List<PCSubDetail> pcsubdetails = null;
  47. foreach(var item in mst.CurrDetails)
  48. {
  49. result = CurrDb.InsertFor<ParamsComDetail>(item, usercode);
  50. if (result < 0)
  51. return null;
  52. objid = CurrDb.FindObject("select @@IDENTITY");
  53. if (objid.ToString() == "")
  54. {
  55. return null;
  56. }
  57. int subid = int.Parse(objid.ToString());
  58. pcsubdetails = item.pcsubdetails;
  59. foreach (var subitem in pcsubdetails)
  60. {
  61. subitem.PreID = subid;
  62. subitem.ID = 0;
  63. subitem.EntityStatusID = 1;
  64. }
  65. result = CurrDb.InsertFor<PCSubDetail>(pcsubdetails, usercode);
  66. if (result < 0)
  67. return null;
  68. }
  69. mst = CurrDb.FindEntityFor<ParamsComMst>(id);
  70. return mst;
  71. }
  72. catch (Exception e)
  73. {
  74. errorinfo = e.Message;
  75. return null;
  76. }
  77. }
  78. private string GetSecCode(int ftype, ref string errorinfo)
  79. {
  80. try
  81. {
  82. StringBuilder sqlstr = new StringBuilder(100);
  83. string code = "";
  84. switch (ftype)
  85. {
  86. case 1:
  87. code = "S";
  88. break;
  89. case 2:
  90. code = "E";
  91. break;
  92. case 3:
  93. code = "C";
  94. break;
  95. }
  96. sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code);
  97. object obj = CurrDb.FindObject(sqlstr.ToString());
  98. if (obj.ToString() == "")
  99. return code + "00001";
  100. int fnum = int.Parse(obj.ToString().Substring(1, 5));
  101. fnum++;
  102. return code + fnum.ToString().PadLeft(5, '0');
  103. }
  104. catch (Exception ex)
  105. {
  106. errorinfo = ex.Message.ToString();
  107. return "";
  108. }
  109. }
  110. public ParamsComMst CompareParams(Machine mac, ProgramMst programmst, ref string errorinfo)
  111. {
  112. try
  113. {
  114. //读取机台信息
  115. //Machine mac = ReadMachine(maccode, ref errorinfo);
  116. //if (mac == null)
  117. // return null;
  118. List<ParamsComDetail> deslist = CompareParamsDetail(mac, programmst.ID, ref errorinfo);
  119. if (deslist == null)
  120. return null;
  121. ParamsComMst mst = new ParamsComMst();
  122. mst.CurrDetails = deslist;
  123. List<ParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
  124. if (nopass.Count > 0)
  125. {
  126. mst.IsPass = -1;
  127. }
  128. else
  129. {
  130. mst.IsPass = 1;
  131. }
  132. mst.MacID = mac.ID;
  133. mst.ProgramID = programmst.ID;
  134. return mst;
  135. }
  136. catch (Exception ex)
  137. {
  138. errorinfo = ex.Message.ToString();
  139. return null;
  140. }
  141. }
  142. //public ParamsComMst CompareParams(string maccode, string programname, ref string errorinfo)
  143. //{
  144. // try
  145. // {
  146. // string condition = $" and a.fname='{programname}'";
  147. // List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
  148. // if (msts == null)
  149. // return null;
  150. // if(msts.Count<=0)
  151. // {
  152. // errorinfo = $"未找到程序名称【{programname}】的记录。";
  153. // return null;
  154. // }
  155. // return CompareParams(maccode,msts[0].ID,ref errorinfo);
  156. // }
  157. // catch (Exception ex)
  158. // {
  159. // errorinfo = ex.Message.ToString();
  160. // return null;
  161. // }
  162. //}
  163. /// <summary>
  164. /// 根据机台ID、程序ID,产生参数比较结果
  165. /// </summary>
  166. /// <param name="macid"></param>
  167. /// <param name="programid"></param>
  168. /// <param name="errorinfo"></param>
  169. /// <returns></returns>
  170. public List<ParamsComDetail> CompareParamsDetail(Machine mac,int programid,ref string errorinfo)
  171. {
  172. try
  173. {
  174. //读取程序参数
  175. string condition = $" and a.preid={programid} and a.IsCompare=1";
  176. List<ProgramParamsDetail> programparams = CurrDb.FindListForCondition< ProgramParamsDetail>(condition, ref errorinfo).ToList();
  177. if (programparams == null)
  178. return null;
  179. //if(programparams.Count<=0)
  180. //读取程序参数子参数
  181. condition = $" and a.preid in(select id from ProgramParamsDetail where preid={programid} and IsCompare=1)";
  182. List<PPSubDetail> ppsubdetails = CurrDb.FindListForCondition<PPSubDetail>(condition, ref errorinfo).ToList();
  183. if (ppsubdetails == null)
  184. return null;
  185. List<ParamsComDetail> deslist = MadeParamsComDetail(programparams,ppsubdetails, ref errorinfo);
  186. if (deslist == null)
  187. return null;
  188. if (deslist.Count <= 0)//没有设置比对参数
  189. return deslist;
  190. #region 构造参数结构
  191. //List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == SecType.SVID).OrderBy(t => t.FNum).ToList();
  192. //List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
  193. //if (datas == null)
  194. // return null;
  195. //OrderDetail order= ReadMachineOrderDetail(mac.ID,1,3, ref errorinfo);
  196. //if (order == null)
  197. // return null;
  198. //HsmsWeb accessmac = new HsmsWeb();
  199. //OrderBlock rec= accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  200. //if (rec == null)
  201. // return null;
  202. ////将返回值填写到参数列表
  203. //int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
  204. //if (result <= 0)
  205. // return null;
  206. //order = ReadMachineOrderDetail(mac.ID, 2, 13, ref errorinfo);
  207. //if (order == null)
  208. // return null;
  209. ////构造参数结构
  210. //templist = deslist.Where(t => t.SecFType == SecType.ECID).OrderBy(t => t.FNum).ToList();
  211. //datas = MadeOrderData(templist, ref errorinfo);
  212. //if (datas == null)
  213. // return null;
  214. //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  215. //if (rec == null)
  216. // return null;
  217. ////将返回值填写到参数列表
  218. //result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
  219. //if (result <= 0)
  220. // return null;
  221. #endregion
  222. int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
  223. if (result <= 0)
  224. return null;
  225. result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
  226. if (result <= 0)
  227. return null;
  228. //遍历参数明细,判断每条的比对结果
  229. foreach (var item in deslist)
  230. {
  231. result = CompareParamsComDetail(item);
  232. }
  233. return deslist;
  234. }
  235. catch(Exception ex)
  236. {
  237. errorinfo = ex.Message.ToString();
  238. return null;
  239. }
  240. }
  241. public int CompareParamsDetail(List<ParamsComDetail> deslist,Machine mac, int ftype, ref string errorinfo)
  242. {
  243. try
  244. {
  245. int sval = 1;
  246. int fval = 3;
  247. if(ftype== SecType.ECID)
  248. {
  249. sval = 2;
  250. fval = 13;
  251. }
  252. //构造参数结构
  253. List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == ftype).OrderBy(t => t.FNum).ToList();
  254. if (templist.Count <= 0)
  255. return 1;//没有要比较的参数,直接返回
  256. List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
  257. if (datas == null)
  258. return -1;
  259. OrderDetail order = ReadMachineOrderDetail(mac.ID, sval, fval, ref errorinfo);
  260. if (order == null)
  261. return -1;
  262. HsmsWeb accessmac = new HsmsWeb();
  263. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  264. if (rec == null)
  265. return -1;
  266. //将返回值填写到参数列表
  267. int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
  268. if (result <= 0)
  269. return -1;
  270. return 1;
  271. }
  272. catch (Exception ex)
  273. {
  274. errorinfo = ex.Message.ToString();
  275. return -1;
  276. }
  277. }
  278. private int CompareParamsComDetail(ParamsComDetail entity)
  279. {
  280. if (entity.pcsubdetails.Count<=0)
  281. {
  282. if (!string.IsNullOrEmpty(entity.CL))
  283. {
  284. //说明是比较固定值
  285. if (entity.CL == entity.FVal)
  286. {
  287. entity.IsPass = 1;
  288. }
  289. else
  290. {
  291. entity.IsPass = -1;
  292. }
  293. return 1;
  294. }
  295. if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
  296. {
  297. entity.IsPass = -1;
  298. return 1;
  299. }
  300. if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
  301. {
  302. entity.IsPass = -1;
  303. return 1;
  304. }
  305. entity.IsPass = 1;
  306. }
  307. else
  308. {
  309. foreach(var item in entity.pcsubdetails)
  310. {
  311. if(item.IsCompare<=0)
  312. {
  313. item.IsPass = 1;
  314. continue;
  315. }
  316. if(string.IsNullOrEmpty(item.FVal))
  317. {
  318. item.IsPass = -1;
  319. continue;
  320. }
  321. if (!string.IsNullOrEmpty(item.CL))
  322. {
  323. //说明是比较固定值
  324. if (item.CL == item.FVal)
  325. {
  326. item.IsPass = 1;
  327. }
  328. else
  329. {
  330. item.IsPass = -1;
  331. }
  332. return 1;
  333. }
  334. if (decimal.Parse(item.FVal) < decimal.Parse(item.LCL))
  335. {
  336. item.IsPass = -1;
  337. return 1;
  338. }
  339. if (decimal.Parse(item.FVal) > decimal.Parse(item.UCL))
  340. {
  341. item.IsPass = -1;
  342. return 1;
  343. }
  344. item.IsPass = 1;
  345. }
  346. List<PCSubDetail> tempsub = entity.pcsubdetails.Where(t => t.IsCompare >= 0 && t.IsPass <= 0).ToList();
  347. if(tempsub.Count>0)
  348. {
  349. entity.IsPass = -1;
  350. }
  351. else
  352. {
  353. entity.IsPass = 1;
  354. }
  355. }
  356. return 1;
  357. }
  358. /// <summary>
  359. /// 根据机台ID读取机台信息
  360. /// </summary>
  361. /// <param name="macid"></param>
  362. /// <param name="errorinfo"></param>
  363. /// <returns></returns>
  364. private Machine ReadMachine(string maccode,ref string errorinfo)
  365. {
  366. //读取机台信息
  367. string condition = $" and a.fcode='{maccode}'";
  368. List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  369. if (macs == null)
  370. return null;
  371. if (macs.Count <= 0)
  372. {
  373. errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
  374. return null;
  375. }
  376. return macs[0];
  377. }
  378. /// <summary>
  379. /// 读取机台S1F3指令
  380. /// </summary>
  381. /// <param name="macid"></param>
  382. /// <param name="errorinfo"></param>
  383. /// <returns></returns>
  384. private OrderDetail ReadMachineOrderDetail(int macid,int sval,int fval, ref string errorinfo)
  385. {
  386. //读取机台信息
  387. string condition = $" and a.macid={macid}";
  388. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  389. if (macs == null)
  390. return null;
  391. if (macs.Count <= 0)
  392. {
  393. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  394. return null;
  395. }
  396. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
  397. List<OrderDetail> details= CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  398. if (details == null)
  399. return null;
  400. if(details.Count<=0)
  401. {
  402. errorinfo = "未找到您要的指令。";
  403. return null;
  404. }
  405. return details[0];
  406. }
  407. private OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval,string fname, ref string errorinfo)
  408. {
  409. //读取机台信息
  410. string condition = $" and a.macid={macid}";
  411. List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
  412. if (macs == null)
  413. return null;
  414. if (macs.Count <= 0)
  415. {
  416. errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
  417. return null;
  418. }
  419. condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
  420. List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
  421. if (details == null)
  422. return null;
  423. if (details.Count <= 0)
  424. {
  425. errorinfo = "未找到您要的指令。";
  426. return null;
  427. }
  428. return details[0];
  429. }
  430. /// <summary>
  431. /// 产生指令数据
  432. /// </summary>
  433. /// <param name="programparams"></param>
  434. /// <param name="errorinfo"></param>
  435. /// <returns></returns>
  436. private List<OrderData> MadeOrderData(List<ParamsComDetail> programparams, ref string errorinfo)
  437. {
  438. List<OrderData> datas = new List<OrderData>();
  439. OrderData preid = new OrderData();
  440. preid.ID = 1;
  441. preid.ParentID = 0;
  442. preid.PreID = 0;
  443. preid.FCode = "L";
  444. preid.FLen = programparams.Count;
  445. datas.Add(preid);
  446. OrderData tempentity = null;
  447. int id = 2;
  448. int fnum = 10;
  449. foreach(var item in programparams)
  450. {
  451. tempentity = new OrderData();
  452. tempentity.ID = id;
  453. tempentity.FNum = fnum;
  454. tempentity.FCode = item.DCode;
  455. tempentity.FContent = item.FVal;
  456. tempentity.ParentID = preid.ID;
  457. datas.Add(tempentity);
  458. id++;
  459. fnum += 10;
  460. }
  461. return datas;
  462. }
  463. /// <summary>
  464. /// 将程序参数列表转换成比对明细列表
  465. /// </summary>
  466. /// <param name="programparams"></param>
  467. /// <param name="errorinfo"></param>
  468. /// <returns></returns>
  469. private List<ParamsComDetail> MadeParamsComDetail(List<ProgramParamsDetail> programparams,List<PPSubDetail> subdetails, ref string errorinfo)
  470. {
  471. List<ParamsComDetail> datas = new List<ParamsComDetail>();
  472. ParamsComDetail tempentity = null;
  473. List<PCSubDetail> pcsubdetails = null;
  474. List<PPSubDetail> ppsubdetails = null;
  475. PCSubDetail subentity = null;
  476. foreach (var item in programparams)
  477. {
  478. tempentity = new ParamsComDetail();
  479. tempentity.ID = 0;
  480. tempentity.FNum = item.FNum.Value;
  481. tempentity.SecID = item.SecID;
  482. tempentity.DCode = item.DCode;
  483. tempentity.FVal = item.FVal;
  484. tempentity.UCL = item.UCL;
  485. tempentity.CL = item.CL;
  486. tempentity.LCL = item.LCL;
  487. tempentity.SVType = item.SVType.Value;
  488. tempentity.Module = item.Module.Value;
  489. tempentity.CTLType = item.CTLType.Value;
  490. tempentity.SecFType = item.SecFType;
  491. datas.Add(tempentity);
  492. pcsubdetails = new List<PCSubDetail>();
  493. tempentity.pcsubdetails = pcsubdetails;
  494. ppsubdetails = subdetails.Where(t => t.PreID == item.ID).ToList();
  495. foreach(var subitem in ppsubdetails)
  496. {
  497. subentity = new PCSubDetail();
  498. subentity.ID = 0;
  499. subentity.FNum = subitem.FNum;
  500. subentity.FName = subitem.ParameFName;
  501. subentity.UCL = subitem.UCL;
  502. subentity.CL = subitem.CL;
  503. subentity.LCL = subitem.LCL;
  504. subentity.IsCompare = subitem.IsCompare;
  505. pcsubdetails.Add(subentity);
  506. }
  507. }
  508. return datas;
  509. }
  510. /// <summary>
  511. /// 根据返回的参数列表值,更新比对结果值
  512. /// </summary>
  513. /// <param name="deslist"></param>
  514. /// <param name="orglist"></param>
  515. /// <param name="errorinfo"></param>
  516. /// <returns></returns>
  517. private int UpdateParamsComDetail(List<ParamsComDetail> deslist,List<OrderData> orglist, ref string errorinfo)
  518. {
  519. List<OrderData> ldata = orglist.Where(t => t.ParentID == 0).ToList();
  520. int preid = ldata[0].ID;
  521. ldata = orglist.Where(t => t.ParentID == preid).OrderBy(t=>t.FNum).ToList();
  522. if(deslist.Count!=ldata.Count)
  523. {
  524. errorinfo = "获取到的参数与实际参数个数不一致。";
  525. return -1;
  526. }
  527. List<PCSubDetail> pcsubdetails = null;
  528. List<OrderData> subdata = null;
  529. for(int i=0;i<deslist.Count;i++)
  530. {
  531. deslist[i].FVal = ldata[i].FContent;
  532. //更新子列表值
  533. pcsubdetails = deslist[i].pcsubdetails;
  534. subdata = orglist.Where(t => t.ParentID == ldata[i].ID).ToList();
  535. for(int j=0;j<pcsubdetails.Count;j++)
  536. {
  537. pcsubdetails[j].FVal = subdata.Count > j ? subdata[j].FContent : "";
  538. }
  539. }
  540. return 1;
  541. }
  542. /// <summary>
  543. /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
  544. /// </summary>
  545. /// <param name="maccode"></param>
  546. /// <param name="partcode"></param>
  547. /// <param name="pcode"></param>
  548. /// <param name="errorinfo"></param>
  549. /// <returns></returns>
  550. public ProgramMst FindProgram(string maccode, string partcode, string pcode, ref string errorinfo)
  551. {
  552. try
  553. {
  554. Machine mac = ReadMachine(maccode, ref errorinfo);
  555. if (mac == null)
  556. return null;
  557. string condition = $" and b.ModelID={mac.MModeID} and b.ProcessCode='{pcode}'";
  558. List<ProgramRule> rules = CurrDb.FindListForCondition<ProgramRule>(condition, ref errorinfo).ToList();
  559. if (rules == null)
  560. return null;
  561. if (rules.Count<=0)
  562. {
  563. errorinfo = "未找到对应的程序。";
  564. return null;
  565. }
  566. //先精确匹配产品编号
  567. List<ProgramRule> templist = rules.Where(t => t.PartCode == partcode).ToList();
  568. if (templist.Count > 0)
  569. return CurrDb.FindEntityFor<ProgramMst>(templist[0].ProgramMstID);
  570. //取模糊匹配的规则
  571. rules = rules.Where(t => t.PartCode.Contains("%")).ToList();
  572. if(rules.Count<=0)
  573. {
  574. errorinfo = "未找到对应的程序。";
  575. return null;
  576. }
  577. //去掉规则里产品编号的“%”符号
  578. foreach(var item in rules)
  579. {
  580. item.PartCode = item.PartCode.Replace("%", "");
  581. }
  582. //排序规则列表,按产品编号长度从大到小排列
  583. rules = rules.OrderByDescending(t => t.PartCode.Length).ToList();
  584. foreach(var item in rules)
  585. {
  586. if (partcode.Contains(item.PartCode))
  587. {
  588. return CurrDb.FindEntityFor<ProgramMst>(item.ProgramMstID);
  589. }
  590. }
  591. errorinfo = "未找到对应的程序。";
  592. return null;
  593. }
  594. catch (Exception ex)
  595. {
  596. errorinfo = ex.Message.ToString();
  597. return null;
  598. }
  599. }
  600. /// <summary>
  601. /// DP机台装载程序指令数据
  602. /// </summary>
  603. /// <param name="programname">要装载的程序名称</param>
  604. /// <param name="preid"></param>
  605. /// <param name="errorinfo"></param>
  606. /// <returns></returns>
  607. private List<OrderData> GetS2F41Data(string programname, int preid, ref string errorinfo)
  608. {
  609. string rcmd = "PP-SELECT";
  610. List<OrderData> ldata = new List<OrderData>();
  611. OrderData lentity = new OrderData();
  612. lentity.ID = 1;
  613. lentity.ParentID = 0;
  614. lentity.PreID = preid;
  615. lentity.FCode = "L";
  616. lentity.FLen = 2;
  617. ldata.Add(lentity);
  618. OrderData entity = new OrderData();
  619. entity.ID = 2;
  620. entity.ParentID = 1;
  621. entity.FNum = 10;
  622. entity.PreID = preid;
  623. entity.FCode = "A";
  624. entity.FContent = rcmd;
  625. entity.FLen = rcmd.Length;
  626. ldata.Add(entity);
  627. entity = new OrderData();//添加L节点
  628. entity.ID = 3;
  629. entity.ParentID = 1;
  630. entity.FNum = 20;
  631. entity.PreID = preid;
  632. entity.FCode = "L";
  633. entity.FLen = 1;
  634. ldata.Add(entity);
  635. entity = new OrderData();//添加L节点
  636. entity.ID = 4;
  637. entity.ParentID = 3;
  638. entity.FNum = 10;
  639. entity.PreID = preid;
  640. entity.FCode = "L";
  641. entity.FLen = 2;
  642. ldata.Add(entity);
  643. string cpname = "PPID";
  644. entity = new OrderData();
  645. entity.ID = 5;
  646. entity.ParentID = 4;
  647. entity.FNum = 10;
  648. entity.PreID = preid;
  649. entity.FCode = "A";
  650. entity.FContent = cpname;
  651. entity.FLen = cpname.Length;
  652. ldata.Add(entity);
  653. entity = new OrderData();
  654. entity.ID = 6;
  655. entity.ParentID = 4;
  656. entity.FNum = 20;
  657. entity.PreID = preid;
  658. entity.FCode = "A";
  659. entity.FContent = programname;
  660. entity.FLen = programname.Length;
  661. ldata.Add(entity);
  662. return ldata;
  663. }
  664. /// <summary>
  665. /// 构造S7F3数据
  666. /// </summary>
  667. /// <param name="programname"></param>
  668. /// <param name="filestr"></param>
  669. /// <param name="errorinfo"></param>
  670. /// <returns></returns>
  671. private List<OrderData> GetS7F3Data(string programname,int filelen, string filepath,int preid, ref string errorinfo)
  672. {
  673. List<OrderData> ldata = new List<OrderData>();
  674. OrderData lentity = new OrderData();
  675. lentity.ID = 1;
  676. lentity.ParentID = 0;
  677. lentity.PreID = preid;
  678. lentity.FCode = "L";
  679. lentity.FLen = 2;
  680. ldata.Add(lentity);
  681. OrderData entity = new OrderData();
  682. entity.ID = 2;
  683. entity.ParentID = 1;
  684. entity.FNum = 10;
  685. entity.PreID = preid;
  686. entity.FCode = "A";
  687. entity.FContent = programname;
  688. entity.FLen = programname.Length;
  689. ldata.Add(entity);
  690. entity = new OrderData();
  691. entity.ID = 2;
  692. entity.ParentID = 1;
  693. entity.FNum = 20;
  694. entity.PreID = preid;
  695. entity.FCode = "B";
  696. entity.FContent = filepath;
  697. entity.OrgDatas = null;
  698. entity.FLen = filelen;
  699. //entity.FLen = programname.Length;
  700. ldata.Add(entity);
  701. return ldata;
  702. }
  703. /// <summary>
  704. /// 构造S7F1数据
  705. /// </summary>
  706. /// <param name="programname"></param>
  707. /// <param name="filestr"></param>
  708. /// <param name="errorinfo"></param>
  709. /// <returns></returns>
  710. private List<OrderData> GetS7F1Data(string programname,int len, int preid, ref string errorinfo)
  711. {
  712. List<OrderData> ldata = new List<OrderData>();
  713. OrderData lentity = new OrderData();
  714. lentity.ID = 1;
  715. lentity.ParentID = 0;
  716. lentity.PreID = preid;
  717. lentity.FCode = "L";
  718. lentity.FLen = 2;
  719. ldata.Add(lentity);
  720. OrderData entity = new OrderData();
  721. entity.ID = 2;
  722. entity.ParentID = 1;
  723. entity.FNum = 10;
  724. entity.PreID = preid;
  725. entity.FCode = "A";
  726. entity.FContent = programname;
  727. entity.FLen = programname.Length;
  728. ldata.Add(entity);
  729. entity = new OrderData();
  730. entity.ID = 2;
  731. entity.ParentID = 1;
  732. entity.FNum = 20;
  733. entity.PreID = preid;
  734. entity.FCode = "U4";
  735. entity.FLen = 1;
  736. entity.FContent = len.ToString();
  737. //entity.OrgDatas = filedatas;
  738. //entity.FLen = programname.Length;
  739. ldata.Add(entity);
  740. return ldata;
  741. }
  742. /// <summary>
  743. /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台
  744. /// </summary>
  745. /// <param name="maccode"></param>
  746. /// <param name="partcode"></param>
  747. /// <param name="pcode"></param>
  748. /// <param name="errorinfo"></param>
  749. /// <returns></returns>
  750. public ProgramMst DownloadProgram(string maccode, string partcode, string pcode, int filelen,string filepath, ref string errorinfo)
  751. {
  752. try
  753. {
  754. Machine mac = ReadMachine(maccode, ref errorinfo);
  755. if (mac == null)
  756. return null;
  757. //查找程序文件
  758. ProgramMst mst = FindProgram(maccode, partcode, pcode, ref errorinfo);
  759. if(mst==null)
  760. {
  761. return null;
  762. }
  763. //OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
  764. //if (order == null)
  765. // return null;
  766. ////string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
  767. ////filedir += "\\" + maccode;
  768. ////string filepath = filedir + "\\" + mst.FName;
  769. ////byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  770. ////if (filedatas == null)
  771. ////{
  772. //// return null;
  773. ////}
  774. //string condition = $" and a.preid={order.ID}";
  775. //List<OrderData> datas = GetS7F1Data(mst.FName, filelen, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  776. //if (datas == null)
  777. // return null;
  778. //HsmsWeb accessmac = new HsmsWeb();
  779. //OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  780. //if (rec == null)
  781. // return null;
  782. //if(int.Parse(rec.Datalists[0].FContent)!=0)
  783. //{
  784. // errorinfo = "机台不接受程序。";
  785. // return null;
  786. //}
  787. //order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
  788. //datas = GetS7F3Data(mst.FName, filelen, filepath, order.PreID, ref errorinfo);
  789. //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  790. //if (rec == null)
  791. // return null;
  792. //if (int.Parse(rec.Datalists[0].FContent) != 0)
  793. //{
  794. // errorinfo = "机台没有正确接受程序。";
  795. // return null;
  796. //}
  797. ////发送程序装载命令,暂时还不能执行
  798. //order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  799. //datas = GetS2F41Data(mst.FName, order.PreID, ref errorinfo);
  800. //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  801. //if (rec == null)
  802. // return null;
  803. //if (int.Parse(rec.Datalists[1].FContent) != 0)
  804. //{
  805. // errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  806. // return null;
  807. //}
  808. return mst;
  809. }
  810. catch (Exception ex)
  811. {
  812. errorinfo = ex.Message.ToString();
  813. return null;
  814. }
  815. }
  816. private void SendInfo(string maccode, string info)
  817. {
  818. try
  819. {
  820. MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  821. string temperrorinfo = "";
  822. senddal.SendS10F3(maccode, info, ref temperrorinfo);
  823. }
  824. catch
  825. {
  826. return;
  827. }
  828. }
  829. private List<OrderData> GetS7F3Data(string programname, byte[] filedatas, int preid, ref string errorinfo)
  830. {
  831. List<OrderData> ldata = new List<OrderData>();
  832. OrderData lentity = new OrderData();
  833. lentity.ID = 1;
  834. lentity.ParentID = 0;
  835. lentity.PreID = preid;
  836. lentity.FCode = "L";
  837. lentity.FLen = 2;
  838. ldata.Add(lentity);
  839. OrderData entity = new OrderData();
  840. entity.ID = 2;
  841. entity.ParentID = 1;
  842. entity.FNum = 10;
  843. entity.PreID = preid;
  844. entity.FCode = "A";
  845. entity.FContent = programname;
  846. entity.FLen = programname.Length;
  847. entity.Remark = "";
  848. ldata.Add(entity);
  849. entity = new OrderData();
  850. entity.ID = 2;
  851. entity.ParentID = 1;
  852. entity.FNum = 20;
  853. entity.PreID = preid;
  854. entity.FCode = "B";
  855. entity.FContent = "";
  856. entity.OrgDatas = filedatas;
  857. entity.FLen = filedatas.Length;
  858. //entity.FLen = programname.Length;
  859. ldata.Add(entity);
  860. return ldata;
  861. }
  862. public int DownloadProgram(Machine mac, string programname, byte[] filedatas, ref string errorinfo)
  863. {
  864. programname = programname + "";
  865. try
  866. {
  867. //要修改程序名称
  868. OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
  869. if (order == null)
  870. return -1;
  871. string condition = $" and a.preid={order.ID}";
  872. List<OrderData> datas = GetS7F1Data(programname, filedatas.Length, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  873. if (datas == null)
  874. return -1;
  875. HsmsWeb accessmac = new HsmsWeb();
  876. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  877. if (rec == null)
  878. return -1;
  879. int result = int.Parse(rec.Datalists[0].FContent);
  880. if (result > 1)
  881. {
  882. //errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
  883. //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  884. errorinfo = rec.Datalists[0].FContent;
  885. return -100;
  886. }
  887. if (result == 0)
  888. {
  889. //产生文件
  890. order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
  891. datas = GetS7F3Data(programname, filedatas, order.PreID, ref errorinfo);
  892. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  893. if (rec == null)
  894. return -1;
  895. if (int.Parse(rec.Datalists[0].FContent) != 0)
  896. {
  897. //errorinfo = "机台没有正确接受程序。";
  898. //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
  899. errorinfo = rec.Datalists[0].FContent;
  900. return -200;
  901. }
  902. }
  903. return 1;
  904. }
  905. catch (Exception ex)
  906. {
  907. errorinfo = ex.Message.ToString();
  908. return -1;
  909. }
  910. }
  911. public int DownloadProgram(Machine mac, string orgprogramname, ref string errorinfo)
  912. {
  913. try
  914. {
  915. orgprogramname = orgprogramname.Trim();
  916. #region
  917. #endregion
  918. string programname = orgprogramname;
  919. int result = 0;
  920. if (mac.CallPMode <= 2)
  921. {
  922. //说明要下载程序
  923. MacProgramDal macprogramdal = new MacProgramDal(CurrDb);
  924. int orgtypeid = 1;
  925. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  926. if (mac.CallPMode == 1)
  927. {
  928. orgtypeid = 1;
  929. }
  930. else
  931. {
  932. orgtypeid = 2;
  933. }
  934. byte[] filedatas = macprogramdal.ReadMacProgram(mac, orgprogramname, orgtypeid, programdir, ref errorinfo);
  935. if (filedatas == null)
  936. return -30;
  937. result = DownloadProgram(mac, programname, filedatas, ref errorinfo);
  938. if (result <= 0)
  939. return result;
  940. }
  941. //提示下载程序成功
  942. //MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
  943. //string temperrorinfo = "";
  944. //senddal.SendS10F3(mac.FCode, $"Recipe download success [{orgprogramname}]", ref temperrorinfo);
  945. //result = SelProgram(mac, programname, ref errorinfo);
  946. //if (result <= 0)
  947. // return -10;
  948. return 1;
  949. }
  950. catch (Exception ex)
  951. {
  952. errorinfo = ex.Message.ToString();
  953. return -1;
  954. }
  955. }
  956. /// <summary>
  957. /// 根据程序名称下载程序
  958. /// </summary>
  959. /// <param name="maccode"></param>
  960. /// <param name="programname"></param>
  961. /// <param name="errorinfo"></param>
  962. /// <returns></returns>
  963. public int SelProgram(Machine mac, string programname,ref string errorinfo)
  964. {
  965. try
  966. {
  967. //发送程序装载命令,暂时还不能执行
  968. HsmsWeb accessmac = new HsmsWeb();
  969. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41select", ref errorinfo);
  970. //从机型参数中读取程序参数信息
  971. string condition = $" and a.preid={order.ID}";
  972. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  973. if (datas == null)
  974. return -1;
  975. //< L[2]
  976. // < A[9] PP_SELECT >
  977. // < L[2]
  978. // < L[2]
  979. // < A[4] PPID >
  980. // < A[13] 4521D - SB - 13_1 >
  981. // >
  982. // < L[2]
  983. // < A[6] PROMPT >
  984. // < BOOLEAN[1] 0 >
  985. // >
  986. // >
  987. // >
  988. //List<OrderData> datas = GetS2F41Data(programname, order.PreID, ref errorinfo);
  989. List <OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
  990. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t=>t.FNum).ToList();//第一个L下的节点集合
  991. templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
  992. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
  993. templist[1].FContent = programname;
  994. templist[1].FLen = programname.Length;
  995. //string aa = JsonConvert.SerializeObject(datas);
  996. //return 1;//20201225
  997. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  998. if (rec == null)
  999. return -1;
  1000. if (int.Parse(rec.Datalists[1].FContent) != 0)
  1001. {
  1002. errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  1003. errorinfo = rec.Datalists[1].FContent;
  1004. return -300;
  1005. }
  1006. return 1;
  1007. }
  1008. catch (Exception ex)
  1009. {
  1010. errorinfo = ex.Message.ToString();
  1011. return -1;
  1012. }
  1013. }
  1014. public int SelProgramSineTest(Machine mac, string programname,string lotID, ref string errorinfo)
  1015. {
  1016. try
  1017. {
  1018. string prigramDir = $"O:\\{programname}\\Debug\\{programname}.dll";
  1019. //发送程序装载命令,暂时还不能执行
  1020. HsmsWeb accessmac = new HsmsWeb();
  1021. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT_LOTID", ref errorinfo);
  1022. //从机型参数中读取程序参数信息
  1023. string condition = $" and a.preid={order.ID}";
  1024. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  1025. if (datas == null)
  1026. return -1;
  1027. List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
  1028. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
  1029. //int pid = templist[1].ID;//第二个L的ID
  1030. templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
  1031. List<OrderData> templiststr = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第四个L下的节点集合
  1032. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
  1033. templist[1].FContent = prigramDir;
  1034. templist[1].FLen = prigramDir.Length;
  1035. templiststr[1].FContent = lotID;
  1036. templiststr[1].FLen = lotID.Length;
  1037. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1038. if (rec == null)
  1039. return -1;
  1040. if (int.Parse(rec.Datalists[1].FContent) != 0)
  1041. {
  1042. errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  1043. errorinfo = rec.Datalists[1].FContent;
  1044. return -300;
  1045. }
  1046. return 1;
  1047. }
  1048. catch (Exception ex)
  1049. {
  1050. errorinfo = ex.Message.ToString();
  1051. return -1;
  1052. }
  1053. }
  1054. public int SelProgramQuickMark(Machine mac, string programname, ref string errorinfo)
  1055. {
  1056. try
  1057. {
  1058. //发送程序装载命令,暂时还不能执行
  1059. HsmsWeb accessmac = new HsmsWeb();
  1060. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT", ref errorinfo);
  1061. //从机型参数中读取程序参数信息
  1062. string condition = $" and a.preid={order.ID}";
  1063. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  1064. if (datas == null)
  1065. return -1;
  1066. List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
  1067. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
  1068. templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
  1069. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
  1070. templist[1].FContent = programname;
  1071. templist[1].FLen = programname.Length;
  1072. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1073. if (rec == null)
  1074. return -1;
  1075. if (int.Parse(rec.Datalists[1].FContent) != 0)
  1076. {
  1077. errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  1078. errorinfo = rec.Datalists[1].FContent;
  1079. return -300;
  1080. }
  1081. return 1;
  1082. }
  1083. catch (Exception ex)
  1084. {
  1085. errorinfo = ex.Message.ToString();
  1086. return -1;
  1087. }
  1088. }
  1089. public int SelProgramQTTest(Machine mac, string programname, string lotID,string userID,string stationID, ref string errorinfo)
  1090. {
  1091. try
  1092. {
  1093. //发送程序装载命令,暂时还不能执行
  1094. HsmsWeb accessmac = new HsmsWeb();
  1095. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT_QT", ref errorinfo);
  1096. //从机型参数中读取程序参数信息
  1097. string condition = $" and a.preid={order.ID}";
  1098. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  1099. if (datas == null)
  1100. return -1;
  1101. List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
  1102. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
  1103. //int pid = templist[1].ID;//第二个L的ID
  1104. templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
  1105. List<OrderData> templiststr = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第2个L的节点集合
  1106. templiststr[1].FContent = lotID;
  1107. templiststr[1].FLen = lotID.Length;
  1108. //templiststr[1].FContent = programname;
  1109. //templiststr[1].FLen = programname.Length;
  1110. templiststr = datas.Where(t => t.ParentID == templist[2].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第3个L的节点集合
  1111. templiststr[1].FContent = userID;
  1112. templiststr[1].FLen = userID.Length;
  1113. templiststr = datas.Where(t => t.ParentID == templist[3].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第4个L的节点集合
  1114. templiststr[1].FContent = stationID;
  1115. templiststr[1].FLen = stationID.Length;
  1116. templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第1个L的节点集合
  1117. templist[1].FContent = programname;
  1118. templist[1].FLen = programname.Length;
  1119. //templist[1].FContent = lotID;
  1120. //templist[1].FLen = lotID.Length;
  1121. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1122. if (rec == null)
  1123. return -1;
  1124. if (int.Parse(rec.Datalists[1].FContent) != 0)
  1125. {
  1126. errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  1127. errorinfo = rec.Datalists[1].FContent;
  1128. return -300;
  1129. }
  1130. return 1;
  1131. }
  1132. catch (Exception ex)
  1133. {
  1134. errorinfo = ex.Message.ToString();
  1135. return -1;
  1136. }
  1137. }
  1138. /// <summary>
  1139. /// 构造获取机台程序名称的数据
  1140. /// </summary>
  1141. /// <param name="mmsec"></param>
  1142. /// <param name="preid"></param>
  1143. /// <param name="errorinfo"></param>
  1144. /// <returns></returns>
  1145. private List<OrderData> GetMacProgram(MMSecDetail mmsec, int preid, ref string errorinfo)
  1146. {
  1147. List<OrderData> ldata = new List<OrderData>();
  1148. OrderData lentity = new OrderData();
  1149. lentity.ID = 1;
  1150. lentity.ParentID = 0;
  1151. lentity.PreID = preid;
  1152. lentity.FCode = "L";
  1153. lentity.FLen = 1;
  1154. ldata.Add(lentity);
  1155. OrderData entity = new OrderData();
  1156. entity.ID = 2;
  1157. entity.ParentID = 1;
  1158. entity.FNum = 10;
  1159. entity.PreID = preid;
  1160. entity.FCode = mmsec.DCode;
  1161. entity.FContent = mmsec.FVal;
  1162. entity.FLen = mmsec.FVal.Length;
  1163. ldata.Add(entity);
  1164. return ldata;
  1165. }
  1166. /// <summary>
  1167. /// 根据机台编号,读取机台当前正在使用的程序名称
  1168. /// </summary>
  1169. /// <param name="maccode"></param>
  1170. /// <param name="errorinfo"></param>
  1171. /// <returns></returns>
  1172. public string GetMacProgram(string maccode, ref string errorinfo)
  1173. {
  1174. try
  1175. {
  1176. Machine mac = ReadMachine(maccode, ref errorinfo);
  1177. if (mac == null)
  1178. return "";
  1179. OrderDetail order = ReadMachineOrderDetail(mac.ID,1, 3, ref errorinfo);
  1180. if (order == null)
  1181. return "";
  1182. //从机型参数中读取程序参数信息
  1183. string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
  1184. List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  1185. if (mmsecs == null)
  1186. return "";
  1187. if(mmsecs.Count<=0)
  1188. {
  1189. errorinfo = "未找到此机台对应的程序参数说明。";
  1190. return "";
  1191. }
  1192. List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
  1193. if (datas == null)
  1194. return "";
  1195. HsmsWeb accessmac = new HsmsWeb();
  1196. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1197. if (rec == null)
  1198. return "";
  1199. return rec.Datalists[1].FContent;
  1200. }
  1201. catch (Exception ex)
  1202. {
  1203. errorinfo = ex.Message.ToString();
  1204. return "";
  1205. }
  1206. }
  1207. /// <summary>
  1208. /// 根据机台查找机台使用的程序
  1209. /// </summary>
  1210. /// <param name="mac"></param>
  1211. /// <param name="errorinfo"></param>
  1212. /// <returns></returns>
  1213. public ProgramMst GetMacProgram(Machine mac, ref string errorinfo)
  1214. {
  1215. try
  1216. {
  1217. OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
  1218. if (order == null)
  1219. return null;
  1220. //从机型参数中读取程序参数信息
  1221. string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
  1222. List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
  1223. if (mmsecs == null)
  1224. return null;
  1225. if (mmsecs.Count <= 0)
  1226. {
  1227. errorinfo = "未找到此机台对应的程序参数说明。";
  1228. return null;
  1229. }
  1230. List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
  1231. if (datas == null)
  1232. return null;
  1233. HsmsWeb accessmac = new HsmsWeb();
  1234. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1235. if (rec == null)
  1236. return null;
  1237. string programname= rec.Datalists[1].FContent;
  1238. condition = $" and a.fname='{programname}'";
  1239. List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
  1240. if (msts == null)
  1241. return null;
  1242. if (msts.Count <= 0)
  1243. {
  1244. errorinfo = $"未找到程序名称【{programname}】的记录。";
  1245. return null;
  1246. }
  1247. return msts[0];
  1248. }
  1249. catch (Exception ex)
  1250. {
  1251. errorinfo = ex.Message.ToString();
  1252. return null;
  1253. }
  1254. }
  1255. /// <summary>
  1256. /// 发送停机指令
  1257. /// </summary>
  1258. /// <param name="maccode"></param>
  1259. /// <param name="errorinfo"></param>
  1260. /// <returns></returns>
  1261. public int SendStopMac(string maccode, ref string errorinfo)
  1262. {
  1263. try
  1264. {
  1265. Machine mac = ReadMachine(maccode, ref errorinfo);
  1266. if (mac == null)
  1267. return -1;
  1268. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41,"s2f41stop", ref errorinfo);
  1269. if (order == null)
  1270. return -1;
  1271. //从机型参数中读取程序参数信息
  1272. string condition = $" and a.preid={order.ID}";
  1273. List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  1274. if (datas == null)
  1275. return -1;
  1276. HsmsWeb accessmac = new HsmsWeb();
  1277. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1278. if (rec == null)
  1279. return -1;
  1280. //休眠5秒,等待机台状态恢复
  1281. Thread.Sleep(5000);
  1282. order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
  1283. if (order == null)
  1284. return -1;
  1285. //从机型参数中读取程序参数信息
  1286. condition = $" and a.preid={order.ID}";
  1287. datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
  1288. if (datas == null)
  1289. return -1;
  1290. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1291. if (rec == null)
  1292. return -1;
  1293. return 1;
  1294. }
  1295. catch (Exception ex)
  1296. {
  1297. errorinfo = ex.Message.ToString();
  1298. return -1;
  1299. }
  1300. }
  1301. #region Web接口使用
  1302. public IEnumerable<ParamsComMst> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  1303. {
  1304. var sql = new ParamsComMst().GetSelectSql();
  1305. var pros = CurrDb.FindListForCondition<ParamsComMst>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
  1306. // 加入参数信息
  1307. if (pros != null && pros.Count() > 0)
  1308. {
  1309. for (var i = 0; i < pros.Count(); i++)
  1310. {
  1311. var detail = CurrDb.FindListForCondition<ParamsComDetail>($" and a.PreID='{pros[i].ID}'", ref errorinfo).ToList();
  1312. // var detail = CurrDb.FindList<ParamsComDetail>(sql).ToList();
  1313. if (detail == null)
  1314. {
  1315. detail = new List<ParamsComDetail>();
  1316. }
  1317. pros[i].CurrDetails = detail;
  1318. }
  1319. }
  1320. return pros;
  1321. }
  1322. public int GetCount(string filter)
  1323. {
  1324. string errorinfo = string.Empty;
  1325. var entities = CurrDb.FindListForCondition<ParamsComMst>(filter, ref errorinfo);
  1326. if (entities != null)
  1327. {
  1328. return entities.Count();
  1329. }
  1330. return 0;
  1331. }
  1332. #endregion
  1333. private List<FileParamsComDetail> MadeFileParamsComDetail(List<ProgramFileParams> programparams, ref string errorinfo)
  1334. {
  1335. List<FileParamsComDetail> datas = new List<FileParamsComDetail>();
  1336. FileParamsComDetail tempentity = null;
  1337. foreach (var item in programparams)
  1338. {
  1339. tempentity = new FileParamsComDetail();
  1340. tempentity.ID = 0;
  1341. tempentity.FName = item.FName;
  1342. tempentity.FNum = item.FNum;
  1343. tempentity.FVal = "";
  1344. tempentity.UCL = item.UCL;
  1345. tempentity.CL = item.CL;
  1346. tempentity.LCL = item.LCL;
  1347. datas.Add(tempentity);
  1348. }
  1349. return datas;
  1350. }
  1351. private int CompareFileParamsComDetail(FileParamsComDetail entity)
  1352. {
  1353. if (!string.IsNullOrEmpty(entity.CL))
  1354. {
  1355. //说明是比较固定值
  1356. if (entity.CL == entity.FVal)
  1357. {
  1358. entity.IsPass = 1;
  1359. }
  1360. else
  1361. {
  1362. entity.IsPass = -1;
  1363. }
  1364. return 1;
  1365. }
  1366. if(string.IsNullOrEmpty(entity.FVal))
  1367. {
  1368. entity.IsPass = -1;
  1369. return 1;
  1370. }
  1371. if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
  1372. {
  1373. entity.IsPass = -1;
  1374. return 1;
  1375. }
  1376. if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
  1377. {
  1378. entity.IsPass = -1;
  1379. return 1;
  1380. }
  1381. entity.IsPass = 1;
  1382. return 1;
  1383. }
  1384. public List<FileParamsComDetail> CompareFileParamsDetail(IDatabase db,Machine mac, ProgramMst program,MacProgram macprogram, ref string errorinfo)
  1385. {
  1386. try
  1387. {
  1388. //读取程序参数
  1389. string condition = $" and a.preid={program.ID} and a.IsCompare=1";
  1390. List<ProgramFileParams> programparams = CurrDb.FindListForCondition<ProgramFileParams>(condition, ref errorinfo).ToList();
  1391. if (programparams == null)
  1392. return null;
  1393. List<FileParamsComDetail> deslist = MadeFileParamsComDetail(programparams, ref errorinfo);
  1394. if (deslist == null)
  1395. return null;
  1396. if (deslist.Count <= 0)//没有设置比对参数
  1397. return deslist;
  1398. //int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
  1399. //if (result <= 0)
  1400. // return null;
  1401. //result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
  1402. //if (result <= 0)
  1403. // return null;
  1404. //读取机台程序文件,并解析出参数值
  1405. string rarpath = AppConfigurtaionServices.Configuration["rarpath"];
  1406. string desdir = AppConfigurtaionServices.Configuration["DesDir"];
  1407. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  1408. Dad3350ProgramDal knsprogram = new Dad3350ProgramDal(db);
  1409. List<FileParams> fileparams = knsprogram.GetProgramParams(rarpath, mac, program, desdir,programdir,macprogram, ref errorinfo);
  1410. if (fileparams == null)
  1411. return null;
  1412. //遍历参数明细,判断每条的比对结果
  1413. foreach (var item in deslist)
  1414. {
  1415. List<FileParams> tempfileparams = fileparams.Where(t => t.FName == item.FName).ToList();
  1416. if (tempfileparams.Count > 0)
  1417. item.FVal = tempfileparams[0].FVal;
  1418. int result = CompareFileParamsComDetail(item);
  1419. }
  1420. return deslist;
  1421. }
  1422. catch (Exception ex)
  1423. {
  1424. errorinfo = ex.Message.ToString();
  1425. return null;
  1426. }
  1427. }
  1428. /// <summary>
  1429. /// 文件参数比对
  1430. /// </summary>
  1431. /// <param name="mac"></param>
  1432. /// <param name="programmst"></param>
  1433. /// <param name="errorinfo"></param>
  1434. /// <returns></returns>
  1435. //public FileParamsComMst CompareFileParams(IDatabase db,Machine mac, ProgramMst programmst, ref string errorinfo)
  1436. //{
  1437. // try
  1438. // {
  1439. // //读取机台信息
  1440. // //Machine mac = ReadMachine(maccode, ref errorinfo);
  1441. // //if (mac == null)
  1442. // // return null;
  1443. // List<FileParamsComDetail> deslist = CompareFileParamsDetail(db,mac, programmst, ref errorinfo);
  1444. // if (deslist == null)
  1445. // return null;
  1446. // FileParamsComMst mst = new FileParamsComMst();
  1447. // mst.CurrDetails = deslist;
  1448. // List<FileParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
  1449. // if (nopass.Count > 0)
  1450. // {
  1451. // mst.IsPass = -1;
  1452. // }
  1453. // else
  1454. // {
  1455. // mst.IsPass = 1;
  1456. // }
  1457. // mst.MacID = mac.ID;
  1458. // mst.ProgramID = programmst.ID;
  1459. // return mst;
  1460. // }
  1461. // catch (Exception ex)
  1462. // {
  1463. // errorinfo = ex.Message.ToString();
  1464. // return null;
  1465. // }
  1466. //}
  1467. public FileParamsComMst IUFileParamsComMst(FileParamsComMst mst, string usercode, ref string errorinfo)
  1468. {
  1469. try
  1470. {
  1471. int result = CurrDb.InsertFor(mst, usercode);
  1472. if (result < 0)
  1473. {
  1474. return null;
  1475. }
  1476. object objid = CurrDb.FindObject("select @@IDENTITY");
  1477. if (objid.ToString() == "")
  1478. {
  1479. return null;
  1480. }
  1481. int id = int.Parse(objid.ToString());
  1482. foreach (var item in mst.CurrDetails)
  1483. {
  1484. item.PreID = id;
  1485. item.EntityStatusID = 1;
  1486. item.ID = 0;
  1487. }
  1488. result = CurrDb.InsertFor<FileParamsComDetail>(mst.CurrDetails, usercode);
  1489. if (result < 0)
  1490. return null;
  1491. mst = CurrDb.FindEntityFor<FileParamsComMst>(id);
  1492. return mst;
  1493. }
  1494. catch (Exception e)
  1495. {
  1496. errorinfo = e.Message;
  1497. return null;
  1498. }
  1499. }
  1500. private List<OrderData> GetS2F41DataForHit(int preid, string rcmd, ref string errorinfo)
  1501. {
  1502. //string rcmd = "ENTER_HIT";
  1503. List<OrderData> ldata = new List<OrderData>();
  1504. OrderData lentity = new OrderData();
  1505. lentity.ID = 1;
  1506. lentity.ParentID = 0;
  1507. lentity.PreID = preid;
  1508. lentity.FCode = "L";
  1509. lentity.FLen = 2;
  1510. ldata.Add(lentity);
  1511. OrderData entity = new OrderData();
  1512. entity.ID = 2;
  1513. entity.ParentID = 1;
  1514. entity.FNum = 10;
  1515. entity.PreID = preid;
  1516. entity.FCode = "A";
  1517. entity.FContent = rcmd;
  1518. entity.FLen = rcmd.Length;
  1519. ldata.Add(entity);
  1520. entity = new OrderData();//添加L节点
  1521. entity.ID = 3;
  1522. entity.ParentID = 1;
  1523. entity.FNum = 20;
  1524. entity.PreID = preid;
  1525. entity.FCode = "L";
  1526. entity.FLen = 0;
  1527. ldata.Add(entity);
  1528. return ldata;
  1529. }
  1530. private int SetHit(Machine mac, string rcmd, ref string errorinfo)
  1531. {
  1532. try
  1533. {
  1534. HsmsWeb accessmac = new HsmsWeb();
  1535. //发送指令,将程序模式设置为hit模式
  1536. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  1537. List<OrderData> datas = GetS2F41DataForHit(order.PreID, rcmd, ref errorinfo);
  1538. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1539. if (rec == null)
  1540. return -1;
  1541. int result = int.Parse(rec.Datalists[1].FContent);
  1542. if (result == 5)
  1543. return 1;
  1544. if (result == 0)
  1545. return 1;
  1546. return -1;
  1547. }
  1548. catch (Exception ex)
  1549. {
  1550. errorinfo = ex.Message.ToString();
  1551. return -1;
  1552. }
  1553. }
  1554. private List<OrderData> GetS7F5Data(string programname, int preid, ref string errorinfo)
  1555. {
  1556. List<OrderData> ldata = new List<OrderData>();
  1557. OrderData lentity = new OrderData();
  1558. lentity.ID = 1;
  1559. lentity.ParentID = 0;
  1560. lentity.PreID = preid;
  1561. lentity.FCode = "A";
  1562. lentity.FContent = programname;
  1563. lentity.FLen = programname.Length;
  1564. ldata.Add(lentity);
  1565. return ldata;
  1566. }
  1567. public MacProgram SaveProgram(string maccode, string usercode, ref string errorinfo)
  1568. {
  1569. try
  1570. {
  1571. //读取机台信息
  1572. string condition = $" and a.FCode='{maccode}'";
  1573. Machine mac = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList()[0];
  1574. //string programname = (string)imputds["programname"];
  1575. ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
  1576. string programname = comdal.GetMacProgramName(mac, ref errorinfo);
  1577. if (string.IsNullOrEmpty(programname))
  1578. {
  1579. errorinfo = $"未找到机台【{maccode}】对应的程序。{errorinfo}";
  1580. return null;
  1581. }
  1582. //从机台上读取文件并保存
  1583. string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
  1584. MacProgram macprogram = SaveProgramFromMac(mac, programname, programdir, ref errorinfo);
  1585. if (macprogram == null)
  1586. {
  1587. return null;
  1588. }
  1589. return macprogram;
  1590. }
  1591. catch (Exception ex)
  1592. {
  1593. errorinfo = ex.Message.ToString();
  1594. CurrDb.Rollback();
  1595. return null;
  1596. }
  1597. }
  1598. public MacProgram SaveProgramFromMac(Machine mac, string programname, string desdir, ref string errorinfo)
  1599. {
  1600. try
  1601. {
  1602. HsmsWeb accessmac = new HsmsWeb();
  1603. //int result = SetHit(mac, "ENTER_HIT", ref errorinfo);
  1604. //if (result <= 0)
  1605. //{
  1606. // return null;
  1607. //}
  1608. //发送取程序的指令
  1609. OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
  1610. List<OrderData> datas = GetS2F41Data(programname, order.PreID, ref errorinfo);
  1611. OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1612. if (rec == null)
  1613. return null;
  1614. int result = int.Parse(rec.Datalists[1].FContent);
  1615. if (result != 0 && result != 4)
  1616. {
  1617. errorinfo = $"下载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
  1618. return null;
  1619. }
  1620. if (result == 4)
  1621. {
  1622. //说明机台已经开始处理程序,休眠5秒钟,等待机台处理
  1623. //或等待处理完成事件上来
  1624. Thread.Sleep(1000 * 5);
  1625. }
  1626. order = ReadMachineOrderDetail(mac.ID, 7, 5, ref errorinfo);
  1627. datas = GetS7F5Data(programname, order.PreID, ref errorinfo);
  1628. rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
  1629. if (rec == null)
  1630. return null;
  1631. datas = rec.Datalists;
  1632. if (datas[0].FLen == 0)
  1633. {
  1634. errorinfo = "未能正确的获取程序文件。";
  1635. return null;
  1636. }
  1637. //result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo);
  1638. //if (result <= 0)
  1639. // return null;
  1640. MacProgramDal dal = new MacProgramDal(CurrDb);
  1641. MacProgram macprogram = dal.IMacProgram(mac, programname, desdir, datas[2].OrgDatas, ref errorinfo);
  1642. return macprogram;
  1643. }
  1644. catch (Exception ex)
  1645. {
  1646. errorinfo = ex.Message.ToString();
  1647. return null;
  1648. }
  1649. }
  1650. }
  1651. }