DataCollectionControl.cs 12 KB

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