ProductDataControl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 ProductDataCollection(object o, List<StorageCollectModel> e);
  13. public class ProductDataControl
  14. {
  15. public event ProductDataCollection EventDataCollectionArgs;//定义事件
  16. private Thread CollectionThread;//采集线程
  17. private string[] strPntNo = null;//计量点编号CAR11 CAR12
  18. /// <summary>
  19. /// 开启数据采集线程
  20. /// </summary>
  21. public void Start(params string[] strPntID)
  22. {
  23. this.strPntNo = strPntID;
  24. CollectionThread = new Thread(new ThreadStart(WgtThread));
  25. CollectionThread.Start();
  26. }
  27. public void Stop()
  28. {
  29. if (CollectionThread != null)
  30. {
  31. CollectionThread.Abort();
  32. CollectionThread = null;
  33. }
  34. }
  35. /// <summary>
  36. /// 重量采集线程
  37. /// </summary>
  38. private void WgtThread()
  39. {
  40. MemoryTableDataSocket MemoClass = new MemoryTableDataSocket("", "tarantool://guest@172.22.42.3:2101");//内存表
  41. while (true)
  42. {
  43. try
  44. {
  45. Thread.Sleep(500);
  46. List<StorageCollectModel> larg = new List<StorageCollectModel>();
  47. List<JArray> lj = MemoClass.TrackTable(strPntNo);
  48. //*
  49. if (lj != null) //正常采集,若为null则重量采集线程中断了
  50. {
  51. foreach (JArray jr in lj)
  52. {
  53. StorageCollectModel arg = new StorageCollectModel();
  54. arg.pointid = jr[0].ToString();
  55. arg.mainWeightStatus = Convert.ToInt32(jr[2].ToString()); //0:重量稳定; 1:重量不稳定; 2:空磅
  56. arg.datetime = Convert.ToDateTime(jr[3].ToString().Replace("T", " ").Replace("Z", "")); //上称时间 uint 当前时间的距离1970.1.1.08:00时间的秒数; 秒
  57. arg.mainWgt = Convert.ToDouble(jr[4].ToString());
  58. arg.viceWgt = jr.Count > 5 ? Convert.ToDouble(jr[5].ToString()) : 0;
  59. //arg.voiceWeightStatus = Convert.ToInt32(jArray[2].ToString());
  60. larg.Add(arg);
  61. }
  62. }
  63. //每隔0.5秒调用一次写入到界面数据
  64. EventDataCollectionArgs(this, larg);
  65. }
  66. catch (Exception exp)
  67. {
  68. WriteLog("数据采集线程异常!" + exp.Message);
  69. }
  70. }
  71. }
  72. public void WriteLog(string str)
  73. {
  74. try
  75. {
  76. string m_szRunPath;
  77. m_szRunPath = System.Environment.CurrentDirectory;
  78. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  79. {
  80. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  81. }
  82. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  83. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  84. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  85. {
  86. Directory.CreateDirectory(strPathFile);
  87. }
  88. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\数据采集_" + strDate + ".log", true);
  89. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  90. tw.WriteLine(str);
  91. tw.WriteLine("\r\n");
  92. tw.Close();
  93. }
  94. catch (Exception exp)
  95. {
  96. }
  97. }
  98. }
  99. }