ScanCore.cs 35 KB

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