DataCollectionControl.cs 12 KB

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