Esempio n. 1
0
        public static void restoreStat(UsageStat usageStat)
        {
            using (SQLiteCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = @"SELECT CURR_DATE, TOTAL_TIME, SESS_TIME FROM RT_STATS WHERE CURR_DATE=@currDate";
                cmd.CommandType = CommandType.Text;
                SQLiteParameter p1 = new SQLiteParameter("currDate", DateTime.Today.Date);
                cmd.Parameters.Add(p1);
                SQLiteDataReader r = cmd.ExecuteReader();
                if (r.HasRows)
                {
                    r.Read();
                    int systemRunningTime = (int)r["TOTAL_TIME"];
                    int SessionTime       = (int)r["SESS_TIME"];
                    usageStat.StartUpTime = DateTime.Now.AddSeconds(-systemRunningTime);
                    usageStat.SessionTime = SessionTime;
                }
                r.Close();
            }

            using (SQLiteCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = @"SELECT LOG_DATE, PROCESS_NAME, WIN_TITLE, SPEND_TIME FROM HIST_STATS WHERE LOG_DATE=@currDate";
                cmd.CommandType = CommandType.Text;
                SQLiteParameter p1 = new SQLiteParameter("currDate", DateTime.Today.Date);
                cmd.Parameters.Add(p1);
                SQLiteDataReader r = cmd.ExecuteReader();
                if (r.HasRows)
                {
                    while (r.Read())
                    {
                        usageStat.addProgramTime((string)r["PROCESS_NAME"], (string)r["WIN_TITLE"], (int)r["SPEND_TIME"]);
                    }
                }
                r.Close();
            }
        }
Esempio n. 2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!isDaemonRunning())
            {
                runDaemon();
            }

            int timerInterval = timer1.Interval / 1000;

            // if station is locked, don't do anything
            if (stationLocked)
            {
                return;
            }

            if (usageStat.State == "LOCK")
            {
                if (paused)
                {
                    return;
                }

                if (((TimeSpan)(DateTime.Now - usageStat.LockStartTime)).TotalSeconds >= AppConfig.breakTime * 60)
                {
                    logger.Info("Lock Period Ended");
                    usageStat.RestartSession();
                    saveStat();
                }
                else
                {
                    reminderInterval--;
                    if (reminderInterval == 0)
                    {
                        reminderInterval = AppConfig.reminderInterval;
                        // unlock during locking period, lock again
                        logger.Info("Within Lock Period, relock station!!!");
                        Win32.LockWorkStation();
                        return;
                    }
                }
                updateForm();
            }
            else
            {
                timer1.Enabled = false;

                // if paused, don't do anything
                if (!paused)
                {
                    // update running time
                    usageStat.UpdateSessionTime(1);
                    updateForm();
                }

                // update process using time
                String title   = Win32.GetActiveWindowTitle();
                string process = Win32.GetActiveProcessName();

                updateStatInterval--;
                if (updateStatInterval == 0)
                {
                    updateStatInterval = AppConfig.updateStatInterval;
                    if (!AppConfig.isProcessExcluded(process))
                    {
                        usageStat.addProgramTime(process, title, 10);
                        programPersistSet.Add(process + ":" + title);
                    }
                }

                // save to db every one minute
                saveInterval--;
                if (saveInterval == 0)
                {
                    saveInterval = AppConfig.saveInterval;
                    saveStat();
                }

                // kill prohibited process
                if (!AppConfig.isTitleAllowed(title))
                {
                    Win32.KillActiveProcess();
                }

                _appBlocker.process(process, title, timerInterval);

                // break reminder
                if (!paused)
                {
                    breakReminder.checkBreakTime(usageStat);
                }

                timer1.Enabled = true;
            }
        }