StorageDataDbCollection.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  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 StorageMeterSystem
  11. {
  12. public delegate void StorageEventDbCollection(object o, List<StorageCollectModel> e);
  13. public class StorageDataDbCollection
  14. {
  15. public event StorageEventDbCollection EventDataCollectionArgs;//定义事件
  16. private Thread CollectionThread;//采集线程
  17. /// <summary>
  18. /// 开启数据采集线程
  19. /// </summary>
  20. public void Start()
  21. {
  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. int icount = 0;
  39. MeterWorkStockOnlineService stockOnlineService = new MeterWorkStockOnlineService();
  40. while (true)
  41. {
  42. try
  43. {
  44. icount++;
  45. Thread.Sleep(500);
  46. StorageCollectModel arg = new StorageCollectModel();
  47. RESTfulResult<List<StorageCollectModel>> rES = stockOnlineService.doQueryStaticDbWf();
  48. if (rES.Succeed)
  49. {
  50. #if DEBUG
  51. if (rES.Data.Count == 0)
  52. {
  53. rES.Data.Add(new StorageCollectModel { pointid = "CAR20", mainWeightStatus = 0, mainWgt = 1000, viceWgt = 1000, voiceWeightStatus = 1 });
  54. }
  55. #endif
  56. if (rES.Data.Count > 0)
  57. {
  58. foreach (StorageCollectModel scm in rES.Data)
  59. {
  60. scm.pointid = PbStorageCache.ltMonitor.Where(s => s.pointNo == scm.pointid).FirstOrDefault().baseSpotNo;
  61. }
  62. }
  63. EventDataCollectionArgs(this, rES.Data);
  64. }
  65. else
  66. {
  67. WriteLog("数据采集线程异常!" + rES.Message);
  68. }
  69. }
  70. catch (Exception exp)
  71. {
  72. WriteLog("数据采集线程异常!" + exp.Message);
  73. }
  74. }
  75. }
  76. public void WriteLog(string str)
  77. {
  78. try
  79. {
  80. string m_szRunPath;
  81. m_szRunPath = System.Environment.CurrentDirectory;
  82. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  83. {
  84. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  85. }
  86. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  87. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  88. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  89. {
  90. Directory.CreateDirectory(strPathFile);
  91. }
  92. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\成品数据采集_" + strDate + ".log", true);
  93. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  94. tw.WriteLine(str);
  95. tw.WriteLine("\r\n");
  96. tw.Close();
  97. }
  98. catch (Exception exp)
  99. {
  100. }
  101. }
  102. }
  103. }