DataCollectionControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. arg.videoCarNo1 = carNo1;
  90. arg.videoCarNo2 = carNo2;
  91. if (arg.licType == 0) //摄像头的数据
  92. {
  93. //arg.carno = jArray[2].ToString();
  94. // 2. 判断逻辑:
  95. // a.A车号识别,B车号不识别;B车号识别,A车号不识别,正常计量,标记只有1个车号识别
  96. if (carNo1 != "" && carNo2 == "")
  97. {
  98. arg.carno = carNo1;
  99. arg.carnoAlert = "1";
  100. }
  101. else if (carNo2 != "" && carNo1 == "")
  102. {
  103. arg.carno = carNo2;
  104. arg.carnoAlert = "1";
  105. }
  106. // b.A车号识别,B车号识别,且识别一致,正常计量
  107. else if (carNo1 != "" && carNo2 != "" && carNo1 == carNo2)
  108. {
  109. arg.carno = carNo1;
  110. arg.carnoAlert = "0";
  111. }
  112. // c.A车号识别,B车号识别,且识别不一致,停止计量,标记只有车号不一致识别
  113. // c.A车号识别,B车号识别,且识别不一致,以枪机为准。张存斌、李亚军批准 By BourneCao 20221214
  114. else if (carNo1 != "" && carNo2 != "" && carNo1 != carNo2)
  115. {
  116. arg.carno = carNo2;
  117. arg.carnoAlert = "1";
  118. }
  119. // d.都不识别
  120. else
  121. {
  122. arg.carno = "";
  123. arg.carnoAlert = "3";
  124. }
  125. }
  126. else
  127. {
  128. arg.carno = jArray[8].ToString();
  129. }
  130. arg.vdioCarNos = arg.carno; //jArray[8].ToString();
  131. arg.RfidNos = jArray[9].ToString();
  132. #region 判稳代码
  133. /*
  134. if (PbCache.range != null)
  135. {
  136. if (PbCache.range.stableDiff != null)
  137. {
  138. if (Math.Abs(arg.weight - iOldWgt) > PbCache.range.stableDiff.Value)
  139. {
  140. iOldWgt = arg.weight;
  141. iWdCount = 0;
  142. }
  143. else
  144. {
  145. arg.weight = iOldWgt;
  146. iWdCount++;
  147. }
  148. }
  149. }
  150. if (arg.weightStatus == 2)
  151. {
  152. arg.weightStatus = 0; //重量稳定
  153. iWdCount = 0;
  154. }
  155. //0.1秒采集一次,一共采集5次,若稳定则认为重量稳定
  156. if (iWdCount >= 10)
  157. {
  158. arg.weightStatus = 0; //重量稳定
  159. iWdCount = 10;
  160. }
  161. //*/
  162. #endregion
  163. }
  164. //每隔0.5秒调用一次写入到界面数据
  165. //if (icount > 4)
  166. {
  167. icount = 0;
  168. // 将数据提交移出数据采集线程
  169. // 避免HTTP数据提交影响到数据采集的刷新
  170. // By BourneCao 20220811
  171. EventDataCollectionArgs(this, arg);
  172. }
  173. }
  174. catch (Exception exp)
  175. {
  176. WriteThreadLog("数据采集线程异常!" + exp.Message);
  177. }
  178. }
  179. }
  180. public void WriteLog(string str)
  181. {
  182. try
  183. {
  184. string m_szRunPath;
  185. m_szRunPath = System.Environment.CurrentDirectory;
  186. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  187. {
  188. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  189. }
  190. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  191. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  192. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  193. {
  194. Directory.CreateDirectory(strPathFile);
  195. }
  196. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  197. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  198. tw.WriteLine(str);
  199. tw.WriteLine("\r\n");
  200. tw.Close();
  201. }
  202. catch (Exception exp)
  203. {
  204. }
  205. }
  206. /// <summary>
  207. /// 写线程日志
  208. /// </summary>
  209. /// <param name="str"></param>
  210. public void WriteThreadLog(string str)
  211. {
  212. try
  213. {
  214. string m_szRunPath;
  215. m_szRunPath = System.Environment.CurrentDirectory;
  216. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  217. {
  218. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  219. }
  220. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  221. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  222. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  223. {
  224. Directory.CreateDirectory(strPathFile);
  225. }
  226. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集线程_" + strDate + ".log", true);
  227. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  228. tw.WriteLine(str);
  229. tw.WriteLine("\r\n");
  230. tw.Close();
  231. }
  232. catch (Exception exp)
  233. {
  234. }
  235. }
  236. public void WriteLogUpCardNo(string str)
  237. {
  238. try
  239. {
  240. string m_szRunPath;
  241. m_szRunPath = System.Environment.CurrentDirectory;
  242. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  243. {
  244. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  245. }
  246. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  247. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  248. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  249. {
  250. Directory.CreateDirectory(strPathFile);
  251. }
  252. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\修正车号_" + strDate + ".log", true);
  253. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  254. tw.WriteLine(str);
  255. tw.WriteLine("\r\n");
  256. tw.Close();
  257. }
  258. catch (Exception exp)
  259. {
  260. }
  261. }
  262. public void WriteLogStatus(string str)
  263. {
  264. try
  265. {
  266. string m_szRunPath;
  267. m_szRunPath = System.Environment.CurrentDirectory;
  268. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  269. {
  270. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  271. }
  272. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  273. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  274. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  275. {
  276. Directory.CreateDirectory(strPathFile);
  277. }
  278. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\重量稳定判断_" + strDate + ".log", true);
  279. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  280. tw.WriteLine(str);
  281. tw.WriteLine("\r\n");
  282. tw.Close();
  283. }
  284. catch (Exception exp)
  285. {
  286. }
  287. }
  288. }
  289. }