DataCollectionControl.cs 13 KB

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