| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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
- {
- /// <summary>
- /// 扫描统计相关
- /// </summary>
- public class biz_scanHistory
- {
- //private static Stopwatch sw = new Stopwatch();
- /// <summary>
- /// 总扫描张数
- /// </summary>
- private static int total_sum = 0;
- /// <summary>
- /// 总扫描计时
- /// </summary>
- private static double total_seconds = 0;
- /// <summary>
- /// 批次扫描计时
- /// </summary>
- private static double batch_seconds = 0;
- /// <summary>
- /// 上次扫描张数
- /// </summary>
- private static int latest_sum = 0;
- /// <summary>
- /// 上次扫描开始时间
- /// </summary>
- private static DateTime latest_start_time = DateTime.Now.Date;
- /// <summary>
- /// 上次扫描结束时间
- /// </summary>
- private static DateTime latest_end_time = DateTime.Now;
- /// <summary>
- /// 当前扫描状态
- /// </summary>
- private static bool on_scan = false;
- /// <summary>
- /// 同步块
- /// </summary>
- private static object lockObj = new object();
- /// <summary>
- /// 开始一次新统计
- /// </summary>
- public static void start_new()
- {
- lock (lockObj)
- {
- latest_start_time = DateTime.Now;
- latest_sum = 0;
- on_scan = true;
- }
- }
- /// <summary>
- /// 增加一次统计计算
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 结束统计
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 获取统计记录
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 根据秒总数获取时间格式
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|