using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Configuration; using System.Diagnostics; namespace HXX.Scanner.Biz { /// /// 扫描统计相关 /// public class biz_scanHistory { //private static Stopwatch sw = new Stopwatch(); /// /// 总扫描张数 /// private static int total_sum = 0; /// /// 总扫描计时 /// private static double total_seconds = 0; /// /// 批次扫描计时 /// private static double batch_seconds = 0; /// /// 上次扫描张数 /// private static int latest_sum = 0; /// /// 上次扫描开始时间 /// private static DateTime latest_start_time = DateTime.Now.Date; /// /// 上次扫描结束时间 /// private static DateTime latest_end_time = DateTime.Now; /// /// 当前扫描状态 /// private static bool on_scan = false; /// /// 同步块 /// private static object lockObj = new object(); /// /// 开始一次新统计 /// public static void start_new() { lock (lockObj) { latest_start_time = DateTime.Now; latest_sum = 0; on_scan = true; } } /// /// 增加一次统计计算 /// public static void add_history() { lock (lockObj) { total_sum++; latest_sum++; var temp_seconds = (DateTime.Now - latest_start_time).TotalSeconds; batch_seconds = total_seconds + temp_seconds; } } /// /// 结束统计 /// public static void end_new() { lock (lockObj) { on_scan = false; latest_end_time = DateTime.Now; var latest_total_seconds = (latest_end_time - latest_start_time).TotalSeconds; total_seconds += latest_total_seconds; } } /// /// 获取统计记录 /// /// public static scan_history get_history() { var history = new scan_history(); lock (lockObj) { double latest_total_seconds = 0; double latest_speed = 0; if (on_scan) { latest_total_seconds = (DateTime.Now - latest_start_time).TotalSeconds; } else { if (latest_start_time == DateTime.Now.Date) { latest_total_seconds = 0; latest_speed = 0; } else { latest_total_seconds = (latest_end_time - latest_start_time).TotalSeconds; latest_speed = latest_sum / latest_total_seconds; } } history.total_sum = total_sum; history.last_start_time = latest_start_time; history.last_scan_elapse = SecondToHour(batch_seconds); history.last_speed = (int)latest_speed; if (history.last_speed == 0) { history.last_speed = 1; } } return history; } /// /// 根据秒总数获取时间格式 /// /// /// private static string SecondToHour(double time) { string str = ""; int hour = 0; int minute = 0; int second = 0; second = Convert.ToInt32(time); if (second > 60) { minute = second / 60; second = second % 60; } if (minute > 60) { hour = minute / 60; minute = minute % 60; } if (hour == 0) { str =minute + "分钟" + second + "秒"; } else { str = hour + "小时" + minute + "分钟" + second + "秒"; } return str; } } }