StaticDataCollectionControl.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Common;
  2. using Common.vo.pb;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MeterPlugInLibrary
  12. {
  13. public delegate void StaticEventDataCollection(object o, StaticCollectModel e);
  14. public class StaticDataCollectionControl
  15. {
  16. public event StaticEventDataCollection EventDataCollectionArgs;//定义事件
  17. private Thread CollectionThread;//采集线程
  18. private string strPntNo = "";//计量点编号CAR11 CAR12
  19. /// <summary>
  20. /// 开启数据采集线程
  21. /// </summary>
  22. public void Start(string strPntID)
  23. {
  24. this.strPntNo = strPntID;
  25. PbStaticRailwayCache.collect = new StaticCollectModel();
  26. CollectionThread = new Thread(new ThreadStart(WgtThread));
  27. CollectionThread.Start();
  28. }
  29. public void Stop()
  30. {
  31. if (CollectionThread != null)
  32. {
  33. CollectionThread.Abort();
  34. CollectionThread = null;
  35. }
  36. }
  37. /// <summary>
  38. /// 重量采集线程
  39. /// </summary>
  40. private void WgtThread()
  41. {
  42. int icount = 0;
  43. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);//内存表
  44. while (true)
  45. {
  46. try
  47. {
  48. icount++;
  49. Thread.Sleep(500);
  50. StaticCollectModel arg = new StaticCollectModel();
  51. JArray jArray = MemoClass.TrackTable(strPntNo);
  52. if (jArray != null) //正常采集,若为null则重量采集线程中断了
  53. {
  54. arg.carno = jArray[2].ToString().Length > 14 ? jArray[2].ToString().Substring(7, 7) : jArray[2].ToString();
  55. arg.mainWgt = Convert.ToInt32(jArray[3].ToString());
  56. arg.weightStatus = Convert.ToInt32(jArray[4].ToString());
  57. //arg.parkStatus = Convert.ToInt32(jArray[5].ToString());
  58. arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
  59. //arg.licType = Convert.ToInt32(jArray[7].ToString());
  60. //arg.vdioCarNos = jArray[8].ToString();
  61. //arg.RfidNos = jArray[9].ToString();
  62. arg.mainWgt = jArray.Count > 14 ? Convert.ToInt32(jArray[14]) : 0;
  63. arg.viceWgt= jArray.Count > 15 ? Convert.ToInt32(jArray[15]) : 0;
  64. //2021年7月16日 杨秀东对丢失精度进行处理
  65. if (arg.mainWgt.ToString().EndsWith("1"))
  66. arg.mainWgt = arg.mainWgt - 1;
  67. if (arg.mainWgt.ToString().EndsWith("9"))
  68. arg.mainWgt = arg.mainWgt + 1;
  69. if (arg.viceWgt.ToString().EndsWith("1"))
  70. arg.viceWgt = arg.viceWgt - 1;
  71. if (arg.viceWgt.ToString().EndsWith("9"))
  72. arg.viceWgt = arg.viceWgt + 1;
  73. arg.carType = "";//车型,在rfid中取
  74. arg.firstRed = "";//第一个红外
  75. arg.secondRed = "";//第二个红外
  76. arg.thirdRed = "";//第三个红外
  77. }
  78. if (strPntNo == "") //两个红外的情况
  79. {
  80. JArray jArray1 = MemoClass.LiveTable(strPntNo, "tagName");
  81. if (jArray1 != null)
  82. {
  83. arg.firstRed = jArray1[1].ToString();
  84. }
  85. JArray jArray2 = MemoClass.LiveTable(strPntNo, "tagName");
  86. if (jArray2 != null)
  87. {
  88. arg.secondRed = jArray2[1].ToString();
  89. }
  90. }
  91. else if (strPntNo == "") //三个红外的情况
  92. {
  93. JArray jArray1 = MemoClass.LiveTable(strPntNo, "tagName");
  94. if (jArray1 != null)
  95. {
  96. arg.firstRed = jArray1[1].ToString();
  97. }
  98. JArray jArray2 = MemoClass.LiveTable(strPntNo, "tagName");
  99. if (jArray2 != null)
  100. {
  101. arg.secondRed = jArray2[1].ToString();
  102. }
  103. JArray jArray3 = MemoClass.LiveTable(strPntNo, "tagName");
  104. if (jArray3 != null)
  105. {
  106. arg.thirdRed = jArray3[1].ToString();
  107. }
  108. }
  109. //每隔0.5秒调用一次写入到界面数据
  110. //if (icount > 4)
  111. {
  112. icount = 0;
  113. EventDataCollectionArgs(this, arg);
  114. }
  115. }
  116. catch (Exception exp)
  117. {
  118. WriteLog("数据采集线程异常!" + exp.Message);
  119. }
  120. }
  121. }
  122. public void WriteLog(string str)
  123. {
  124. try
  125. {
  126. string m_szRunPath;
  127. m_szRunPath = System.Environment.CurrentDirectory;
  128. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  129. {
  130. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  131. }
  132. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  133. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  134. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  135. {
  136. Directory.CreateDirectory(strPathFile);
  137. }
  138. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  139. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  140. tw.WriteLine(str);
  141. tw.WriteLine("\r\n");
  142. tw.Close();
  143. }
  144. catch (Exception exp)
  145. {
  146. }
  147. }
  148. }
  149. }