BusinessServer.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapDal;
  5. using DllEapDal.OFILM;
  6. using DllEapEntity;
  7. using DllHsms;
  8. using Microsoft.Extensions.Logging;
  9. using RabbitMQ.Client;
  10. using RabbitMQ.Client.Events;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Linq;
  15. using System.Text;
  16. namespace DllBusinessServer
  17. {
  18. public class BusinessServer:IBusinessServer
  19. {
  20. private ILogger loger = null;
  21. public BusinessServer(ILogger<BusinessServer> loger)
  22. {
  23. this.loger = loger;
  24. CurrMaxBlock = long.Parse(AppConfigurtaionServices.Configuration["MaxBlock"]);
  25. CurrShareFileDir = AppConfigurtaionServices.Configuration["ShareFileDir"];
  26. }
  27. private long CurrCount = 0;
  28. private IModel CurrQueueChannel = null;
  29. private string CurrRecQueueName = "hello";//接收队列名称 hsmsreceive+机台编号
  30. private DataSet CurrDs = null;
  31. private DateTime CurrSTime = DateTime.Now;//开始时间
  32. private int CurrDbTime = 30;//提交数据到数据库的间接时间,以秒为单位
  33. //private DataSet CurrSecMstDs = null;
  34. private List<ReportDetail> CurrReportDetail = null;
  35. private List<ServerInfo> CurrInfo = new List<ServerInfo>();//记录错误信息
  36. private int CurrStatus = -1;//未启动状态
  37. private string CurrShareFileDir = "";
  38. private long CurrMaxBlock = 0;
  39. //开始接收队列线程
  40. public int StartRecQueue(ref string errorinfo)
  41. {
  42. try
  43. {
  44. //注册通道
  45. CurrQueueChannel = RegeditChannel(ref errorinfo);
  46. if (CurrQueueChannel == null)
  47. {
  48. errorinfo = "注册队列通道发生错误,错误信息为:" + errorinfo;
  49. return -1;
  50. }
  51. //清空接收队列
  52. //int result = ClearRecQueue(ref errorinfo);
  53. //if (result < 0)
  54. //{
  55. // errorinfo = "清空接收队列发生错误,错误信息为:" + errorinfo;
  56. // return -1;
  57. //}
  58. CurrQueueChannel.QueueDeclare(CurrRecQueueName, false, false, false, null);
  59. //BasicQos函数是针对通道的,必须放在注册消费者前执行,否则注册的消费者会接受多条信息
  60. CurrQueueChannel.BasicQos(0, 1, false);
  61. var consumer = new EventingBasicConsumer(CurrQueueChannel);
  62. CurrQueueChannel.BasicConsume(queue: CurrRecQueueName,
  63. autoAck: false, //是否不要手动应答(no manual Ack),ture自动应答,自动删除处理消息;false手动应答,服务器的消息会等待应答结果才消除
  64. consumer: consumer);
  65. consumer.Received += QueueReceive;
  66. CurrSTime = DateTime.Now;
  67. return 1;
  68. }
  69. catch (Exception ex)
  70. {
  71. errorinfo = ex.Message.ToString();
  72. return -1;
  73. }
  74. }
  75. //注册通道
  76. private IModel RegeditChannel(ref string errorinfo)
  77. {
  78. try
  79. {
  80. var factory = new ConnectionFactory();
  81. factory.HostName = AppConfigurtaionServices.Configuration["rabbitmq:IPAddress"];
  82. factory.UserName = AppConfigurtaionServices.Configuration["rabbitmq:UserName"];
  83. factory.Password = AppConfigurtaionServices.Configuration["rabbitmq:Password"];
  84. CurrDbTime = int.Parse(AppConfigurtaionServices.Configuration["rabbitmq:dbtime"]);
  85. var connection = factory.CreateConnection();
  86. var channel = connection.CreateModel();
  87. return channel;
  88. }
  89. catch (Exception ex)
  90. {
  91. errorinfo = ex.Message.ToString();
  92. return null;
  93. }
  94. }
  95. private int ClearRecQueue(ref string errorinfo)
  96. {
  97. try
  98. {
  99. CurrQueueChannel.QueueDelete(CurrRecQueueName, true, false);
  100. return 1;
  101. }
  102. catch (Exception ex)
  103. {
  104. errorinfo = ex.Message.ToString();
  105. return -1;
  106. }
  107. }
  108. private void QueueReceive(object model, BasicDeliverEventArgs ea)
  109. {
  110. CurrCount++;
  111. //textBox2.Text = CurrCount.ToString();
  112. //SetText2(CurrCount.ToString());
  113. CurrCount++;
  114. string errorinfo = "";
  115. string maccode = "";
  116. int result = IMcaSecVMst01(ea.Body,ref maccode, ref errorinfo);
  117. if (result <= 0)
  118. {
  119. SetText(maccode,errorinfo);
  120. CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: true);
  121. }
  122. else
  123. {
  124. CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  125. }
  126. }
  127. //判断事件是否与最后事件相同,返回值为该机台最后事件记录ID值
  128. public int JudgeEventSame(DataSet orgds, McaSecVMst mst, List<McaSecVDetail> lists, ref string errorinfo)
  129. {
  130. try
  131. {
  132. //说明不是事件,直接返回
  133. if (mst.FType <= 0)
  134. {
  135. if (mst.FType == int.Parse(orgds.Tables["McaSecVMst"].Rows[0]["FType"].ToString()) && mst.FStatus == int.Parse(orgds.Tables["McaSecVMst"].Rows[0]["FStatus"].ToString()))
  136. return 1;
  137. return 0;
  138. }
  139. string mcacode = mst.McaCode;// mainrow["McaCode"].ToString();
  140. string eventcode = orgds.Tables["McaSecVMst"].Rows[0]["eventcode"].ToString();
  141. if (eventcode != mst.EventCode)
  142. return 0;
  143. //DataRow[] orgrows = ds.Tables["oab"].Select("", "参数代码");
  144. if (lists.Count != orgds.Tables["McaSecVDetail"].Rows.Count)
  145. return 0;
  146. DataRow[] orgrows = orgds.Tables["McaSecVDetail"].Select("", "fnum");
  147. for (int i = 0; i < orgrows.Length; i++)
  148. {
  149. if (orgrows[i]["fcode"].ToString() != lists[i].FCode)
  150. return 0;
  151. if (orgrows[i]["fval"].ToString() != lists[i].FVal)
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. catch (Exception ex)
  157. {
  158. errorinfo = "JudgeEventSame" + ex.Message.ToString();
  159. return -1;
  160. }
  161. }
  162. private Guid AddRowToCurrDs(DataRow desrow, McaSecVMst orgrow, ref string errorinfo)
  163. {
  164. try
  165. {
  166. Guid id = Guid.NewGuid();
  167. desrow["McaCode"] = orgrow.McaCode;
  168. desrow["ptime"] = orgrow.ptime;
  169. desrow["FType"] = orgrow.FType;
  170. desrow["FStatus"] = orgrow.FStatus;
  171. desrow["EventCode"] = orgrow.EventCode;
  172. desrow["EventID"] = orgrow.EventID;
  173. desrow["ID"] = id;
  174. if(orgrow.EventCode==StandardCode.CEID_EachFinish)
  175. {
  176. desrow["etime"] = orgrow.ptime;
  177. desrow["FCount"] = 0;
  178. }
  179. if (orgrow.FType<=0)
  180. {
  181. desrow["etime"] = orgrow.ptime;
  182. desrow["FCount"] = 0;
  183. }
  184. return id;
  185. }
  186. catch (Exception ex)
  187. {
  188. errorinfo = "AddRowToCurrDs" + ex.Message.ToString();
  189. return Guid.Empty;
  190. }
  191. }
  192. /// <summary>
  193. /// 将取到的事件数据添加到数据集里,原数据集里保存了最后事件数据
  194. /// </summary>
  195. /// <param name="ds"></param>
  196. /// <param name="errorinfo"></param>
  197. /// <returns></returns>
  198. private int AddDataToCurrDs01(McaSecVMst mst, List<McaSecVDetail> lists, UInt32 orgfnum, ref string errorinfo)
  199. {
  200. try
  201. {
  202. StringBuilder sqlstr = new StringBuilder(100);
  203. //sqlstr.AppendFormat($"McaCode='{mainrow["McaCode"].ToString()}' and EventID={mainrow["EventID"].ToString()} and FType={mainrow["FType"].ToString()}");
  204. //sqlstr.AppendFormat($"FStatus={mainrow["FStatus"].ToString()} and eventcode={mainrow["eventcode"].ToString()} and islast=1");
  205. sqlstr.AppendFormat($"McaCode='{mst.McaCode}' and islast=1");
  206. DataRow[] rows = CurrDs.Tables["mcasecvmst"].Select(sqlstr.ToString());
  207. int result = 0;
  208. DataRow addrow = null;
  209. Guid newid = Guid.Empty;
  210. if (rows.Length > 0)
  211. {
  212. DataSet tempds = CurrDs.Clone();
  213. tempds.Tables["mcasecvmst"].ImportRow(rows[0]);
  214. sqlstr.Clear();
  215. sqlstr.AppendFormat($"preid='{rows[0]["ID"]}'");
  216. DataRow[] ttrows = CurrDs.Tables["McaSecVDetail"].Select(sqlstr.ToString());
  217. foreach (DataRow temprow in ttrows)
  218. {
  219. tempds.Tables["McaSecVDetail"].ImportRow(temprow);
  220. }
  221. result = JudgeEventSame(tempds, mst, lists, ref errorinfo);
  222. if (result < 0)
  223. return -1;
  224. if (result == 0)
  225. {
  226. //说明要新增
  227. addrow = CurrDs.Tables["mcasecvmst"].NewRow();
  228. CurrDs.Tables["mcasecvmst"].Rows.Add(addrow);
  229. newid = AddRowToCurrDs(addrow, mst, ref errorinfo);
  230. if (newid == Guid.Empty)
  231. return -1;
  232. }
  233. else
  234. {
  235. addrow = rows[0];
  236. }
  237. }
  238. else
  239. {
  240. addrow = CurrDs.Tables["mcasecvmst"].NewRow();
  241. CurrDs.Tables["mcasecvmst"].Rows.Add(addrow);
  242. newid = AddRowToCurrDs(addrow, mst, ref errorinfo);
  243. if (newid == Guid.Empty)
  244. return -1;
  245. }
  246. if (newid != Guid.Empty && mst.FType > 0)
  247. {
  248. //说明新增了主档,需要复制明细
  249. foreach (var temprow in lists)
  250. {
  251. DataRow ttaddrow = CurrDs.Tables["McaSecVDetail"].NewRow();
  252. CurrDs.Tables["McaSecVDetail"].Rows.Add(ttaddrow);
  253. ttaddrow["fcode"] = temprow.FCode;
  254. ttaddrow["fval"] = temprow.FVal;
  255. ttaddrow["fnum"] = temprow.FNum;
  256. ttaddrow["preid"] = newid;
  257. }
  258. }
  259. //将该机台的所有 事件标记为不是最后事件
  260. sqlstr.Clear();
  261. sqlstr.AppendFormat($"McaCode='{mst.McaCode}'");
  262. rows = CurrDs.Tables["mcasecvmst"].Select(sqlstr.ToString());
  263. foreach (DataRow temprow in rows)
  264. temprow["islast"] = 0;
  265. //将新增的行置为最后行
  266. addrow["islast"] = 1;
  267. //添加跟踪记录,不管是否新增,这条记录都是要添加的
  268. DataRow detailrow = CurrDs.Tables["McaSecTime"].NewRow();
  269. CurrDs.Tables["McaSecTime"].Rows.Add(detailrow);
  270. detailrow["preid"] = addrow["id"];
  271. detailrow["ptime"] = mst.ptime;
  272. detailrow["OrgFNum"] = orgfnum;// mainrow["OrgFNum"];
  273. return 1;
  274. }
  275. catch (Exception ex)
  276. {
  277. errorinfo = ex.Message.ToString();
  278. return -1;
  279. }
  280. }
  281. private List<McaSecVDetail> ComParams(OrderDetail order, List<OrderData> lists, ref string errorinfo)
  282. {
  283. try
  284. {
  285. int sval = order.SVal;
  286. int fval = order.FVal;
  287. List<McaSecVDetail> tempdt = null;
  288. if (sval == 6 && fval == 11)
  289. {
  290. tempdt = HsmsUnity.GetOrderS6F11Dt(lists, CurrReportDetail, ref errorinfo);
  291. if (tempdt == null)
  292. {
  293. errorinfo = "GetOrderS6F11Dt函数错误:" + errorinfo;
  294. return null;
  295. }
  296. }
  297. else if (sval == 5 && fval == 1)
  298. {
  299. tempdt = HsmsUnity.GetOrderS5F1Dt(lists, ref errorinfo);
  300. if (tempdt == null)
  301. {
  302. errorinfo = "GetOrderS5F1Dt函数错误:" + errorinfo;
  303. return null;
  304. }
  305. }
  306. else
  307. {
  308. errorinfo = "未知指令。";
  309. return null;
  310. }
  311. return tempdt;
  312. }
  313. catch (Exception ex)
  314. {
  315. errorinfo = ex.Message.ToString();
  316. return null;
  317. }
  318. }
  319. //使用新的函数,将数据存入mysql
  320. private int IMcaSecVMst01(byte[] datas, ref string maccode, ref string errorinfo)
  321. {
  322. IDatabase db = null;
  323. try
  324. {
  325. OrderBlock entity = EntityHelper.DeSerializeBytes<OrderBlock>(datas);
  326. int result = entity.RecoverData(CurrShareFileDir, ref errorinfo);
  327. if (result <= 0)
  328. return -1;
  329. if(entity.MainMsg.McaCode=="DIBD0049")
  330. {
  331. int a = 100;
  332. }
  333. if (!(entity.CurrOrder.SVal == 5 && entity.CurrOrder.FVal == 1))
  334. return 1;
  335. string eventcode = HsmsUnity.GetEventCode(DbFactory.Base(DllSqlCoreAcc.SqlCoreAcc.CurrMySqlCode), entity.MainMsg.McaCode, entity.CurrOrder, entity.Datalists, ref errorinfo);
  336. entity.MainMsg.EventCode = eventcode;
  337. //数据转换
  338. maccode = entity.MainMsg.McaCode;
  339. int ftype = entity.MainMsg.FType;// int.Parse(orgds.Tables["McaSecVMst"].Rows[0]["FType"].ToString());
  340. OrderDetail orderdetail = entity.CurrOrder;
  341. List<OrderData> lists = entity.Datalists;
  342. List<McaSecVDetail> details = null;
  343. UInt32 orgfnum = 0;
  344. if (ftype > 0)
  345. {
  346. //orderdetail = entity.blockdata.GetDataContext(lists, ref errorinfo);
  347. details = ComParams(orderdetail, lists, ref errorinfo);
  348. if (details == null)
  349. return -1;
  350. orgfnum = UInt32.Parse(orderdetail.Remark);
  351. }
  352. db = DbFactory.Base(DllSqlCoreAcc.SqlCoreAcc.CurrMySqlCode);
  353. db.BeginTrans();
  354. McaSecVMstDal dal = new McaSecVMstDal(db);
  355. McaSecVMst mst = dal.BatMcaSecVMst(entity.MainMsg, details, orgfnum, "", ref errorinfo);
  356. if(eventcode== StandardCode.CEID_AlarmOccurred)
  357. {
  358. MtbacodeDal mtbacodeDal = new MtbacodeDal(db);
  359. mtbacodeDal.InsertMtabcode(entity.MainMsg, details, ref errorinfo);
  360. }
  361. if (mst == null)
  362. {
  363. db.Rollback();
  364. return -1;
  365. }
  366. db.Commit();
  367. return 1;
  368. }
  369. catch (Exception ex)
  370. {
  371. errorinfo = "IMcaSecVMst01" + ex.ToString();
  372. return -1;
  373. }
  374. finally
  375. {
  376. if (db != null)
  377. db.Close();
  378. }
  379. }
  380. //2019-10-28注销,改用新的方法,把数据存入mysql数据库
  381. //private int IMcaSecVMst01(byte[] datas,ref string maccode, ref string errorinfo)
  382. //{
  383. // try
  384. // {
  385. // OrderBlock entity = EntityHelper.DeSerializeBytes<OrderBlock>(datas);
  386. // //DataSet orgds = MsgPackage.MsgPackage.BytesToDataSet(datas, ref errorinfo);
  387. // //if (orgds == null)
  388. // // return -1;
  389. // //数据转换
  390. // maccode = entity.MainMsg.McaCode;
  391. // int ftype = entity.MainMsg.FType;// int.Parse(orgds.Tables["McaSecVMst"].Rows[0]["FType"].ToString());
  392. // OrderDetail orderdetail = entity.CurrOrder;
  393. // List<OrderData> lists = entity.Datalists;
  394. // List<McaSecVDetail> details = null;
  395. // UInt32 orgfnum = 0;
  396. // if (ftype > 0)
  397. // {
  398. // //orderdetail = entity.blockdata.GetDataContext(lists, ref errorinfo);
  399. // details = ComParams(orderdetail, lists, ref errorinfo);
  400. // if (details == null)
  401. // return -1;
  402. // orgfnum = UInt32.Parse(orderdetail.Remark);
  403. // }
  404. // //DataSet orgds = MsgPackage.MsgPackage.BytesToDataSet(datas, ref errorinfo);
  405. // //if (orgds == null)
  406. // // return -1;
  407. // ////数据转换
  408. // //int ftype = int.Parse(orgds.Tables["McaSecVMst"].Rows[0]["FType"].ToString());
  409. // //if (ftype > 0)
  410. // //{
  411. // // int ttresult = ComParams(orgds, ref errorinfo);
  412. // // if (ttresult <= 0)
  413. // // return -1;
  414. // //}
  415. // //如果是机器状态变化,变成运行中的状态,则要判断是否符合开始生产条件
  416. // //将数据添加到currds中
  417. // int result = AddDataToCurrDs01(entity.MainMsg, details, orgfnum, ref errorinfo);
  418. // if (result <= 0)
  419. // {
  420. // return -1;
  421. // }
  422. // //如果是料盒装载或卸载事件,则提交数据
  423. // switch(entity.MainMsg.EventCode)
  424. // {
  425. // case StandardCode.CEID_InBoxDown:
  426. // case StandardCode.CEID_InBoxLoad:
  427. // case StandardCode.CEID_OutBoxDown:
  428. // case StandardCode.CEID_OutBoxLoad:
  429. // result = SingleProcessCurrDs(ref errorinfo);
  430. // return result;
  431. // }
  432. // DateTime etime = DateTime.Now;
  433. // TimeSpan diff = etime - CurrSTime;
  434. // if ((diff.Minutes * 60 + diff.Seconds) > CurrDbTime)
  435. // {
  436. // //说明已经过了60秒没有提交数据库,需要提交
  437. // //SqlAcc mysqlacc = new SqlAcc();
  438. // result = SingleProcessCurrDs(ref errorinfo);
  439. // return result;
  440. // }
  441. // return 1;
  442. // }
  443. // catch (Exception ex)
  444. // {
  445. // errorinfo = "IMcaSecVMst01" + ex.Message.ToString();
  446. // return -1;
  447. // }
  448. //}
  449. /// <summary>
  450. /// 处理CurrDs,针对单个芯片完成事件,累加
  451. /// </summary>
  452. /// <param name="errorinfo"></param>
  453. /// <returns></returns>
  454. private int SingleProcessCurrDs(ref string errorinfo)
  455. {
  456. try
  457. {
  458. //处理单个芯片完成事件
  459. DataRow[] finish = CurrDs.Tables["mcasecvmst"].Select($"EventCode='{StandardCode.CEID_EachFinish}'");
  460. int vcount = 0;
  461. //object subcount = "";
  462. DataRow[] subrows = null;
  463. foreach(var row in finish)
  464. {
  465. vcount = string.IsNullOrEmpty(row["FCount"].ToString()) ? 0 : int.Parse(row["FCount"].ToString());
  466. //subcount=CurrDs.Tables["McaSecTime"].Compute("count(*)", $"PreID='{row["ID"].ToString()}'").ToString();
  467. //vcount += string.IsNullOrEmpty(subcount) ? 0 : int.Parse(subcount);
  468. //subcount = CurrDs.Tables["McaSecTime"].Compute("max(ptime)", $"PreID='{row["ID"].ToString()}'").ToString();
  469. //if(string.IsNullOrEmpty(subcount.ToString()))
  470. //{
  471. // //说明是以前的数据
  472. // row["etime"] = row["ptime"];
  473. // row["FCount"] = 1;
  474. //}
  475. //else
  476. //{
  477. // row["etime"] =subcount;
  478. //}
  479. //删除芯片完成操作明细
  480. subrows = CurrDs.Tables["McaSecTime"].Select( $"PreID='{row["ID"].ToString()}'", "ptime");
  481. //if(subrows.Length<=0) 这个地方不对,如果当前操作已经存在数量,而此次没有打的事件,那么就会把当前数量置为1,
  482. //{
  483. // row["etime"] = row["ptime"];
  484. // row["FCount"] = 1;
  485. //}
  486. //else
  487. //{
  488. // row["etime"] = subrows[subrows.Length-1]["ptime"];
  489. //}
  490. if(subrows.Length>0)
  491. {
  492. vcount += subrows.Length;
  493. row["FCount"] = vcount;
  494. row["etime"] = subrows[subrows.Length - 1]["ptime"];
  495. }
  496. //删除芯片完成操作明细记录,机台WKB-076,WKB-075的明细继续保存,以便验证
  497. if (row["McaCode"].ToString().ToLower() == "wkb-076" || row["McaCode"].ToString().ToLower() == "wkb-075")
  498. continue;
  499. foreach (DataRow ttrow in subrows)
  500. ttrow.Delete();
  501. }
  502. //删除ftype的操作明细记录
  503. DataRow[] FTyperows = CurrDs.Tables["mcasecvmst"].Select($"FType<=0");
  504. foreach (var row in FTyperows)
  505. {
  506. vcount = string.IsNullOrEmpty(row["FCount"].ToString()) ? 0 : int.Parse(row["FCount"].ToString());
  507. //删除芯片完成操作明细
  508. subrows = CurrDs.Tables["McaSecTime"].Select($"PreID='{row["ID"].ToString()}'", "ptime");
  509. if (subrows.Length > 0)
  510. {
  511. vcount += subrows.Length;
  512. row["FCount"] = vcount;
  513. row["etime"] = subrows[subrows.Length - 1]["ptime"];
  514. }
  515. //删除芯片完成操作明细记录
  516. foreach (DataRow ttrow in subrows)
  517. ttrow.Delete();
  518. }
  519. DllSqlCoreAcc.SqlCoreAcc mysqlacc = new DllSqlCoreAcc.SqlCoreAcc();
  520. int result = mysqlacc.BatIMcaSecVMst01(CurrDs, ref errorinfo);
  521. CurrSTime = DateTime.Now;
  522. if (result <= 0)
  523. {
  524. //需要将数据保存成文件,供以后处理
  525. }
  526. //清空CurrDs
  527. DataRow[] temprows = CurrDs.Tables["mcasecvmst"].Select("islast=0");
  528. foreach (DataRow temprow in temprows)
  529. {
  530. //删除McaSecVDetail表记录
  531. DataRow[] ttrows = CurrDs.Tables["McaSecVDetail"].Select("preid='" + temprow["ID"].ToString() + "'");
  532. foreach (DataRow ttrow in ttrows)
  533. ttrow.Delete();
  534. temprow.Delete();
  535. }
  536. CurrDs.Tables["McaSecTime"].Clear();
  537. CurrDs.AcceptChanges();
  538. CurrSTime = DateTime.Now;
  539. return 1;
  540. }
  541. catch(Exception ex)
  542. {
  543. errorinfo = ex.Message.ToString();
  544. return -1;
  545. }
  546. }
  547. /// <summary>
  548. /// 如果是机器状态变化,变成运行中的状态,则要判断是否符合开始生产条件
  549. /// </summary>
  550. /// <param name="datas"></param>
  551. /// <param name="errorinfo"></param>
  552. /// <returns></returns>
  553. private int MacIsRun(DataSet datads, ref string errorinfo)
  554. {
  555. try
  556. {
  557. //数据转换
  558. int ftype = int.Parse(datads.Tables["McaSecVMst"].Rows[0]["FType"].ToString());
  559. if (ftype <= 0)
  560. return 1;
  561. DataRow orderrow = datads.Tables["oaa"].Rows[0];
  562. int sval = int.Parse(orderrow["S值"].ToString());
  563. int fval = int.Parse(orderrow["F值"].ToString());
  564. if (!(sval == 6 && fval == 11))
  565. return 1;
  566. string eventcode = datads.Tables["McaSecVMst"].Rows[0]["eventcode"].ToString();
  567. if (eventcode != "C00009")
  568. return 1;
  569. DataRow[] rows = datads.Tables["oab"].Select("参数代码='S00004'");
  570. if (rows.Length <= 0)
  571. {
  572. errorinfo = "未找到当前状态参数信息。";
  573. return -1;
  574. }
  575. int val = int.Parse(rows[0]["数据内容"].ToString());
  576. if (val != 1)
  577. return 1;
  578. string maccode = datads.Tables["McaSecVMst"].Rows[0]["机台编号"].ToString();
  579. //说明当前机台状态为开始生产状态,这时候需要判断程序名称是否正确
  580. if (JudgeMacIsReady(maccode, ref errorinfo))
  581. return 1;
  582. //说明机台不符合生产条件,则发送停机指令
  583. return 1;
  584. }
  585. catch (Exception ex)
  586. {
  587. errorinfo = "MacIsRun" + ex.Message.ToString();
  588. return -1;
  589. }
  590. }
  591. /// <summary>
  592. /// 判断机台是否已经准备好生产
  593. /// </summary>
  594. /// <param name="maccode"></param>
  595. /// <param name="errorinfo"></param>
  596. /// <returns></returns>
  597. private bool JudgeMacIsReady(string maccode, ref string errorinfo)
  598. {
  599. try
  600. {
  601. return true;
  602. }
  603. catch (Exception ex)
  604. {
  605. errorinfo = ex.Message.ToString();
  606. return false;
  607. }
  608. }
  609. public void SetText(string maccode,string str)
  610. {
  611. loger.LogError($"机台编号={maccode};{str}");
  612. if (CurrInfo.Count > 100)
  613. CurrInfo.RemoveAt(0);
  614. int id = 1;
  615. if (CurrInfo.Count > 0)
  616. {
  617. id = CurrInfo.Max(t => t.ID);
  618. id++;
  619. }
  620. ServerInfo entity = new ServerInfo();
  621. entity.ID = id;
  622. entity.Info = str;
  623. CurrInfo.Add(entity);
  624. ////写入日志
  625. //string errorinfo = "";
  626. //WriteLog.WriteServicesLogStr(maccode, DateTime.Now, str, ref errorinfo);
  627. }
  628. public int Start(ref string errorinfo)
  629. {
  630. IDatabase CurrDb = null;
  631. try
  632. {
  633. CurrStatus = -1;
  634. CurrRecQueueName = AppConfigurtaionServices.Configuration["rabbitmq:recmqname"];
  635. //DllSqlCoreAcc.SqlCoreAcc mysqlacc = new DllSqlCoreAcc.SqlCoreAcc();
  636. //CurrDs = mysqlacc.ReadLastMcaSecVMst(ref errorinfo);
  637. //if (CurrDs == null)
  638. //{
  639. // //MessageBox.Show("读取数据失败:" + errorinfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  640. // errorinfo = $"读取数据失败,错误信息:{errorinfo}";
  641. // SetText("",errorinfo);
  642. // return -1;
  643. //}
  644. CurrDb = DbFactory.Base(DllSqlCoreAcc.SqlCoreAcc.CurrMySqlCode);
  645. CurrReportDetail = CurrDb.FindListForCondition<ReportDetail>("", ref errorinfo).ToList();
  646. if (CurrReportDetail == null)
  647. {
  648. errorinfo = $"读取数据失败,错误信息:{errorinfo}";
  649. SetText("", errorinfo);
  650. return -1;
  651. }
  652. int result = StartRecQueue(ref errorinfo);
  653. if (result < 0)
  654. {
  655. //MessageBox.Show("启动接受失败!" + errorinfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  656. errorinfo = $"启动消息队列失败,错误信息:{errorinfo}";
  657. SetText("", errorinfo);
  658. return -1;
  659. }
  660. CurrStatus = 1;
  661. SetText("","启动成功。");
  662. return 1;
  663. }
  664. catch(Exception ex)
  665. {
  666. errorinfo = ex.Message.ToString();
  667. SetText("", errorinfo);
  668. return -1;
  669. }
  670. }
  671. public long GetAccount()
  672. {
  673. return CurrCount;
  674. }
  675. public int GetStatus(ref string errorinfo)
  676. {
  677. if (CurrStatus <= 0 && CurrInfo.Count > 0)
  678. {
  679. errorinfo = CurrInfo.Last().Info;
  680. }
  681. return CurrStatus;
  682. }
  683. //public List<ServerInfo> GetInfo()
  684. //{
  685. // return CurrInfo;
  686. //}
  687. public int Stop(ref string errorinfo)
  688. {
  689. try
  690. {
  691. CurrStatus = -1;
  692. if (CurrQueueChannel != null)
  693. CurrQueueChannel.Close();
  694. SetText("","停止服务成功。");
  695. return 1;
  696. }
  697. catch (Exception ex)
  698. {
  699. SetText("",ex.Message.ToString());
  700. return -1;
  701. }
  702. }
  703. public List<ServerInfo> GetInfo()
  704. {
  705. if (CurrInfo.Count <= 100)
  706. return CurrInfo;
  707. List<ServerInfo> templist = new List<ServerInfo>();
  708. for (int i = CurrInfo.Count - 100; i < CurrInfo.Count; i++)
  709. templist.Add(CurrInfo[i]);
  710. return templist;
  711. }
  712. public int ClearInfo(ref string errorinfo)
  713. {
  714. try
  715. {
  716. CurrInfo.Clear();
  717. return 1;
  718. }
  719. catch (Exception ex)
  720. {
  721. SetText("",ex.Message.ToString());
  722. return -1;
  723. }
  724. }
  725. }
  726. }