using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllEapDal; using DllEapEntity; using DllHsms; using Microsoft.Extensions.Logging; using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace DllLotServer { public class LotServer:ILotServer { private ILogger loger = null; public LotServer(ILogger loger) { this.loger = loger; CurrMaxBlock = long.Parse(AppConfigurtaionServices.Configuration["MaxBlock"]); CurrShareFileDir = AppConfigurtaionServices.Configuration["ShareFileDir"]; CurrProgramDir = AppConfigurtaionServices.Configuration["ProgramDir"]; } private long CurrCount = 0; private IModel CurrQueueChannel = null; private string CurrRecQueueName = "hello";// private string CurrStatusQueueName = "hello";//状态显示队列名称 private string StatusExchange = "";//状态交换机名称 private string StatusTotalExchange = ""; private DateTime CurrSTime = DateTime.Now;//开始时间 //private int CurrDbTime = 30;//提交数据到数据库的间接时间,以秒为单位 private List CurrReportDetail = null; private List CurrMacOrder = null; private List CurrOrderStatus = null; private List CurrInfo = new List();//记录错误信息 private IDatabase CurrDb = null; private int CurrStatus = -1;//未启动状态 private string DbCode = "eap"; public long CurrMaxBlock = 0; public string CurrShareFileDir = ""; private string CurrProgramDir = ""; //开始接收队列线程 public int StartRecQueue(ref string errorinfo) { try { //清空接收队列 //int result = ClearRecQueue(ref errorinfo); //if (result < 0) //{ // errorinfo = "清空接收队列发生错误,错误信息为:" + errorinfo; // return -1; //} CurrQueueChannel.QueueDeclare(CurrRecQueueName, false, false, false, null); //BasicQos函数是针对通道的,必须放在注册消费者前执行,否则注册的消费者会接受多条信息 CurrQueueChannel.BasicQos(0, 1, false); var consumer = new EventingBasicConsumer(CurrQueueChannel); CurrQueueChannel.BasicConsume(queue: CurrRecQueueName, autoAck: false, //是否不要手动应答(no manual Ack),ture自动应答,自动删除处理消息;false手动应答,服务器的消息会等待应答结果才消除 consumer: consumer); consumer.Received += QueueReceive; CurrSTime = DateTime.Now; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } //注册通道 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"]; //CurrQueueName = AppConfigurtaionServices.Configuration["mqname:macrecmqname"]; var connection = factory.CreateConnection(); var channel = connection.CreateModel(); return channel; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } private int ClearRecQueue(ref string errorinfo) { try { CurrQueueChannel.QueueDelete(CurrRecQueueName, true, false); return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } private void QueueReceive(object model, BasicDeliverEventArgs ea) { CurrCount++; //textBox2.Text = CurrCount.ToString(); //SetText2(CurrCount.ToString()); string errorinfo = ""; int result = ProcessEvent(ea.Body, ref errorinfo); CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); if (result <= 0) { string logerrorinfo = ""; //WriteLog.WriteLogStr("All", "", DateTime.Now, $"处理包发生错误,错误信息:{errorinfo}", ref logerrorinfo); SetText(errorinfo + "\r\n"); } } private List ComParams(OrderDetail order, List lists, ref string errorinfo) { try { int sval = order.SVal; int fval = order.FVal; List tempdt = null; tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo); if (tempdt == null) { errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo; return null; } return tempdt; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } private int ProcessEvent(byte[] datas, ref string errorinfo) { try { OrderBlock entity = EntityHelper.DeSerializeBytes(datas); int result = entity.RecoverData(CurrShareFileDir, ref errorinfo); if (result <= 0) return -1; //如果不是S6F11,直接返回 if (!(entity.CurrOrder.SVal == 6 && entity.CurrOrder.FVal == 11)) return 1; //修改为实时从数据库中读取指令信息 List tempmacorders = CurrMacOrder.Where(t => t.MacFCode == entity.MainMsg.McaCode).ToList(); if (tempmacorders.Count <= 0) { errorinfo = $"未找到机台【{entity.MainMsg.McaCode}】对应的指令信息。"; return -1; } loger.LogError($"机台编号={entity.MainMsg.McaCode};开始处理:{entity.ToJson()}"); string eventcode = HsmsUnity.GetEventCode(CurrDb, entity.MainMsg.McaCode, entity.CurrOrder, entity.Datalists, ref errorinfo); if (eventcode == "") return -1; List mcadetails = HsmsUnity.GetOrderS6F11Dt(entity.Datalists, CurrReportDetail, ref errorinfo); if (mcadetails == null) return -1; //if(entity.MainMsg.McaCode=="DIBD0049") //{ // result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); // if (result <= 0) // return -1; //} List ttlists = mcadetails.Where(t => t.FCode == StandardCode.SVID_CurrentStatus).ToList(); string status = ttlists[0].FVal; //if (entity.MainMsg.McaCode == "DIBD0038") //{ // result = CallFunction.LotEnd(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); // if (result <= 0) // return -1; //} //if (eventcode== StandardCode.CEID_StatusChange && (status == "7" || status == "6")) //{ // result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); // if (result <= 0) // return -1; // return 1; //} switch (eventcode) { case StandardCode.CEID_LotStart: loger.LogError($"接受到lot开始事件"); result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); if (result <= 0) return -1; break; case StandardCode.CEID_LotEnd://lot完成 loger.LogError($"接受到lot结束事件"); result = CallFunction.LotEnd(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); if (result <= 0) return -1; break; case StandardCode.CEID_CollectLife://吸嘴清洗 loger.LogError($"接受到lot吸嘴清洗事件"); result = CallFunction.CollectLife(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo); if (result <= 0) return -1; break; } return 1; } catch (Exception ex) { errorinfo = "IMcaSecVMst01" + ex.Message.ToString(); return -1; } } private void Test() { string filepath = @"D:\eap\1N4148W-7-F.W1003FM-FW001.4CH_1"; string errorinfo = ""; byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); 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); string filedir = $"d:\\aaaa\\{programname}"; if (!Directory.Exists(@filedir)) { Directory.CreateDirectory(@filedir); } } public void SetText(string str) { loger.LogError(str); if (CurrInfo.Count > 100) CurrInfo.Clear(); int id = 1; if (CurrInfo.Count > 0) { id = CurrInfo.Max(t => t.ID); id++; } ServerInfo entity = new ServerInfo(); entity.ID = id; entity.Info = str; CurrInfo.Add(entity); //写入日志 } /// /// 启动RMS系统 /// public int Start(ref string errorinfo) { //Test(); //IDatabase CurrDb = null; try { Stop(ref errorinfo); if(errorinfo!="") { SetText($"停止服务发生错误,错误信息为:{errorinfo}"); } CurrRecQueueName = AppConfigurtaionServices.Configuration["mqname:recmqname"]; CurrStatusQueueName = AppConfigurtaionServices.Configuration["mqname:statusmqname"]; StatusExchange = AppConfigurtaionServices.Configuration["mqname:statusexchange"]; StatusTotalExchange = AppConfigurtaionServices.Configuration["mqname:StatusTotalExchange"]; CurrDb = DbFactory.Base(CallFunction.CurrDbCode); CurrStatus = -1; CurrReportDetail =CurrDb.FindListForCondition("", ref errorinfo).ToList(); if (CurrReportDetail == null) { SetText(errorinfo); return -1; } CurrMacOrder = CurrDb.FindListForCondition("", ref errorinfo).ToList(); if (CurrMacOrder == null) { SetText(errorinfo); return -1; } CurrOrderStatus = CurrDb.FindListForCondition("", ref errorinfo).ToList(); if (CurrMacOrder == null) { SetText(errorinfo); return -1; } CurrQueueChannel = RegeditChannel(ref errorinfo); if(CurrQueueChannel==null) { SetText("注册队列通道发生错误:" + errorinfo); return -1; } int result = StartRecQueue(ref errorinfo); if (result < 0) { SetText("启动发生错误:"+errorinfo); return -1; } SetText("启动成功。"); CurrStatus = 1; return 1; } catch (Exception ex) { SetText(ex.Message.ToString()); return -1; } //finally //{ // if (CurrDb != null) // CurrDb.Close(); //} } public int GetStatus(ref string errorinfo) { if (CurrStatus <= 0 && CurrInfo.Count > 0) { errorinfo = CurrInfo.Last().Info; } return CurrStatus; } public long GetAccount() { return CurrCount; } public List GetInfo() { if(CurrInfo.Count<=100) return CurrInfo; List templist = new List(); for (int i = CurrInfo.Count - 100; i < CurrInfo.Count; i++) templist.Add(CurrInfo[i]); return templist; } public int Stop(ref string errorinfo) { try { if (CurrDb != null) CurrDb.Close(); CurrStatus = -1; if(CurrQueueChannel!=null) CurrQueueChannel.Close(); SetText("停止服务成功。"); return 1; } catch (Exception ex) { SetText(ex.Message.ToString()); return -1; } } public int ClearInfo(ref string errorinfo) { try { CurrInfo.Clear(); return 1; } catch(Exception ex) { SetText(ex.Message.ToString()); return -1; } } } }