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 { /// /// 参数比对数据访问层 /// 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 pcsubdetails = null; foreach(var item in mst.CurrDetails) { result = CurrDb.InsertFor(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(pcsubdetails, usercode); if (result < 0) return null; } mst = CurrDb.FindEntityFor(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 deslist = CompareParamsDetail(mac, programmst.ID, ref errorinfo); if (deslist == null) return null; ParamsComMst mst = new ParamsComMst(); mst.CurrDetails = deslist; List 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 msts = CurrDb.FindListForCondition(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; // } //} /// /// 根据机台ID、程序ID,产生参数比较结果 /// /// /// /// /// public List CompareParamsDetail(Machine mac,int programid,ref string errorinfo) { try { //读取程序参数 string condition = $" and a.preid={programid} and a.IsCompare=1"; List 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 ppsubdetails = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (ppsubdetails == null) return null; List deslist = MadeParamsComDetail(programparams,ppsubdetails, ref errorinfo); if (deslist == null) return null; if (deslist.Count <= 0)//没有设置比对参数 return deslist; #region 构造参数结构 //List templist = deslist.Where(t => t.SecFType == SecType.SVID).OrderBy(t => t.FNum).ToList(); //List 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 deslist,Machine mac, int ftype, ref string errorinfo) { try { int sval = 1; int fval = 3; if(ftype== SecType.ECID) { sval = 2; fval = 13; } //构造参数结构 List templist = deslist.Where(t => t.SecFType == ftype).OrderBy(t => t.FNum).ToList(); if (templist.Count <= 0) return 1;//没有要比较的参数,直接返回 List 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 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; } /// /// 根据机台ID读取机台信息 /// /// /// /// private Machine ReadMachine(string maccode,ref string errorinfo) { //读取机台信息 string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs == null) return null; if (macs.Count <= 0) { errorinfo = $"未找到机台编号【{maccode}】的机台信息。"; return null; } return macs[0]; } /// /// 读取机台S1F3指令 /// /// /// /// private OrderDetail ReadMachineOrderDetail(int macid,int sval,int fval, ref string errorinfo) { //读取机台信息 string condition = $" and a.macid={macid}"; List macs = CurrDb.FindListForCondition(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 details= CurrDb.FindListForCondition(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 macs = CurrDb.FindListForCondition(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 details = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (details == null) return null; if (details.Count <= 0) { errorinfo = "未找到您要的指令。"; return null; } return details[0]; } /// /// 产生指令数据 /// /// /// /// private List MadeOrderData(List programparams, ref string errorinfo) { List datas = new List(); 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; } /// /// 将程序参数列表转换成比对明细列表 /// /// /// /// private List MadeParamsComDetail(List programparams,List subdetails, ref string errorinfo) { List datas = new List(); ParamsComDetail tempentity = null; List pcsubdetails = null; List 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(); 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; } /// /// 根据返回的参数列表值,更新比对结果值 /// /// /// /// /// private int UpdateParamsComDetail(List deslist,List orglist, ref string errorinfo) { List 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 pcsubdetails = null; List subdata = null; for(int i=0;i t.ParentID == ldata[i].ID).ToList(); for(int j=0;j j ? subdata[j].FContent : ""; } } return 1; } /// /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID /// /// /// /// /// /// 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 rules = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (rules == null) return null; if (rules.Count<=0) { errorinfo = "未找到对应的程序。"; return null; } //先精确匹配产品编号 List templist = rules.Where(t => t.PartCode == partcode).ToList(); if (templist.Count > 0) return CurrDb.FindEntityFor(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(item.ProgramMstID); } } errorinfo = "未找到对应的程序。"; return null; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } /// /// DP机台装载程序指令数据 /// /// 要装载的程序名称 /// /// /// private List GetS2F41Data(string programname, int preid, ref string errorinfo) { string rcmd = "PP-SELECT"; List ldata = new List(); 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; } /// /// 构造S7F3数据 /// /// /// /// /// private List GetS7F3Data(string programname,int filelen, string filepath,int preid, ref string errorinfo) { List ldata = new List(); 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; } /// /// 构造S7F1数据 /// /// /// /// /// private List GetS7F1Data(string programname,int len, int preid, ref string errorinfo) { List ldata = new List(); 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; } /// /// 根据机台编号、产品编号、制程代码等信息,下发程序到机台 /// /// /// /// /// /// 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 datas = GetS7F1Data(mst.FName, filelen, order.ID, ref errorinfo);// CurrDb.FindListForCondition(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 GetS7F3Data(string programname, byte[] filedatas, int preid, ref string errorinfo) { List ldata = new List(); 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 datas = GetS7F1Data(programname, filedatas.Length, order.ID, ref errorinfo);// CurrDb.FindListForCondition(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; } } /// /// 根据程序名称下载程序 /// /// /// /// /// 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 datas = CurrDb.FindListForCondition(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 datas = GetS2F41Data(programname, order.PreID, ref errorinfo); List 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 datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; List 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 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 datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; List 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 datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; List 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 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; } } /// /// 构造获取机台程序名称的数据 /// /// /// /// /// private List GetMacProgram(MMSecDetail mmsec, int preid, ref string errorinfo) { List ldata = new List(); 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; } /// /// 根据机台编号,读取机台当前正在使用的程序名称 /// /// /// /// 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 mmsecs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (mmsecs == null) return ""; if(mmsecs.Count<=0) { errorinfo = "未找到此机台对应的程序参数说明。"; return ""; } List 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 ""; } } /// /// 根据机台查找机台使用的程序 /// /// /// /// 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 mmsecs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (mmsecs == null) return null; if (mmsecs.Count <= 0) { errorinfo = "未找到此机台对应的程序参数说明。"; return null; } List 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 msts = CurrDb.FindListForCondition(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; } } /// /// 发送停机指令 /// /// /// /// 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 datas = CurrDb.FindListForCondition(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(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 Get(int start, int length, string order, string sort, string filter, string errorinfo) { var sql = new ParamsComMst().GetSelectSql(); var pros = CurrDb.FindListForCondition($" {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($" and a.PreID='{pros[i].ID}'", ref errorinfo).ToList(); // var detail = CurrDb.FindList(sql).ToList(); if (detail == null) { detail = new List(); } pros[i].CurrDetails = detail; } } return pros; } public int GetCount(string filter) { string errorinfo = string.Empty; var entities = CurrDb.FindListForCondition(filter, ref errorinfo); if (entities != null) { return entities.Count(); } return 0; } #endregion private List MadeFileParamsComDetail(List programparams, ref string errorinfo) { List datas = new List(); 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 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 programparams = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (programparams == null) return null; List 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 = knsprogram.GetProgramParams(rarpath, mac, program, desdir,programdir,macprogram, ref errorinfo); if (fileparams == null) return null; //遍历参数明细,判断每条的比对结果 foreach (var item in deslist) { List 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; } } /// /// 文件参数比对 /// /// /// /// /// //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 deslist = CompareFileParamsDetail(db,mac, programmst, ref errorinfo); // if (deslist == null) // return null; // FileParamsComMst mst = new FileParamsComMst(); // mst.CurrDetails = deslist; // List 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(mst.CurrDetails, usercode); if (result < 0) return null; mst = CurrDb.FindEntityFor(id); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } private List GetS2F41DataForHit(int preid, string rcmd, ref string errorinfo) { //string rcmd = "ENTER_HIT"; List ldata = new List(); 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 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 GetS7F5Data(string programname, int preid, ref string errorinfo) { List ldata = new List(); 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(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 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; } } } }