DataCollectionControl.cs 13 KB

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