LotServer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapDal;
  5. using DllEapEntity;
  6. using DllHsms;
  7. using Microsoft.Extensions.Logging;
  8. using RabbitMQ.Client;
  9. using RabbitMQ.Client.Events;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. namespace DllLotServer
  16. {
  17. public class LotServer:ILotServer
  18. {
  19. private ILogger loger = null;
  20. public LotServer(ILogger<LotServer> loger)
  21. {
  22. this.loger = loger;
  23. CurrMaxBlock = long.Parse(AppConfigurtaionServices.Configuration["MaxBlock"]);
  24. CurrShareFileDir = AppConfigurtaionServices.Configuration["ShareFileDir"];
  25. CurrProgramDir = AppConfigurtaionServices.Configuration["ProgramDir"];
  26. }
  27. private long CurrCount = 0;
  28. private IModel CurrQueueChannel = null;
  29. private string CurrRecQueueName = "hello";//
  30. private string CurrStatusQueueName = "hello";//状态显示队列名称
  31. private string StatusExchange = "";//状态交换机名称
  32. private string StatusTotalExchange = "";
  33. private DateTime CurrSTime = DateTime.Now;//开始时间
  34. //private int CurrDbTime = 30;//提交数据到数据库的间接时间,以秒为单位
  35. private List<ReportDetail> CurrReportDetail = null;
  36. private List<MacOrder> CurrMacOrder = null;
  37. private List<OrderStatus> CurrOrderStatus = null;
  38. private List<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
  39. private IDatabase CurrDb = null;
  40. private int CurrStatus = -1;//未启动状态
  41. private string DbCode = "eap";
  42. public long CurrMaxBlock = 0;
  43. public string CurrShareFileDir = "";
  44. private string CurrProgramDir = "";
  45. //开始接收队列线程
  46. public int StartRecQueue(ref string errorinfo)
  47. {
  48. try
  49. {
  50. //清空接收队列
  51. //int result = ClearRecQueue(ref errorinfo);
  52. //if (result < 0)
  53. //{
  54. // errorinfo = "清空接收队列发生错误,错误信息为:" + errorinfo;
  55. // return -1;
  56. //}
  57. CurrQueueChannel.QueueDeclare(CurrRecQueueName, false, false, false, null);
  58. //BasicQos函数是针对通道的,必须放在注册消费者前执行,否则注册的消费者会接受多条信息
  59. CurrQueueChannel.BasicQos(0, 1, false);
  60. var consumer = new EventingBasicConsumer(CurrQueueChannel);
  61. CurrQueueChannel.BasicConsume(queue: CurrRecQueueName,
  62. autoAck: false, //是否不要手动应答(no manual Ack),ture自动应答,自动删除处理消息;false手动应答,服务器的消息会等待应答结果才消除
  63. consumer: consumer);
  64. consumer.Received += QueueReceive;
  65. CurrSTime = DateTime.Now;
  66. return 1;
  67. }
  68. catch (Exception ex)
  69. {
  70. errorinfo = ex.Message.ToString();
  71. return -1;
  72. }
  73. }
  74. //注册通道
  75. private IModel RegeditChannel(ref string errorinfo)
  76. {
  77. try
  78. {
  79. var factory = new ConnectionFactory();
  80. factory.HostName = AppConfigurtaionServices.Configuration["rabbitmq:IPAddress"];
  81. factory.UserName = AppConfigurtaionServices.Configuration["rabbitmq:UserName"];
  82. factory.Password = AppConfigurtaionServices.Configuration["rabbitmq:Password"];
  83. //CurrQueueName = AppConfigurtaionServices.Configuration["mqname:macrecmqname"];
  84. var connection = factory.CreateConnection();
  85. var channel = connection.CreateModel();
  86. return channel;
  87. }
  88. catch (Exception ex)
  89. {
  90. errorinfo = ex.Message.ToString();
  91. return null;
  92. }
  93. }
  94. private int ClearRecQueue(ref string errorinfo)
  95. {
  96. try
  97. {
  98. CurrQueueChannel.QueueDelete(CurrRecQueueName, true, false);
  99. return 1;
  100. }
  101. catch (Exception ex)
  102. {
  103. errorinfo = ex.Message.ToString();
  104. return -1;
  105. }
  106. }
  107. private void QueueReceive(object model, BasicDeliverEventArgs ea)
  108. {
  109. CurrCount++;
  110. //textBox2.Text = CurrCount.ToString();
  111. //SetText2(CurrCount.ToString());
  112. string errorinfo = "";
  113. int result = ProcessEvent(ea.Body, ref errorinfo);
  114. CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  115. if (result <= 0)
  116. {
  117. string logerrorinfo = "";
  118. //WriteLog.WriteLogStr("All", "", DateTime.Now, $"处理包发生错误,错误信息:{errorinfo}", ref logerrorinfo);
  119. SetText(errorinfo + "\r\n");
  120. }
  121. }
  122. private List<McaSecVDetail> ComParams(OrderDetail order, List<OrderData> lists, ref string errorinfo)
  123. {
  124. try
  125. {
  126. int sval = order.SVal;
  127. int fval = order.FVal;
  128. List<McaSecVDetail> tempdt = null;
  129. tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo);
  130. if (tempdt == null)
  131. {
  132. errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo;
  133. return null;
  134. }
  135. return tempdt;
  136. }
  137. catch (Exception ex)
  138. {
  139. errorinfo = ex.Message.ToString();
  140. return null;
  141. }
  142. }
  143. private int ProcessEvent(byte[] datas, ref string errorinfo)
  144. {
  145. try
  146. {
  147. OrderBlock entity = EntityHelper.DeSerializeBytes<OrderBlock>(datas);
  148. int result = entity.RecoverData(CurrShareFileDir, ref errorinfo);
  149. if (result <= 0)
  150. return -1;
  151. //如果不是S6F11,直接返回
  152. if (!(entity.CurrOrder.SVal == 6 && entity.CurrOrder.FVal == 11))
  153. return 1;
  154. //修改为实时从数据库中读取指令信息
  155. List<MacOrder> tempmacorders = CurrMacOrder.Where(t => t.MacFCode == entity.MainMsg.McaCode).ToList();
  156. if (tempmacorders.Count <= 0)
  157. {
  158. errorinfo = $"未找到机台【{entity.MainMsg.McaCode}】对应的指令信息。";
  159. return -1;
  160. }
  161. loger.LogError($"机台编号={entity.MainMsg.McaCode};开始处理:{entity.ToJson()}");
  162. string eventcode = HsmsUnity.GetEventCode(CurrDb, entity.MainMsg.McaCode, entity.CurrOrder, entity.Datalists, ref errorinfo);
  163. if (eventcode == "")
  164. return -1;
  165. List<McaSecVDetail> mcadetails = HsmsUnity.GetOrderS6F11Dt(entity.Datalists, CurrReportDetail, ref errorinfo);
  166. if (mcadetails == null)
  167. return -1;
  168. //if(entity.MainMsg.McaCode=="DIBD0049")
  169. //{
  170. // result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  171. // if (result <= 0)
  172. // return -1;
  173. //}
  174. List<McaSecVDetail> ttlists = mcadetails.Where(t => t.FCode == StandardCode.SVID_CurrentStatus).ToList();
  175. string status = ttlists[0].FVal;
  176. //if (entity.MainMsg.McaCode == "DIBD0038")
  177. //{
  178. // result = CallFunction.LotEnd(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  179. // if (result <= 0)
  180. // return -1;
  181. //}
  182. //if (eventcode== StandardCode.CEID_StatusChange && (status == "7" || status == "6"))
  183. //{
  184. // result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  185. // if (result <= 0)
  186. // return -1;
  187. // return 1;
  188. //}
  189. switch (eventcode)
  190. {
  191. case StandardCode.CEID_LotStart:
  192. loger.LogError($"接受到lot开始事件");
  193. result = CallFunction.LotStart(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  194. if (result <= 0)
  195. return -1;
  196. break;
  197. case StandardCode.CEID_LotEnd://lot完成
  198. loger.LogError($"接受到lot结束事件");
  199. result = CallFunction.LotEnd(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  200. if (result <= 0)
  201. return -1;
  202. break;
  203. case StandardCode.CEID_CollectLife://吸嘴清洗
  204. loger.LogError($"接受到lot吸嘴清洗事件");
  205. result = CallFunction.CollectLife(entity.MainMsg.McaCode, mcadetails, "", ref errorinfo);
  206. if (result <= 0)
  207. return -1;
  208. break;
  209. }
  210. return 1;
  211. }
  212. catch (Exception ex)
  213. {
  214. errorinfo = "IMcaSecVMst01" + ex.Message.ToString();
  215. return -1;
  216. }
  217. }
  218. private void Test()
  219. {
  220. string filepath = @"D:\eap\1N4148W-7-F.W1003FM-FW001.4CH_1";
  221. string errorinfo = "";
  222. byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo);
  223. byte[] tempbytes = new byte[200];
  224. if (filedatas.Length < 200)
  225. {
  226. tempbytes = filedatas;
  227. }
  228. else
  229. {
  230. Array.Copy(filedatas, 0, tempbytes, 0, 200);
  231. }
  232. //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  233. string programname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo);
  234. string filedir = $"d:\\aaaa\\{programname}";
  235. if (!Directory.Exists(@filedir))
  236. {
  237. Directory.CreateDirectory(@filedir);
  238. }
  239. }
  240. public void SetText(string str)
  241. {
  242. loger.LogError(str);
  243. if (CurrInfo.Count > 100)
  244. CurrInfo.Clear();
  245. int id = 1;
  246. if (CurrInfo.Count > 0)
  247. {
  248. id = CurrInfo.Max(t => t.ID);
  249. id++;
  250. }
  251. ServerInfo entity = new ServerInfo();
  252. entity.ID = id;
  253. entity.Info = str;
  254. CurrInfo.Add(entity);
  255. //写入日志
  256. }
  257. /// <summary>
  258. /// 启动RMS系统
  259. /// </summary>
  260. public int Start(ref string errorinfo)
  261. {
  262. //Test();
  263. //IDatabase CurrDb = null;
  264. try
  265. {
  266. Stop(ref errorinfo);
  267. if(errorinfo!="")
  268. {
  269. SetText($"停止服务发生错误,错误信息为:{errorinfo}");
  270. }
  271. CurrRecQueueName = AppConfigurtaionServices.Configuration["mqname:recmqname"];
  272. CurrStatusQueueName = AppConfigurtaionServices.Configuration["mqname:statusmqname"];
  273. StatusExchange = AppConfigurtaionServices.Configuration["mqname:statusexchange"];
  274. StatusTotalExchange = AppConfigurtaionServices.Configuration["mqname:StatusTotalExchange"];
  275. CurrDb = DbFactory.Base(CallFunction.CurrDbCode);
  276. CurrStatus = -1;
  277. CurrReportDetail =CurrDb.FindListForCondition<ReportDetail>("", ref errorinfo).ToList();
  278. if (CurrReportDetail == null)
  279. {
  280. SetText(errorinfo);
  281. return -1;
  282. }
  283. CurrMacOrder = CurrDb.FindListForCondition<MacOrder>("", ref errorinfo).ToList();
  284. if (CurrMacOrder == null)
  285. {
  286. SetText(errorinfo);
  287. return -1;
  288. }
  289. CurrOrderStatus = CurrDb.FindListForCondition<OrderStatus>("", ref errorinfo).ToList();
  290. if (CurrMacOrder == null)
  291. {
  292. SetText(errorinfo);
  293. return -1;
  294. }
  295. CurrQueueChannel = RegeditChannel(ref errorinfo);
  296. if(CurrQueueChannel==null)
  297. {
  298. SetText("注册队列通道发生错误:" + errorinfo);
  299. return -1;
  300. }
  301. int result = StartRecQueue(ref errorinfo);
  302. if (result < 0)
  303. {
  304. SetText("启动发生错误:"+errorinfo);
  305. return -1;
  306. }
  307. SetText("启动成功。");
  308. CurrStatus = 1;
  309. return 1;
  310. }
  311. catch (Exception ex)
  312. {
  313. SetText(ex.Message.ToString());
  314. return -1;
  315. }
  316. //finally
  317. //{
  318. // if (CurrDb != null)
  319. // CurrDb.Close();
  320. //}
  321. }
  322. public int GetStatus(ref string errorinfo)
  323. {
  324. if (CurrStatus <= 0 && CurrInfo.Count > 0)
  325. {
  326. errorinfo = CurrInfo.Last().Info;
  327. }
  328. return CurrStatus;
  329. }
  330. public long GetAccount()
  331. {
  332. return CurrCount;
  333. }
  334. public List<ServerInfo> GetInfo()
  335. {
  336. if(CurrInfo.Count<=100)
  337. return CurrInfo;
  338. List<ServerInfo> templist = new List<ServerInfo>();
  339. for (int i = CurrInfo.Count - 100; i < CurrInfo.Count; i++)
  340. templist.Add(CurrInfo[i]);
  341. return templist;
  342. }
  343. public int Stop(ref string errorinfo)
  344. {
  345. try
  346. {
  347. if (CurrDb != null)
  348. CurrDb.Close();
  349. CurrStatus = -1;
  350. if(CurrQueueChannel!=null)
  351. CurrQueueChannel.Close();
  352. SetText("停止服务成功。");
  353. return 1;
  354. }
  355. catch (Exception ex)
  356. {
  357. SetText(ex.Message.ToString());
  358. return -1;
  359. }
  360. }
  361. public int ClearInfo(ref string errorinfo)
  362. {
  363. try
  364. {
  365. CurrInfo.Clear();
  366. return 1;
  367. }
  368. catch(Exception ex)
  369. {
  370. SetText(ex.Message.ToString());
  371. return -1;
  372. }
  373. }
  374. }
  375. }