DataCollectionControl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Common;
  2. using Common.vo.pb;
  3. using MeterModelLibrary;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MeterPlugInLibrary
  13. {
  14. public delegate void EventDataCollection(object o, DataCollectionArgs e);
  15. public class DataCollectionArgs
  16. {
  17. public string carno { get; set; }//车号
  18. public int weight { get; set; }//重量
  19. public int weightStatus { get; set; }//0稳定,1不稳定,2空磅
  20. public int parkStatus { get; set; }//红外对射 0未挡住,1:车头压线 2:车尾压线 3:两头压线; 记录在重量稳定后生成
  21. public int licType { get; set; }//0 抓拍 1 RFID
  22. public DateTime datetime { get; set; } //当前时间的距离1970.1.1.08:00时间的秒数
  23. public string vdioCarNos { get; set; }
  24. public string RfidNos { get; set; }
  25. }
  26. public class DataCollectionControl
  27. {
  28. public event EventDataCollection EventDataCollectionArgs;//定义事件
  29. private Thread CollectionThread;//采集线程
  30. private string strPntNo = "";//计量点编号
  31. private string strPntName = "";//计量点编号
  32. /// <summary>
  33. /// 开启数据采集线程
  34. /// </summary>
  35. public void Start(string strPntID, string strPntName)
  36. {
  37. PbCache.collect = new CollectModel();
  38. this.strPntNo = strPntID;
  39. this.strPntName = strPntName;
  40. CollectionThread = new Thread(new ThreadStart(WgtThread));
  41. CollectionThread.Start();
  42. }
  43. public void Stop()
  44. {
  45. if (CollectionThread != null)
  46. {
  47. CollectionThread.Abort();
  48. CollectionThread = null;
  49. }
  50. }
  51. /// <summary>
  52. /// 重量采集线程
  53. /// </summary>
  54. private void WgtThread()
  55. {
  56. //===========eason 2020 注释================
  57. int icount = 0, iOldWgt = 0, iWdCount = 0; //iWdCount稳定次数,这里判断为5次稳定即可
  58. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(PbCache.collect_no);//内存表
  59. //CarNoModfiy carNoModifyClass = new CarNoModfiy();//车号修正
  60. while (true)
  61. {
  62. try
  63. {
  64. icount++;
  65. Thread.Sleep(500);
  66. DataCollectionArgs arg = new DataCollectionArgs();
  67. //WriteThreadLog("数据采集线执行跟踪!");//2021年3月16日 杨秀东添加
  68. JArray jArray = MemoClass.TrackTable(PbCache.collect_no);
  69. if (jArray != null) //正常采集,若为null则重量采集线程中断了
  70. {
  71. //arg.carno = jArray[2].ToString();
  72. arg.weight = Convert.ToInt32(jArray[3].ToString());
  73. arg.weightStatus = Convert.ToInt32(jArray[4].ToString());
  74. arg.parkStatus = Convert.ToInt32(jArray[5].ToString());
  75. arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
  76. arg.licType = Convert.ToInt32(jArray[7].ToString());
  77. if (arg.licType == 0) //摄像头的数据
  78. {
  79. arg.carno = jArray[2].ToString();
  80. }
  81. else
  82. {
  83. arg.carno = jArray[8].ToString();
  84. }
  85. arg.vdioCarNos = arg.carno; //jArray[8].ToString();
  86. arg.RfidNos = jArray[9].ToString();
  87. #region 判稳代码
  88. /*
  89. if (PbCache.range != null)
  90. {
  91. if (PbCache.range.stableDiff != null)
  92. {
  93. if (Math.Abs(arg.weight - iOldWgt) > PbCache.range.stableDiff.Value)
  94. {
  95. iOldWgt = arg.weight;
  96. iWdCount = 0;
  97. }
  98. else
  99. {
  100. arg.weight = iOldWgt;
  101. iWdCount++;
  102. }
  103. }
  104. }
  105. if (arg.weightStatus == 2)
  106. {
  107. arg.weightStatus = 0; //重量稳定
  108. iWdCount = 0;
  109. }
  110. //0.1秒采集一次,一共采集5次,若稳定则认为重量稳定
  111. if (iWdCount >= 10)
  112. {
  113. arg.weightStatus = 0; //重量稳定
  114. iWdCount = 10;
  115. }
  116. //*/
  117. #endregion
  118. }
  119. //每隔0.5秒调用一次写入到界面数据
  120. //if (icount > 4)
  121. {
  122. icount = 0;
  123. EventDataCollectionArgs(this, arg);
  124. }
  125. }
  126. catch (Exception exp)
  127. {
  128. WriteThreadLog("数据采集线程异常!" + exp.Message);
  129. }
  130. }
  131. }
  132. public void WriteLog(string str)
  133. {
  134. try
  135. {
  136. string m_szRunPath;
  137. m_szRunPath = System.Environment.CurrentDirectory;
  138. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  139. {
  140. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  141. }
  142. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  143. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  144. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  145. {
  146. Directory.CreateDirectory(strPathFile);
  147. }
  148. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  149. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  150. tw.WriteLine(str);
  151. tw.WriteLine("\r\n");
  152. tw.Close();
  153. }
  154. catch (Exception exp)
  155. {
  156. }
  157. }
  158. /// <summary>
  159. /// 写线程日志
  160. /// </summary>
  161. /// <param name="str"></param>
  162. public void WriteThreadLog(string str)
  163. {
  164. try
  165. {
  166. string m_szRunPath;
  167. m_szRunPath = System.Environment.CurrentDirectory;
  168. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  169. {
  170. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  171. }
  172. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  173. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  174. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  175. {
  176. Directory.CreateDirectory(strPathFile);
  177. }
  178. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集线程_" + strDate + ".log", true);
  179. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  180. tw.WriteLine(str);
  181. tw.WriteLine("\r\n");
  182. tw.Close();
  183. }
  184. catch (Exception exp)
  185. {
  186. }
  187. }
  188. public void WriteLogUpCardNo(string str)
  189. {
  190. try
  191. {
  192. string m_szRunPath;
  193. m_szRunPath = System.Environment.CurrentDirectory;
  194. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  195. {
  196. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  197. }
  198. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  199. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  200. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  201. {
  202. Directory.CreateDirectory(strPathFile);
  203. }
  204. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\修正车号_" + strDate + ".log", true);
  205. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  206. tw.WriteLine(str);
  207. tw.WriteLine("\r\n");
  208. tw.Close();
  209. }
  210. catch (Exception exp)
  211. {
  212. }
  213. }
  214. public void WriteLogStatus(string str)
  215. {
  216. try
  217. {
  218. string m_szRunPath;
  219. m_szRunPath = System.Environment.CurrentDirectory;
  220. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  221. {
  222. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  223. }
  224. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  225. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  226. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  227. {
  228. Directory.CreateDirectory(strPathFile);
  229. }
  230. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\重量稳定判断_" + strDate + ".log", true);
  231. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  232. tw.WriteLine(str);
  233. tw.WriteLine("\r\n");
  234. tw.Close();
  235. }
  236. catch (Exception exp)
  237. {
  238. }
  239. }
  240. }
  241. }