2017-10-07 15:10:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Safe Eyes is a utility to remind you to take break frequently
|
|
|
|
# to protect your eyes from eye strain.
|
|
|
|
|
|
|
|
# Copyright (C) 2017 Gobinath
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
Show health statistics on the break screen.
|
|
|
|
"""
|
|
|
|
|
2020-11-15 23:41:13 +01:00
|
|
|
import croniter
|
2019-02-24 17:53:20 +01:00
|
|
|
import datetime
|
2017-10-07 15:10:31 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
context = None
|
|
|
|
session = None
|
2023-11-29 14:59:21 +01:00
|
|
|
statistics_reset_cron = None
|
|
|
|
default_statistics_reset_cron = '0 0 * * *' # Every midnight
|
2020-11-15 23:41:13 +01:00
|
|
|
next_reset_time = None
|
2023-11-29 14:59:21 +01:00
|
|
|
start_time = None
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def init(ctx, safeeyes_config, plugin_config):
|
|
|
|
"""
|
|
|
|
Initialize the plugin.
|
|
|
|
"""
|
|
|
|
global context
|
|
|
|
global session
|
2020-11-15 23:41:13 +01:00
|
|
|
global statistics_reset_cron
|
|
|
|
|
2017-10-07 15:10:31 +02:00
|
|
|
logging.debug('Initialize Health Stats plugin')
|
|
|
|
context = ctx
|
2023-11-29 14:59:21 +01:00
|
|
|
statistics_reset_cron = plugin_config.get('statistics_reset_cron', default_statistics_reset_cron)
|
2020-11-15 23:41:13 +01:00
|
|
|
|
2017-10-07 15:10:31 +02:00
|
|
|
if session is None:
|
2020-11-15 23:41:13 +01:00
|
|
|
# Read the session
|
2023-11-29 14:59:21 +01:00
|
|
|
defaults = {
|
|
|
|
'breaks': 0,
|
|
|
|
'skipped_breaks': 0,
|
|
|
|
'screen_time': 0,
|
|
|
|
'total_breaks': 0,
|
|
|
|
'total_skipped_breaks': 0,
|
|
|
|
'total_screen_time': 0,
|
|
|
|
'total_resets': 0,
|
|
|
|
}
|
2017-10-07 15:10:31 +02:00
|
|
|
|
2023-11-29 14:59:21 +01:00
|
|
|
session = context['session']['plugin'].get('healthstats', {}) | defaults
|
|
|
|
if 'no_of_breaks' in session:
|
|
|
|
# Ignore old format session.
|
|
|
|
session = defaults
|
|
|
|
context['session']['plugin']['healthstats'] = session
|
|
|
|
|
|
|
|
_get_next_reset_time()
|
2017-10-07 15:10:31 +02:00
|
|
|
|
2020-11-15 23:41:13 +01:00
|
|
|
|
2023-11-29 14:59:21 +01:00
|
|
|
def on_stop_break():
|
|
|
|
# Check if break was skipped.
|
|
|
|
global session
|
2017-10-07 15:10:31 +02:00
|
|
|
if context['skipped']:
|
2023-11-29 14:59:21 +01:00
|
|
|
session['skipped_breaks'] += 1
|
|
|
|
|
|
|
|
# Screen time is starting again.
|
|
|
|
on_start()
|
|
|
|
|
|
|
|
|
|
|
|
def on_start_break(break_obj):
|
|
|
|
global session
|
|
|
|
session['breaks'] += 1
|
|
|
|
|
2023-12-30 19:01:40 +01:00
|
|
|
# Screen time has stopped.
|
2023-11-29 14:59:21 +01:00
|
|
|
on_stop()
|
|
|
|
|
|
|
|
|
|
|
|
def on_stop():
|
|
|
|
global start_time
|
|
|
|
_reset_stats()
|
|
|
|
if start_time:
|
|
|
|
screen_time = datetime.datetime.now() - start_time
|
|
|
|
session['screen_time'] += round(screen_time.total_seconds())
|
|
|
|
start_time = None
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_widget_title(break_obj):
|
|
|
|
"""
|
|
|
|
Return the widget title.
|
|
|
|
"""
|
|
|
|
return _('Health Statistics')
|
|
|
|
|
|
|
|
|
2019-03-02 21:56:27 +01:00
|
|
|
def _reset_stats():
|
2023-11-29 14:59:21 +01:00
|
|
|
global session
|
2020-11-15 23:41:13 +01:00
|
|
|
|
|
|
|
# Check if the reset time has passed
|
2023-11-29 14:59:21 +01:00
|
|
|
if next_reset_time and datetime.datetime.now() >= next_reset_time:
|
|
|
|
logging.info("Resetting the health statistics")
|
2020-11-15 23:41:13 +01:00
|
|
|
|
|
|
|
# Update the next_reset_time
|
2023-11-29 14:59:21 +01:00
|
|
|
_get_next_reset_time()
|
2019-03-02 21:56:27 +01:00
|
|
|
|
2023-11-29 14:59:21 +01:00
|
|
|
# Reset statistics
|
|
|
|
session['total_breaks'] += session['breaks']
|
|
|
|
session['total_skipped_breaks'] += session['skipped_breaks']
|
|
|
|
session['total_screen_time'] += session['screen_time']
|
|
|
|
session['total_resets'] += 1
|
|
|
|
session['breaks'] = 0
|
|
|
|
session['skipped_breaks'] = 0
|
|
|
|
session['screen_time'] = 0
|
2019-03-02 02:28:44 +01:00
|
|
|
|
|
|
|
|
2017-10-07 15:10:31 +02:00
|
|
|
def get_widget_content(break_obj):
|
|
|
|
"""
|
|
|
|
Return the statistics.
|
|
|
|
"""
|
2023-11-29 14:59:21 +01:00
|
|
|
global next_reset_time
|
|
|
|
resets = session['total_resets']
|
|
|
|
if session['screen_time'] > 21600 or (session['breaks'] and session['skipped_breaks'] / session['breaks']) >= 0.2:
|
2019-02-24 17:53:20 +01:00
|
|
|
# Unhealthy behavior -> Red broken heart
|
|
|
|
heart = '💔️'
|
|
|
|
else:
|
|
|
|
# Healthy behavior -> Green heart
|
|
|
|
heart = '💚'
|
2023-11-29 14:59:21 +01:00
|
|
|
|
|
|
|
content = [
|
|
|
|
heart,
|
|
|
|
f"BREAKS: {session['breaks']}",
|
|
|
|
f"SKIPPED: {session['skipped_breaks']}",
|
|
|
|
f"SCREEN TIME: {_format_interval(session['screen_time'])}",
|
|
|
|
]
|
|
|
|
|
|
|
|
if resets:
|
|
|
|
content[1] += f" [{round(session['total_breaks'] / resets, 1)}]"
|
|
|
|
content[2] += f" [{round(session['total_skipped_breaks'] / resets, 1)}]"
|
|
|
|
content[3] += f" [{_format_interval(session['total_screen_time'] / resets)}]"
|
|
|
|
|
|
|
|
content = "\t".join(content)
|
|
|
|
if resets:
|
|
|
|
content += f"\n\t[] = average of {resets} reset(s)"
|
|
|
|
if next_reset_time is None:
|
|
|
|
content += f"\n\tSettings error in statistics reset interval: {statistics_reset_cron}"
|
|
|
|
return content
|
2019-02-24 17:53:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def on_start():
|
|
|
|
"""
|
2023-11-29 14:59:21 +01:00
|
|
|
Track the start time.
|
2019-02-24 17:53:20 +01:00
|
|
|
"""
|
2023-11-29 14:59:21 +01:00
|
|
|
global start_time
|
2019-03-02 21:56:27 +01:00
|
|
|
_reset_stats()
|
2023-11-29 14:59:21 +01:00
|
|
|
start_time = datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_next_reset_time():
|
|
|
|
global next_reset_time
|
|
|
|
global session
|
2020-11-15 23:41:13 +01:00
|
|
|
|
|
|
|
try:
|
2023-11-29 14:59:21 +01:00
|
|
|
cron = croniter.croniter(statistics_reset_cron, datetime.datetime.now())
|
|
|
|
next_reset_time = cron.get_next(datetime.datetime)
|
|
|
|
session['next_reset_time'] = next_reset_time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
logging.debug("Health stats will be reset at " + session['next_reset_time'])
|
2020-11-15 23:41:13 +01:00
|
|
|
except:
|
2023-11-29 14:59:21 +01:00
|
|
|
logging.error("Error in statistics reset expression: " + statistics_reset_cron)
|
|
|
|
next_reset_time = None
|
|
|
|
|
|
|
|
|
|
|
|
def _format_interval(seconds):
|
|
|
|
screen_time = round(seconds / 60)
|
|
|
|
hours, minutes = divmod(screen_time, 60)
|
|
|
|
return '{:02d}:{:02d}'.format(hours, minutes)
|