123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- 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<LotServer> 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<ReportDetail> CurrReportDetail = null;
- private List<MacOrder> CurrMacOrder = null;
- private List<OrderStatus> CurrOrderStatus = null;
- private List<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
- 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<McaSecVDetail> ComParams(OrderDetail order, List<OrderData> lists, ref string errorinfo)
- {
- try
- {
- int sval = order.SVal;
- int fval = order.FVal;
- List<McaSecVDetail> 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<OrderBlock>(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<MacOrder> 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<McaSecVDetail> 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<McaSecVDetail> 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);
- //写入日志
- }
- /// <summary>
- /// 启动RMS系统
- /// </summary>
- 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<ReportDetail>("", ref errorinfo).ToList();
- if (CurrReportDetail == null)
- {
- SetText(errorinfo);
- return -1;
- }
- CurrMacOrder = CurrDb.FindListForCondition<MacOrder>("", ref errorinfo).ToList();
- if (CurrMacOrder == null)
- {
- SetText(errorinfo);
- return -1;
- }
- CurrOrderStatus = CurrDb.FindListForCondition<OrderStatus>("", 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<ServerInfo> GetInfo()
- {
- if(CurrInfo.Count<=100)
- return CurrInfo;
- List<ServerInfo> templist = new List<ServerInfo>();
- 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;
- }
- }
-
- }
- }
|