123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- using Cksoft.Data;
- using Cksoft.Data.Repository;
- using Cksoft.Unity;
- using DllEapDal;
- using DllEapEntity;
- using RabbitMQ.Client;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Linq;
- using Microsoft.Extensions.Logging;
- using Mes.Model;
- using System.Threading.Tasks;
- namespace DllScan
- {
- public class ScanCore
- {
- private ILogger loger = null;
- public ScanCore(ILogger loger)
- {
- this.loger = loger;
- }
- private Thread SocketRecThread = null;//socket接受数据线程
- private ScanSocket CurrHsms = null;
- public MacOrder CurrMac = null;//机台数据
- public List<ConstItem> CurrItemDt { set; get; } = null;//固定项表
- //public string CurrLogQueueName="";
- //private IModel CurrQueueChannel = null;
- private string CurrFileDir = "";//日志文件主路径
- //扫描链表,将扫描的数据存放在此,供调程任务消费
- private List<ScanBlock> ScanList = new List<ScanBlock>();
- public bool IsRun = false;//检查线程是否在运行
- public bool SocketIsRun = false;//socket接受线程是否在运行
- //private string CurrRecQueueName = "";//接收队列名称 hsmsreceive+机台编号
- //private string CurrSendQueueName = "";//发送队列名称 hsmssend+机台编号
- public delegate void SetText(string str);
- public event SetText eventSetText = null;
- private string CurrLogFile = "";
- //装载或卸载事件交换机
- private string LOrDExchange = "";
- //private string CurrStatusShowQueueName = "";//状态显示队列
- //private Thread CheckSocketThread = null;//检查socket是否正常的线程
- //运行中发生错误信息
- public delegate void SetErrorText(int id, string errorinfo);
- public event SetErrorText eventSetErrorText = null;
- private Thread CheckThread = null;//检查线程
- //private bool ReadDbStatus = false;//是否正确读取配置文件
- //private bool QueueStatus = false;//消息队列是否注册成功
- //private long LastRecTick = 0;//记录最后接受机台数据时间戳
- private bool RecordSend = false;//标记开始记录发送包
- public ScanCore(MacOrder mac, ILogger loger)
- {
- CurrMac = mac;
- this.loger = loger;
- //CurrLogQueueName = AppConfigurtaionServices.Configuration["mqname:scanlogmqname"];
- }
- public int SetCurrID(int id, ref string errorinfo)
- {
- return 1;
- }
-
- public int ShutDown(ref string errorinfo)
- {
- try
- {
- IsRun = false;
- SocketIsRun = false;
- //关闭连接
- ShutDownSocket(ref errorinfo);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //记录程序内部错误日志
- private void WriteErrorInfoLog(string errorinfo)
- {
- StringBuilder sqlstr = new StringBuilder(100);
- try
- {
- //if (CurrLogFile != "")
- //{
- // DllUnity.Unity.WriteLog(CurrLogFile, errorinfo);
- //}
- //eventSetErrorText?.Invoke(CurrMac.ID, errorinfo);
- string temperrorinfo = "";
- ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
- int result = WriteLogToQueue(errorinfo, ref temperrorinfo);
- if (result <= 0)
- {
- sqlstr.AppendFormat("\r\n");
- sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
- sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
- //写入本地错误日志
- WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
- }
- }
- catch (Exception ex)
- {
- eventSetErrorText?.Invoke(CurrMac.MacID, ex.Message.ToString());
- sqlstr.AppendFormat("\r\n");
- sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("错误信息:{0}\r\n", ex.Message.ToString());
- WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
- }
- }
- //将日志写到消息队列中
- //总的启动
- //private IModel RegeditChannel(ref string errorinfo)
- //{
- // try
- // {
- // var factory = new ConnectionFactory();
- // factory.HostName = AppConfigurtaionServices.Configuration["rabbitmq:IPAddress"];
- // factory.UserName = AppConfigurtaionServices.Configuration["rabbitmq:UserName"];
- // factory.Password = AppConfigurtaionServices.Configuration["rabbitmq:Password"];
- // var connection = factory.CreateConnection();
- // var channel = connection.CreateModel();
- // return channel;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return null;
- // }
- //}
- public int Start(ref string errorinfo)
- {
- //string errorinfo = "";
- CurrLogFile = CreateLogFile(CurrMac.MacID, CurrMac.MacFCode, ref errorinfo);
- if (errorinfo != "")
- {
- return -1;
- }
- StringBuilder sqlstr = new StringBuilder(100);
- //消息队列必须正常连接,如果无法连接则不能启动程序
- //CurrQueueChannel = RegeditChannel(ref errorinfo);
- //if (CurrQueueChannel == null)
- //{
- // sqlstr.Clear();
- // sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- // sqlstr.AppendFormat("初始化队列发生错误\r\n");
- // sqlstr.AppendFormat("{0}", errorinfo);
- // WriteErrorInfoLog(sqlstr.ToString());
- // return -1;
- //}
-
- int result = StartCheckThread(ref errorinfo);
- if (result <= 0)
- {
- IsRun = false;
- //处理接受失败,写入数据库
- sqlstr.Clear();
- sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("开始检查线程发生错误\r\n");
- sqlstr.AppendFormat("{0}", errorinfo);
- WriteErrorInfoLog(sqlstr.ToString());
- return -1;
- }
- return 1;
- }
- //启动总的检查线程,此线程应该一直运行,直到程序退出
- public int StartCheckThread(ref string errorinfo)
- {
- try
- {
- IsRun = true;
- //成功后,启动接受线程
- CheckThread = new Thread(CheckThreadFun);
- CheckThread.Start();
- return 1;
- }
- catch (Exception ex)
- {
- IsRun = false;
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int StopCheckThread(ref string errorinfo)
- {
- try
- {
- IsRun = false;
- SocketIsRun = false;
- ShutDownSocket(ref errorinfo);
- if (CheckThread == null)
- return 1;
- //CheckThread.Interrupt();//中断线程
- //CheckThread.IsBackground = true;//让后台处理
- CheckThread = null;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private void CheckThreadFun()
- {
- try
- {
- StringBuilder sqlstr = new StringBuilder(100);
- int sleeptime = int.Parse(CurrItemDt[0].FCode);
- string errorinfo = "";
- string closesocketerror = "";
- while (IsRun)
- {
- errorinfo = "";
- closesocketerror = "";
- int result = CheckThreadFunFor(ref errorinfo);
- if (result <= 0)
- {
- //发生了错误,需要清理
- SocketIsRun = false;
- ShutDownSocket(ref closesocketerror);
- Thread.Sleep(1000 * 5);//休眠5秒钟
- sqlstr.Clear();
- sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("检查线程发生错误\r\n");
- sqlstr.AppendFormat("{0}", errorinfo);
- if (closesocketerror != "")
- {
- sqlstr.AppendFormat("关闭SOCKET错误:{0}", closesocketerror);
- }
- eventSetErrorText?.Invoke(CurrMac.ID, errorinfo);
- WriteErrorInfoLog(sqlstr.ToString());
- if (result == -11)//说明是验证指令出现错误,需要休眠
- {
- Thread.Sleep(1000 * 60);
- }
- //Thread.Sleep(sleeptime);
- }
- else
- {
- //说明是重新连接后的,需要刷新界面状态
- if (result != 100)
- {
- eventSetErrorText?.Invoke(CurrMac.ID, "正常接受数据");
- }
- //发送测试包
- result = SendLinkTest(ref errorinfo);
- if (result <= 0)
- {
- sqlstr.Clear();
- sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("发送连接测试错误:\r\n");
- sqlstr.AppendFormat("{0}", errorinfo);
- eventSetErrorText?.Invoke(CurrMac.MacID, errorinfo);
- WriteErrorInfoLog(sqlstr.ToString());
- ShutDownSocket(ref errorinfo);
- }
- }
- Thread.Sleep(sleeptime);
- }
- }
- catch (Exception ex)
- {
- IsRun = false;
- WriteErrorInfoLog("检查线程出错:" + ex.Message.ToString());
- }
- }
- //初始化连接线程函数
- private int CheckThreadFunFor(ref string errorinfo)
- {
- try
- {
- int result = 0;
- //判断socket是否正常通行
- if (CurrHsms == null)
- {
- //修改状态为开始连接机台
- RecordSend = false;
- CurrHsms = new ScanSocket();
- bool init = CurrHsms.InitSocket(CurrMac.ScanIp, CurrMac.FPort, ref errorinfo);
- if (!init)
- {
- errorinfo = "初始化连接发生错误:" + errorinfo;
- return -1;
- }
- if (!IsRun)
- {
- errorinfo = "检查线程已终止。";
- return -1;
- }
- //设置SOCKET超时时间,防止接受不到数据
- CurrHsms.SetReceiveTimeout(1000 * 15);
- CurrHsms.SetSendTimeout(1000 * 15);
-
- result = StartSocketRecThread(ref errorinfo);
- if (result < 0)
- {
- errorinfo = "启动接受线程发生错误:" + errorinfo;
- return -1;
- }
- RecordSend = true;
- //设置SOCKET超时时间,一直接受
- CurrHsms.SetReceiveTimeout(0);
- CurrHsms.SetSendTimeout(1000 * 30);//设置发送超时为30秒
- return 1;
- }
- else
- {
- //检查是否存在超时未回复的包,如果存在则关闭socket连接
- result = CheckList(10, ref errorinfo);
- if (result <= 0)
- {
- errorinfo = "启动接受线程检查数据包错误发生错误:" + errorinfo;
- return -1;
- }
- return 100;
- }
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //发送测试连接包
- private int SendLinkTest(ref string errorinfo)
- {
- try
- {
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
-
- //检查已发送的数据,是否在规定的时间内得到回复
- private int CheckList(int timeout, ref string errorinfo)
- {
- try
- {
- //long start = DateTime.Now.Ticks;
- //long end = start + timeout * 10000000;
- //for (int i = 0; i < CurrSendList.Count; i++)
- //{
- // start = CurrSendList[i].SendTime;
- // end = start + timeout * 10000000;
- // if (DateTime.Now.Ticks > end)
- // {
- // errorinfo = "存在超时未收到响应包。" + CurrSendList[i].CurrOrder.FCode;
- // return -1;
- // }
- //}
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- public int StartSocketRecThread(ref string errorinfo)
- {
- try
- {
- //if (SocketRecThread != null)
- //{
- // SocketRecThread.Abort();
- //}
- SocketIsRun = true;
- //成功后,启动接受线程
- SocketRecThread = new Thread(SocketRecThreadFun);
- SocketRecThread.Start();
- return 1;
- }
- catch (Exception ex)
- {
- SocketIsRun = false;
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //关闭连接
- private bool ShutDownSocket(ref string errorinfo)
- {
- try
- {
- SocketIsRun = false;
- CurrHsms.ShutDownSocket(ref errorinfo);
- CurrHsms = null;
- ////上报断开连接
- //McaSecVMst msgdt = GetMsgDt(CurrMac.MacFCode, DateTime.Now, 0, 0, -1, -1, "", ref errorinfo);
- //if (msgdt == null)
- // return false;
- //OrderBlock tempentity = new OrderBlock(msgdt);
- //int result = WriteBusinessToQueue(CurrMesQueueName, tempentity, ref errorinfo);
- //if (result <= 0)
- // return false;
- ////断开后,需要报告给状态管理系统,标志机台状态已经断开
- //result = WriteBusinessToQueue(CurrMacStatusQueueName, tempentity, ref errorinfo);
- //if (result <= 0)
- // return false;
- string temperrorinfo = "";
- ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
- int result = WriteLogToQueue("关闭连接", ref temperrorinfo);
- if (result <= 0)
- {
- StringBuilder sqlstr = new StringBuilder(100);
- sqlstr.AppendFormat("\r\n");
- sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
- sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
- //写入本地错误日志
- WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
- }
- return true;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return false;
- }
- }
- private string CreateLogFile(int mcaid, string code, ref string errorinfo)
- {
- try
- {
- string sFilePath = System.IO.Directory.GetCurrentDirectory() + "\\log" + DateTime.Now.ToString("yyyyMMdd");
- string sFileName = mcaid.ToString() + "_" + code + "_" + DateTime.Now.ToString("HHmmss") + ".log";
- sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
- if (!Directory.Exists(sFilePath))//验证路径是否存在
- {
- Directory.CreateDirectory(sFilePath);
- //不存在则创建
- }
- return sFileName;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- private void SocketRecThreadFun()
- {
- StringBuilder sqlstr = new StringBuilder(100);
- try
- {
- int result = 0;
- string errorinfo = "";
- while (IsRun && SocketIsRun)
- {
- errorinfo = "";
- result = TheadFunNew(ref errorinfo);
- //if(errorinfo!="")
- //{
- // //说明发生了错误,先写日志,然后停止该线程
- //}
- //if (result == -11)
- //{
- // //socket错误
- //}
- if (result <= 0)
- {
- //sqlstr.Clear();
- //sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- //sqlstr.AppendFormat("接受数据线程发生错误\r\n");
- //sqlstr.AppendFormat("{0}", errorinfo);
- //WriteErrorInfoLog(errorinfo);
- ShutDownSocket(ref errorinfo);
- ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
- node.Remark = errorinfo;
- string temperrorinfo = "";
- result = WriteLogToQueue($"TheadFunNew 出错{errorinfo}", ref temperrorinfo);
- if (result <= 0)
- {
- sqlstr.AppendFormat("\r\n");
- sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
- sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
- //写入本地错误日志
- //WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
- WriteErrorInfoLog(sqlstr.ToString());
- }
- break;
- }
- // isrun = false;
- }
- }
- catch (Exception ex)
- {
- sqlstr.Clear();
- sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- sqlstr.AppendFormat("接受数据线程发生错误\r\n");
- sqlstr.AppendFormat("{0}", ex.Message.ToString());
- WriteErrorInfoLog(sqlstr.ToString());
- //启动线程出错
- }
- //处理接受线程停止错误
- }
- //线程处理函数
- private int TheadFunNew(ref string errorinfo)
- {
- try
- {
- ScanBlock node = ReceiveBlock(ref errorinfo);
- if (node == null)
- return -1;
- WriteLogToQueue($"接受到扫描信息= {node.ScanStr} ......", ref errorinfo);
- //查找扫描链表,看看是否有一样的信息存在,如果存在则不处理此扫描信息
- lock (ScanList)
- {
- List<ScanBlock> templist = ScanList.Where(t => t.ScanStr == node.ScanStr).ToList();
- if(templist.Count>0)
- {
- WriteLogToQueue($"链表中存在相同的节点= {node.ScanStr} 无需处理......", ref errorinfo);
- return 1;
- }
- else
- {
- //开启新的任务处理此扫描
- //Task<int>.Factory.StartNew(new Func<ScanBlock,int>(ProcessBlockForTask),node);
- //Task.Factory.StartNew(new Action<ScanBlock>(ProcessBlockForTask), node);
- //将节点添加到链表
- ScanList.Add(node);
- //启动调程任务
- Task.Run(() => ProcessBlockForTask(node));
- }
- }
- //LastRecTick = DateTime.Now.Ticks;//更新时间戳
- //将从机台收到的数据打包发送给消息队列
- //WriteLogToQueue($"接受到程序名称= {node.ScanStr} 开始处理......", ref errorinfo);
- //int result = ProcessBlock(node, ref errorinfo);
- //if (result <= 0)
- //{
- // errorinfo = "调程发生错误,错误信息为:" + errorinfo;
- // string temperrorinfo = "";
- // node.Remark = errorinfo;
- // WriteLogToQueue($"机台={node.MacCode}调程出现错误,错误信息为:{errorinfo}", ref temperrorinfo);
- // errorinfo = "";
- // return 100;
- //}
- //string temperrorinfo1 = "";
- //node.Remark = $"download success";
- //WriteLogToQueue($"机台={node.MacCode}调程完成。", ref temperrorinfo1);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.ToString();
- return -1;
- }
- }
- private void ProcessBlockForTask(ScanBlock sendentity)
- {
- string errorinfo = "";
- IDatabase db = null;
- try
- {
- //调程
- int result = 0;
- db = DbFactory.Base("eap");
- string scanstr = sendentity.ScanStr.Replace('\r', ' ').Trim();
- loger.LogError($"开始处理扫描调程,{scanstr}");
- string client = AppConfigurtaionServices.Configuration["Client"];
- //下载程序前先清空auto目录下所有程序
- Dad3350ProgramDal dad3350 = new Dad3350ProgramDal(db);
- if (client == "Onsemi")
- {
- result = dad3350.ClearProgramAutoDir(CurrMac.MacFCode, ref errorinfo);
- }
- else
- {
- result = dad3350.ClearProgram(CurrMac.MacFCode, ref errorinfo);
- }
- if (CurrMac.MacModelFCode.Trim().ToLower() == "dad3350"&&client=="qingdao")
- {
- loger.LogError($"开始处理dad3350调程,以文件的方式,{scanstr}");
- FilesProgramDal filedal = new FilesProgramDal(db);
- result = filedal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
- if (result <= 0)
- {
- loger.LogError($"文件方式调程失败,{scanstr}:{errorinfo}");
- }
- loger.LogError($"文件方式调程成功。{scanstr}");
- }
- else
- {
-
- ProgramDal dal = new ProgramDal(db, loger);
- result = dal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
- if (result <= 0)
- {
- loger.LogError($"调程失败,{scanstr}:{errorinfo}");
- }
- loger.LogError($"调程成功。{scanstr}");
- }
- //删除队列中的数据
- lock(ScanList)
- {
- ScanList.RemoveAll(t => t.ScanStr == sendentity.ScanStr);
- //List<ScanBlock> templist = ScanList.Where(t => t.ScanStr == sendentity.ScanStr).ToList();
- //if(templist.Count>0)
- //{
- // foreach (var item in templist)
- // ScanList.Remove(item);
- //}
- }
- }
- catch (Exception ex)
- {
- db.Rollback();
- errorinfo = ex.Message.ToString();
- }
- finally
- {
- if (db != null)
- db.Close();
- }
- }
- private int ProcessBlock(ScanBlock sendentity, ref string errorinfo)
- {
- IDatabase db = null;
- try
- {
- loger.LogError($"机台【{sendentity.MacCode}】开始执行调程任务:{sendentity.ScanStr}");
- //调程
- db = DbFactory.Base("eap");
- //db.BeginTrans();
- string scanstr = sendentity.ScanStr.Replace('\r', ' ').Trim();
- if (CurrMac.MacModelFCode.Trim().ToLower() == "dad3350")
- {
- loger.LogError($"开始处理dad3350调程,以文件的方式,{scanstr}");
- FilesProgramDal filedal = new FilesProgramDal(db);
- int result = filedal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
- if (result <= 0)
- {
- loger.LogError($"文件方式调程失败,{scanstr}:{errorinfo}");
- }
- loger.LogError($"文件方式调程成功。{scanstr}");
- return result;
- }
- ProgramDal dal = new ProgramDal(db,loger);
- return dal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
- //int result = 0;
- //string condition = $" and a.id={CurrMac.MacID}";
- //List<Machine> macs = db.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- //if(macs==null||macs.Count<=0)
- //{
- // errorinfo = "未找到机台信息。";
- // return -1;
- //}
- //switch (CurrMac.MacModelFCode.ToLower())
- //{
- // case "dad3350":
- // case "dfd6341":
- // Dad3350ProgramDal dal = new Dad3350ProgramDal(db);
- // result = dal.DownloadProgram(macs[0], scanstr, ref errorinfo);
- // break;
- // case "bg":
- // BgProgramDal bgdal = new BgProgramDal(db);
- // result = bgdal.DownloadProgram(macs[0], scanstr, ref errorinfo);
- // break;
- // case "ad838":
- // AsmProgramDal asmdal = new AsmProgramDal(db);
- // result = asmdal.DownloadProgram(macs[0], scanstr, ref errorinfo);
- // break;
- //}
- //if (result < 0)
- //{
- // db.Rollback();
- // MacOrderSendDal senddal = new MacOrderSendDal(db);
- // string temperrorinfo = "";
- // senddal.SendS10F3(CurrMac.MacFCode, $"Download recipe[{scanstr}] faile", ref temperrorinfo);
- // return -1;
- //}
- //db.Commit();
- //return 1;
- }
- catch (Exception ex)
- {
- db.Rollback();
- errorinfo = ex.Message.ToString();
- return -1;
- }
- finally
- {
- if (db != null)
- db.Close();
- }
- }
- private string GetLogFile(string code, ref string errorinfo)
- {
- try
- {
- //每天1个日志
- CurrFileDir= AppConfigurtaionServices.Configuration["ScanLogDir"];
- string sFilePath = CurrFileDir + "\\log" + DateTime.Now.ToString("yyyyMMdd");
- string sFileName = code + ".log";
- sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
- if (!Directory.Exists(sFilePath))//验证路径是否存在
- {
- Directory.CreateDirectory(sFilePath);
- //不存在则创建
- }
- return sFileName;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- //将日志写到消息队列中
- //private int WriteLogToQueue(ScanBlock orgblock, ref string errorinfo)
- //{
- // try
- // {
- // //byte[] queuedata = EntityHelper.SerializeBytes(orgblock);
- // //if (queuedata == null)
- // // return -1;
- // //CurrQueueChannel.QueueDeclare(CurrLogQueueName, false, false, false, null);//创建一个名称为hello的消息队列
- // //CurrQueueChannel.BasicPublish("", CurrLogQueueName, null, queuedata); //开始传递
- // //本地写日志
- // string filename = GetLogFile(CurrMac.MacFCode, ref errorinfo);
- // if (filename == "")
- // return -1;
- // string logstr = $"发生时间:{orgblock.OccurTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")} 机台编号:{CurrMac.MacFCode} ";
- // FileStream fs;
- // StreamWriter sw;
- // if (File.Exists(filename))
- // {
- // //验证文件是否存在,有则追加,无则创建
- // fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
- // }
- // else
- // {
- // fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
- // }
- // sw = new StreamWriter(fs);
- // sw.WriteLine(logstr);
- // sw.WriteLine($"扫描信息:{orgblock.ScanStr}");
- // sw.WriteLine($"备注:{orgblock.Remark}");
- // sw.Close();
- // fs.Close();
- // return 1;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return -1;
- // }
- //}
- private int WriteLogToQueue(string log, ref string errorinfo)
- {
- try
- {
- loger.LogError($"机台编号={CurrMac.MacFCode},{log}");
- ////byte[] queuedata = EntityHelper.SerializeBytes(orgblock);
- ////if (queuedata == null)
- //// return -1;
- ////CurrQueueChannel.QueueDeclare(CurrLogQueueName, false, false, false, null);//创建一个名称为hello的消息队列
- ////CurrQueueChannel.BasicPublish("", CurrLogQueueName, null, queuedata); //开始传递
- ////本地写日志
- //string filename = GetLogFile(CurrMac.MacFCode, ref errorinfo);
- //if (filename == "")
- // return -1;
- //string logstr = $"发生时间:{DateTime.Now} 机台编号:{CurrMac.MacFCode} ";
- //FileStream fs;
- //StreamWriter sw;
- //if (File.Exists(filename))
- //{
- // //验证文件是否存在,有则追加,无则创建
- // fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
- //}
- //else
- //{
- // fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
- //}
- //sw = new StreamWriter(fs);
- //sw.WriteLine(logstr);
- //sw.WriteLine($"{log}");
- //sw.Close();
- //fs.Close();
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //接受数据并处理
- private ScanBlock ReceiveBlock(ref string errorinfo)
- {
- try
- {
- ScanBlock blocknode = CurrHsms.ReceiveForBlock(ref errorinfo);
- if (blocknode == null)
- {
- errorinfo = $"接受数据发生异常:{errorinfo}";
- return null;
- }
- blocknode.MacCode = CurrMac.MacFCode;
- //写入日志
- int result = WriteLogToQueue($"机台={CurrMac.MacFCode}接受到扫码指令:{blocknode.ScanStr}", ref errorinfo);
- if (result <= 0)
- {
- return null;
- }
- return blocknode;
- }
- catch (Exception ex)
- {
- errorinfo = "ReceiveBlock 出现异常:" + ex.ToString();
- return null;
- }
- }
- //初始化发送数据链表
- private int InitCurrSendList(ref string errorinfo)
- {
- //lock (CurrSendList)
- //{
- // try
- // {
- // //清空链表
- // CurrSendList.Clear();
- // return 1;
- // }
- // catch (Exception ex)
- // {
- // errorinfo = ex.Message.ToString();
- // return -1;
- // }
- //}
- return 1;
- }
- }
- }
|