public static LogTimeIntervalResource ToResource(this LogTimeIntervalLocal log, int computerId, string userName) { return(new LogTimeIntervalResource() { ComputerId = computerId, UserName = userName, StartTime = log.StartTime, Duration = log.Duration, State = log.State, LogTimeIntervalId = log.LogTimeIntervalId }); }
//private static Timer _timer; //public static void Start() //{ // _timer = new Timer(Run, null, 0, Settings.LogTimeIntervalInSeconds * 1000); //} //public static void Stop() //{ // _timer.Change(Timeout.Infinite, Timeout.Infinite); // _timer.Dispose(); //} //private static void Run(object obj) public static void Run() { var computer = Db.Computer; var intervals = computer.LogTimeIntervals?.OrderBy(l => l.StartTime).ToList() ?? new List <LogTimeIntervalLocal>(); var username = SessionManager.GetActive(); var interval = intervals.LastOrDefault(); var currentDateTime = DateTime.Now; var state = SessionManager.GetIdleTime().TotalSeconds < 120 ? "Active" : "Idle"; if (interval == null) { interval = new LogTimeIntervalLocal() { StartTime = currentDateTime } } ; var timeDifference = (currentDateTime - (interval.StartTime + interval.Duration)); LocalLogger.Log($"TimeDifferenceInSeconds: {timeDifference}"); // User names and state must be the same. // Also time difference between previous and current interval must be less than 2 minutes. var isIntervalUpToDate = interval.ComputerUser == username && interval.State == state && timeDifference.TotalSeconds < 120 && interval.StartTime.Day == currentDateTime.Day; if (isIntervalUpToDate) { // Update the current interval LocalLogger.Log("Update the current interval"); interval.Duration += timeDifference; interval.Synced = false; } else { // Create a new interval LocalLogger.Log("Create a new interval"); intervals.Add(new LogTimeIntervalLocal() { ComputerUser = username, Duration = new TimeSpan(0, 0, 1),// interval starts with 0 minutes StartTime = currentDateTime, State = state, Synced = false }); } computer.LogTimeIntervals = intervals; Db.Computer = computer; } }
private static void GenerateLogTimeIntervals(ComputerLocal computer) { var now = DateTime.Now; computer.ComputerUsers.Add(null); var dateTime = new DateTime(now.Year, now.Month, now.Day, 2.Random(), 60.Random(), 0); for (var i = 0; i < Settings.MaxAmountOfLogTimeIntervals.Random(6); i++) { var timeSpan = new TimeSpan(2.Random(), 57.Random(), 0); var log = new LogTimeIntervalLocal() { ComputerUser = computer.ComputerUsers.Random().Name, StartTime = dateTime, Duration = timeSpan, State = computer.ComputerUsers.Random() == null ? "Idle" : (2.Random() == 0 ? "Idle" : "Active"), }; computer.LogTimeIntervals.Add(log); computer.ComputerUsers.RemoveAll(u => u == null); } }