Skip to content

Commit

Permalink
Credit idle time back
Browse files Browse the repository at this point in the history
  • Loading branch information
slgobinath committed Oct 7, 2017
1 parent 3c3b1bd commit 395c0d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions safeeyes/SafeEyes.py
Expand Up @@ -200,13 +200,13 @@ def save_settings(self, config):
Timer(1.0, self.safe_eyes_core.start).start()
self.plugins_manager.start()

def enable_safeeyes(self):
def enable_safeeyes(self, scheduled_next_break_time=-1):
"""
Listen to tray icon enable action and send the signal to core.
"""
if not self.active and self.safe_eyes_core.has_breaks():
self.active = True
self.safe_eyes_core.start()
self.safe_eyes_core.start(scheduled_next_break_time)
self.plugins_manager.start()

def disable_safeeyes(self):
Expand Down
12 changes: 10 additions & 2 deletions safeeyes/SafeEyesCore.py
Expand Up @@ -50,6 +50,7 @@ def __init__(self, context):
self.pre_break_warning_time = 0
self.running = False
self.short_break_duration = 0
self.scheduled_next_break_time = -1
# This event is fired before <time-to-prepare> for a break
self.on_pre_break = EventHook()
# This event is fired at the start of a break
Expand Down Expand Up @@ -89,7 +90,7 @@ def initialize(self, config):
self.next_break_index = (self.next_break_index) % self.break_count
self.context['session']['next_break_index'] = self.next_break_index

def start(self):
def start(self, next_break_time=-1):
"""
Start Safe Eyes is it is not running already.
"""
Expand All @@ -99,6 +100,7 @@ def start(self):
if not self.running:
logging.info("Start Safe Eyes core")
self.running = True
self.scheduled_next_break_time = int(next_break_time)
Utility.start_thread(self.__scheduler_job)

def stop(self):
Expand Down Expand Up @@ -190,7 +192,13 @@ def __scheduler_job(self):
time_to_wait = self.postpone_duration
self.context['postponed'] = False

next_break_time = datetime.datetime.now() + datetime.timedelta(minutes=time_to_wait)
current_time = datetime.datetime.now()
current_timestamp = int(current_time.timestamp())
if current_timestamp < self.scheduled_next_break_time:
time_to_wait = int((self.scheduled_next_break_time - current_timestamp) / 60)
self.scheduled_next_break_time = -1

next_break_time = current_time + datetime.timedelta(minutes=time_to_wait)
self.on_update_next_break.fire(next_break_time)

if self.__is_long_break():
Expand Down
25 changes: 22 additions & 3 deletions safeeyes/plugins/smartpause/plugin.py
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import datetime
import logging
import subprocess
import threading
Expand All @@ -35,7 +36,9 @@
enable_safe_eyes = None
disable_safe_eyes = None
smart_pause_activated = False

idle_start_time = None
next_break_time = None
break_interval = 0

def __system_idle_time():
"""
Expand Down Expand Up @@ -75,18 +78,21 @@ def init(ctx, safeeyes_config, plugin_config):
global enable_safe_eyes
global disable_safe_eyes
global idle_time
global break_interval
logging.debug('Initialize Smart Pause plugin')
context = ctx
enable_safe_eyes = context['api']['enable_safeeyes']
disable_safe_eyes = context['api']['disable_safeeyes']
idle_time = plugin_config['idle_time']
break_interval = safeeyes_config.get('break_interval') * 60


def __start_idle_monitor():
"""
Continuously check the system idle time and pause/resume Safe Eyes based on it.
"""
global smart_pause_activated
global idle_start_time
while __is_active():
# Wait for 2 seconds
idle_condition.acquire()
Expand All @@ -98,12 +104,18 @@ def __start_idle_monitor():
system_idle_time = __system_idle_time()
if system_idle_time >= idle_time and context['state'] == State.WAITING:
smart_pause_activated = True
idle_start_time = datetime.datetime.now()
logging.info('Pause Safe Eyes due to system idle')
disable_safe_eyes()
elif system_idle_time < idle_time and context['state'] == State.STOPPED:
smart_pause_activated = False
logging.info('Resume Safe Eyes due to user activity')
enable_safe_eyes()
smart_pause_activated = False
idle_period = (datetime.datetime.now() - idle_start_time)
if idle_period.total_seconds() < break_interval:
next_break = next_break_time + idle_period
enable_safe_eyes(next_break.timestamp())
else:
enable_safe_eyes()


def on_start():
Expand Down Expand Up @@ -133,3 +145,10 @@ def on_stop():
idle_condition.acquire()
idle_condition.notify_all()
idle_condition.release()

def update_next_break(dateTime):
"""
Update the next break time.
"""
global next_break_time
next_break_time = dateTime

0 comments on commit 395c0d0

Please sign in to comment.