updated readme

This commit is contained in:
randogoth 2025-11-13 15:22:30 +02:00
parent 9bd23d3105
commit e9bc00b1c8
4 changed files with 50 additions and 8 deletions

View file

@ -1,4 +1,7 @@
check-interval: 1 check-interval: 1
status-server:
host: 127.0.0.1
port: 8090
timers: timers:
gaming: gaming:
apps: apps:

View file

@ -253,3 +253,21 @@ class Config(object):
'''How often the usage check should happen''' '''How often the usage check should happen'''
interval = self.data.get('check-interval', 1) interval = self.data.get('check-interval', 1)
return int(interval) return int(interval)
@property
def statusServer(self):
'''Optional host/port overrides for the embedded status server'''
config = self.data.get('status-server') or {}
host = config.get('host')
port = config.get('port')
if port is None:
parsed_port = None
else:
try:
parsed_port = int(port)
except (TypeError, ValueError):
raise ValueError('status-server.port must be an integer')
return {
'host': host,
'port': parsed_port,
}

View file

@ -11,6 +11,10 @@ I'm using this at a raspberry pi gaming station (retropie) to enforce usage limi
```yaml ```yaml
# how often should the script check for apps runtime? # how often should the script check for apps runtime?
check-interval: 1 check-interval: 1
# optional http status endpoint configuration
status-server:
host: 127.0.0.1
port: 8090
# timers: list of apps that will be watched # timers: list of apps that will be watched
timers: timers:
# the app name (list key) # the app name (list key)
@ -40,6 +44,8 @@ warn-command: >
runuser -l randogoth -c 'DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "App Timer" "{timer_name}: {time_left_int} minutes left"' runuser -l randogoth -c 'DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "App Timer" "{timer_name}: {time_left_int} minutes left"'
``` ```
The status server shows timer usage at `http://host:port/` (defaults `127.0.0.1:8090`). Override the bind address or port via the `status-server` block, or keep using the `APP_TIMER_STATUS_HOST` / `APP_TIMER_STATUS_PORT` environment variables if you prefer exporting them in your service unit.
## Installation ## Installation
There's a systemd service file ready for use. There's a systemd service file ready for use.

View file

@ -6,9 +6,6 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from lib import Config from lib import Config
STATUS_SERVER_HOST = os.environ.get('APP_TIMER_STATUS_HOST', '127.0.0.1')
STATUS_SERVER_PORT = int(os.environ.get('APP_TIMER_STATUS_PORT', '8090'))
STATUS_CONTEXT = {'config': None} STATUS_CONTEXT = {'config': None}
@ -166,15 +163,32 @@ class StatusRequestHandler(BaseHTTPRequestHandler):
print("HTTP %s - %s" % (self.log_date_time_string(), format % args)) print("HTTP %s - %s" % (self.log_date_time_string(), format % args))
def start_status_server(): def _resolve_status_server_bindings(config):
server_config = getattr(config, 'statusServer', {}) or {}
env_host = os.environ.get('APP_TIMER_STATUS_HOST')
env_port = os.environ.get('APP_TIMER_STATUS_PORT')
host = env_host or server_config.get('host') or '127.0.0.1'
if env_port is not None:
try: try:
httpd = ThreadingHTTPServer((STATUS_SERVER_HOST, STATUS_SERVER_PORT), StatusRequestHandler) port = int(env_port)
except ValueError:
raise ValueError('APP_TIMER_STATUS_PORT must be an integer')
elif server_config.get('port') is not None:
port = server_config['port']
else:
port = 8090
return host, port
def start_status_server(host, port):
try:
httpd = ThreadingHTTPServer((host, port), StatusRequestHandler)
except OSError as exc: except OSError as exc:
print('Failed to start status server on %s:%s (%s)' % (STATUS_SERVER_HOST, STATUS_SERVER_PORT, exc)) print('Failed to start status server on %s:%s (%s)' % (host, port, exc))
return None return None
thread = threading.Thread(target=httpd.serve_forever, daemon=True) thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start() thread.start()
print('Status server running on http://%s:%s' % (STATUS_SERVER_HOST, STATUS_SERVER_PORT)) print('Status server running on http://%s:%s' % (host, port))
return httpd return httpd
def check_timers(config): def check_timers(config):
@ -197,7 +211,8 @@ def check_timers(config):
config = Config() config = Config()
STATUS_CONTEXT['config'] = config STATUS_CONTEXT['config'] = config
status_server = start_status_server() status_host, status_port = _resolve_status_server_bindings(config)
status_server = start_status_server(status_host, status_port)
while True: while True:
# check config changes # check config changes
if config.hasChanges(): if config.hasChanges():