123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- using Cksoft.Data;
- using Cksoft.Unity;
- using DllEapEntity;
- using DllHsms;
- using DllHsmsWeb;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace DllEapDal
- {
- public class MacOrderDalForEverlight
- {
- private IDatabase CurrDb = null;
- public MacOrderDalForEverlight(IDatabase db)
- {
- CurrDb = db;
- }
- public Machine ReadMachine(string maccode, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号【{maccode}】的机台信息。";
- return null;
- }
- return macs[0];
- }
- 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];
- }
- public OrderDetail ReadMachineOrderDetail(int macid, int sval, int fval, string fname, ref string errorinfo)
- {
- //读取机台信息
- string condition = $" and a.macid={macid}";
- List<MacOrder> macs = CurrDb.FindListForCondition<MacOrder>(condition, ref errorinfo).ToList();
- if (macs == null)
- return null;
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台ID【{macid}】的机台指令信息。";
- return null;
- }
- condition = $" and a.preid={macs[0].PreID} and a.sval={sval} and a.fval={fval} and lower(a.FName)='{fname.ToLower()}'";
- List<OrderDetail> details = CurrDb.FindListForCondition<OrderDetail>(condition, ref errorinfo).ToList();
- if (details == null)
- return null;
- if (details.Count <= 0)
- {
- errorinfo = "未找到您要的指令。";
- return null;
- }
- return details[0];
- }
- public int SendStopMac(string maccode, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return -1;
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41stop", ref errorinfo);
- if (order == null)
- return -1;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return -1;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- //休眠5秒,等待机台状态恢复
- //Thread.Sleep(5000);
- //order = ReadMachineOrderDetail(mac.ID, 2, 41, "s2f41resume", ref errorinfo);
- //if (order == null)
- // return -1;
- ////从机型参数中读取程序参数信息
- //condition = $" and a.preid={order.ID}";
- //datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- //if (datas == null)
- // return -1;
- //rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- //if (rec == null)
- // return -1;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int SendS10F3(string maccode,string info, ref string errorinfo)
- {
- try
- {
- Machine mac = ReadMachine(maccode, ref errorinfo);
- if (mac == null)
- return -1;
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 10, 3, "s10f3", ref errorinfo);
- if (order == null)
- return -1;
- //从机型参数中读取程序参数信息
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).OrderBy(t => t.FNum).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[1].FContent = info;
- tempdatas[1].FLen = info.Length;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return -1;
- if (rec.Datalists[0].FContent != "00")
- {
- errorinfo = rec.Datalists[0].FContent;
- return -1;
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public string GetProgramName(Machine mac, ref string errorinfo)
- {
- try
- {
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 1, 3, "programname", ref errorinfo);
- if (order == null)
- return "";
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(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;
- //condition = $" and a.id={org}";
- //List<MacProgram> programs = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
- //if (programs.Count <= 0)
- //{
- // errorinfo = $"未找到ID【{org}】的程序。";
- // return "";
- //}
- //return programs[0].ProgramName;
- return org;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- public byte[] GetProgram(Machine mac,string programname, ref string errorinfo)
- {
- try
- {
- OrderDetail order = ReadMachineOrderDetail(mac.ID, 7, 5, "requestprogram", ref errorinfo);
- if (order == null)
- return null;
- string condition = $" and a.preid={order.ID}";
- List<OrderData> datas = CurrDb.FindListForCondition<OrderData>(condition, ref errorinfo).ToList();
- if (datas == null)
- return null;
- datas[0].FContent = programname;
- datas[0].FLen = programname.Length;
- HsmsWeb accessmac = new HsmsWeb();
- OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo);
- if (rec == null)
- return null;
- if(rec.Datalists[2].OrgDatas==null)
- {
- errorinfo = rec.ToJson();
- return null;
- }
- return rec.Datalists[2].OrgDatas;
- //string org = rec.Datalists[1].FContent;
- //condition = $" and a.id={org}";
- //List<MacProgram> programs = CurrDb.FindListForCondition<MacProgram>(condition, ref errorinfo).ToList();
- //if (programs.Count <= 0)
- //{
- // errorinfo = $"未找到ID【{org}】的程序。";
- // return "";
- //}
- //return programs[0].ProgramName;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- 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;
- }
- private void SendInfo(string maccode, string info)
- {
- try
- {
- string temperrorinfo = "";
- SendS10F3(maccode, info, ref temperrorinfo);
- }
- catch
- {
- return;
- }
- }
- 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;
- 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 = filepath;
- entity.OrgDatas = null;
- entity.FLen = filelen;
- //entity.FLen = programname.Length;
- ldata.Add(entity);
- return ldata;
- }
- public int DownloadProgram(Machine mac, string programname, int filelen, string filepath, 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<OrderData> datas = GetS7F1Data(programname, filelen, order.ID, ref errorinfo);//
- 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 -1;
- }
- if (result == 0)
- {
- //产生文件
- ShareFileDal filedal = new ShareFileDal(CurrDb);
- ShareFile fileentity = filedal.MadeShareFile(filepath, ref errorinfo);
- if (fileentity == null)
- return -1;
- order = ReadMachineOrderDetail(mac.ID, 7, 3, ref errorinfo);
- datas = GetS7F3Data(programname, filelen, fileentity.FilePath, 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}]");
- return -1;
- }
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- }
- }
|