StorageDataCollectionControl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Common;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MeterPlugInLibrary
  11. {
  12. public delegate void StorageEventDataCollection(object o, StorageCollectModel e);
  13. public class StorageDataCollectionControl
  14. {
  15. public event StorageEventDataCollection EventDataCollectionArgs;//定义事件
  16. private Thread CollectionThread;//采集线程
  17. private string strPntNo = "";//计量点编号CAR11 CAR12
  18. /// <summary>
  19. /// 开启数据采集线程
  20. /// </summary>
  21. public void Start(string strPntID)
  22. {
  23. this.strPntNo = strPntID;
  24. PbStorageCache.collect = new StorageCollectModel();
  25. CollectionThread = new Thread(new ThreadStart(WgtThread));
  26. CollectionThread.Start();
  27. }
  28. public void Stop()
  29. {
  30. if (CollectionThread != null)
  31. {
  32. CollectionThread.Abort();
  33. CollectionThread = null;
  34. }
  35. }
  36. /// <summary>
  37. /// 重量采集线程
  38. /// </summary>
  39. private void WgtThread()
  40. {
  41. int icount = 0;
  42. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket(strPntNo);//内存表
  43. while (true)
  44. {
  45. try
  46. {
  47. icount++;
  48. Thread.Sleep(500);
  49. StorageCollectModel arg = new StorageCollectModel();
  50. JArray jArray = MemoClass.TrackTable(strPntNo);
  51. if (jArray != null) //正常采集,若为null则重量采集线程中断了
  52. {
  53. arg.mainWgt = Convert.ToInt32(jArray[3].ToString());
  54. arg.mainWeightStatus = Convert.ToInt32(jArray[4].ToString());
  55. arg.voiceWeightStatus = Convert.ToInt32(jArray[4].ToString());
  56. arg.datetime = Convert.ToDateTime(jArray[6].ToString().Replace("T", " ").Replace("Z", ""));
  57. arg.mainWgt = jArray.Count > 14 ? Convert.ToInt32(jArray[14]) : 0;
  58. arg.viceWgt = jArray.Count > 15 ? Convert.ToInt32(jArray[15]) : 0;
  59. }
  60. //每隔0.5秒调用一次写入到界面数据
  61. //if (icount > 4)
  62. {
  63. icount = 0;
  64. EventDataCollectionArgs(this, arg);
  65. }
  66. }
  67. catch (Exception exp)
  68. {
  69. WriteLog("数据采集线程异常!" + exp.Message);
  70. }
  71. }
  72. }
  73. public void WriteLog(string str)
  74. {
  75. try
  76. {
  77. string m_szRunPath;
  78. m_szRunPath = System.Environment.CurrentDirectory;
  79. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  80. {
  81. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  82. }
  83. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  84. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  85. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  86. {
  87. Directory.CreateDirectory(strPathFile);
  88. }
  89. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  90. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  91. tw.WriteLine(str);
  92. tw.WriteLine("\r\n");
  93. tw.Close();
  94. }
  95. catch (Exception exp)
  96. {
  97. }
  98. }
  99. }
  100. }