123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllEapEntity.Rms;
- using DllHsms;
- using DllHsmsWeb;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using ZipFileHelper;
- namespace DllEapDal
- {
- public class FilesProgramDal
- {
- private IDatabase CurrDb = null;
- public FilesProgramDal(IDatabase db)
- {
- CurrDb = db;
- }
- public 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];
- }
- /// <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>
- public int SendOrder(Machine mac, int sval, int fval, ref string errorinfo)
- {
- try
- {
- string condition = $"";
- List<MacOrder> macorder = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macorder.Count <= 0)
- {
- errorinfo = $"未找到机台【{mac.FCode}】对应的指令。";
- return -1;
- }
- condition = $" and a.preid={macorder[0].PreID} and a.sval={sval} and a.fval={fval}";
- List<OrderDetail> order = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (order.Count <= 0)
- {
- errorinfo = $"未找到机台【{mac.FCode}】对应的S2F15指令。";
- return -1;
- }
- condition = $" and a.preid={order[0].ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order[0], datas, ref errorinfo);
- if (rec == null)
- return -1;
- int result = int.Parse(rec.Datalists[0].FContent);
- if (result > 0)
- {
- errorinfo = $"设置机台ECID参数出错,错误代码为:{result}";
- return -1;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- /// <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>
- /// 获取机台当前使用的程序文件,并将文件放到指定的目录
- /// </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 "";
- }
- }
- /// <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, ref string errorinfo)
- {
- try
- {
- string programname = ReadProgramFromMac(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;
- }
- 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>
- public ProgramMst FindProgram(Machine mac, string partcode, string pcode, ref string errorinfo)
- {
- try
- {
- string condition = $" and b.ModelID={mac.MModeID} and b.ProcessCode='{pcode}'";
- List<ProgramRule> rules = CurrDb.FindListForCondition<ProgramRule>(condition, ref errorinfo).ToList();
- if (rules == null)
- return null;
- if (rules.Count <= 0)
- {
- errorinfo = "未找到对应的程序规则。";
- return null;
- }
- //先精确匹配产品编号
- List<ProgramRule> templist = rules.Where(t => t.PartCode == partcode).ToList();
- if (templist.Count > 0)
- return CurrDb.FindEntityFor<ProgramMst>(templist[0].ProgramMstID);
- //取模糊匹配的规则
- rules = rules.Where(t => t.PartCode.Contains("%")).ToList();
- if (rules.Count <= 0)
- {
- errorinfo = "未找到对应的程序规则。";
- return null;
- }
- //去掉规则里产品编号的“%”符号
- foreach (var item in rules)
- {
- item.PartCode = item.PartCode.Replace("%", "");
- }
- //排序规则列表,按产品编号长度从大到小排列
- rules = rules.OrderByDescending(t => t.PartCode.Length).ToList();
- foreach (var item in rules)
- {
- if (partcode.Contains(item.PartCode))
- {
- return CurrDb.FindEntityFor<ProgramMst>(item.ProgramMstID);
- }
- }
- errorinfo = "未找到对应的程序规则。";
- return null;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- /// <summary>
- /// 根据机台编号读取机台
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public Machine ReadMachine(string maccode, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs == null)
- {
- errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
- return null;
- }
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
- return null;
- }
- return macs[0];
- }
- 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;
- }
- }
- public string GetMacStatus(Machine mac, ref string errorinfo)
- {
- try
- {
- 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_CurrentStatus}'";
- List<MMSecDetail> mmsecs = CurrDb.FindListForCondition<MMSecDetail>(condition, ref errorinfo).ToList();
- if (mmsecs.Count <= 0)
- {
- errorinfo = $"未找到机台【{mac.FCode}】对应的状态参数定义。";
- return "";
- }
- List<OrderData> datas = new List<OrderData>();
- OrderData lentity = new OrderData();
- lentity.ID = 1;
- lentity.ParentID = 0;
- lentity.PreID = order.ID;
- lentity.FCode = "L";
- lentity.FContent = "";
- lentity.FLen = 1;
- lentity.FNum = 10;
- datas.Add(lentity);
- lentity = new OrderData();
- lentity.ID = 2;
- lentity.ParentID = 1;
- lentity.PreID = order.ID;
- lentity.FCode = mmsecs[0].DCode;
- lentity.FContent = mmsecs[0].FVal;
- lentity.FLen = 1;
- lentity.FNum = 10;
- datas.Add(lentity);
- 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 int DownloadProgram(Machine mac, string orgprogramname, ref string errorinfo)
- {
- try
- {
- orgprogramname = orgprogramname.Trim();
- #region
- #endregion
- //产生随机文件
- int rdcount = new Random(Guid.NewGuid().GetHashCode()).Next(1, 9999);
- string programid = rdcount.ToString("0000");
- 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)
- {
- errorinfo = $"读取文件发生错误:{errorinfo}";
- return -30;
- }
- string rootdir = AppConfigurtaionServices.Configuration["RootDir"];
- string rootpath = $@"\\{mac.IPAddress}\{rootdir}\auto";
- //rootpath = $@"\\10.2.30.199\c\Disco\3000\Cver\000\dev\auto";
- //清空目录
- DirectoryInfo dirinfo = new DirectoryInfo(rootpath);
- if (dirinfo.Exists)
- {
- dirinfo.Delete(true);
- }
- //清空所有程序
- //string parentdir = $@"\\10.2.30.199\c\Disco\3000\Cver\000\dev";
- //parentdir = $@"\\{mac.IPAddress}\{rootdir}";
- //ClearProgram(rootpath);
- result = DownloadProgram(mac, programid, orgprogramname, filedatas, rootpath, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"下载文件发生错误:{errorinfo}";
- return -20;
- }
- //修改文件内容
- string filecontent = $"{programid},{orgprogramname}";
- result = WriteFile(rootpath, filecontent, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"修改列表文件发生错误:{errorinfo}";
- return result - 1;
- }
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = "FilesProgramDal类函数DownloadProgram发生异常:" + ex.Message.ToString();
- return -1;
- }
- }
- //清空程序目录下所有程序引导
- private bool ClearProgram(string dir)
- {
- FileInfo files = new FileInfo($@"{dir}\dev.LST");
- if (files.Exists)
- {
- //清空内容
- StreamWriter sw = new StreamWriter(files.FullName, false, System.Text.Encoding.ASCII);
- sw.WriteLine("");
- sw.Close();
- }
- files = new FileInfo($@"{dir}\devid.LST");
- if (files.Exists)
- {
- //清空内容
- StreamWriter sw = new StreamWriter(files.FullName, false, System.Text.Encoding.ASCII);
- sw.WriteLine("");
- sw.Close();
- }
- DirectoryInfo dirinfo = new DirectoryInfo(dir);
- foreach (var item in dirinfo.GetDirectories())
- {
- return ClearProgram(item.FullName);
- }
- return true;
- }
- public int DownloadProgram(Machine mac, string programid, string programname, byte[] filedatas, string rootpath, ref string errorinfo)
- {
- try
- {
- byte[] filelen = new byte[4];
- Array.Copy(filedatas, 0, filelen, 0, 4);
- int dfdlen = GetFileLen(filelen);
- byte[] dfdfiles = new byte[dfdlen];
- Array.Copy(filedatas, 12, dfdfiles, 0, dfdlen);
- //替换程序名称
- dfdfiles = DiodesProgramFileHelper.ReplaceProgramNameForDfd(dfdfiles, programname, ref errorinfo);
- if (dfdfiles == null)
- {
- errorinfo = $"替换程序名称发生异常:{errorinfo}";
- return -100;
- }
- int result = UnityHelper.WriteFile(rootpath, $"{programid}.dfd", dfdfiles, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"写入DFD文件发生异常:{errorinfo}";
- return -1;
- }
- Array.Copy(filedatas, 4, filelen, 0, 4);
- int clnlen = GetFileLen(filelen);
- byte[] clnfiles = new byte[clnlen];
- Array.Copy(filedatas, 12 + dfdlen, clnfiles, 0, clnlen);
- result = UnityHelper.WriteFile(rootpath, $"{programid}.cln", clnfiles, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"写入CLN文件发生异常:{errorinfo}";
- return -1;
- }
- Array.Copy(filedatas, 8, filelen, 0, 4);
- int alulen = GetFileLen(filelen);
- byte[] alufiles = new byte[alulen];
- Array.Copy(filedatas, 12 + dfdlen + clnlen, alufiles, 0, alulen);
- result = UnityHelper.WriteFile(rootpath, $"{programid}.alu", alufiles, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = $"写入ALU文件发生异常:{errorinfo}";
- return -1;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = $"机台编号={mac.FCode},程序ID={programid}, 程序名称={programname}, 文件长度={filedatas.Length},文件路径={rootpath} 下载文件发生异常:" + ex.Message.ToString();
- return -1;
- }
- }
- //private int WriteFile(string filedir, string filecontent, ref string errorinfo)
- //{
- // StreamWriter sw = null;
- // try
- // {
- // string filepath = filedir + "\\dev.LST";
- // if (!File.Exists(@filepath))
- // {
- // FileStream fs= File.Create(filepath);
- // fs.Close();
- // }
- // sw = new StreamWriter(filepath, false, System.Text.Encoding.ASCII);
- // sw.WriteLine(filecontent);
- // sw.Close();
- // filepath = filedir + "\\devid.LST";
- // if (!File.Exists(@filepath))
- // {
- // FileStream fs = File.Create(filepath);
- // fs.Close();
- // }
- // sw = new StreamWriter(filepath, false, System.Text.Encoding.ASCII);
- // sw.WriteLine(filecontent);
- // sw.Close();
- // return 1;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message;
- // return -1;
- // }
- // finally
- // {
- // if (sw != null)
- // sw.Close();
- // }
- //}
- private int WriteFile(string filedir, string filecontent, ref string errorinfo)
- {
- try
- {
- int result = UnityHelper.WriteFile(filedir, $"dev.LST", System.Text.Encoding.ASCII.GetBytes(filecontent), ref errorinfo);
- if (result <= 0)
- return -1;
- result = UnityHelper.WriteFile(filedir, $"devid.LST", System.Text.Encoding.ASCII.GetBytes(filecontent), ref errorinfo);
- if (result <= 0)
- return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message;
- return -1;
- }
- }
- private int GetFileLen(byte[] filelen)
- {
- return BitConverter.ToInt32(filelen, 0);
- }
- public int DownloadProgram(string maccode, string orgprogramname, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs == null || macs.Count <= 0)
- {
- errorinfo = "未找到机台信息。";
- return -1;
- }
- return DownloadProgram(macs[0], orgprogramname, ref errorinfo);
- }
- catch (Exception ex)
- {
- errorinfo = $"机台编号={maccode},程序名称={orgprogramname} 发生异常:" + ex.Message.ToString();
- return -1;
- }
- }
- }
- }
|