biz_scanHistory.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. namespace HXX.Scanner.Biz
  10. {
  11. /// <summary>
  12. /// 扫描统计相关
  13. /// </summary>
  14. public class biz_scanHistory
  15. {
  16. //private static Stopwatch sw = new Stopwatch();
  17. /// <summary>
  18. /// 总扫描张数
  19. /// </summary>
  20. private static int total_sum = 0;
  21. /// <summary>
  22. /// 总扫描计时
  23. /// </summary>
  24. private static double total_seconds = 0;
  25. /// <summary>
  26. /// 批次扫描计时
  27. /// </summary>
  28. private static double batch_seconds = 0;
  29. /// <summary>
  30. /// 上次扫描张数
  31. /// </summary>
  32. private static int latest_sum = 0;
  33. /// <summary>
  34. /// 上次扫描开始时间
  35. /// </summary>
  36. private static DateTime latest_start_time = DateTime.Now.Date;
  37. /// <summary>
  38. /// 上次扫描结束时间
  39. /// </summary>
  40. private static DateTime latest_end_time = DateTime.Now;
  41. /// <summary>
  42. /// 当前扫描状态
  43. /// </summary>
  44. private static bool on_scan = false;
  45. /// <summary>
  46. /// 同步块
  47. /// </summary>
  48. private static object lockObj = new object();
  49. /// <summary>
  50. /// 开始一次新统计
  51. /// </summary>
  52. public static void start_new()
  53. {
  54. lock (lockObj)
  55. {
  56. latest_start_time = DateTime.Now;
  57. latest_sum = 0;
  58. on_scan = true;
  59. }
  60. }
  61. /// <summary>
  62. /// 增加一次统计计算
  63. /// </summary>
  64. public static void add_history()
  65. {
  66. lock (lockObj)
  67. {
  68. total_sum++;
  69. latest_sum++;
  70. var temp_seconds = (DateTime.Now - latest_start_time).TotalSeconds;
  71. batch_seconds = total_seconds + temp_seconds;
  72. }
  73. }
  74. /// <summary>
  75. /// 结束统计
  76. /// </summary>
  77. public static void end_new()
  78. {
  79. lock (lockObj)
  80. {
  81. on_scan = false;
  82. latest_end_time = DateTime.Now;
  83. var latest_total_seconds = (latest_end_time - latest_start_time).TotalSeconds;
  84. total_seconds += latest_total_seconds;
  85. }
  86. }
  87. /// <summary>
  88. /// 获取统计记录
  89. /// </summary>
  90. /// <returns></returns>
  91. public static scan_history get_history()
  92. {
  93. var history = new scan_history();
  94. lock (lockObj)
  95. {
  96. double latest_total_seconds = 0;
  97. double latest_speed = 0;
  98. if (on_scan)
  99. {
  100. latest_total_seconds = (DateTime.Now - latest_start_time).TotalSeconds;
  101. }
  102. else
  103. {
  104. if (latest_start_time == DateTime.Now.Date)
  105. {
  106. latest_total_seconds = 0;
  107. latest_speed = 0;
  108. }
  109. else
  110. {
  111. latest_total_seconds = (latest_end_time - latest_start_time).TotalSeconds;
  112. latest_speed = latest_sum / latest_total_seconds;
  113. }
  114. }
  115. history.total_sum = total_sum;
  116. history.last_start_time = latest_start_time;
  117. history.last_scan_elapse = SecondToHour(batch_seconds);
  118. history.last_speed = (int)latest_speed;
  119. if (history.last_speed == 0)
  120. {
  121. history.last_speed = 1;
  122. }
  123. }
  124. return history;
  125. }
  126. /// <summary>
  127. /// 根据秒总数获取时间格式
  128. /// </summary>
  129. /// <param name="time"></param>
  130. /// <returns></returns>
  131. private static string SecondToHour(double time)
  132. {
  133. string str = "";
  134. int hour = 0;
  135. int minute = 0;
  136. int second = 0;
  137. second = Convert.ToInt32(time);
  138. if (second > 60)
  139. {
  140. minute = second / 60;
  141. second = second % 60;
  142. }
  143. if (minute > 60)
  144. {
  145. hour = minute / 60;
  146. minute = minute % 60;
  147. }
  148. if (hour == 0)
  149. {
  150. str =minute + "分钟" + second + "秒";
  151. }
  152. else
  153. {
  154. str = hour + "小时" + minute + "分钟" + second + "秒";
  155. }
  156. return str;
  157. }
  158. }
  159. }