123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Rms;
- using DllHsms;
- using DllHsmsWeb;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace DllEapDal
- {
- /// <summary>
- /// 参数比对数据访问层
- /// </summary>
- public class AsmProgramDal
- {
- private IDatabase CurrDb = null;
- public AsmProgramDal(IDatabase db)
- {
- CurrDb = db;
- }
- public ParamsComMst IUParamsComMst(ParamsComMst mst,string usercode, ref string errorinfo)
- {
- try
- {
- int result = CurrDb.InsertFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- foreach (var item in mst.CurrDetails)
- {
- item.PreID = id;
- item.EntityStatusID = 1;
- item.ID = 0;
- }
- List<PCSubDetail> pcsubdetails = null;
- foreach(var item in mst.CurrDetails)
- {
- result = CurrDb.InsertFor<ParamsComDetail>(item, usercode);
- if (result < 0)
- return null;
- objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int subid = int.Parse(objid.ToString());
- pcsubdetails = item.pcsubdetails;
- foreach (var subitem in pcsubdetails)
- {
- subitem.PreID = subid;
- subitem.ID = 0;
- subitem.EntityStatusID = 1;
- }
- result = CurrDb.InsertFor<PCSubDetail>(pcsubdetails, usercode);
- if (result < 0)
- return null;
- }
- mst = CurrDb.FindEntityFor<ParamsComMst>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private string GetSecCode(int ftype, ref string errorinfo)
- {
- try
- {
- StringBuilder sqlstr = new StringBuilder(100);
- string code = "";
- switch (ftype)
- {
- case 1:
- code = "S";
- break;
- case 2:
- code = "E";
- break;
- case 3:
- code = "C";
- break;
- }
- sqlstr.AppendFormat("SELECT max(fcode) FROM sec where fcode like '{0}%'", code);
- object obj = CurrDb.FindObject(sqlstr.ToString());
- if (obj.ToString() == "")
- return code + "00001";
- int fnum = int.Parse(obj.ToString().Substring(1, 5));
- fnum++;
- return code + fnum.ToString().PadLeft(5, '0');
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
-
- public ParamsComMst CompareParams(Machine mac, ProgramMst programmst, ref string errorinfo)
- {
- try
- {
- //读取机台信息
- //Machine mac = ReadMachine(maccode, ref errorinfo);
- //if (mac == null)
- // return null;
- List<ParamsComDetail> deslist = CompareParamsDetail(mac, programmst.ID, ref errorinfo);
- if (deslist == null)
- return null;
- ParamsComMst mst = new ParamsComMst();
- mst.CurrDetails = deslist;
- List<ParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
- if (nopass.Count > 0)
- {
- mst.IsPass = -1;
- }
- else
- {
- mst.IsPass = 1;
- }
- mst.MacID = mac.ID;
- mst.ProgramID = programmst.ID;
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- //public ParamsComMst CompareParams(string maccode, string programname, ref string errorinfo)
- //{
- // try
- // {
- // string condition = $" and a.fname='{programname}'";
- // List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- // if (msts == null)
- // return null;
- // if(msts.Count<=0)
- // {
- // errorinfo = $"未找到程序名称【{programname}】的记录。";
- // return null;
- // }
- // return CompareParams(maccode,msts[0].ID,ref errorinfo);
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return null;
- // }
- //}
- /// <summary>
- /// 根据机台ID、程序ID,产生参数比较结果
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="programid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public List<ParamsComDetail> CompareParamsDetail(Machine mac,int programid,ref string errorinfo)
- {
- try
- {
- //读取程序参数
- string condition = $" and a.preid={programid} and a.IsCompare=1";
- List<ProgramParamsDetail> programparams = CurrDb.FindListForCondition< ProgramParamsDetail>(condition, ref errorinfo).ToList();
- if (programparams == null)
- return null;
- //if(programparams.Count<=0)
- //读取程序参数子参数
- condition = $" and a.preid in(select id from ProgramParamsDetail where preid={programid} and IsCompare=1)";
- List<PPSubDetail> ppsubdetails = CurrDb.FindListForCondition<PPSubDetail>(condition, ref errorinfo).ToList();
- if (ppsubdetails == null)
- return null;
- List<ParamsComDetail> deslist = MadeParamsComDetail(programparams,ppsubdetails, ref errorinfo);
- if (deslist == null)
- return null;
- if (deslist.Count <= 0)//没有设置比对参数
- return deslist;
- #region 构造参数结构
- //List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == SecType.SVID).OrderBy(t => t.FNum).ToList();
- //List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
- //if (datas == null)
- // return null;
- //OrderDetail order= ReadMachineOrderDetail(mac.ID,1,3, ref errorinfo);
- //if (order == null)
- // return null;
- //HsmsWeb accessmac = new HsmsWeb();
- //OrderBlock rec= accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- ////将返回值填写到参数列表
- //int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- //if (result <= 0)
- // return null;
- //order = ReadMachineOrderDetail(mac.ID, 2, 13, ref errorinfo);
- //if (order == null)
- // return null;
- ////构造参数结构
- //templist = deslist.Where(t => t.SecFType == SecType.ECID).OrderBy(t => t.FNum).ToList();
- //datas = MadeOrderData(templist, ref errorinfo);
- //if (datas == null)
- // return null;
- //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- ////将返回值填写到参数列表
- //result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- //if (result <= 0)
- // return null;
- #endregion
- int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
- if (result <= 0)
- return null;
- result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
- if (result <= 0)
- return null;
- //遍历参数明细,判断每条的比对结果
- foreach (var item in deslist)
- {
- result = CompareParamsComDetail(item);
- }
- return deslist;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- public int CompareParamsDetail(List<ParamsComDetail> deslist,Machine mac, int ftype, ref string errorinfo)
- {
- try
- {
- int sval = 1;
- int fval = 3;
- if(ftype== SecType.ECID)
- {
- sval = 2;
- fval = 13;
- }
- //构造参数结构
- List<ParamsComDetail> templist = deslist.Where(t => t.SecFType == ftype).OrderBy(t => t.FNum).ToList();
- if (templist.Count <= 0)
- return 1;//没有要比较的参数,直接返回
- List<OrderData> datas = MadeOrderData(templist, ref errorinfo);
- if (datas == null)
- return -1;
- OrderDetail order = ReadMachineOrderDetail(mac.ID, sval, fval, ref errorinfo);
- if (order == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- //将返回值填写到参数列表
- int result = UpdateParamsComDetail(templist, rec.Datalists, ref errorinfo);
- if (result <= 0)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private int CompareParamsComDetail(ParamsComDetail entity)
- {
- if (entity.pcsubdetails.Count<=0)
- {
- if (!string.IsNullOrEmpty(entity.CL))
- {
- //说明是比较固定值
- if (entity.CL == entity.FVal)
- {
- entity.IsPass = 1;
- }
- else
- {
- entity.IsPass = -1;
- }
- return 1;
- }
- if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- entity.IsPass = 1;
-
- }
- else
- {
- foreach(var item in entity.pcsubdetails)
- {
- if(item.IsCompare<=0)
- {
- item.IsPass = 1;
- continue;
- }
- if(string.IsNullOrEmpty(item.FVal))
- {
- item.IsPass = -1;
- continue;
- }
- if (!string.IsNullOrEmpty(item.CL))
- {
- //说明是比较固定值
- if (item.CL == item.FVal)
- {
- item.IsPass = 1;
- }
- else
- {
- item.IsPass = -1;
- }
- return 1;
- }
- if (decimal.Parse(item.FVal) < decimal.Parse(item.LCL))
- {
- item.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(item.FVal) > decimal.Parse(item.UCL))
- {
- item.IsPass = -1;
- return 1;
- }
- item.IsPass = 1;
- }
- List<PCSubDetail> tempsub = entity.pcsubdetails.Where(t => t.IsCompare >= 0 && t.IsPass <= 0).ToList();
- if(tempsub.Count>0)
- {
- entity.IsPass = -1;
- }
- else
- {
- entity.IsPass = 1;
- }
- }
- return 1;
- }
- /// <summary>
- /// 根据机台ID读取机台信息
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private Machine ReadMachine(string maccode,ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
- return null;
- }
- return macs[0];
- }
- /// <summary>
- /// 读取机台S1F3指令
- /// </summary>
- /// <param name="macid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private OrderDetail ReadMachineOrderDetail(int macid,int sval,int fval, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.macid={macid}";
- List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
- return null;
- }
- condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval}";
- List<OrderDetail> details= CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (details == null)
- return null;
- if(details.Count<=0)
- {
- errorinfo = "未找到您要的指令。";
- return null;
- }
- return details[0];
- }
- private OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval,string fname, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.macid={macid}";
- List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
- return null;
- }
- condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
- List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (details == null)
- return null;
- if (details.Count <= 0)
- {
- errorinfo = "未找到您要的指令。";
- return null;
- }
- return details[0];
- }
- /// <summary>
- /// 产生指令数据
- /// </summary>
- /// <param name="programparams"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> MadeOrderData(List<ParamsComDetail> programparams, ref string errorinfo)
- {
- List<OrderData> datas = new List<OrderData>();
- OrderData preid = new OrderData();
- preid.ID = 1;
- preid.ParentID = 0;
- preid.PreID = 0;
- preid.FCode = "L";
- preid.FLen = programparams.Count;
- datas.Add(preid);
- OrderData tempentity = null;
- int id = 2;
- int fnum = 10;
- foreach(var item in programparams)
- {
- tempentity = new OrderData();
- tempentity.ID = id;
- tempentity.FNum = fnum;
- tempentity.FCode = item.DCode;
- tempentity.FContent = item.FVal;
- tempentity.ParentID = preid.ID;
- datas.Add(tempentity);
- id++;
- fnum += 10;
- }
- return datas;
- }
- /// <summary>
- /// 将程序参数列表转换成比对明细列表
- /// </summary>
- /// <param name="programparams"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<ParamsComDetail> MadeParamsComDetail(List<ProgramParamsDetail> programparams,List<PPSubDetail> subdetails, ref string errorinfo)
- {
- List<ParamsComDetail> datas = new List<ParamsComDetail>();
- ParamsComDetail tempentity = null;
- List<PCSubDetail> pcsubdetails = null;
- List<PPSubDetail> ppsubdetails = null;
- PCSubDetail subentity = null;
- foreach (var item in programparams)
- {
- tempentity = new ParamsComDetail();
- tempentity.ID = 0;
- tempentity.FNum = item.FNum.Value;
- tempentity.SecID = item.SecID;
- tempentity.DCode = item.DCode;
- tempentity.FVal = item.FVal;
- tempentity.UCL = item.UCL;
- tempentity.CL = item.CL;
- tempentity.LCL = item.LCL;
- tempentity.SVType = item.SVType.Value;
- tempentity.Module = item.Module.Value;
- tempentity.CTLType = item.CTLType.Value;
- tempentity.SecFType = item.SecFType;
- datas.Add(tempentity);
- pcsubdetails = new List<PCSubDetail>();
- tempentity.pcsubdetails = pcsubdetails;
- ppsubdetails = subdetails.Where(t => t.PreID == item.ID).ToList();
- foreach(var subitem in ppsubdetails)
- {
- subentity = new PCSubDetail();
- subentity.ID = 0;
- subentity.FNum = subitem.FNum;
- subentity.FName = subitem.ParameFName;
- subentity.UCL = subitem.UCL;
- subentity.CL = subitem.CL;
- subentity.LCL = subitem.LCL;
- subentity.IsCompare = subitem.IsCompare;
- pcsubdetails.Add(subentity);
- }
- }
- return datas;
- }
- /// <summary>
- /// 根据返回的参数列表值,更新比对结果值
- /// </summary>
- /// <param name="deslist"></param>
- /// <param name="orglist"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private int UpdateParamsComDetail(List<ParamsComDetail> deslist,List<OrderData> orglist, ref string errorinfo)
- {
- List<OrderData> ldata = orglist.Where(t => t.ParentID == 0).ToList();
- int preid = ldata[0].ID;
- ldata = orglist.Where(t => t.ParentID == preid).OrderBy(t=>t.FNum).ToList();
- if(deslist.Count!=ldata.Count)
- {
- errorinfo = "获取到的参数与实际参数个数不一致。";
- return -1;
- }
- List<PCSubDetail> pcsubdetails = null;
- List<OrderData> subdata = null;
- for(int i=0;i<deslist.Count;i++)
- {
- deslist[i].FVal = ldata[i].FContent;
- //更新子列表值
- pcsubdetails = deslist[i].pcsubdetails;
- subdata = orglist.Where(t => t.ParentID == ldata[i].ID).ToList();
- for(int j=0;j<pcsubdetails.Count;j++)
- {
- pcsubdetails[j].FVal = subdata.Count > j ? subdata[j].FContent : "";
- }
- }
- return 1;
- }
- /// <summary>
- /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="partcode"></param>
- /// <param name="pcode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst FindProgram(string maccode, string partcode, string pcode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return null;
- string condition = $" and b.ModelID={mac.MModeID} and b.ProcessCode='{pcode}'";
- List<ProgramRule> rules = CurrDb.FindListForCondition<ProgramRule>(condition, ref errorinfo).ToList();
- if (rules == null)
- return null;
- if (rules.Count<=0)
- {
- errorinfo = "未找到对应的程序。";
- return null;
- }
- //先精确匹配产品编号
- List<ProgramRule> templist = rules.Where(t => t.PartCode == partcode).ToList();
- if (templist.Count > 0)
- return CurrDb.FindEntityFor<ProgramMst>(templist[0].ProgramMstID);
- //取模糊匹配的规则
- rules = rules.Where(t => t.PartCode.Contains("%")).ToList();
- if(rules.Count<=0)
- {
- errorinfo = "未找到对应的程序。";
- return null;
- }
- //去掉规则里产品编号的“%”符号
- foreach(var item in rules)
- {
- item.PartCode = item.PartCode.Replace("%", "");
- }
- //排序规则列表,按产品编号长度从大到小排列
- rules = rules.OrderByDescending(t => t.PartCode.Length).ToList();
- foreach(var item in rules)
- {
- if (partcode.Contains(item.PartCode))
- {
- return CurrDb.FindEntityFor<ProgramMst>(item.ProgramMstID);
- }
- }
- errorinfo = "未找到对应的程序。";
- return null;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// DP机台装载程序指令数据
- /// </summary>
- /// <param name="programname">要装载的程序名称</param>
- /// <param name="preid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS2F41Data(string programname, int preid, ref string errorinfo)
- {
- string rcmd = "PP-SELECT";
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = rcmd;
- entity.FLen = rcmd.Length;
- ldata.Add(entity);
- entity = new OrderData();//添加L节点
- entity.ID = 3;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "L";
- entity.FLen = 1;
- ldata.Add(entity);
- entity = new OrderData();//添加L节点
- entity.ID = 4;
- entity.ParentID = 3;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "L";
- entity.FLen = 2;
- ldata.Add(entity);
- string cpname = "PPID";
- entity = new OrderData();
- entity.ID = 5;
- entity.ParentID = 4;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = cpname;
- entity.FLen = cpname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 6;
- entity.ParentID = 4;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 构造S7F3数据
- /// </summary>
- /// <param name="programname"></param>
- /// <param name="filestr"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS7F3Data(string programname,int filelen, string filepath,int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "B";
- entity.FContent = filepath;
- entity.OrgDatas = null;
- entity.FLen = filelen;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 构造S7F1数据
- /// </summary>
- /// <param name="programname"></param>
- /// <param name="filestr"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS7F1Data(string programname,int len, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "U4";
- entity.FLen = 1;
- entity.FContent = len.ToString();
- //entity.OrgDatas = filedatas;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="partcode"></param>
- /// <param name="pcode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst DownloadProgram(string maccode, string partcode, string pcode, int filelen,string filepath, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return null;
- //查找程序文件
- ProgramMst mst = FindProgram(maccode, partcode, pcode, ref errorinfo);
- if(mst==null)
- {
- return null;
- }
- //OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
- //if (order == null)
- // return null;
- ////string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
- ////filedir += "\\" + maccode;
- ////string filepath = filedir + "\\" + mst.FName;
- ////byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
- ////if (filedatas == null)
- ////{
- //// return null;
- ////}
- //string condition = $" and a.preid={order.ID}";
- //List<OrderData> datas = GetS7F1Data(mst.FName, filelen, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- //if (datas == null)
- // return null;
- //HsmsWeb accessmac = new HsmsWeb();
- //OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- //if(int.Parse(rec.Datalists[0].FContent)!=0)
- //{
- // errorinfo = "机台不接受程序。";
- // return null;
- //}
- //order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
- //datas = GetS7F3Data(mst.FName, filelen, filepath, order.PreID, ref errorinfo);
- //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- //if (int.Parse(rec.Datalists[0].FContent) != 0)
- //{
- // errorinfo = "机台没有正确接受程序。";
- // return null;
- //}
- ////发送程序装载命令,暂时还不能执行
- //order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- //datas = GetS2F41Data(mst.FName, order.PreID, ref errorinfo);
- //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return null;
- //if (int.Parse(rec.Datalists[1].FContent) != 0)
- //{
- // errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- // return null;
- //}
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- private void SendInfo(string maccode, string info)
- {
- try
- {
- MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
- string temperrorinfo = "";
- senddal.SendS10F3(maccode, info, ref temperrorinfo);
- }
- catch
- {
- return;
- }
- }
- private List<OrderData> GetS7F3Data(string programname, byte[] filedatas, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = programname;
- entity.FLen = programname.Length;
- entity.Remark = "";
- ldata.Add(entity);
- entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "B";
- entity.FContent = "";
- entity.OrgDatas = filedatas;
- entity.FLen = filedatas.Length;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- public int DownloadProgram(Machine mac, string programname, byte[] filedatas, ref string errorinfo)
- {
- programname = programname + "";
- try
- {
- //要修改程序名称
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
- if (order == null)
- return -1;
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = GetS7F1Data(programname, filedatas.Length, order.ID, ref errorinfo);// CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- int result = int.Parse(rec.Datalists[0].FContent);
- if (result > 1)
- {
- //errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
- //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
- errorinfo = rec.Datalists[0].FContent;
- return -100;
- }
- if (result == 0)
- {
- //产生文件
- order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
- datas = GetS7F3Data(programname, filedatas, order.PreID, ref errorinfo);
- rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[0].FContent) != 0)
- {
- //errorinfo = "机台没有正确接受程序。";
- //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]");
- errorinfo = rec.Datalists[0].FContent;
- return -200;
- }
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int DownloadProgram(Machine mac, string orgprogramname, ref string errorinfo)
- {
- try
- {
- orgprogramname = orgprogramname.Trim();
- #region
- #endregion
- string programname = orgprogramname;
- int result = 0;
- if (mac.CallPMode <= 2)
- {
- //说明要下载程序
- MacProgramDal macprogramdal = new MacProgramDal(CurrDb);
- int orgtypeid = 1;
- string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
- if (mac.CallPMode == 1)
- {
- orgtypeid = 1;
- }
- else
- {
- orgtypeid = 2;
- }
- byte[] filedatas = macprogramdal.ReadMacProgram(mac, orgprogramname, orgtypeid, programdir, ref errorinfo);
- if (filedatas == null)
- return -30;
- result = DownloadProgram(mac, programname, filedatas, ref errorinfo);
- if (result <= 0)
- return result;
- }
- //提示下载程序成功
- //MacOrderSendDal senddal = new MacOrderSendDal(CurrDb);
- //string temperrorinfo = "";
- //senddal.SendS10F3(mac.FCode, $"Recipe download success [{orgprogramname}]", ref temperrorinfo);
- //result = SelProgram(mac, programname, ref errorinfo);
- //if (result <= 0)
- // return -10;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- /// <summary>
- /// 根据程序名称下载程序
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="programname"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int SelProgram(Machine mac, string programname,ref string errorinfo)
- {
- try
- {
- //发送程序装载命令,暂时还不能执行
- HsmsWeb accessmac = new HsmsWeb();
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41select", ref errorinfo);
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- //< L[2]
- // < A[9] PP_SELECT >
- // < L[2]
- // < L[2]
- // < A[4] PPID >
- // < A[13] 4521D - SB - 13_1 >
- // >
-
- // < L[2]
- // < A[6] PROMPT >
-
- // < BOOLEAN[1] 0 >
-
- // >
-
- // >
- // >
- //List<OrderData> datas = GetS2F41Data(programname, order.PreID, ref errorinfo);
- List <OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t=>t.FNum).ToList();//第一个L下的节点集合
- templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
- templist[1].FContent = programname;
- templist[1].FLen = programname.Length;
- //string aa = JsonConvert.SerializeObject(datas);
- //return 1;//20201225
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- errorinfo = rec.Datalists[1].FContent;
- return -300;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SelProgramSineTest(Machine mac, string programname,string lotID, ref string errorinfo)
- {
- try
- {
- string prigramDir = $"O:\\{programname}\\Debug\\{programname}.dll";
- //发送程序装载命令,暂时还不能执行
- HsmsWeb accessmac = new HsmsWeb();
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT_LOTID", ref errorinfo);
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
- //int pid = templist[1].ID;//第二个L的ID
- templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
- List<OrderData> templiststr = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第四个L下的节点集合
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
-
- templist[1].FContent = prigramDir;
- templist[1].FLen = prigramDir.Length;
- templiststr[1].FContent = lotID;
- templiststr[1].FLen = lotID.Length;
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- errorinfo = rec.Datalists[1].FContent;
- return -300;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SelProgramQuickMark(Machine mac, string programname, ref string errorinfo)
- {
- try
- {
- //发送程序装载命令,暂时还不能执行
- HsmsWeb accessmac = new HsmsWeb();
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT", ref errorinfo);
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
- templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第三个L下的节点集合
- templist[1].FContent = programname;
- templist[1].FLen = programname.Length;
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- errorinfo = rec.Datalists[1].FContent;
- return -300;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SelProgramQTTest(Machine mac, string programname, string lotID,string userID,string stationID, ref string errorinfo)
- {
- try
- {
-
- //发送程序装载命令,暂时还不能执行
- HsmsWeb accessmac = new HsmsWeb();
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41_PP_SELECT_QT", ref errorinfo);
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- List<OrderData> templist = datas.Where(t => t.ParentID == 0).ToList();//第一个L
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第一个L下的节点集合
- //int pid = templist[1].ID;//第二个L的ID
- templist = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的节点集合
- List<OrderData> templiststr = datas.Where(t => t.ParentID == templist[1].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第2个L的节点集合
- templiststr[1].FContent = lotID;
- templiststr[1].FLen = lotID.Length;
- //templiststr[1].FContent = programname;
- //templiststr[1].FLen = programname.Length;
- templiststr = datas.Where(t => t.ParentID == templist[2].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第3个L的节点集合
- templiststr[1].FContent = userID;
- templiststr[1].FLen = userID.Length;
- templiststr = datas.Where(t => t.ParentID == templist[3].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第4个L的节点集合
- templiststr[1].FContent = stationID;
- templiststr[1].FLen = stationID.Length;
- templist = datas.Where(t => t.ParentID == templist[0].ID).OrderBy(t => t.FNum).ToList();//第二个L下的第1个L的节点集合
- templist[1].FContent = programname;
- templist[1].FLen = programname.Length;
- //templist[1].FContent = lotID;
- //templist[1].FLen = lotID.Length;
-
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"[{mac.FCode}]装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- errorinfo = rec.Datalists[1].FContent;
- return -300;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- /// <summary>
- /// 构造获取机台程序名称的数据
- /// </summary>
- /// <param name="mmsec"></param>
- /// <param name="preid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetMacProgram(MMSecDetail mmsec, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 1;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = mmsec.DCode;
- entity.FContent = mmsec.FVal;
- entity.FLen = mmsec.FVal.Length;
- ldata.Add(entity);
- return ldata;
- }
- /// <summary>
- /// 根据机台编号,读取机台当前正在使用的程序名称
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public string GetMacProgram(string maccode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return "";
-
- OrderDetail order = ReadMachineOrderDetail(mac.ID,1, 3, ref errorinfo);
- if (order == null)
- return "";
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs == null)
- return "";
- if(mmsecs.Count<=0)
- {
- errorinfo = "未找到此机台对应的程序参数说明。";
- return "";
- }
- List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
- if (datas == null)
- return "";
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return "";
- return rec.Datalists[1].FContent;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- /// <summary>
- /// 根据机台查找机台使用的程序
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public ProgramMst GetMacProgram(Machine mac, ref string errorinfo)
- {
- try
- {
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, ref errorinfo);
- if (order == null)
- return null;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={mac.MModeID} and b.FCode='{StandardCode.SVID_ProgramName}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs == null)
- return null;
- if (mmsecs.Count <= 0)
- {
- errorinfo = "未找到此机台对应的程序参数说明。";
- return null;
- }
- List<OrderData> datas = GetMacProgram(mmsecs[0], order.ID, ref errorinfo);
- if (datas == null)
- return null;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- string programname= rec.Datalists[1].FContent;
- condition = $" and a.fname='{programname}'";
- List<ProgramMst> msts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
- if (msts == null)
- return null;
- if (msts.Count <= 0)
- {
- errorinfo = $"未找到程序名称【{programname}】的记录。";
- return null;
- }
- return msts[0];
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 发送停机指令
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int SendStopMac(string maccode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return -1;
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41,"s2f41stop", ref errorinfo);
- if (order == null)
- return -1;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- //休眠5秒,等待机台状态恢复
- Thread.Sleep(5000);
- order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
- if (order == null)
- return -1;
- //从机型参数中读取程序参数信息
- condition = $" and a.preid={order.ID}";
- datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- #region Web接口使用
- public IEnumerable<ParamsComMst> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var sql = new ParamsComMst().GetSelectSql();
- var pros = CurrDb.FindListForCondition<ParamsComMst>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
- // 加入参数信息
- if (pros != null && pros.Count() > 0)
- {
- for (var i = 0; i < pros.Count(); i++)
- {
- var detail = CurrDb.FindListForCondition<ParamsComDetail>($" and a.PreID='{pros[i].ID}'", ref errorinfo).ToList();
- // var detail = CurrDb.FindList<ParamsComDetail>(sql).ToList();
- if (detail == null)
- {
- detail = new List<ParamsComDetail>();
- }
- pros[i].CurrDetails = detail;
- }
- }
- return pros;
- }
- public int GetCount(string filter)
- {
- string errorinfo = string.Empty;
- var entities = CurrDb.FindListForCondition<ParamsComMst>(filter, ref errorinfo);
- if (entities != null)
- {
- return entities.Count();
- }
- return 0;
- }
- #endregion
- private List<FileParamsComDetail> MadeFileParamsComDetail(List<ProgramFileParams> programparams, ref string errorinfo)
- {
- List<FileParamsComDetail> datas = new List<FileParamsComDetail>();
- FileParamsComDetail tempentity = null;
- foreach (var item in programparams)
- {
- tempentity = new FileParamsComDetail();
- tempentity.ID = 0;
- tempentity.FName = item.FName;
- tempentity.FNum = item.FNum;
- tempentity.FVal = "";
- tempentity.UCL = item.UCL;
- tempentity.CL = item.CL;
- tempentity.LCL = item.LCL;
- datas.Add(tempentity);
- }
- return datas;
- }
- private int CompareFileParamsComDetail(FileParamsComDetail entity)
- {
- if (!string.IsNullOrEmpty(entity.CL))
- {
- //说明是比较固定值
- if (entity.CL == entity.FVal)
- {
- entity.IsPass = 1;
- }
- else
- {
- entity.IsPass = -1;
- }
- return 1;
- }
- if(string.IsNullOrEmpty(entity.FVal))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) < decimal.Parse(entity.LCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- if (decimal.Parse(entity.FVal) > decimal.Parse(entity.UCL))
- {
- entity.IsPass = -1;
- return 1;
- }
- entity.IsPass = 1;
- return 1;
- }
- public List<FileParamsComDetail> CompareFileParamsDetail(IDatabase db,Machine mac, ProgramMst program,MacProgram macprogram, ref string errorinfo)
- {
- try
- {
- //读取程序参数
- string condition = $" and a.preid={program.ID} and a.IsCompare=1";
- List<ProgramFileParams> programparams = CurrDb.FindListForCondition<ProgramFileParams>(condition, ref errorinfo).ToList();
- if (programparams == null)
- return null;
- List<FileParamsComDetail> deslist = MadeFileParamsComDetail(programparams, ref errorinfo);
- if (deslist == null)
- return null;
- if (deslist.Count <= 0)//没有设置比对参数
- return deslist;
- //int result = CompareParamsDetail(deslist, mac, SecType.SVID, ref errorinfo);
- //if (result <= 0)
- // return null;
- //result = CompareParamsDetail(deslist, mac, SecType.ECID, ref errorinfo);
- //if (result <= 0)
- // return null;
- //读取机台程序文件,并解析出参数值
- string rarpath = AppConfigurtaionServices.Configuration["rarpath"];
- string desdir = AppConfigurtaionServices.Configuration["DesDir"];
- string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
- Dad3350ProgramDal knsprogram = new Dad3350ProgramDal(db);
- List<FileParams> fileparams = knsprogram.GetProgramParams(rarpath, mac, program, desdir,programdir,macprogram, ref errorinfo);
- if (fileparams == null)
- return null;
- //遍历参数明细,判断每条的比对结果
- foreach (var item in deslist)
- {
- List<FileParams> tempfileparams = fileparams.Where(t => t.FName == item.FName).ToList();
- if (tempfileparams.Count > 0)
- item.FVal = tempfileparams[0].FVal;
- int result = CompareFileParamsComDetail(item);
- }
- return deslist;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 文件参数比对
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="programmst"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- //public FileParamsComMst CompareFileParams(IDatabase db,Machine mac, ProgramMst programmst, ref string errorinfo)
- //{
- // try
- // {
- // //读取机台信息
- // //Machine mac = ReadMachine(maccode, ref errorinfo);
- // //if (mac == null)
- // // return null;
- // List<FileParamsComDetail> deslist = CompareFileParamsDetail(db,mac, programmst, ref errorinfo);
- // if (deslist == null)
- // return null;
- // FileParamsComMst mst = new FileParamsComMst();
- // mst.CurrDetails = deslist;
- // List<FileParamsComDetail> nopass = deslist.Where(t => t.IsPass <= 0).ToList();
- // if (nopass.Count > 0)
- // {
- // mst.IsPass = -1;
- // }
- // else
- // {
- // mst.IsPass = 1;
- // }
- // mst.MacID = mac.ID;
- // mst.ProgramID = programmst.ID;
- // return mst;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return null;
- // }
- //}
- public FileParamsComMst IUFileParamsComMst(FileParamsComMst mst, string usercode, ref string errorinfo)
- {
- try
- {
- int result = CurrDb.InsertFor(mst, usercode);
- if (result < 0)
- {
- return null;
- }
- object objid = CurrDb.FindObject("select @@IDENTITY");
- if (objid.ToString() == "")
- {
- return null;
- }
- int id = int.Parse(objid.ToString());
- foreach (var item in mst.CurrDetails)
- {
- item.PreID = id;
- item.EntityStatusID = 1;
- item.ID = 0;
- }
- result = CurrDb.InsertFor<FileParamsComDetail>(mst.CurrDetails, usercode);
- if (result < 0)
- return null;
- mst = CurrDb.FindEntityFor<FileParamsComMst>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- private List<OrderData> GetS2F41DataForHit(int preid, string rcmd, ref string errorinfo)
- {
- //string rcmd = "ENTER_HIT";
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "L";
- lentity.FLen = 2;
- ldata.Add(lentity);
- OrderData entity = new OrderData();
- entity.ID = 2;
- entity.ParentID = 1;
- entity.FNum = 10;
- entity.PreID = preid;
- entity.FCode = "A";
- entity.FContent = rcmd;
- entity.FLen = rcmd.Length;
- ldata.Add(entity);
- entity = new OrderData();//添加L节点
- entity.ID = 3;
- entity.ParentID = 1;
- entity.FNum = 20;
- entity.PreID = preid;
- entity.FCode = "L";
- entity.FLen = 0;
- ldata.Add(entity);
- return ldata;
- }
- private int SetHit(Machine mac, string rcmd, ref string errorinfo)
- {
- try
- {
- HsmsWeb accessmac = new HsmsWeb();
- //发送指令,将程序模式设置为hit模式
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- List<OrderData> datas = GetS2F41DataForHit(order.PreID, rcmd, ref errorinfo);
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- int result = int.Parse(rec.Datalists[1].FContent);
- if (result == 5)
- return 1;
- if (result == 0)
- return 1;
- return -1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private List<OrderData> GetS7F5Data(string programname, int preid, ref string errorinfo)
- {
- List<OrderData> ldata = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = preid;
- lentity.FCode = "A";
- lentity.FContent = programname;
- lentity.FLen = programname.Length;
- ldata.Add(lentity);
- return ldata;
- }
- public MacProgram SaveProgram(string maccode, string usercode, ref string errorinfo)
- {
- try
- {
- //读取机台信息
- string condition = $" and a.FCode='{maccode}'";
- Machine mac = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList()[0];
- //string programname = (string)imputds["programname"];
- ParamsComMstDal comdal = new ParamsComMstDal(CurrDb);
- string programname = comdal.GetMacProgramName(mac, ref errorinfo);
- if (string.IsNullOrEmpty(programname))
- {
- errorinfo = $"未找到机台【{maccode}】对应的程序。{errorinfo}";
- return null;
- }
- //从机台上读取文件并保存
- string programdir = AppConfigurtaionServices.Configuration["ProgramDir"];
- MacProgram macprogram = SaveProgramFromMac(mac, programname, programdir, ref errorinfo);
- if (macprogram == null)
- {
- return null;
- }
- return macprogram;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- CurrDb.Rollback();
- return null;
- }
- }
- public MacProgram SaveProgramFromMac(Machine mac, string programname, string desdir, ref string errorinfo)
- {
- try
- {
- HsmsWeb accessmac = new HsmsWeb();
- //int result = SetHit(mac, "ENTER_HIT", ref errorinfo);
- //if (result <= 0)
- //{
- // return null;
- //}
- //发送取程序的指令
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- List<OrderData> datas = GetS2F41Data(programname, order.PreID, ref errorinfo);
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- int result = int.Parse(rec.Datalists[1].FContent);
- if (result != 0 && result != 4)
- {
- errorinfo = $"下载程序不成功,错误代码为:{rec.Datalists[1].FContent}。";
- return null;
- }
- if (result == 4)
- {
- //说明机台已经开始处理程序,休眠5秒钟,等待机台处理
- //或等待处理完成事件上来
- Thread.Sleep(1000 * 5);
- }
- order = ReadMachineOrderDetail(mac.ID, 7, 5, ref errorinfo);
- datas = GetS7F5Data(programname, order.PreID, ref errorinfo);
- rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- datas = rec.Datalists;
- if (datas[0].FLen == 0)
- {
- errorinfo = "未能正确的获取程序文件。";
- return null;
- }
- //result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo);
- //if (result <= 0)
- // return null;
- MacProgramDal dal = new MacProgramDal(CurrDb);
- MacProgram macprogram = dal.IMacProgram(mac, programname, desdir, datas[2].OrgDatas, ref errorinfo);
- return macprogram;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- }
- }
|