ScanCore.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapDal;
  5. using DllEapEntity;
  6. using RabbitMQ.Client;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Linq;
  14. using Microsoft.Extensions.Logging;
  15. namespace DllScanForFiles
  16. {
  17. public class ScanCore
  18. {
  19. private ILogger loger = null;
  20. public ScanCore(ILogger<ScanCore> loger)
  21. {
  22. this.loger = loger;
  23. }
  24. private Thread SocketRecThread = null;//socket接受数据线程
  25. private ScanSocket CurrHsms = null;
  26. public MacOrder CurrMac = null;//机台数据
  27. public List<ConstItem> CurrItemDt { set; get; } = null;//固定项表
  28. //public string CurrLogQueueName="";
  29. //private IModel CurrQueueChannel = null;
  30. private string CurrFileDir = "";//日志文件主路径
  31. public bool IsRun = false;//检查线程是否在运行
  32. public bool SocketIsRun = false;//socket接受线程是否在运行
  33. //private string CurrRecQueueName = "";//接收队列名称 hsmsreceive+机台编号
  34. //private string CurrSendQueueName = "";//发送队列名称 hsmssend+机台编号
  35. public delegate void SetText(string str);
  36. public event SetText eventSetText = null;
  37. private string CurrLogFile = "";
  38. //装载或卸载事件交换机
  39. private string LOrDExchange = "";
  40. //private string CurrStatusShowQueueName = "";//状态显示队列
  41. //private Thread CheckSocketThread = null;//检查socket是否正常的线程
  42. //运行中发生错误信息
  43. public delegate void SetErrorText(int id, string errorinfo);
  44. public event SetErrorText eventSetErrorText = null;
  45. private Thread CheckThread = null;//检查线程
  46. //private bool ReadDbStatus = false;//是否正确读取配置文件
  47. //private bool QueueStatus = false;//消息队列是否注册成功
  48. //private long LastRecTick = 0;//记录最后接受机台数据时间戳
  49. private bool RecordSend = false;//标记开始记录发送包
  50. public ScanCore(MacOrder mac, ILogger loger)
  51. {
  52. CurrMac = mac;
  53. this.loger = loger;
  54. //CurrLogQueueName = AppConfigurtaionServices.Configuration["mqname:scanlogmqname"];
  55. }
  56. public int SetCurrID(int id, ref string errorinfo)
  57. {
  58. return 1;
  59. }
  60. public int ShutDown(ref string errorinfo)
  61. {
  62. try
  63. {
  64. IsRun = false;
  65. SocketIsRun = false;
  66. //关闭连接
  67. ShutDownSocket(ref errorinfo);
  68. return 1;
  69. }
  70. catch (Exception ex)
  71. {
  72. errorinfo = ex.Message.ToString();
  73. return -1;
  74. }
  75. }
  76. //记录程序内部错误日志
  77. private void WriteErrorInfoLog(string errorinfo)
  78. {
  79. StringBuilder sqlstr = new StringBuilder(100);
  80. try
  81. {
  82. //if (CurrLogFile != "")
  83. //{
  84. // DllUnity.Unity.WriteLog(CurrLogFile, errorinfo);
  85. //}
  86. //eventSetErrorText?.Invoke(CurrMac.ID, errorinfo);
  87. string temperrorinfo = "";
  88. ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
  89. int result = WriteLogToQueue(node, ref temperrorinfo);
  90. if (result <= 0)
  91. {
  92. sqlstr.AppendFormat("\r\n");
  93. sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  94. sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
  95. sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
  96. //写入本地错误日志
  97. WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. eventSetErrorText?.Invoke(CurrMac.MacID, ex.Message.ToString());
  103. sqlstr.AppendFormat("\r\n");
  104. sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  105. sqlstr.AppendFormat("错误信息:{0}\r\n", ex.Message.ToString());
  106. WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
  107. }
  108. }
  109. //将日志写到消息队列中
  110. //总的启动
  111. //private IModel RegeditChannel(ref string errorinfo)
  112. //{
  113. // try
  114. // {
  115. // var factory = new ConnectionFactory();
  116. // factory.HostName = AppConfigurtaionServices.Configuration["rabbitmq:IPAddress"];
  117. // factory.UserName = AppConfigurtaionServices.Configuration["rabbitmq:UserName"];
  118. // factory.Password = AppConfigurtaionServices.Configuration["rabbitmq:Password"];
  119. // var connection = factory.CreateConnection();
  120. // var channel = connection.CreateModel();
  121. // return channel;
  122. // }
  123. // catch (Exception ex)
  124. // {
  125. // errorinfo = ex.Message.ToString();
  126. // return null;
  127. // }
  128. //}
  129. public int Start(ref string errorinfo)
  130. {
  131. //string errorinfo = "";
  132. CurrLogFile = CreateLogFile(CurrMac.MacID, CurrMac.MacFCode, ref errorinfo);
  133. if (errorinfo != "")
  134. {
  135. return -1;
  136. }
  137. StringBuilder sqlstr = new StringBuilder(100);
  138. //消息队列必须正常连接,如果无法连接则不能启动程序
  139. //CurrQueueChannel = RegeditChannel(ref errorinfo);
  140. //if (CurrQueueChannel == null)
  141. //{
  142. // sqlstr.Clear();
  143. // sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  144. // sqlstr.AppendFormat("初始化队列发生错误\r\n");
  145. // sqlstr.AppendFormat("{0}", errorinfo);
  146. // WriteErrorInfoLog(sqlstr.ToString());
  147. // return -1;
  148. //}
  149. int result = StartCheckThread(ref errorinfo);
  150. if (result <= 0)
  151. {
  152. IsRun = false;
  153. //处理接受失败,写入数据库
  154. sqlstr.Clear();
  155. sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  156. sqlstr.AppendFormat("开始检查线程发生错误\r\n");
  157. sqlstr.AppendFormat("{0}", errorinfo);
  158. WriteErrorInfoLog(sqlstr.ToString());
  159. return -1;
  160. }
  161. return 1;
  162. }
  163. //启动总的检查线程,此线程应该一直运行,直到程序退出
  164. public int StartCheckThread(ref string errorinfo)
  165. {
  166. try
  167. {
  168. IsRun = true;
  169. //成功后,启动接受线程
  170. CheckThread = new Thread(CheckThreadFun);
  171. CheckThread.Start();
  172. return 1;
  173. }
  174. catch (Exception ex)
  175. {
  176. IsRun = false;
  177. errorinfo = ex.Message.ToString();
  178. return -1;
  179. }
  180. }
  181. public int StopCheckThread(ref string errorinfo)
  182. {
  183. try
  184. {
  185. IsRun = false;
  186. SocketIsRun = false;
  187. ShutDownSocket(ref errorinfo);
  188. if (CheckThread == null)
  189. return 1;
  190. //CheckThread.Interrupt();//中断线程
  191. //CheckThread.IsBackground = true;//让后台处理
  192. CheckThread = null;
  193. return 1;
  194. }
  195. catch (Exception ex)
  196. {
  197. errorinfo = ex.Message.ToString();
  198. return -1;
  199. }
  200. }
  201. private void CheckThreadFun()
  202. {
  203. try
  204. {
  205. StringBuilder sqlstr = new StringBuilder(100);
  206. int sleeptime = int.Parse(CurrItemDt[0].FCode);
  207. string errorinfo = "";
  208. string closesocketerror = "";
  209. while (IsRun)
  210. {
  211. errorinfo = "";
  212. closesocketerror = "";
  213. int result = CheckThreadFunFor(ref errorinfo);
  214. if (result <= 0)
  215. {
  216. //发生了错误,需要清理
  217. SocketIsRun = false;
  218. ShutDownSocket(ref closesocketerror);
  219. Thread.Sleep(1000 * 5);//休眠5秒钟
  220. sqlstr.Clear();
  221. sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  222. sqlstr.AppendFormat("检查线程发生错误\r\n");
  223. sqlstr.AppendFormat("{0}", errorinfo);
  224. if (closesocketerror != "")
  225. {
  226. sqlstr.AppendFormat("关闭SOCKET错误:{0}", closesocketerror);
  227. }
  228. eventSetErrorText?.Invoke(CurrMac.ID, errorinfo);
  229. WriteErrorInfoLog(sqlstr.ToString());
  230. if (result == -11)//说明是验证指令出现错误,需要休眠
  231. {
  232. Thread.Sleep(1000 * 60);
  233. }
  234. //Thread.Sleep(sleeptime);
  235. }
  236. else
  237. {
  238. //说明是重新连接后的,需要刷新界面状态
  239. if (result != 100)
  240. {
  241. eventSetErrorText?.Invoke(CurrMac.ID, "正常接受数据");
  242. }
  243. //发送测试包
  244. result = SendLinkTest(ref errorinfo);
  245. if (result <= 0)
  246. {
  247. sqlstr.Clear();
  248. sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  249. sqlstr.AppendFormat("发送连接测试错误:\r\n");
  250. sqlstr.AppendFormat("{0}", errorinfo);
  251. eventSetErrorText?.Invoke(CurrMac.MacID, errorinfo);
  252. WriteErrorInfoLog(sqlstr.ToString());
  253. ShutDownSocket(ref errorinfo);
  254. }
  255. }
  256. Thread.Sleep(sleeptime);
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. IsRun = false;
  262. WriteErrorInfoLog("检查线程出错:" + ex.Message.ToString());
  263. }
  264. }
  265. //初始化连接线程函数
  266. private int CheckThreadFunFor(ref string errorinfo)
  267. {
  268. try
  269. {
  270. int result = 0;
  271. //判断socket是否正常通行
  272. if (CurrHsms == null)
  273. {
  274. //修改状态为开始连接机台
  275. RecordSend = false;
  276. CurrHsms = new ScanSocket();
  277. bool init = CurrHsms.InitSocket(CurrMac.ScanIp, CurrMac.FPort, ref errorinfo);
  278. if (!init)
  279. {
  280. errorinfo = "初始化连接发生错误:" + errorinfo;
  281. return -1;
  282. }
  283. if (!IsRun)
  284. {
  285. errorinfo = "检查线程已终止。";
  286. return -1;
  287. }
  288. //设置SOCKET超时时间,防止接受不到数据
  289. CurrHsms.SetReceiveTimeout(1000 * 15);
  290. CurrHsms.SetSendTimeout(1000 * 15);
  291. result = StartSocketRecThread(ref errorinfo);
  292. if (result < 0)
  293. {
  294. errorinfo = "启动接受线程发生错误:" + errorinfo;
  295. return -1;
  296. }
  297. RecordSend = true;
  298. //设置SOCKET超时时间,一直接受
  299. CurrHsms.SetReceiveTimeout(0);
  300. CurrHsms.SetSendTimeout(1000 * 30);//设置发送超时为30秒
  301. return 1;
  302. }
  303. else
  304. {
  305. //检查是否存在超时未回复的包,如果存在则关闭socket连接
  306. result = CheckList(10, ref errorinfo);
  307. if (result <= 0)
  308. {
  309. errorinfo = "启动接受线程检查数据包错误发生错误:" + errorinfo;
  310. return -1;
  311. }
  312. return 100;
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. errorinfo = ex.Message.ToString();
  318. return -1;
  319. }
  320. }
  321. //发送测试连接包
  322. private int SendLinkTest(ref string errorinfo)
  323. {
  324. try
  325. {
  326. return 1;
  327. }
  328. catch (Exception ex)
  329. {
  330. errorinfo = ex.Message.ToString();
  331. return -1;
  332. }
  333. }
  334. //检查已发送的数据,是否在规定的时间内得到回复
  335. private int CheckList(int timeout, ref string errorinfo)
  336. {
  337. try
  338. {
  339. //long start = DateTime.Now.Ticks;
  340. //long end = start + timeout * 10000000;
  341. //for (int i = 0; i < CurrSendList.Count; i++)
  342. //{
  343. // start = CurrSendList[i].SendTime;
  344. // end = start + timeout * 10000000;
  345. // if (DateTime.Now.Ticks > end)
  346. // {
  347. // errorinfo = "存在超时未收到响应包。" + CurrSendList[i].CurrOrder.FCode;
  348. // return -1;
  349. // }
  350. //}
  351. return 1;
  352. }
  353. catch (Exception ex)
  354. {
  355. errorinfo = ex.Message.ToString();
  356. return -1;
  357. }
  358. }
  359. public int StartSocketRecThread(ref string errorinfo)
  360. {
  361. try
  362. {
  363. //if (SocketRecThread != null)
  364. //{
  365. // SocketRecThread.Abort();
  366. //}
  367. SocketIsRun = true;
  368. //成功后,启动接受线程
  369. SocketRecThread = new Thread(SocketRecThreadFun);
  370. SocketRecThread.Start();
  371. return 1;
  372. }
  373. catch (Exception ex)
  374. {
  375. SocketIsRun = false;
  376. errorinfo = ex.Message.ToString();
  377. return -1;
  378. }
  379. }
  380. //关闭连接
  381. private bool ShutDownSocket(ref string errorinfo)
  382. {
  383. try
  384. {
  385. SocketIsRun = false;
  386. CurrHsms.ShutDownSocket(ref errorinfo);
  387. CurrHsms = null;
  388. ////上报断开连接
  389. //McaSecVMst msgdt = GetMsgDt(CurrMac.MacFCode, DateTime.Now, 0, 0, -1, -1, "", ref errorinfo);
  390. //if (msgdt == null)
  391. // return false;
  392. //OrderBlock tempentity = new OrderBlock(msgdt);
  393. //int result = WriteBusinessToQueue(CurrMesQueueName, tempentity, ref errorinfo);
  394. //if (result <= 0)
  395. // return false;
  396. ////断开后,需要报告给状态管理系统,标志机台状态已经断开
  397. //result = WriteBusinessToQueue(CurrMacStatusQueueName, tempentity, ref errorinfo);
  398. //if (result <= 0)
  399. // return false;
  400. string temperrorinfo = "";
  401. ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
  402. int result = WriteLogToQueue(node, ref temperrorinfo);
  403. if (result <= 0)
  404. {
  405. StringBuilder sqlstr = new StringBuilder(100);
  406. sqlstr.AppendFormat("\r\n");
  407. sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  408. sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
  409. sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
  410. //写入本地错误日志
  411. WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
  412. }
  413. return true;
  414. }
  415. catch (Exception ex)
  416. {
  417. errorinfo = ex.Message.ToString();
  418. return false;
  419. }
  420. }
  421. private string CreateLogFile(int mcaid, string code, ref string errorinfo)
  422. {
  423. try
  424. {
  425. string sFilePath = System.IO.Directory.GetCurrentDirectory() + "\\log" + DateTime.Now.ToString("yyyyMMdd");
  426. string sFileName = mcaid.ToString() + "_" + code + "_" + DateTime.Now.ToString("HHmmss") + ".log";
  427. sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
  428. if (!Directory.Exists(sFilePath))//验证路径是否存在
  429. {
  430. Directory.CreateDirectory(sFilePath);
  431. //不存在则创建
  432. }
  433. return sFileName;
  434. }
  435. catch (Exception ex)
  436. {
  437. errorinfo = ex.Message.ToString();
  438. return "";
  439. }
  440. }
  441. private void SocketRecThreadFun()
  442. {
  443. StringBuilder sqlstr = new StringBuilder(100);
  444. try
  445. {
  446. int result = 0;
  447. string errorinfo = "";
  448. while (IsRun && SocketIsRun)
  449. {
  450. errorinfo = "";
  451. result = TheadFunNew(ref errorinfo);
  452. //if(errorinfo!="")
  453. //{
  454. // //说明发生了错误,先写日志,然后停止该线程
  455. //}
  456. //if (result == -11)
  457. //{
  458. // //socket错误
  459. //}
  460. if (result <= 0)
  461. {
  462. //sqlstr.Clear();
  463. //sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  464. //sqlstr.AppendFormat("接受数据线程发生错误\r\n");
  465. //sqlstr.AppendFormat("{0}", errorinfo);
  466. //WriteErrorInfoLog(errorinfo);
  467. ShutDownSocket(ref errorinfo);
  468. ScanBlock node = new ScanBlock("", CurrMac.MacFCode, DateTime.Now);
  469. node.Remark = errorinfo;
  470. string temperrorinfo = "";
  471. result = WriteLogToQueue(node, ref temperrorinfo);
  472. if (result <= 0)
  473. {
  474. sqlstr.AppendFormat("\r\n");
  475. sqlstr.AppendFormat("写日志到MQ发生错误:{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  476. sqlstr.AppendFormat("原始错误信息:{0}\r\n", errorinfo);
  477. sqlstr.AppendFormat("错误信息:{0}\r\n", temperrorinfo);
  478. //写入本地错误日志
  479. //WriteLog.WriteLogFor(CurrLogFile, sqlstr.ToString());
  480. WriteErrorInfoLog(sqlstr.ToString());
  481. }
  482. break;
  483. }
  484. // isrun = false;
  485. }
  486. }
  487. catch (Exception ex)
  488. {
  489. sqlstr.Clear();
  490. sqlstr.AppendFormat("{0}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  491. sqlstr.AppendFormat("接受数据线程发生错误\r\n");
  492. sqlstr.AppendFormat("{0}", ex.Message.ToString());
  493. WriteErrorInfoLog(sqlstr.ToString());
  494. //启动线程出错
  495. }
  496. //处理接受线程停止错误
  497. }
  498. //线程处理函数
  499. private int TheadFunNew(ref string errorinfo)
  500. {
  501. try
  502. {
  503. ScanBlock node = ReceiveBlock(ref errorinfo);
  504. if (node == null)
  505. return -1;
  506. //LastRecTick = DateTime.Now.Ticks;//更新时间戳
  507. //将从机台收到的数据打包发送给消息队列
  508. WriteLogToQueue($"开始处理......", ref errorinfo);
  509. int result = ProcessBlock(node, ref errorinfo);
  510. if (result <= 0)
  511. {
  512. errorinfo = "调程发生错误,错误信息为:" + errorinfo;
  513. string temperrorinfo = "";
  514. node.Remark = errorinfo;
  515. WriteLogToQueue(node, ref temperrorinfo);
  516. errorinfo = "";
  517. return 100;
  518. }
  519. string temperrorinfo1 = "";
  520. node.Remark = $"download success";
  521. WriteLogToQueue(node, ref temperrorinfo1);
  522. return 1;
  523. }
  524. catch (Exception ex)
  525. {
  526. errorinfo = ex.ToString();
  527. return -1;
  528. }
  529. }
  530. ////处理块 20200415
  531. //private int ProcessBlock(ScanBlock sendentity, ref string errorinfo)
  532. //{
  533. // IDatabase db = null;
  534. // try
  535. // {
  536. // //调程
  537. // db = DbFactory.Base("eap");
  538. // db.BeginTrans();
  539. // string scanstr = sendentity.ScanStr.Replace('\r',' ').Trim();
  540. // //MesDal mesdal = new MesDal();
  541. // //string programname = mesdal.GetLotProgramName(scanstr, ref errorinfo);
  542. // //if(programname=="")
  543. // //{
  544. // // errorinfo = $"从MES库中获取程序名称出错:{errorinfo},lot={scanstr}";
  545. // // MacOrderSendDal senddal = new MacOrderSendDal(db);
  546. // // string temperrorinfo = "";
  547. // // senddal.SendS10F3(CurrMac.MacFCode, $"Read recipe from MES error [{scanstr}]", ref temperrorinfo);
  548. // // return -1;
  549. // //}
  550. // //WriteLogToQueue($"程序名称:{programname}", ref errorinfo);
  551. // errorinfo = "";
  552. // ProgramDal dal = new ProgramDal(db);
  553. // int result = dal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
  554. // if (result <= 0)
  555. // {
  556. // db.Rollback();
  557. // MacOrderSendDal senddal = new MacOrderSendDal(db);
  558. // string temperrorinfo = "";
  559. // senddal.SendS10F3(CurrMac.MacFCode, $"Download recipe[{scanstr}] faile", ref temperrorinfo);
  560. // return -1;
  561. // }
  562. // //if(result>-10&&result<=0)
  563. // //{
  564. // // MacOrderSendDal senddal = new MacOrderSendDal(db);
  565. // // string temperrorinfo = "";
  566. // // senddal.SendS10F3(CurrMac.MacFCode, $"Can not found recipe", ref temperrorinfo);
  567. // //}
  568. // db.Commit();
  569. // return 1;
  570. // }
  571. // catch (Exception ex)
  572. // {
  573. // db.Rollback();
  574. // errorinfo = ex.Message.ToString();
  575. // return -1;
  576. // }
  577. // finally
  578. // {
  579. // if (db != null)
  580. // db.Close();
  581. // }
  582. //}
  583. private int ProcessBlock(ScanBlock sendentity, ref string errorinfo)
  584. {
  585. IDatabase db = null;
  586. try
  587. {
  588. //调程
  589. db = DbFactory.Base("eap");
  590. //db.BeginTrans();
  591. string scanstr = sendentity.ScanStr.Replace('\r', ' ').Trim();
  592. FilesProgramDal dal = new FilesProgramDal(db);
  593. return dal.DownloadProgram(CurrMac.MacFCode, scanstr, ref errorinfo);
  594. //int result = 0;
  595. //string condition = $" and a.id={CurrMac.MacID}";
  596. //List<Machine> macs = db.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
  597. //if(macs==null||macs.Count<=0)
  598. //{
  599. // errorinfo = "未找到机台信息。";
  600. // return -1;
  601. //}
  602. //switch (CurrMac.MacModelFCode.ToLower())
  603. //{
  604. // case "dad3350":
  605. // case "dfd6341":
  606. // Dad3350ProgramDal dal = new Dad3350ProgramDal(db);
  607. // result = dal.DownloadProgram(macs[0], scanstr, ref errorinfo);
  608. // break;
  609. // case "bg":
  610. // BgProgramDal bgdal = new BgProgramDal(db);
  611. // result = bgdal.DownloadProgram(macs[0], scanstr, ref errorinfo);
  612. // break;
  613. // case "ad838":
  614. // AsmProgramDal asmdal = new AsmProgramDal(db);
  615. // result = asmdal.DownloadProgram(macs[0], scanstr, ref errorinfo);
  616. // break;
  617. //}
  618. //if (result < 0)
  619. //{
  620. // db.Rollback();
  621. // MacOrderSendDal senddal = new MacOrderSendDal(db);
  622. // string temperrorinfo = "";
  623. // senddal.SendS10F3(CurrMac.MacFCode, $"Download recipe[{scanstr}] faile", ref temperrorinfo);
  624. // return -1;
  625. //}
  626. //db.Commit();
  627. //return 1;
  628. }
  629. catch (Exception ex)
  630. {
  631. db.Rollback();
  632. errorinfo = ex.Message.ToString();
  633. return -1;
  634. }
  635. finally
  636. {
  637. if (db != null)
  638. db.Close();
  639. }
  640. }
  641. private string GetLogFile(string code, ref string errorinfo)
  642. {
  643. try
  644. {
  645. //每天1个日志
  646. CurrFileDir= AppConfigurtaionServices.Configuration["ScanLogDir"];
  647. string sFilePath = CurrFileDir + "\\log" + DateTime.Now.ToString("yyyyMMdd");
  648. string sFileName = code + ".log";
  649. sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
  650. if (!Directory.Exists(sFilePath))//验证路径是否存在
  651. {
  652. Directory.CreateDirectory(sFilePath);
  653. //不存在则创建
  654. }
  655. return sFileName;
  656. }
  657. catch (Exception ex)
  658. {
  659. errorinfo = ex.Message.ToString();
  660. return "";
  661. }
  662. }
  663. //将日志写到消息队列中
  664. private int WriteLogToQueue(ScanBlock orgblock, ref string errorinfo)
  665. {
  666. try
  667. {
  668. //byte[] queuedata = EntityHelper.SerializeBytes(orgblock);
  669. //if (queuedata == null)
  670. // return -1;
  671. //CurrQueueChannel.QueueDeclare(CurrLogQueueName, false, false, false, null);//创建一个名称为hello的消息队列
  672. //CurrQueueChannel.BasicPublish("", CurrLogQueueName, null, queuedata); //开始传递
  673. //本地写日志
  674. string filename = GetLogFile(CurrMac.MacFCode, ref errorinfo);
  675. if (filename == "")
  676. return -1;
  677. string logstr = $"发生时间:{orgblock.OccurTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")} 机台编号:{CurrMac.MacFCode} ";
  678. FileStream fs;
  679. StreamWriter sw;
  680. if (File.Exists(filename))
  681. {
  682. //验证文件是否存在,有则追加,无则创建
  683. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  684. }
  685. else
  686. {
  687. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  688. }
  689. sw = new StreamWriter(fs);
  690. sw.WriteLine(logstr);
  691. sw.WriteLine($"扫描信息:{orgblock.ScanStr}");
  692. sw.WriteLine($"备注:{orgblock.Remark}");
  693. sw.Close();
  694. fs.Close();
  695. return 1;
  696. }
  697. catch (Exception ex)
  698. {
  699. errorinfo = ex.Message.ToString();
  700. return -1;
  701. }
  702. }
  703. private int WriteLogToQueue(string log, ref string errorinfo)
  704. {
  705. try
  706. {
  707. loger.LogError($"机台编号={CurrMac.MacFCode},{log}");
  708. ////byte[] queuedata = EntityHelper.SerializeBytes(orgblock);
  709. ////if (queuedata == null)
  710. //// return -1;
  711. ////CurrQueueChannel.QueueDeclare(CurrLogQueueName, false, false, false, null);//创建一个名称为hello的消息队列
  712. ////CurrQueueChannel.BasicPublish("", CurrLogQueueName, null, queuedata); //开始传递
  713. ////本地写日志
  714. //string filename = GetLogFile(CurrMac.MacFCode, ref errorinfo);
  715. //if (filename == "")
  716. // return -1;
  717. //string logstr = $"发生时间:{DateTime.Now} 机台编号:{CurrMac.MacFCode} ";
  718. //FileStream fs;
  719. //StreamWriter sw;
  720. //if (File.Exists(filename))
  721. //{
  722. // //验证文件是否存在,有则追加,无则创建
  723. // fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  724. //}
  725. //else
  726. //{
  727. // fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  728. //}
  729. //sw = new StreamWriter(fs);
  730. //sw.WriteLine(logstr);
  731. //sw.WriteLine($"{log}");
  732. //sw.Close();
  733. //fs.Close();
  734. return 1;
  735. }
  736. catch (Exception ex)
  737. {
  738. errorinfo = ex.Message.ToString();
  739. return -1;
  740. }
  741. }
  742. //接受数据并处理
  743. private ScanBlock ReceiveBlock(ref string errorinfo)
  744. {
  745. try
  746. {
  747. ScanBlock blocknode = CurrHsms.ReceiveForBlock(ref errorinfo);
  748. blocknode.MacCode = CurrMac.MacFCode;
  749. if (blocknode == null)
  750. return null;
  751. //写入日志
  752. int result = WriteLogToQueue(blocknode, ref errorinfo);
  753. if (result <= 0)
  754. {
  755. return null;
  756. }
  757. return blocknode;
  758. }
  759. catch (Exception ex)
  760. {
  761. errorinfo = ex.ToString();
  762. return null;
  763. }
  764. }
  765. //初始化发送数据链表
  766. private int InitCurrSendList(ref string errorinfo)
  767. {
  768. //lock (CurrSendList)
  769. //{
  770. // try
  771. // {
  772. // //清空链表
  773. // CurrSendList.Clear();
  774. // return 1;
  775. // }
  776. // catch (Exception ex)
  777. // {
  778. // errorinfo = ex.Message.ToString();
  779. // return -1;
  780. // }
  781. //}
  782. return 1;
  783. }
  784. }
  785. }