BeltScaleDataCollectionControl.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Common;
  2. using Common.vo.pb;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Threading;
  8. namespace MeterPlugInLibrary
  9. {
  10. public delegate void EventBeltScaleDataCollection(object o, List<CollectModel> e);
  11. public class BeltScaleDataCollectionControl
  12. {
  13. public event EventBeltScaleDataCollection EventBeltScaleDataCollectionArgs;//定义事件
  14. private Thread CollectionThread;//采集线程
  15. private string[] strPntNo = null;//计量点编号CAR40,CAR41,CAR42,CAR43,CAR44,CAR45
  16. /// <summary>
  17. /// 开启数据采集线程
  18. /// </summary>
  19. public void Start(params string[] strPntID)
  20. {
  21. this.strPntNo = strPntID;
  22. CollectionThread = new Thread(new ThreadStart(WgtThread));
  23. CollectionThread.Start();
  24. }
  25. public void Stop()
  26. {
  27. if (CollectionThread != null)
  28. {
  29. CollectionThread.Abort();
  30. CollectionThread = null;
  31. }
  32. }
  33. /// <summary>
  34. /// 重量采集线程
  35. /// </summary>
  36. private void WgtThread()
  37. {
  38. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket("", AppConfigCache.icoredbTcp);//内存表
  39. //MemoryTableDataSocket MemoClass = new MemoryTableDataSocket("", "tarantool://guest@172.22.42.3:2101");//内存表
  40. while (true)
  41. {
  42. try
  43. {
  44. Thread.Sleep(500);
  45. List<CollectModel> larg = new List<CollectModel>();
  46. List<JArray> lj = MemoClass.TrackTableList(strPntNo);
  47. if (lj != null) //正常采集,若为null则重量采集线程中断了
  48. {
  49. foreach (JArray jArray in lj)
  50. {
  51. CollectModel arg = new CollectModel();
  52. arg.pointid = jArray[0].ToString(); //计量点的编号:CAR41
  53. arg.weight = Convert.ToInt32(jArray[3].ToString());
  54. arg.weightStatus = Convert.ToInt32(jArray[4].ToString());
  55. arg.parkStatus = Convert.ToInt32(jArray[5].ToString());
  56. arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
  57. arg.licType = Convert.ToInt32(jArray[7].ToString());
  58. if (arg.licType == 0) //抓拍摄像头的数据
  59. {
  60. arg.carno = jArray[2].ToString();
  61. }
  62. else
  63. {
  64. arg.carno = jArray[8].ToString();
  65. }
  66. larg.Add(arg);
  67. }
  68. }
  69. EventBeltScaleDataCollectionArgs(this, larg); //赋值给主页面调用
  70. }
  71. catch (Exception exp)
  72. {
  73. WriteThreadLog("数据采集线程异常!" + exp.Message);
  74. }
  75. }
  76. }
  77. public void WriteLog(string str)
  78. {
  79. try
  80. {
  81. string m_szRunPath;
  82. m_szRunPath = System.Environment.CurrentDirectory;
  83. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  84. {
  85. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  86. }
  87. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  88. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  89. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  90. {
  91. Directory.CreateDirectory(strPathFile);
  92. }
  93. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  94. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  95. tw.WriteLine(str);
  96. tw.WriteLine("\r\n");
  97. tw.Close();
  98. }
  99. catch (Exception exp)
  100. {
  101. }
  102. }
  103. /// <summary>
  104. /// 写线程日志
  105. /// </summary>
  106. /// <param name="str"></param>
  107. public void WriteThreadLog(string str)
  108. {
  109. try
  110. {
  111. string m_szRunPath;
  112. m_szRunPath = System.Environment.CurrentDirectory;
  113. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  114. {
  115. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  116. }
  117. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  118. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  119. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  120. {
  121. Directory.CreateDirectory(strPathFile);
  122. }
  123. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集线程_" + strDate + ".log", true);
  124. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  125. tw.WriteLine(str);
  126. tw.WriteLine("\r\n");
  127. tw.Close();
  128. }
  129. catch (Exception exp)
  130. {
  131. }
  132. }
  133. public void WriteLogUpCardNo(string str)
  134. {
  135. try
  136. {
  137. string m_szRunPath;
  138. m_szRunPath = System.Environment.CurrentDirectory;
  139. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  140. {
  141. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  142. }
  143. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  144. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  145. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  146. {
  147. Directory.CreateDirectory(strPathFile);
  148. }
  149. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\修正车号_" + strDate + ".log", true);
  150. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  151. tw.WriteLine(str);
  152. tw.WriteLine("\r\n");
  153. tw.Close();
  154. }
  155. catch (Exception exp)
  156. {
  157. }
  158. }
  159. public void WriteLogStatus(string str)
  160. {
  161. try
  162. {
  163. string m_szRunPath;
  164. m_szRunPath = System.Environment.CurrentDirectory;
  165. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  166. {
  167. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  168. }
  169. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  170. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  171. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  172. {
  173. Directory.CreateDirectory(strPathFile);
  174. }
  175. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\重量稳定判断_" + strDate + ".log", true);
  176. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  177. tw.WriteLine(str);
  178. tw.WriteLine("\r\n");
  179. tw.Close();
  180. }
  181. catch (Exception exp)
  182. {
  183. }
  184. }
  185. }
  186. }