12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Rms;
- using DllHsms;
- using DllHsmsWeb;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using ZipFileHelper;
- namespace DllEapDal
- {
- public class KnsProgramDal
- {
- private IDatabase CurrDb = null;
- public KnsProgramDal(IDatabase db)
- {
- CurrDb = db;
- }
- 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,string ordername, 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} and a.fname='{ordername}'";
- 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="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_PREP";
- 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>
- /// DP机台装载程序指令数据
- /// </summary>
- /// <param name="programname">要装载的程序名称</param>
- /// <param name="preid"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private List<OrderData> GetS2F41DataForPPSelect(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;
- }
- 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 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;
- }
- /// <summary>
- /// 将机台设置为1086参数
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private int SetEc1086(Machine mac, ref string errorinfo)
- {
- try
- {
- HsmsWeb accessmac = new HsmsWeb();
- //发送指令,将程序模式设置为hit模式
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- List<OrderData> 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;
- }
- }
- /// <summary>
- /// 发送指令
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
-
- /// <summary>
- /// 将机台设置为HIT模式
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 获取机台当前使用的程序文件,并将文件放到指定的目录 2019-09-04修改,使用下面的函数
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- //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<OrderData> 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<OrderData> 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<OrderData> 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;
- }
- }
- /// <summary>
- /// 获取程序参数
- /// </summary>
- /// <param name="mac"></param>
- /// <param name="program"></param>
- /// <param name="desdir"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public List<FileParams> 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<FileParams> 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<FileParams> filesdata = UnCompressForKs.GetParamsFor(rarpath, filepath, macprogram.ProgramName
- , tempdesdir, mac.MModeID, ref errorinfo);
- return filesdata;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 根据机台编号、产品编号、制程代码等信息查找程序,返回程序ID
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="partcode"></param>
- /// <param name="pcode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
-
- /// <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(Machine mac, ProgramMst mst,int filelen,string filepath, ref string errorinfo)
- {
- try
- {
- ProgramDal programdal = new ProgramDal(CurrDb);
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 1, ref errorinfo);
- if (order == null)
- return null;
- //string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
- //filedir += "\\" + mac.FCode;
- //string filepath = filedir + "\\" + mst.FName;
- //byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
- //if (filedatas == null)
- //{
- // return null;
- //}
- int result = programdal.SendOrder(mac,2,15, ref errorinfo);
- if (result <= 0)
- return null;
- result = SetHit(mac, "ENTER_HIT", ref errorinfo);
- if (result <= 0)
- {
- 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;
- result = int.Parse(rec.Datalists[0].FContent);
- if (result>1)
- {
- errorinfo = $"机台不接受程序。{rec.Datalists[0].FContent}";
- return null;
- }
- if(result==0)
- {
- 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;
- }
- }
- return mst;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- 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)
- {
- try
- {
- ProgramDal programdal = new ProgramDal(CurrDb);
- int result = programdal.SendOrder(mac, 2, 15, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"send s2f15 error {errorinfo}";
- return -400;
- }
-
- result = SetHit(mac, "ENTER_HIT", ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"Set ENTER_HIT fail {errorinfo}";
- return -500;
- }
- //要修改程序名称
- 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;
- 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 DownloadProgramS7F6(Machine mac, string programname, byte[] filedatas, ref string errorinfo)
- {
- try
- {
- ProgramDal programdal = new ProgramDal(CurrDb);
- int result = programdal.SendOrder(mac, 2, 15, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"send s2f15 error {errorinfo}";
- return -400;
- }
- result = SetHit(mac, "ENTER_HIT", ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"Set ENTER_HIT fail {errorinfo}";
- return -500;
- }
- //要修改程序名称
- OrderDetail order = ReadMachineOrderDetail(mac.ID, "s7f6body", 7, 6, 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;
- List<OrderData> tempdatas = datas.Where(t => t.ParentID == 0).ToList();
- tempdatas = datas.Where(t => t.ParentID == tempdatas[0].ID).OrderBy(t=>t.FNum).ToList();
- tempdatas[0].FContent = programname;
- tempdatas[1].OrgDatas = filedatas;
- tempdatas[1].FContent = "";
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- 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;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int DownloadProgramS7F6(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 = DownloadProgramS7F6(mac, programname, filedatas, ref errorinfo);
- if (result <= 0)
- return result;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SelectProgram(Machine mac, string programname, ref string errorinfo)
- {
- try
- {
- ProgramDal programdal = new ProgramDal(CurrDb);
- int result = programdal.SendOrder(mac, 2, 15, ref errorinfo);
- if (result <= 0)
- return -1;
- result = SetHit(mac, "EXIT_HIT", ref errorinfo);
- if (result <= 0)
- {
- return -1;
- }
- //发送程序装载命令,暂时还不能执行
- HsmsWeb accessmac = new HsmsWeb();
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, ref errorinfo);
- List<OrderData> datas = GetS2F41DataForPPSelect(programname, order.PreID, ref errorinfo);
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (int.Parse(rec.Datalists[1].FContent) != 0)
- {
- errorinfo = $"装载程序不成功,错误代码为:{rec.Datalists[1].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<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 int DownloadProgramS7F6(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;
- // }
- // return 1;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return -1;
- // }
- //}
- //public int DownloadProgramS7F6(Machine mac, string programname, byte[] filedatas, ref string errorinfo)
- //{
- // try
- // {
-
- // //要修改程序名称
- // OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 6, 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;
- // 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;
- // }
- //}
- }
- }
|