using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using DllEapEntity.Rms; using DllHsms; using DllHsmsWeb; using DllSocketFile; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using ZipFileHelper; namespace DllEapDal { public class Dad3350ProgramDal { private IDatabase CurrDb = null; private ILogger loger = null; public Dad3350ProgramDal(IDatabase db) { CurrDb = db; } public Dad3350ProgramDal(IDatabase db, ILogger loger) { CurrDb = db; this.loger = loger; } 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 List GetS2F41Data(string programname, int preid, ref string errorinfo) { string rcmd = "PP_PREP"; 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; } /// /// DP机台装载程序指令数据 /// /// 要装载的程序名称 /// /// /// private List GetS2F41DataForPPSelect(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; } 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 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; } /// /// 将机台设置为1086参数 /// /// /// /// private int SetEc1086(Machine mac, ref string errorinfo) { try { HsmsWeb accessmac = new HsmsWeb(); //发送指令,将程序模式设置为hit模式 OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo); List datas = GetS2F41DataForHit(order.PreID, "ENTER_HIT", 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 > 0) { errorinfo = $"将机台设置为HIT模式出错,错误代码为:{result}"; return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } /// /// 发送指令 /// /// /// /// /// /// 将机台设置为HIT模式 /// /// /// /// 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; errorinfo = $"设置ENTER_HIT模式失败,返回代码为{rec.Datalists[1].FContent}"; return -1; } catch(Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } /// /// 获取机台当前使用的程序文件,并将文件放到指定的目录 2019-09-04修改,使用下面的函数 /// /// /// /// //public string ReadProgramFromMac(Machine mac,ProgramMst program,string desdir,ref string errorinfo) //{ // try // { // HsmsWeb accessmac = new HsmsWeb(); // //发送指令,将程序模式设置为hit模式 // //OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo); // //List datas = GetS2F41DataForHit(program.FName, 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) // //{ // // errorinfo = $"将机台设置为HIT模式出错,错误代码为:{result}"; // // return null; // //} // 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(program.FName, order.PreID, ref errorinfo); // OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); // if (rec == null) // return null; // 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(program.FName, 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 ""; // } // result = UnityHelper.WriteFile(desdir, program.FName, datas[2].OrgDatas, ref errorinfo); // if (result <= 0) // return null; // return program.FName; // } // catch(Exception ex) // { // errorinfo = ex.Message.ToString(); // return ""; // } //} 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; 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; } } /// /// 获取程序参数 /// /// /// /// /// /// public List GetProgramParams(string rarpath,Machine mac, ProgramMst program, string desdir ,string programdir,MacProgram macprogram, ref string errorinfo) { try { // string programname = ReadProgramFromMac(db,mac, program, desdir, ref errorinfo); // if (string.IsNullOrEmpty(programname)) // return null; // string filepath = desdir + "\\" + programname; // string tempdesdir = desdir + "\\temp"; // List filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, programname //, tempdesdir, mac.MModeID, ref errorinfo); // return filesdata; string filepath = $"{programdir}\\{mac.FCode}\\{macprogram.ProgramName}\\{macprogram.ProgramName}_{macprogram.Version}"; string tempdesdir = desdir + "\\temp"; List filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, macprogram.ProgramName , tempdesdir, mac.MModeID, ref errorinfo); return filesdata; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } /// /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID /// /// /// /// /// /// /// /// 构造S7F3数据 /// /// /// /// /// 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; } /// /// 构造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 int DownloadProgram(Machine mac,string programname, byte[] filedatas, ref string errorinfo) { 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}]"); 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) ==2) { return 1; } if (int.Parse(rec.Datalists[0].FContent) != 0) { errorinfo = rec.Datalists[0].FContent; //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]"); return -200; } } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } private void SendInfo(string maccode, string info) { try { MacOrderSendDal senddal = new MacOrderSendDal(CurrDb); string temperrorinfo = ""; senddal.SendS10F3(maccode, info, ref temperrorinfo); } catch { return; } } public int SelectProgram(Machine mac, string programname, ref string errorinfo) { try { MacOrderSendDal dal = new MacOrderSendDal(CurrDb); loger.LogError($"开始读取S2F41SelProgram"); OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 2, 41, "S2F41SelProgram", ref errorinfo); if (order == null) { errorinfo = $"读取S2F41SelProgram指令出错。{errorinfo}"; return -1; } loger.LogError($"读取S2F41SelProgram完成,开始读取指令数据"); string condition = $" and a.preid={order.ID}"; List datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) { errorinfo = $"读取S2F41SelProgram指令数据出错。{errorinfo}"; return -1; } List tempdatas = datas.Where(t => t.ParentID == 0).ToList();//取得第一行 第一个L tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t=>t.FNum).ToList();//取得第二层的2个节点 tempdatas = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList();//去 tempdatas = datas.Where(t => t.ParentID == tempdatas[1].ID).OrderBy(t => t.FNum).ToList(); tempdatas[1].FContent = programname; tempdatas[1].FLen = programname.Length; loger.LogError($"发送装载指令给机台"); HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); loger.LogError($"装载指令发送完成"); if (rec == null) { errorinfo = $"发送S2F41SelProgram指令出错。{errorinfo}"; return -1; } if (int.Parse(rec.Datalists[1].FContent) != 0) { errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].FContent}。"; //SendInfo(mac.FCode, $" Recipe download fail [{rec.Datalists[0].FContent}]"); return -1; } return 1; } catch (Exception ex) { errorinfo ="选择程序异常"+ ex.Message.ToString(); return -1; } } 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; } } private List GetS2F13Data( 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 = "U2"; entity.FContent = "4200"; entity.FLen = 4; ldata.Add(entity); return ldata; } public string GetProgramName(Machine mac,ref string errorinfo) { try { OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 13, ref errorinfo); if (order == null) return ""; string condition = $" and a.preid={order.ID}"; List datas = GetS2F13Data(order.ID, ref errorinfo);// CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return ""; HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); if (rec == null) return ""; string org = rec.Datalists[1].FContent; return org; } catch(Exception ex) { errorinfo = ex.Message.ToString(); return ""; } } //public string GetMacProgramName(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; // return programname; // } // catch (Exception ex) // { // errorinfo = ex.Message.ToString(); // return ""; // } //} public int CompareParams(Machine mac, string usercode, ref string errorinfo) { try { string programname = GetProgramName(mac, ref errorinfo); if (errorinfo != "") { return -1; } if (programname == "") { return -1; } //string programname = "BCP5316TA.BCX5316T3D"; string condition = $" and a.FName='{programname}' and a.ProcessCode='{mac.PCode}' and a.ModelID={mac.MModeID}"; List programmsts = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if(programmsts.Count<=0) { errorinfo = $"未找到程序名称【{programname}】,制程代码【{mac.PCode}】,机型代码【{mac.MModeCode}】对应的信息。"; return -1; } ProgramMst programmst = programmsts[0]; ParamsComMst mst = null; //比较机台程序名称 ParamsComMstDal comdal = new ParamsComMstDal(CurrDb); //比对程序参数 mst = comdal.CompareParams(mac, programmst, ref errorinfo); if (mst == null) { errorinfo = $"未找到机台【{mac.FCode}】程序【{programmst.FName}】对应的比对主档。{errorinfo}"; return -1; } //插入比对记录 mst = comdal.IUParamsComMst(mst, usercode, ref errorinfo); if (mst == null) { errorinfo = $"插入比对结果发生错误:{errorinfo}"; return -1; } //如果参数不对,就发送停机指令 if (mst.IsPass <= 0) { return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int DownloadProgram(Machine mac, string orgprogramname,ref string programname, ref string errorinfo) { try { orgprogramname = orgprogramname.Trim(); #region //string status = GetMacStatus(mac, ref errorinfo); //if (string.IsNullOrEmpty(status)) //{ // errorinfo = $"获取机台状态发生错误:{errorinfo}"; // return -1; //} //string condition = $" and a.MacID={mac.ID}"; //List macorder = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //if (macorder.Count <= 0) //{ // errorinfo = $"未找到机台【{mac.FCode}】对应的指令信息,可能您未定义。"; // return -1; //} //condition = $" and a.PreID={macorder[0].PreID} and a.OrderVal='{status}'"; //List macstatus = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //if (macstatus.Count <= 0) //{ // errorinfo = $"未找到指令状态值【{status}】,可能您未定义。"; // return -1; //} //if (macstatus[0].StatusID == MacStatusVal.Run) //{ // errorinfo = $"机台正在运行,不能进行此操作。"; // return -1; //} #endregion int rdcount = new Random(Guid.NewGuid().GetHashCode()).Next(1, 9999); programname = rdcount.ToString("0000"); programname = $"Auto\\{programname}"; 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; //替换程序名称 filedatas=DiodesProgramFileHelper.ReplaceProgramName(filedatas, orgprogramname, ref errorinfo); if(filedatas==null) { errorinfo = $"替换程序名称出错:{errorinfo}"; return -40; } result = DownloadProgram(mac, programname, filedatas, ref errorinfo); //if (result <= 0) // return -20; return result; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int UpProgram(Machine mac,byte[] filedatas,string filedir,string usercode, ref string errorinfo) { try { byte[] tempbytes = new byte[200]; if (filedatas.Length < 200) { tempbytes = filedatas; } else { Array.Copy(filedatas, 0, tempbytes, 0, 200); } //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); string programname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); if (!string.IsNullOrEmpty(errorinfo)) { return -1; } BusinessFileDal dal = new BusinessFileDal(CurrDb); BusinessFile busifile = dal.UpdownFile(filedatas, filedir, programname, mac,"", usercode, ref errorinfo); if (busifile == null) return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int ClearProgram(string maccode, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } List datas = GetMacAutoProgram(macs[0], ref errorinfo); if (datas == null) return -1; if (datas.Count <= 1) return 1; MacOrderSendDal dal = new MacOrderSendDal(CurrDb); OrderDetail order = dal.ReadMachineOrderDetail(macs[0].ID, 7, 17, "S7F17ClearProgram", ref errorinfo); if (order == null) return -1; HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(macs[0].FCode, order, datas, ref errorinfo); if (rec == null) return -1; if (int.Parse(rec.Datalists[0].FContent) != 0) { errorinfo = $"delete program has occ:{rec.Datalists[1].FContent}。"; return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } /// /// 清除auto目录下所有程序 /// /// /// /// public int ClearProgramAutoDir(string maccode, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } List datas = GetMacAutoProgram(macs[0], ref errorinfo); if (datas == null) { errorinfo = $"获取auto目录程序名称失败:{errorinfo}"; return -1; } if (datas.Count <= 0) return 1; MacOrderSendDal dal = new MacOrderSendDal(CurrDb); OrderDetail order = dal.ReadMachineOrderDetail(macs[0].ID, 7, 17, "S7F17ClearProgram", ref errorinfo); if (order == null) return -1; List orderdatas = CurrDb.FindListForCondition($" and a.preid={order.ID}", ref errorinfo).ToList(); int fnum = 0; foreach (var item in datas) { fnum += 10; item.PreID = order.ID; item.ParentID = orderdatas[0].ID; item.FNum = fnum; orderdatas.Add(item); } HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(macs[0].FCode, order, orderdatas, ref errorinfo); if (rec == null) return -1; if (int.Parse(rec.Datalists[0].FContent) != 0) { errorinfo = $"delete program has occ:{rec.Datalists[1].FContent}。"; return -1; } return 1; } catch (Exception ex) { errorinfo = ex.ToString(); return -1; } } private List GetMacAutoProgram(Machine mac,ref string errorinfo) { try { MacOrderSendDal dal = new MacOrderSendDal(CurrDb); OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 7, 19, "GetAutoProgram", ref errorinfo); if (order == null) return null; string condition = $" and a.preid={order.ID}"; List datas = 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; rec.Datalists.RemoveAll(t =>t.FContent.Trim().Length<=5|| t.FContent.Substring(0, 5).ToLower() != "auto\\"); return rec.Datalists; //List programs = rec.Datalists.Where(t => t.ParentID == 0).ToList(); //programs = rec.Datalists.Where(t => t.ParentID == programs[0].ID).ToList(); //programs.RemoveAll(t => !(t.FContent.Substring(0, 5).ToLower() == "auto\\")); //foreach(var item in programs) //{ // if (!(item.FContent.Substring(0, 5).ToLower() == "auto\\")) // programs.Remove(item); //} //return programs.Where(t => t.FContent.Substring(0, 4).ToLower() == "auto").ToList(); } catch(Exception ex) { errorinfo = ex.ToString(); return null; } } public int DownloadProgramForWB(Machine mac, string programname,int ver, ref string errorinfo) { try { 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, programname,ver, orgtypeid, programdir, ref errorinfo); if (filedatas == null) return -30; result = DownloadProgram(mac, programname, filedatas, ref errorinfo); //if (result <= 0) // return -20; return result; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int DownloadProgramForFile(Machine mac, string programname, int ver, ref string errorinfo) { try { 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; } TFileInfo files = macprogramdal.ReadMacProgramForFile(mac, programname, ver, orgtypeid, programdir, ref errorinfo); if (files == null) return -30; SocketFile dal = new SocketFile(); return dal.DownloadFile(mac,programname, files.FilePath, ref errorinfo); } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } } }