mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-04 22:35:08 +00:00
* fix(sports): share the ESPN rejected-range memo with the background service BackgroundDataService always sent a season range first and, on a 400, fell back to chunks without recording the rejection, so every background season fetch spent a doomed request and live scoreboards learned nothing from it (or it from them). The worker now consults and sets the same 6-hour memo fetch_espn_scoreboard() uses: a known rejection goes straight to month/day chunks, and if every chunk fails the range is asked once for a real error without re-spending the chunks. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): keep plugin asset and action routes inside their directories POST /plugins/assets/upload, GET /plugins/assets/list and POST /plugins/assets/delete joined the request's plugin_id onto assets/plugins unchecked, so '../../config' created, wrote, listed and deleted outside it. #561 guarded only the route that serves the files. All three now go through path_safety.resolve_under and answer 400 for anything but a plain name, and delete only unlinks a metadata path that resolves into that plugin's uploads directory. PluginManager.get_plugin_directory refuses ids that are not one plain path segment, so /plugins/action (which runs a manifest script from the returned directory) and every other caller get the guard; the action route also rejects such ids up front, covering its no-manager fallback. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): report a no-op plugin update as already up to date update_plugin() returns True both for a real update and for "nothing to do" (a ZIP-installed monorepo plugin already at the registry version, a bundled plugin). With no git commit to compare, POST /plugins/update called every such success "updated successfully", so Check & Update All counted most official plugins as updated on every run. The route now reads what changed off the plugin itself (commit, else manifest version, else last_updated) and returns data.update_status (updated / up_to_date / local_only). The update-all toast is summarised by PluginInstallManager.summarizeUpdateResults from that status, falling back to the message for older servers. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(sports): scoreboard scroll speed no longer follows target_fps sports_scroll computed the crisp speed ladder against the global target_fps whenever limit_refresh_rate_hz was the 100 Hz default. Since frame-locked presentation (#545) the helper steps a fixed number of whole pixels per presented frame and the panel presents at its real refresh, so the General tab's "Scroll Frame Rate" became a speed multiplier: 60 ran a 50 px/s scoreboard at 100 px/s, 200 ran it at 25 px/s. The ladder now uses the display manager's refresh_hz, then display.hardware.limit_refresh_rate_hz, then the default. target_fps is not consulted. Docstrings now say scroll_delay is ignored for pacing (no behaviour change there) and describe the fixed-step model. Tests: replace the tests that pinned target_fps as the ladder refresh and described time-based stepping; assert speed independence from target_fps (unit and end-to-end presented px/s against the real helper), that the fixed per-frame step is applied, and that scroll_delay does not change speed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): escape registry and upload values in plugin manager inline handlers The store, saved-repository and custom-registry buttons built onclick='...(${JSON.stringify(id)})...'. JSON.stringify leaves ' alone, so a custom registry entry whose id contained ' closed the attribute and added its own handler. One helper, jsStringAttr(), now HTML-escapes the JSON literal for every one of those handlers, and the store View button opens only http(s) repo links. The live window.updateImageList (plugins_manager.js loads last, so its copy wins over the file-upload widget's) wrote the uploaded file's original name, path and ids into markup raw; they are escaped now. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): note plugin asset, action and inline handler guards Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(update): let the root pip wrapper install web_interface/requirements.txt Update Code, the automatic update's health check and Install Base Requirements install web_interface/requirements.txt through safe_pip_install.sh, which only allowed the root requirements.txt. The first commit changing that file would fail its dependency install, and the automatic updater rolls back any update whose dependencies did not install -- on every device, for every newer commit. The wrapper now lists both core requirement files. Only their folders are resolved, so a requirements.txt symlinked out of the project is compared by its target and refused (previously the root file's own symlink target was what got allowed). The updater's file list is a named constant, and a test runs the real wrapper (pip stubbed) on every file Update Code and the rollback install. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): do not retry plugin requests that got an HTTP answer PluginAPI.request wrapped everything that was not a structured error as NETWORK_ERROR: a proxy's 502 HTML page (response.json() throws) and a JSON error without error_code included. Check & Update All retries NETWORK_ERROR, so those updates were re-sent five more times with backoff, contrary to the #587 contract that an HTTP error response is the server's answer. NETWORK_ERROR now means only that fetch() rejected. Any HTTP response without an error_code, or with a body that is not JSON, is API_ERROR with the HTTP status attached. Tested against the shipped api_client.js. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scroll): restart the stats window when an idle gap is dropped by size #582 dropped an idle gap from the frame stats two ways: the reset_scroll() sentinel, which also restarts the 5s window timer, and a size guard for scrollers that never call reset_scroll(), which did not. On that path the first real frame after the gap found the boundary overdue and logged a stats line for a one-frame window. Both paths now share one seeding helper. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(update): leave plugins alone when update_core's own rollback fails update_core returns rollback_failed directly when a partial pull or an update whose health check never started cannot be rolled back. run() only held plugins back for 'verifying', so those devices still got new plugin versions and a display restart on top of a core in an unknown state -- the opposite of what the health-check path does, and of the 3.4.0 changelog (plugins are left alone if the rollback fails). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(api): make the REST reference match the api_v3 package Every documented request body, query parameter and response shape was re-checked against the handlers in web_interface/blueprints/api_v3/. Fixes calls that failed as documented (repo_url, action_id/params, files/image_id, font_file+font_family, ?font=, cache key, auto_enable_ap_mode, plugin limit keys), removes the font-override endpoints dropped in #566, corrects response shapes (plugins/config, plugins/schema, health, metrics, operation history, github-status, fonts/catalog, cache/list, logs, wifi, on-demand, SSE streams), and adds the 26 routes it omitted (backup, system auto-update/git, wifi radio, starlark editor, MQTT bridge, status endpoints, skins). Documents the merge semantics of partial JSON saves to /config/main and /plugins/config and the dim-schedule POST accepting GET's days shape, which land in the same change set. Replaces app.py line numbers and the removed api_v3.py path with file and function names. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): remove the General-tab plugin system toggles that did nothing plugin_system.auto_discover, auto_load_enabled and development_mode had General-tab toggles whose help tips promised dormant plugins and verbose logging, but nothing reads them: every enabled plugin is discovered and loaded regardless. Remove the three toggles. The keys stay tolerated in stored configs. The save handler now stores a flag only when a client sends it; treating a missing key as an unchecked box would otherwise rewrite all three to false on every General-tab save, which still posts plugins_directory. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * refactor(scroll): remove dead code left by #523/#570 - Drop the optional scipy.ndimage import and HAS_SCIPY; nothing read them since the numpy blend replaced the scipy path. - Drop ScrollHelper._last_integer_position and frame_time_target, which were written but never read. - Keep target_fps and set_target_fps() but document them as informational: nothing paces off them, yet ledmatrix-elections' test_scroll_pacing.py reads helper.target_fps back and third-party plugins may call the setter. - Fix stale comments: fixed_pixels_per_frame's "use scroll_delay to throttle", set_sub_pixel_scrolling's "default: True", and set_frame_based_scrolling's claim that it steps. The plugins monorepo was grepped for every removed name; none is used. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(fonts): point plugins at plugin_manager.font_manager; drop removed overrides UI FONT_MANAGER.md told plugins to read display_manager.font_manager, which does not exist, so a plugin following it failed to load with AttributeError. The shared FontManager lives on the PluginManager and BasePlugin._get_font_manager() returns it (with a fallback for harnesses). Also removes the Fonts-tab override workflow and element-override panels that #566 deleted, from FONT_MANAGER.md and WEB_INTERFACE_GUIDE.md. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(store): search via /plugins/store/list?query=; send Content-Type on registry curls /plugins/store/search does not exist (404) and the list endpoint reads query, not q. The registry guide's curl examples omitted the JSON Content-Type, so the handlers saw an empty body and answered 400. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(config): use the shared core-key list in the last three private copies StartupValidator warned "Plugin 'auto_update' is enabled but not found" on every display start with auto-update or a dim schedule on; the reserved plugin-id check missed auto_update, sync, location and the rest; and ConfigManager's (uncalled) orphan cleanup would have deleted display, schedule and auto_update. All three now read src/core_config_keys.py, which also gains CORE_SECRETS_KEYS for the github/youtube secrets sections. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): partial JSON saves to /config/main change only what they send A JSON body with one field reset every checkbox in the sections it touched: the MQTT bridge's brightness slider turned off disable_hardware_pulsing, inverse_colors, show_refresh_rate and use_short_date_format, and a timezone-only save turned off web-UI autostart and weekly auto-updates. Missing-means-unchecked now applies only to form posts: form-encoded bodies and the v3 forms, which mark themselves with a hidden __form_section input. Also on the config routes: - vegas_min/max_cycle_duration no longer match the generic *_duration rule, so they stop landing in display_durations and a blank one no longer rejects the whole Display save; - saving from the Raw JSON editor calls start_setup_if_needed like the General form, so enabling auto-update there finishes its setup; - the schedule and dim-schedule POSTs accept the per-day days.<day> shape their GETs return, as well as the flat form keys. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scripts): install plugin dependencies from the configured plugins directory install_plugin_dependencies.sh scanned only plugins/, but the Plugin Store installs into plugin_system.plugins_directory (default plugin-repos), so the documented "Recommended" fix found 0 plugins on every store install. It now reads plugins_directory from config/config.json (relative to the project root or absolute, default plugin-repos) and also scans plugins/ for dev symlinks, installing a plugin reached through both only once. With set -e alone, `pip ... | tee` took tee's exit status, so a failed pip install was reported as success; set -o pipefail. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs: replace stale API names, line numbers and the api_v3.py path - ADVANCED_FEATURES: StreamManager methods that exist (get_next_segment, take_next_group, refresh, advance_cycle, ...), and the real on-demand status envelope ({status, data: {state, service}}) - app.py:199 / :144 / :607-619 line citations and web_interface/blueprints/api_v3.py (now a package) replaced with file and function names in ADVANCED_FEATURES, CONFIG_DEBUGGING, PLUGIN_ARCHITECTURE_SPEC, PLUGIN_QUICK_REFERENCE, PLUGIN_CONFIGURATION_TABS, TROUBLESHOOTING and web_interface/README - CONFIG_DEBUGGING: partial /config/main saves change only sent keys; use /config/raw/main to replace the file; describe where validation runs - TROUBLESHOOTING: clear_cache.py needs --clear-all (no args only prints usage) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scripts): verify the web interface that actually ships, on port 5000 verify_installation.sh failed every healthy install: it required the long-removed web_interface_v2.py and looked for a listener on port 5001, while the web interface binds 5000 (web_interface/start.py). It now checks the files ledmatrix-web.service runs (start_web_conditionally.py, web_interface/start.py, app.py) and port 5000. verify_web_ui.sh had the same 5001 port in its listen check, HTTP probe and printed URLs. Port matches are anchored so :50001 no longer counts as :5000. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(plugins): one display-size contract: display_manager.width/height CLAUDE.md (#580) says to read display_manager.width/height because matrix is None when hardware init fails; the development guide, the safety-harness doc and two DisplayManager docstrings still recommended matrix.width/height. The bundled starlark-apps plugin read matrix.width unguarded, so its magnify recommendation and frame scaling raised in fallback mode (e.g. after the Pi 5 hardware refusal). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(install): make install_service.sh --help print usage instead of installing install_service.sh parsed no arguments, so `sudo ./scripts/install/ install_service.sh --help` (presented as harmless in MIGRATION_GUIDE.md) rewrote ledmatrix.service, ledmatrix-web.service and both update-verify units and enabled/started them. It now handles -h/--help (usage, exit 0, no changes) and rejects any other argument with exit 2 before doing anything. Running it with no arguments, as first_time_install.sh does, is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(scroll): describe the fixed-step model and document frame_hold Since #545 a crisp speed from scroll_config.configure() makes the helper advance a fixed whole-pixel step per presented frame with no clock, and the display manager's frame hold is part of the speed. The docs still described the removed wall-clock model: - scroll_config's module and configure() docstrings said speed is applied in time-based mode and that omitting the hold "falls back to fractional pixels"; omitting it actually runs the scroll frame_hold times too fast. - SCROLL_PERFORMANCE.md said ScrollHelper accumulates elapsed time in both modes, and read a 20 ms stats median as missed refreshes although that is a healthy 50 px/s (hold 2) scroll. It now explains the fixed step, the hold-dependent healthy median, that target_fps plays no part, and that a hand-added scroll_pixels_per_second loses to a schema-default pair. - PLUGIN_API_REFERENCE.md documented set_scrolling_state(is_scrolling) without frame_hold; it now documents the parameter (core 3.4.0) with a configure() + set_scrolling_state example. - update_scroll_position/set_scroll_speed and set_scrolling_state docstrings say the same. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(config): mark target_fps legacy; describe what Vegas scroll_delay does - General tab "Scroll Frame Rate" (target_fps) is labelled legacy: after the sports_scroll fix nothing in core scrolling reads it. The field and its API validation stay so saved configs and plugins that read global_config['target_fps'] keep working. CONFIG_REFERENCE says the same. - Vegas frame_based_scrolling/scroll_delay were described as frame-count stepping at ~50 FPS. Neither steps nor sets a frame rate: frame-based mode converts the speed to px per scroll_delay, clamps it to 0.1-5, and still advances by elapsed time, so the applied speed is clamp(scroll_speed * scroll_delay, 0.1, 5) / scroll_delay px/s. The config comments, render_pipeline comment and CONFIG_REFERENCE rows now say so. No behaviour change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(deps): describe how plugin dependencies are really installed The guides said the web service runs as root, that installs pick --user from os.geteuid(), and quoted a warning and a PluginManager._install_plugin_dependencies() method that don't exist. The web unit runs as the installing user; store installs go through install_requirements_file() and sudo safe_pip_install.sh (root), with a user-level fallback that says so, and load-time installs run in the display service's own (root) interpreter. Manual paths now use the configured plugins directory (plugin-repos/ by default) instead of plugins/, which store installs no longer use, and install_plugin_dependencies.sh is described as scanning that directory. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(update): count local changes one way for the preflight and the pull The automatic update's preflight ignored mode-only changes and anything whose status line contained plugins/ or plugin-repos/, then promised "Automatic updates will not stash your changes". perform_core_update used plain git status (modes count) and ignored only 'plugins/', then ran 'git stash push -- :!plugins', which nothing ever pops. So an edit to a bundled plugin under plugin-repos/, or the installer's chmods on tracked scripts, passed the preflight and was stashed away for good. - auto_update.local_changes() is the one predicate both use: core.fileMode=false, porcelain -z, and plugins/ and plugin-repos/ excluded by leading folder rather than substring (a core file under web_interface/static/v3/js/plugins/ now counts). - Update Code's explicit stash leaves out both plugin folders; the pull's --autostash carries their edits and mode changes across and reapplies them. - The automatic updater calls perform_core_update(stash_local_changes= False), which refuses instead of stashing edits that appeared after the preflight; update_core reports that as 'blocked'. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scripts): diagnostics follow the web autostart default and api_v3 package #556 made a missing web_display_autostart mean "start" (only an explicit false/off keeps the web interface down), but the diagnostics still said otherwise: diagnose_web_ui.sh reported a missing key as "defaults to false", diagnose_web_interface.sh said the web interface "will not start unless this is set to true" and recommended enabling it, and debug_web_manual.py printed False. Troubleshooting a down web UI pointed users at a non-cause. Both shell scripts now evaluate the setting with the launcher's own autostart_enabled() (inline fallback if it cannot be imported) and report on / off / not set (on) / unparseable config; debug_web_manual.py uses the same function. They also check web_interface/blueprints/api_v3/ __init__.py: api_v3.py became a package in #553, so every healthy checkout was reported as missing a file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(install): what install_service.sh installs; verify script port; no sudo for --help install_service.sh installs and starts ledmatrix, ledmatrix-web and the update-verify units, not only ledmatrix.service (systemd/README.md, README.md). MIGRATION_GUIDE presented 'sudo install_service.sh --help' as a harmless check; it now shows --help without sudo and warns what a real run does. SSH_UNAVAILABLE_AFTER_INSTALL: verify_installation.sh checks the web interface on port 5000. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): note update-all, plugin system settings and script fixes Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(display): size the preview after orientation and pixel mappers display_geometry.physical_size claimed to give DisplayManager's answer but only computed cols*chain x rows*parallel. RGBMatrix.width/height are measured after the library's pixel mappers, so a Rotate:90 / orientation 90 chain previewed 128x32 for a 32x128 panel and a U-mapper chain of four 256x32 for 128x64. Model the built-in mappers' size effect as the pinned lib/pixel-mapper.cc does (Rotate, U-mapper, V-mapper, StackToRow, Remap; Mirror and unknown names leave it alone), and move the orientation composition here so DisplayManager and the preview share it. The module docstring no longer claims the sync handshake uses it; that imports only DEFAULT_CHAIN_LENGTH. Audit finding F18. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(display): refuse settings the rgbmatrix library aborts on, on every board The library answers several settings with a NULL matrix or abort() rather than an error, so the display service crash-looped (Restart=on-failure) instead of reaching fallback mode: rows above 64, chain_length above 255 (uint8_t binding setter, documented as "no upper limit"), a misspelled hardware_mapping, and parallel 2-3 on a single-output mapping, reachable from the Display form on the default adafruit-hat(-pwm) mapping. #586 only guarded the Pi 5 subset. - src/matrix_support.py holds the rules for every board (Options::Validate ranges, binding integer types, mapping names and outputs from lib/hardware-mapping.c) plus the Pi 5 ones, and is the one source of the API's numeric ranges. - DisplayManager checks them before building options and raises MatrixSettingsRefused, so a hand-edited config falls back with a logged, reported reason. Emulator mode only warns. - The config API refuses them with a 400 naming the setting; combinations are checked against stored values but reported only when the request sets a field involved. - The hardware status file gains "cause" (settings/library/forced). The fallback log and Display banner give the Pi 5 rebuild hint only for a library failure instead of rebuild + gpio_slowdown advice for every failure; one Pi 5 slowdown recommendation (1-3, start at 1). - The Display form offers classic/classic-pi1 and orientation 90/270 and renders any other stored mapping selected with a warning, so an unrelated save no longer rewrites them; the API accepts 90/270. Audit findings F03, F16, F19, F21. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(display): library limits, template defaults and Pi 5 slowdown - rows 8-64, chain_length 1-255, parallel limited by the mapping's outputs, classic/classic-pi1 mappings and orientation 90/270 documented. - Defaults are the config.template.json values: config migration adds missing keys from the template, so the listed "code defaults" never applied. - One Raspberry Pi 5 gpio_slowdown recommendation: 1-3 in PIO mode, starting at 1. - Troubleshooting describes the refused-settings fallback, and CHANGELOG corrects the Unreleased "no upper limit" entry. Audit findings F19, F20, F21. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scripts): scroll_speeds.py opens the panel with the service's options --measure and --demo built RGBMatrixOptions from a private copy of the display service's builder that had drifted: gpio_slowdown came from display.hardware (default 2) instead of display.runtime (default 3), and rp1_rio, panel_type, disable_hardware_pulsing, inverse_colors, pixel_mapper_config and orientation were skipped, with different defaults (hardware_mapping "regular", pwm_bits 11). A panel needing a high slowdown was measured -- or garbled -- in a setup the service never drives. The option filling in DisplayManager._setup_matrix moves, unchanged, into DisplayManager.apply_matrix_options(options, config), which _setup_matrix calls and the script reuses (overriding only limit_refresh_rate_hz for --measure). The script now loads the whole config rather than the hardware block. Tests pin the script's options to the service's attribute for attribute. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(scripts): scroll_speeds.py recommends keys the resolver honours The ladder ended by telling users to set display_options.scroll_pixels_per_second. scroll_config ranks that key below the scroll_speed + scroll_delay pair, deliberately, and several plugin schemas default the pair into config, so the advised key was silently ignored (a schema-default 1/0.02 pair plus an advised 66 still resolved to 50 px/s). The advice is now the pair that selects the crisp speed exactly (pixels_per_frame every frame_hold/refresh seconds), explains that the pair outranks scroll_pixels_per_second, and gives the scoreboards' per-league scroll_settings.scroll_speed (px/s) form. Tests resolve the printed pair over a schema-default pair and check it lands on the advertised speed and hold. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs: withdraw the target_fps claim for sports_scroll; fix the Vegas speed formula - SPORTS_UNIFICATION.md still presented honouring global target_fps as sports_scroll's added behaviour and its one user-visible gain; note that it was withdrawn because it had become a speed multiplier. - ADVANCED_FEATURES.md gave Vegas scrolling as (scroll_speed / target_fps) * elapsed; the real rule is scroll_speed px/s by elapsed time, through a 0.1-5 px per scroll_delay clamp when frame_based_scrolling is on. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): scroll model fixes Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(dev): link-github links plugins from the ledmatrix-plugins monorepo link-github <name> cloned https://github.com/ChuckBuilds/ledmatrix-<name>.git, and those per-plugin repositories no longer exist: official plugins are directories in the ledmatrix-plugins monorepo. It now clones (or pulls) the monorepo once into the dev directory, finds plugins/<name>, plugins/ledmatrix-<name> or the plugin whose manifest id is <name>, and links it under its manifest id. With an explicit repo URL it still links a single-repository plugin as before. dev_plugins.json: github_user is honoured again (monorepo owner, e.g. a fork), plus plugins_repo and plugins_branch; github_pattern, which was documented but never read, is dropped and warned about. Ships dev_plugins.json.example and git-ignores dev_plugins.json, both of which the guide promised. Reading JSON falls back to python3 when jq is missing (get_plugin_id silently returned nothing without jq). update/status/list find the git checkout above a monorepo plugin directory (its .git is not in the plugin dir), and update pulls a shared checkout once. status no longer exits 1 when nothing is broken. Docs: PLUGIN_DEVELOPMENT_GUIDE (quick start, link-github, configuration, workflow, store integration, hello-world link, submission), and the nonexistent scripts/git-hooks/pre-push-plugin-version and scripts/bump_plugin_version.py replaced with the real rule: bump the manifest version and run update_registry.py. scripts/dev/README.md and CLAUDE.md updated to match. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(scripts): monorepo workspace layout; fix_perms and install READMEs MULTI_ROOT_WORKSPACE_SETUP described one sibling repository per plugin; setup_plugin_repos.py links ../ledmatrix-plugins/plugins/* into plugin-repos/ and update_plugin_repos.py pulls only the monorepo, and the workspace file opens LEDMatrix plus ../ledmatrix-plugins. scripts/fix_perms/README.md listed cache directories fix_cache_permissions.sh never touches and a 'ledmatrix' service user that doesn't exist (also in scripts/install/README.md); adds safe_pip_install.sh. install/README: install_service.sh installs the web and update-verify units too. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(update): keep the rollback's pip retries inside the unit time limit The health check reinstalled the previous requirements by trying the next bash path after any failure, including a 600 s pip timeout. Two files, two paths: up to 40 minutes of pip alone, while systemd stops ledmatrix-update-verify.service at TimeoutStartSec=30min -- killing the rollback half-way and leaving the update 'verifying' until the web UI calls it lost. - Like permission_utils.install_requirements_file, only a sudo refusal moves on to the next bash; a pip that ran and failed or timed out is not repeated. The refusal wording is one list (permission_utils.SUDO_REFUSAL_PHRASES), mirrored in the stdlib-only verifier and pinned equal by a test. - All reinstalls in one rollback share a 600 s budget. - WORST_CASE_SECONDS adds up every timeout on the longest path (27.5 min); a test holds it under the unit's TimeoutStartSec and that under the web UI's VERIFY_LOST_SECONDS. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(plugins): prepare plugin configs one way for load, saves, GET, hot reload and dev tools Plugin config was prepared differently depending on how it arrived: - JSON POST /plugins/config built a partial body on schema defaults, so {"enabled": true} reset every other setting of the plugin. It now merges onto the stored section first, as the form path already did. - Legacy-boolean normalization (#588) ran only at load: GET /plugins/config returned the raw boolean, posting it back failed validation, and hot reload handed plugins the raw section (a legacy dynamic_duration: true came back as a boolean). schema_manager.prepare_plugin_config (normalize, then defaults) is now used by PluginManager.load_plugin, both save paths, GET, the save notifications and DisplayController's hot-reload callback. - The JSON save's filter kept only enabled/display_duration/live_priority and dropped a submitted skin, skin_options or vegas_* tuning key. There is now one core-owned per-plugin list, schema_manager.CORE_PLUGIN_PROPERTIES, used by validation and by the save filter; PluginManager's CORE_OWNED_CONFIG_KEYS is its vegas subset. - Plugin sections posted to /config/main were stored verbatim, including values /plugins/config rejects. They now go through the same preparation (_prepare_plugin_config_for_save, extracted from save_plugin_config), and a failing section rejects the whole save before anything is written. - dev_server read only top-level defaults and let a schema enabled:false win; build_full_config shallow-merged overrides, dropping sibling defaults; the harness extracted defaults differently from the device. loading.build_config now uses the device's extraction and preparation, and dev_server, check_plugin, render_plugin and the harness all use it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(mqtt-bridge): brightness changes apply live and touch nothing else The display service's hot reload applies a saved brightness within a few seconds, and /config/main no longer resets other display settings on a brightness-only JSON body. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): automatic update hardening Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(config): rewrite PLUGIN_CONFIG_ARCHITECTURE for the v3 web UI It described web_interface_v2.py and index_v2.html (both gone), client-side form generation, one POST per field with {key, value}, and 'no nested objects'. The v3 UI renders plugin forms server-side from the schema (pages_v3 partial + plugin_config.html macros, nested sections and x-widgets), posts the whole form once, and save_plugin_config() merges onto the stored section, validates, splits x-secret fields and notifies the plugin. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(mqtt): brightness saves apply via hot reload and leave other settings alone The bridge README said brightness is applied on the display's next restart; the display controller's config hot reload applies it within seconds. It also now states that the bridge's partial JSON save changes only brightness (the /config/main merge fix in this change set). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(update): don't log pip's output from the health check's reinstall pip can echo a private index URL with embedded credentials; permission_utils redacts it, the stdlib-only verifier cannot, so it logs the exit code only. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(config): mark the plugin_system toggles as unused legacy keys auto_discover, auto_load_enabled and development_mode are read by nothing and leave the General tab in this change set (F40). CONFIG_REFERENCE said they were read by the plugin loader; PLUGIN_CONFIGURATION_GUIDE and the REST reference listed them as live settings. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): docs and developer tools group Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): legacy plugin-system toggles no longer count as a General save auto_discover, auto_load_enabled and development_mode have left the General form, so a post carrying only one of them is not a general-settings save and must not treat web_display_autostart and auto_update as unchecked. The plugin_system block itself is left as on main for the branch that reworks it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(changelog): config-save and plugin-config preparation fixes Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(claude): re-check matrix_support.py rules when the library submodule is bumped Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: address Codacy findings on the core audit PR - plugin_manager.prepare_plugin_config: when the fallback legacy-boolean pass also fails, log a warning instead of a bare except/pass. - api_client.js: request() refuses any endpoint that is not a plain path under /api/v3 ("//host", backslashes, ".." or "." segments, whitespace, control characters) with INVALID_ENDPOINT before calling fetch(), and plugin ids are URL-encoded wherever they are put into a URL (also in the app-shell batch load). - test_update_all.js: pins both against the shipped client. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(web): check endpoint control characters without a control-character regex Codacy (ESLint no-control-regex, Biome noControlCharactersInRegex) flags the \x00-\x1f range in checkEndpoint's regex. Test the char codes instead; the endpoints refused are unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(auto-update): make the seed script executable on disk, not only in the index On Linux Repo.publish() commits with -a, which recorded scripts/run.sh as 100644 upstream because the seed file was never chmod +x. The pull then brought in the same mode the installer chmod had made locally, so installer_chmod saw no mode change left to check. The updater was fine: with the upstream commit at 100755 the --autostash carries the device's chmod across. Verified under Linux (WSL, git 2.43): the old helper fails exactly as CI did, the fixed one passes all 63 tests in the file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2888 lines
87 KiB
Markdown
2888 lines
87 KiB
Markdown
# LEDMatrix Plugin Architecture Specification
|
|
|
|
> **Historical design document.** This spec was written *before* the
|
|
> plugin system was built. Most of it is still architecturally
|
|
> accurate, but specific details have drifted from the shipped
|
|
> implementation:
|
|
>
|
|
> - Code paths reference `web_interface_v2.py`; the current web UI is
|
|
> `web_interface/app.py` with v3 Blueprint-based templates.
|
|
> - The example Flask routes use `/api/plugins/*`; the real API
|
|
> blueprint (`web_interface/blueprints/api_v3/`) is mounted at `/api/v3`
|
|
> in `web_interface/app.py`.
|
|
> - The default plugin location is `plugin-repos/` (configurable via
|
|
> `plugin_system.plugins_directory`), not `./plugins/`.
|
|
> - Example imports use `src/plugin_system/base_classes/*_plugin.py`;
|
|
> the shipped base classes live in `src/base_classes/` (e.g.
|
|
> `src.base_classes.sports.SportsCore`, `src.base_classes.hockey.Hockey`).
|
|
> - The "Migration Strategy" and "Implementation Roadmap" sections
|
|
> describe work that has now shipped.
|
|
>
|
|
> For the current system, see:
|
|
> [PLUGIN_DEVELOPMENT_GUIDE.md](PLUGIN_DEVELOPMENT_GUIDE.md),
|
|
> [PLUGIN_API_REFERENCE.md](PLUGIN_API_REFERENCE.md), and
|
|
> [REST_API_REFERENCE.md](REST_API_REFERENCE.md).
|
|
|
|
## Executive Summary
|
|
|
|
This document outlines the transformation of the LEDMatrix project into a modular, plugin-based architecture that enables user-created displays. The goal is to create a flexible, extensible system similar to Home Assistant Community Store (HACS) where users can discover, install, and manage custom display managers from GitHub repositories.
|
|
|
|
### Key Decisions
|
|
|
|
1. **Gradual Migration**: Existing managers remain in core while new plugin infrastructure is built
|
|
2. **Migration Required**: Breaking changes with migration tools provided
|
|
3. **GitHub-Based Store**: Simple discovery system, packages served from GitHub repos
|
|
4. **Plugin Location**: `./plugins/` directory in project root *(actual default is now `plugin-repos/`)*
|
|
|
|
---
|
|
|
|
## Table of Contents
|
|
|
|
1. [Current Architecture Analysis](#1-current-architecture-analysis)
|
|
2. [Plugin System Design](#2-plugin-system-design)
|
|
3. [Plugin Store & Discovery](#3-plugin-store--discovery)
|
|
4. [Web UI Transformation](#4-web-ui-transformation)
|
|
5. [Migration Strategy](#5-migration-strategy)
|
|
6. [Plugin Developer Guidelines](#6-plugin-developer-guidelines)
|
|
7. [Technical Implementation Details](#7-technical-implementation-details)
|
|
8. [Best Practices & Standards](#8-best-practices--standards)
|
|
9. [Security Considerations](#9-security-considerations)
|
|
10. [Implementation Roadmap](#10-implementation-roadmap)
|
|
|
|
---
|
|
|
|
## 1. Current Architecture Analysis
|
|
|
|
### Current System Overview
|
|
|
|
**Core Components:**
|
|
- `display_controller.py`: Main orchestrator, hardcoded manager instantiation
|
|
- `display_manager.py`: Handles LED matrix rendering
|
|
- `config_manager.py`: Loads config from JSON files
|
|
- `cache_manager.py`: Caching layer for API calls
|
|
- `web_interface_v2.py`: Web UI with hardcoded manager references
|
|
|
|
**Manager Pattern:**
|
|
- All managers follow similar initialization: `__init__(config, display_manager, cache_manager)`
|
|
- Common methods: `update()` for data fetching, `display()` for rendering
|
|
- Located in `src/` with various naming conventions
|
|
- Hardcoded imports in display_controller and web_interface
|
|
|
|
**Configuration:**
|
|
- Monolithic `config.json` with sections for each manager
|
|
- Template-based updates via `config.template.json`
|
|
- Secrets in separate `config_secrets.json`
|
|
|
|
### Pain Points
|
|
|
|
1. **Tight Coupling**: Display controller has hardcoded imports for ~40+ managers
|
|
2. **Monolithic Config**: 650+ line config file, hard to navigate
|
|
3. **No Extensibility**: Users can't add custom displays without modifying core
|
|
4. **Update Conflicts**: Config template merges can fail with custom setups
|
|
5. **Scaling Issues**: Adding new displays requires core code changes
|
|
|
|
---
|
|
|
|
## 2. Plugin System Design
|
|
|
|
### Plugin Architecture
|
|
|
|
```
|
|
plugins/
|
|
├── clock-simple/
|
|
│ ├── manifest.json # Plugin metadata
|
|
│ ├── manager.py # Main plugin class
|
|
│ ├── requirements.txt # Python dependencies
|
|
│ ├── assets/ # Plugin-specific assets
|
|
│ │ └── fonts/
|
|
│ ├── config_schema.json # JSON schema for validation
|
|
│ └── README.md # Documentation
|
|
│
|
|
├── nhl-scoreboard/
|
|
│ ├── manifest.json
|
|
│ ├── manager.py
|
|
│ ├── requirements.txt
|
|
│ ├── assets/
|
|
│ │ └── logos/
|
|
│ └── README.md
|
|
│
|
|
└── weather-animated/
|
|
├── manifest.json
|
|
├── manager.py
|
|
├── requirements.txt
|
|
├── assets/
|
|
│ └── animations/
|
|
└── README.md
|
|
```
|
|
|
|
### Plugin Manifest Structure
|
|
|
|
```json
|
|
{
|
|
"id": "clock-simple",
|
|
"name": "Simple Clock",
|
|
"version": "1.0.0",
|
|
"author": "ChuckBuilds",
|
|
"description": "A simple clock display with date",
|
|
"homepage": "https://github.com/ChuckBuilds/ledmatrix-clock-simple",
|
|
"entry_point": "manager.py",
|
|
"class_name": "SimpleClock",
|
|
"category": "time",
|
|
"tags": ["clock", "time", "date"],
|
|
"compatible_versions": [">=2.0.0"],
|
|
"min_ledmatrix_version": "2.0.0",
|
|
"max_ledmatrix_version": "3.0.0",
|
|
"requires": {
|
|
"python": ">=3.9",
|
|
"display_size": {
|
|
"min_width": 64,
|
|
"min_height": 32
|
|
}
|
|
},
|
|
"config_schema": "config_schema.json",
|
|
"assets": {
|
|
"fonts": ["assets/fonts/clock.bdf"],
|
|
"images": []
|
|
},
|
|
"update_interval": 1,
|
|
"default_duration": 15,
|
|
"display_modes": ["clock"],
|
|
"api_requirements": []
|
|
}
|
|
```
|
|
|
|
### Base Plugin Interface
|
|
|
|
```python
|
|
# src/plugin_system/base_plugin.py
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import Dict, Any, Optional
|
|
import logging
|
|
|
|
class BasePlugin(ABC):
|
|
"""
|
|
Base class that all plugins must inherit from.
|
|
Provides standard interface and helper methods.
|
|
"""
|
|
|
|
def __init__(self, plugin_id: str, config: Dict[str, Any],
|
|
display_manager, cache_manager, plugin_manager):
|
|
"""
|
|
Standard initialization for all plugins.
|
|
|
|
Args:
|
|
plugin_id: Unique identifier for this plugin instance
|
|
config: Plugin-specific configuration
|
|
display_manager: Shared display manager instance
|
|
cache_manager: Shared cache manager instance
|
|
plugin_manager: Reference to plugin manager for inter-plugin communication
|
|
"""
|
|
self.plugin_id = plugin_id
|
|
self.config = config
|
|
self.display_manager = display_manager
|
|
self.cache_manager = cache_manager
|
|
self.plugin_manager = plugin_manager
|
|
self.logger = logging.getLogger(f"plugin.{plugin_id}")
|
|
self.enabled = config.get('enabled', True)
|
|
|
|
@abstractmethod
|
|
def update(self) -> None:
|
|
"""
|
|
Fetch/update data for this plugin.
|
|
Called every get_update_interval() seconds when that returns a
|
|
number, otherwise at the static interval: the manifest's
|
|
update_interval, else the plugin config's update_interval, else 60s.
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def display(self, force_clear: bool = False) -> None:
|
|
"""
|
|
Render this plugin's display.
|
|
Called during rotation or on-demand.
|
|
|
|
Args:
|
|
force_clear: If True, clear display before rendering
|
|
"""
|
|
pass
|
|
|
|
def get_update_interval(self) -> Optional[float]:
|
|
"""
|
|
Seconds until update() should run again, decided at runtime.
|
|
Return None (the default) to use the static interval.
|
|
|
|
PluginManager._get_plugin_update_interval calls this on every
|
|
scheduling tick. A number overrides the manifest and is clamped up
|
|
to PluginManager.MIN_DYNAMIC_UPDATE_INTERVAL (5s); None, a raise,
|
|
or a non-finite/non-numeric value falls back to the manifest's
|
|
update_interval, then the plugin config's update_interval, then
|
|
60s. The static value is cached until the plugin reloads; the hook
|
|
is not cached, so it must be cheap and must not raise.
|
|
"""
|
|
return None
|
|
|
|
def get_display_duration(self) -> float:
|
|
"""
|
|
Get the display duration for this plugin instance.
|
|
Can be overridden based on dynamic content.
|
|
|
|
Returns:
|
|
Duration in seconds
|
|
"""
|
|
return self.config.get('display_duration', 15.0)
|
|
|
|
def validate_config(self) -> bool:
|
|
"""
|
|
Validate plugin configuration against schema.
|
|
Called during plugin loading.
|
|
|
|
Returns:
|
|
True if config is valid
|
|
"""
|
|
# Implementation uses config_schema.json
|
|
return True
|
|
|
|
def cleanup(self) -> None:
|
|
"""
|
|
Cleanup resources when plugin is unloaded.
|
|
Override if needed.
|
|
"""
|
|
pass
|
|
|
|
def get_info(self) -> Dict[str, Any]:
|
|
"""
|
|
Return plugin info for display in web UI.
|
|
|
|
Returns:
|
|
Dict with name, version, status, etc.
|
|
"""
|
|
return {
|
|
'id': self.plugin_id,
|
|
'enabled': self.enabled,
|
|
'config': self.config
|
|
}
|
|
```
|
|
|
|
### Plugin Manager
|
|
|
|
```python
|
|
# src/plugin_system/plugin_manager.py
|
|
|
|
import os
|
|
import json
|
|
import importlib
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional, Any
|
|
import logging
|
|
|
|
class PluginManager:
|
|
"""
|
|
Manages plugin discovery, loading, and lifecycle.
|
|
"""
|
|
|
|
def __init__(self, plugins_dir: str = "plugins",
|
|
config_manager=None, display_manager=None, cache_manager=None):
|
|
self.plugins_dir = Path(plugins_dir)
|
|
self.config_manager = config_manager
|
|
self.display_manager = display_manager
|
|
self.cache_manager = cache_manager
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
# Active plugins
|
|
self.plugins: Dict[str, Any] = {}
|
|
self.plugin_manifests: Dict[str, Dict] = {}
|
|
|
|
# Ensure plugins directory exists
|
|
self.plugins_dir.mkdir(exist_ok=True)
|
|
|
|
def discover_plugins(self) -> List[str]:
|
|
"""
|
|
Scan plugins directory for installed plugins.
|
|
|
|
Returns:
|
|
List of plugin IDs
|
|
"""
|
|
discovered = []
|
|
|
|
if not self.plugins_dir.exists():
|
|
self.logger.warning(f"Plugins directory not found: {self.plugins_dir}")
|
|
return discovered
|
|
|
|
for item in self.plugins_dir.iterdir():
|
|
if not item.is_dir():
|
|
continue
|
|
|
|
manifest_path = item / "manifest.json"
|
|
if manifest_path.exists():
|
|
try:
|
|
with open(manifest_path, 'r') as f:
|
|
manifest = json.load(f)
|
|
plugin_id = manifest.get('id')
|
|
if plugin_id:
|
|
discovered.append(plugin_id)
|
|
self.plugin_manifests[plugin_id] = manifest
|
|
self.logger.info(f"Discovered plugin: {plugin_id}")
|
|
except Exception as e:
|
|
self.logger.error(f"Error reading manifest in {item}: {e}")
|
|
|
|
return discovered
|
|
|
|
def load_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Load a plugin by ID.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if loaded successfully
|
|
"""
|
|
if plugin_id in self.plugins:
|
|
self.logger.warning(f"Plugin {plugin_id} already loaded")
|
|
return True
|
|
|
|
manifest = self.plugin_manifests.get(plugin_id)
|
|
if not manifest:
|
|
self.logger.error(f"No manifest found for plugin: {plugin_id}")
|
|
return False
|
|
|
|
try:
|
|
# Add plugin directory to Python path
|
|
plugin_dir = self.plugins_dir / plugin_id
|
|
sys.path.insert(0, str(plugin_dir))
|
|
|
|
# Import the plugin module
|
|
entry_point = manifest.get('entry_point', 'manager.py')
|
|
module_name = entry_point.replace('.py', '')
|
|
module = importlib.import_module(module_name)
|
|
|
|
# Get the plugin class
|
|
class_name = manifest.get('class_name')
|
|
if not class_name:
|
|
self.logger.error(f"No class_name in manifest for {plugin_id}")
|
|
return False
|
|
|
|
plugin_class = getattr(module, class_name)
|
|
|
|
# Get plugin config
|
|
plugin_config = self.config_manager.load_config().get(plugin_id, {})
|
|
|
|
# Instantiate the plugin
|
|
plugin_instance = plugin_class(
|
|
plugin_id=plugin_id,
|
|
config=plugin_config,
|
|
display_manager=self.display_manager,
|
|
cache_manager=self.cache_manager,
|
|
plugin_manager=self
|
|
)
|
|
|
|
# Validate configuration
|
|
if not plugin_instance.validate_config():
|
|
self.logger.error(f"Config validation failed for {plugin_id}")
|
|
return False
|
|
|
|
self.plugins[plugin_id] = plugin_instance
|
|
self.logger.info(f"Loaded plugin: {plugin_id} v{manifest.get('version')}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error loading plugin {plugin_id}: {e}", exc_info=True)
|
|
return False
|
|
finally:
|
|
# Clean up Python path
|
|
if str(plugin_dir) in sys.path:
|
|
sys.path.remove(str(plugin_dir))
|
|
|
|
def unload_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Unload a plugin by ID.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if unloaded successfully
|
|
"""
|
|
if plugin_id not in self.plugins:
|
|
self.logger.warning(f"Plugin {plugin_id} not loaded")
|
|
return False
|
|
|
|
try:
|
|
plugin = self.plugins[plugin_id]
|
|
plugin.cleanup()
|
|
del self.plugins[plugin_id]
|
|
self.logger.info(f"Unloaded plugin: {plugin_id}")
|
|
return True
|
|
except Exception as e:
|
|
self.logger.error(f"Error unloading plugin {plugin_id}: {e}")
|
|
return False
|
|
|
|
def reload_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Reload a plugin (unload and load).
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if reloaded successfully
|
|
"""
|
|
if plugin_id in self.plugins:
|
|
if not self.unload_plugin(plugin_id):
|
|
return False
|
|
return self.load_plugin(plugin_id)
|
|
|
|
def get_plugin(self, plugin_id: str) -> Optional[Any]:
|
|
"""
|
|
Get a loaded plugin instance.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
Plugin instance or None
|
|
"""
|
|
return self.plugins.get(plugin_id)
|
|
|
|
def get_all_plugins(self) -> Dict[str, Any]:
|
|
"""
|
|
Get all loaded plugins.
|
|
|
|
Returns:
|
|
Dict of plugin_id: plugin_instance
|
|
"""
|
|
return self.plugins
|
|
|
|
def get_enabled_plugins(self) -> List[str]:
|
|
"""
|
|
Get list of enabled plugin IDs.
|
|
|
|
Returns:
|
|
List of plugin IDs
|
|
"""
|
|
return [pid for pid, plugin in self.plugins.items() if plugin.enabled]
|
|
```
|
|
|
|
### Display Controller Integration
|
|
|
|
```python
|
|
# Modified src/display_controller.py
|
|
|
|
class DisplayController:
|
|
def __init__(self):
|
|
# ... existing initialization ...
|
|
|
|
# Initialize plugin system
|
|
self.plugin_manager = PluginManager(
|
|
plugins_dir="plugins",
|
|
config_manager=self.config_manager,
|
|
display_manager=self.display_manager,
|
|
cache_manager=self.cache_manager
|
|
)
|
|
|
|
# Discover and load plugins
|
|
discovered = self.plugin_manager.discover_plugins()
|
|
logger.info(f"Discovered {len(discovered)} plugins")
|
|
|
|
for plugin_id in discovered:
|
|
if self.config.get(plugin_id, {}).get('enabled', False):
|
|
self.plugin_manager.load_plugin(plugin_id)
|
|
|
|
# Build available modes from plugins + legacy managers
|
|
self.available_modes = []
|
|
|
|
# Add legacy managers (existing code)
|
|
if self.clock: self.available_modes.append('clock')
|
|
# ... etc ...
|
|
|
|
# Add plugin modes
|
|
for plugin_id, plugin in self.plugin_manager.get_all_plugins().items():
|
|
if plugin.enabled:
|
|
manifest = self.plugin_manager.plugin_manifests.get(plugin_id, {})
|
|
display_modes = manifest.get('display_modes', [plugin_id])
|
|
self.available_modes.extend(display_modes)
|
|
|
|
def display_mode(self, mode: str, force_clear: bool = False):
|
|
"""
|
|
Render a specific mode (legacy or plugin).
|
|
"""
|
|
# Check if it's a plugin mode
|
|
for plugin_id, plugin in self.plugin_manager.get_all_plugins().items():
|
|
manifest = self.plugin_manager.plugin_manifests.get(plugin_id, {})
|
|
if mode in manifest.get('display_modes', []):
|
|
plugin.display(force_clear=force_clear)
|
|
return
|
|
|
|
# Fall back to legacy manager handling
|
|
if mode == 'clock' and self.clock:
|
|
self.clock.display_time(force_clear=force_clear)
|
|
# ... etc ...
|
|
```
|
|
|
|
### Base Classes and Code Reuse
|
|
|
|
#### Philosophy: Core Provides Stable Plugin API
|
|
|
|
The core LEDMatrix provides stable base classes and utilities for common plugin types. This approach balances code reuse with plugin independence.
|
|
|
|
#### Plugin API Base Classes
|
|
|
|
```
|
|
src/
|
|
├── plugin_system/
|
|
│ ├── base_plugin.py # Core plugin interface (required)
|
|
│ └── base_classes/ # Optional base classes for common use cases
|
|
│ ├── __init__.py
|
|
│ ├── sports_plugin.py # Generic sports displays
|
|
│ ├── hockey_plugin.py # Hockey-specific features
|
|
│ ├── basketball_plugin.py # Basketball-specific features
|
|
│ ├── baseball_plugin.py # Baseball-specific features
|
|
│ ├── football_plugin.py # Football-specific features
|
|
│ └── display_helpers.py # Common rendering utilities
|
|
```
|
|
|
|
#### Sports Plugin Base Class
|
|
|
|
```python
|
|
# src/plugin_system/base_classes/sports_plugin.py
|
|
|
|
from src.plugin_system.base_plugin import BasePlugin
|
|
from typing import List, Dict, Any, Optional
|
|
import requests
|
|
|
|
class SportsPlugin(BasePlugin):
|
|
"""
|
|
Base class for sports-related plugins.
|
|
|
|
API Version: 1.0.0
|
|
Stability: Stable - maintains backward compatibility
|
|
|
|
Provides common functionality:
|
|
- Favorite team filtering
|
|
- ESPN API integration
|
|
- Standard game data structures
|
|
- Common rendering methods
|
|
"""
|
|
|
|
API_VERSION = "1.0.0"
|
|
|
|
def __init__(self, plugin_id, config, display_manager, cache_manager, plugin_manager):
|
|
super().__init__(plugin_id, config, display_manager, cache_manager, plugin_manager)
|
|
|
|
# Standard sports plugin configuration
|
|
self.favorite_teams = config.get('favorite_teams', [])
|
|
self.show_favorite_only = config.get('show_favorite_teams_only', True)
|
|
self.show_odds = config.get('show_odds', True)
|
|
self.show_records = config.get('show_records', True)
|
|
self.logo_dir = config.get('logo_dir', 'assets/sports/logos')
|
|
|
|
def filter_by_favorites(self, games: List[Dict]) -> List[Dict]:
|
|
"""
|
|
Filter games to show only favorite teams.
|
|
|
|
Args:
|
|
games: List of game dictionaries
|
|
|
|
Returns:
|
|
Filtered list of games
|
|
"""
|
|
if not self.show_favorite_only or not self.favorite_teams:
|
|
return games
|
|
|
|
return [g for g in games if self._is_favorite_game(g)]
|
|
|
|
def _is_favorite_game(self, game: Dict) -> bool:
|
|
"""Check if game involves a favorite team."""
|
|
home_team = game.get('home_team', '')
|
|
away_team = game.get('away_team', '')
|
|
return home_team in self.favorite_teams or away_team in self.favorite_teams
|
|
|
|
def fetch_espn_data(self, sport: str, endpoint: str = "scoreboard",
|
|
params: Dict = None) -> Optional[Dict]:
|
|
"""
|
|
Fetch data from ESPN API.
|
|
|
|
Args:
|
|
sport: Sport identifier (e.g., 'hockey/nhl', 'basketball/nba')
|
|
endpoint: API endpoint (default: 'scoreboard')
|
|
params: Query parameters
|
|
|
|
Returns:
|
|
API response data or None on error
|
|
"""
|
|
url = f"https://site.api.espn.com/apis/site/v2/sports/{sport}/{endpoint}"
|
|
cache_key = f"espn_{sport}_{endpoint}"
|
|
|
|
# Try cache first
|
|
cached = self.cache_manager.get(cache_key, max_age=60)
|
|
if cached:
|
|
return cached
|
|
|
|
try:
|
|
response = requests.get(url, params=params, timeout=10)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
|
|
# Cache the response
|
|
self.cache_manager.set(cache_key, data)
|
|
|
|
return data
|
|
except Exception as e:
|
|
self.logger.error(f"Error fetching ESPN data: {e}")
|
|
return None
|
|
|
|
def render_team_logo(self, team_abbr: str, x: int, y: int, size: int = 16):
|
|
"""
|
|
Render a team logo at specified position.
|
|
|
|
Args:
|
|
team_abbr: Team abbreviation
|
|
x, y: Position on display
|
|
size: Logo size in pixels
|
|
"""
|
|
from pathlib import Path
|
|
from PIL import Image
|
|
|
|
# Try plugin assets first
|
|
logo_path = Path(self.plugin_id) / "assets" / "logos" / f"{team_abbr}.png"
|
|
|
|
# Fall back to core assets
|
|
if not logo_path.exists():
|
|
logo_path = Path(self.logo_dir) / f"{team_abbr}.png"
|
|
|
|
if logo_path.exists():
|
|
try:
|
|
logo = Image.open(logo_path)
|
|
logo = logo.resize((size, size), Image.LANCZOS)
|
|
self.display_manager.image.paste(logo, (x, y))
|
|
except Exception as e:
|
|
self.logger.error(f"Error rendering logo for {team_abbr}: {e}")
|
|
|
|
def render_score(self, away_team: str, away_score: int,
|
|
home_team: str, home_score: int,
|
|
x: int, y: int):
|
|
"""
|
|
Render a game score in standard format.
|
|
|
|
Args:
|
|
away_team, away_score: Away team info
|
|
home_team, home_score: Home team info
|
|
x, y: Position on display
|
|
"""
|
|
# Render away team
|
|
self.render_team_logo(away_team, x, y)
|
|
self.display_manager.draw_text(
|
|
f"{away_score}",
|
|
x=x + 20, y=y + 4,
|
|
color=(255, 255, 255)
|
|
)
|
|
|
|
# Render home team
|
|
self.render_team_logo(home_team, x + 40, y)
|
|
self.display_manager.draw_text(
|
|
f"{home_score}",
|
|
x=x + 60, y=y + 4,
|
|
color=(255, 255, 255)
|
|
)
|
|
```
|
|
|
|
#### Hockey Plugin Base Class
|
|
|
|
```python
|
|
# src/plugin_system/base_classes/hockey_plugin.py
|
|
|
|
from src.plugin_system.base_classes.sports_plugin import SportsPlugin
|
|
from typing import Dict, List, Optional
|
|
|
|
class HockeyPlugin(SportsPlugin):
|
|
"""
|
|
Base class for hockey plugins (NHL, NCAA Hockey, etc).
|
|
|
|
API Version: 1.0.0
|
|
Provides hockey-specific features:
|
|
- Period handling
|
|
- Power play indicators
|
|
- Shots on goal display
|
|
"""
|
|
|
|
def __init__(self, plugin_id, config, display_manager, cache_manager, plugin_manager):
|
|
super().__init__(plugin_id, config, display_manager, cache_manager, plugin_manager)
|
|
|
|
# Hockey-specific config
|
|
self.show_shots = config.get('show_shots_on_goal', True)
|
|
self.show_power_play = config.get('show_power_play', True)
|
|
|
|
def fetch_hockey_games(self, league: str = "nhl") -> List[Dict]:
|
|
"""
|
|
Fetch hockey games from ESPN.
|
|
|
|
Args:
|
|
league: League identifier (nhl, college-hockey)
|
|
|
|
Returns:
|
|
List of standardized game dictionaries
|
|
"""
|
|
sport = f"hockey/{league}"
|
|
data = self.fetch_espn_data(sport)
|
|
|
|
if not data:
|
|
return []
|
|
|
|
return self._parse_hockey_games(data.get('events', []))
|
|
|
|
def _parse_hockey_games(self, events: List[Dict]) -> List[Dict]:
|
|
"""
|
|
Parse ESPN hockey events into standardized format.
|
|
|
|
Returns:
|
|
List of dicts with keys: id, home_team, away_team, home_score,
|
|
away_score, period, clock, status, power_play, shots
|
|
"""
|
|
games = []
|
|
|
|
for event in events:
|
|
try:
|
|
competition = event['competitions'][0]
|
|
|
|
game = {
|
|
'id': event['id'],
|
|
'home_team': competition['competitors'][0]['team']['abbreviation'],
|
|
'away_team': competition['competitors'][1]['team']['abbreviation'],
|
|
'home_score': int(competition['competitors'][0]['score']),
|
|
'away_score': int(competition['competitors'][1]['score']),
|
|
'status': competition['status']['type']['state'],
|
|
'period': competition.get('period', 0),
|
|
'clock': competition.get('displayClock', ''),
|
|
'power_play': self._extract_power_play(competition),
|
|
'shots': self._extract_shots(competition)
|
|
}
|
|
|
|
games.append(game)
|
|
except (KeyError, IndexError, ValueError) as e:
|
|
self.logger.error(f"Error parsing hockey game: {e}")
|
|
continue
|
|
|
|
return games
|
|
|
|
def render_hockey_game(self, game: Dict, x: int = 0, y: int = 0):
|
|
"""
|
|
Render a hockey game in standard format.
|
|
|
|
Args:
|
|
game: Game dictionary (from _parse_hockey_games)
|
|
x, y: Position on display
|
|
"""
|
|
# Render score
|
|
self.render_score(
|
|
game['away_team'], game['away_score'],
|
|
game['home_team'], game['home_score'],
|
|
x, y
|
|
)
|
|
|
|
# Render period and clock
|
|
if game['status'] == 'in':
|
|
period_text = f"P{game['period']} {game['clock']}"
|
|
self.display_manager.draw_text(
|
|
period_text,
|
|
x=x, y=y + 20,
|
|
color=(255, 255, 0)
|
|
)
|
|
|
|
# Render power play indicator
|
|
if self.show_power_play and game.get('power_play'):
|
|
self.display_manager.draw_text(
|
|
"PP",
|
|
x=x + 80, y=y + 20,
|
|
color=(255, 0, 0)
|
|
)
|
|
|
|
# Render shots
|
|
if self.show_shots and game.get('shots'):
|
|
shots_text = f"SOG: {game['shots']['away']}-{game['shots']['home']}"
|
|
self.display_manager.draw_text(
|
|
shots_text,
|
|
x=x, y=y + 28,
|
|
color=(200, 200, 200),
|
|
small_font=True
|
|
)
|
|
|
|
def _extract_power_play(self, competition: Dict) -> Optional[str]:
|
|
"""Extract power play information from competition data."""
|
|
# Implementation details...
|
|
return None
|
|
|
|
def _extract_shots(self, competition: Dict) -> Optional[Dict]:
|
|
"""Extract shots on goal from competition data."""
|
|
# Implementation details...
|
|
return None
|
|
```
|
|
|
|
#### Using Base Classes in Plugins
|
|
|
|
**Example: NHL Scores Plugin**
|
|
|
|
```python
|
|
# plugins/nhl-scores/manager.py
|
|
|
|
from src.plugin_system.base_classes.hockey_plugin import HockeyPlugin
|
|
|
|
class NHLScoresPlugin(HockeyPlugin):
|
|
"""
|
|
NHL Scores plugin using stable hockey base class.
|
|
|
|
Inherits all hockey functionality, just needs to implement
|
|
update() and display() for NHL-specific behavior.
|
|
"""
|
|
|
|
def update(self):
|
|
"""Fetch NHL games using inherited method."""
|
|
self.games = self.fetch_hockey_games(league="nhl")
|
|
|
|
# Filter to favorites
|
|
if self.show_favorite_only:
|
|
self.games = self.filter_by_favorites(self.games)
|
|
|
|
self.logger.info(f"Fetched {len(self.games)} NHL games")
|
|
|
|
def display(self, force_clear=False):
|
|
"""Display NHL games using inherited rendering."""
|
|
if force_clear:
|
|
self.display_manager.clear()
|
|
|
|
if not self.games:
|
|
self._show_no_games()
|
|
return
|
|
|
|
# Show first game using inherited method
|
|
self.render_hockey_game(self.games[0], x=0, y=5)
|
|
|
|
self.display_manager.update_display()
|
|
|
|
def _show_no_games(self):
|
|
"""Show no games message."""
|
|
self.display_manager.draw_text(
|
|
"No NHL games",
|
|
x=5, y=15,
|
|
color=(255, 255, 255)
|
|
)
|
|
```
|
|
|
|
**Example: Custom Hockey Plugin (NCAA Hockey)**
|
|
|
|
```python
|
|
# plugins/ncaa-hockey/manager.py
|
|
|
|
from src.plugin_system.base_classes.hockey_plugin import HockeyPlugin
|
|
|
|
class NCAAHockeyPlugin(HockeyPlugin):
|
|
"""
|
|
NCAA Hockey plugin - different league, same base class.
|
|
"""
|
|
|
|
def update(self):
|
|
"""Fetch NCAA hockey games."""
|
|
self.games = self.fetch_hockey_games(league="college-hockey")
|
|
self.games = self.filter_by_favorites(self.games)
|
|
|
|
def display(self, force_clear=False):
|
|
"""Display using inherited hockey rendering."""
|
|
if force_clear:
|
|
self.display_manager.clear()
|
|
|
|
if self.games:
|
|
# Use inherited rendering method
|
|
self.render_hockey_game(self.games[0], x=0, y=5)
|
|
|
|
self.display_manager.update_display()
|
|
```
|
|
|
|
#### API Versioning and Compatibility
|
|
|
|
**Manifest declares required API version:**
|
|
|
|
```json
|
|
{
|
|
"id": "nhl-scores",
|
|
"plugin_api_version": "1.0.0",
|
|
"compatible_versions": [">=2.0.0"]
|
|
}
|
|
```
|
|
|
|
**Plugin Manager checks compatibility:**
|
|
|
|
```python
|
|
# In plugin_manager.py
|
|
|
|
def load_plugin(self, plugin_id: str) -> bool:
|
|
manifest = self.plugin_manifests.get(plugin_id)
|
|
|
|
# Check API compatibility
|
|
required_api = manifest.get('plugin_api_version', '1.0.0')
|
|
|
|
from src.plugin_system.base_classes.sports_plugin import SportsPlugin
|
|
current_api = SportsPlugin.API_VERSION
|
|
|
|
if not self._is_api_compatible(required_api, current_api):
|
|
self.logger.error(
|
|
f"Plugin {plugin_id} requires API {required_api}, "
|
|
f"but {current_api} is available. Please update plugin or core."
|
|
)
|
|
return False
|
|
|
|
# Continue loading...
|
|
return True
|
|
|
|
def _is_api_compatible(self, required: str, current: str) -> bool:
|
|
"""
|
|
Check if required API version is compatible with current.
|
|
Uses semantic versioning: MAJOR.MINOR.PATCH
|
|
|
|
- Same major version = compatible
|
|
- Different major version = incompatible (breaking changes)
|
|
"""
|
|
req_major = int(required.split('.')[0])
|
|
cur_major = int(current.split('.')[0])
|
|
|
|
return req_major == cur_major
|
|
```
|
|
|
|
#### Handling API Changes
|
|
|
|
**Non-Breaking Changes (Minor/Patch versions):**
|
|
|
|
```python
|
|
# v1.0.0 -> v1.1.0 (new optional parameter)
|
|
class HockeyPlugin:
|
|
def render_hockey_game(self, game, x=0, y=0, show_penalties=False):
|
|
# Added optional parameter, old code still works
|
|
pass
|
|
```
|
|
|
|
**Breaking Changes (Major version):**
|
|
|
|
```python
|
|
# v1.x.x
|
|
class HockeyPlugin:
|
|
def render_hockey_game(self, game, x=0, y=0):
|
|
pass
|
|
|
|
# v2.0.0 (breaking change)
|
|
class HockeyPlugin:
|
|
API_VERSION = "2.0.0"
|
|
|
|
def render_hockey_game(self, game, position=(0, 0), style="default"):
|
|
# Changed signature - plugins need updates
|
|
pass
|
|
```
|
|
|
|
Plugins requiring v1.x would fail to load with v2.0.0 core, prompting user to update.
|
|
|
|
#### Benefits of This Approach
|
|
|
|
1. **No Code Duplication**: Plugins import from core
|
|
2. **Consistent Behavior**: All hockey plugins render the same way
|
|
3. **Easy Updates**: Bug fixes in base classes benefit all plugins
|
|
4. **Smaller Plugins**: No need to bundle common code
|
|
5. **Clear API Contract**: Versioned, stable interface
|
|
6. **Flexibility**: Plugins can override any method
|
|
|
|
#### When NOT to Use Base Classes
|
|
|
|
Plugins should implement BasePlugin directly when:
|
|
|
|
- Creating completely custom displays (no common patterns)
|
|
- Needing full control over every aspect
|
|
- Prototyping new display types
|
|
- External data sources (not ESPN)
|
|
|
|
Example:
|
|
```python
|
|
# plugins/custom-animation/manager.py
|
|
|
|
from src.plugin_system.base_plugin import BasePlugin
|
|
|
|
class CustomAnimationPlugin(BasePlugin):
|
|
"""Fully custom plugin - doesn't need sports base classes."""
|
|
|
|
def update(self):
|
|
# Custom data fetching
|
|
pass
|
|
|
|
def display(self, force_clear=False):
|
|
# Custom rendering
|
|
pass
|
|
```
|
|
|
|
#### Migration Strategy for Existing Base Classes
|
|
|
|
**Current base classes** (`src/base_classes/`):
|
|
- `sports.py`
|
|
- `hockey.py`
|
|
- `basketball.py`
|
|
- etc.
|
|
|
|
**Phase 1**: Create new plugin-specific base classes
|
|
- Keep old ones for backward compatibility
|
|
- New base classes in `src/plugin_system/base_classes/`
|
|
|
|
**Phase 2**: Migrate existing managers
|
|
- Legacy managers still use old base classes
|
|
- New plugins use new base classes
|
|
|
|
**Phase 3**: Deprecate old base classes (v3.0)
|
|
- Remove old `src/base_classes/`
|
|
- All code uses plugin system base classes
|
|
|
|
---
|
|
|
|
## 3. Plugin Store & Discovery
|
|
|
|
### Store Architecture (HACS-inspired)
|
|
|
|
The plugin store will be a simple GitHub-based discovery system where:
|
|
|
|
1. **Central Registry**: A GitHub repo (`ChuckBuilds/ledmatrix-plugin-registry`) contains a JSON file listing approved plugins
|
|
2. **Plugin Repos**: Individual GitHub repos contain plugin code
|
|
3. **Installation**: Clone/download plugin repos directly to `./plugins/` directory
|
|
4. **Updates**: Git pull or re-download from GitHub
|
|
|
|
### Registry Structure
|
|
|
|
```json
|
|
// ledmatrix-plugin-registry/plugins.json
|
|
{
|
|
"version": "1.0.0",
|
|
"plugins": [
|
|
{
|
|
"id": "clock-simple",
|
|
"name": "Simple Clock",
|
|
"description": "A simple clock display with date",
|
|
"author": "ChuckBuilds",
|
|
"category": "time",
|
|
"tags": ["clock", "time", "date"],
|
|
"repo": "https://github.com/ChuckBuilds/ledmatrix-clock-simple",
|
|
"branch": "main",
|
|
"versions": [
|
|
{
|
|
"version": "1.0.0",
|
|
"ledmatrix_min_version": "2.0.0",
|
|
"released": "2025-01-15",
|
|
"download_url": "https://github.com/ChuckBuilds/ledmatrix-clock-simple/archive/refs/tags/v1.0.0.zip"
|
|
}
|
|
],
|
|
"stars": 45,
|
|
"downloads": 1234,
|
|
"last_updated": "2025-01-15",
|
|
"verified": true
|
|
},
|
|
{
|
|
"id": "weather-animated",
|
|
"name": "Animated Weather",
|
|
"description": "Weather display with animated icons",
|
|
"author": "SomeUser",
|
|
"category": "weather",
|
|
"tags": ["weather", "animated", "forecast"],
|
|
"repo": "https://github.com/SomeUser/ledmatrix-weather-animated",
|
|
"branch": "main",
|
|
"versions": [
|
|
{
|
|
"version": "2.1.0",
|
|
"ledmatrix_min_version": "2.0.0",
|
|
"released": "2025-01-10",
|
|
"download_url": "https://github.com/SomeUser/ledmatrix-weather-animated/archive/refs/tags/v2.1.0.zip"
|
|
}
|
|
],
|
|
"stars": 89,
|
|
"downloads": 2341,
|
|
"last_updated": "2025-01-10",
|
|
"verified": true
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
### Plugin Store Manager
|
|
|
|
```python
|
|
# src/plugin_system/store_manager.py
|
|
|
|
import requests
|
|
import subprocess
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import List, Dict, Optional
|
|
import logging
|
|
|
|
class PluginStoreManager:
|
|
"""
|
|
Manages plugin discovery, installation, and updates from GitHub.
|
|
"""
|
|
|
|
REGISTRY_URL = "https://raw.githubusercontent.com/ChuckBuilds/ledmatrix-plugin-registry/main/plugins.json"
|
|
|
|
def __init__(self, plugins_dir: str = "plugins"):
|
|
self.plugins_dir = Path(plugins_dir)
|
|
self.logger = logging.getLogger(__name__)
|
|
self.registry_cache = None
|
|
|
|
def fetch_registry(self, force_refresh: bool = False) -> Dict:
|
|
"""
|
|
Fetch the plugin registry from GitHub.
|
|
|
|
Args:
|
|
force_refresh: Force refresh even if cached
|
|
|
|
Returns:
|
|
Registry data
|
|
"""
|
|
if self.registry_cache and not force_refresh:
|
|
return self.registry_cache
|
|
|
|
try:
|
|
response = requests.get(self.REGISTRY_URL, timeout=10)
|
|
response.raise_for_status()
|
|
self.registry_cache = response.json()
|
|
self.logger.info(f"Fetched registry with {len(self.registry_cache['plugins'])} plugins")
|
|
return self.registry_cache
|
|
except Exception as e:
|
|
self.logger.error(f"Error fetching registry: {e}")
|
|
return {"plugins": []}
|
|
|
|
def search_plugins(self, query: str = "", category: str = "", tags: List[str] = []) -> List[Dict]:
|
|
"""
|
|
Search for plugins in the registry.
|
|
|
|
Args:
|
|
query: Search query string
|
|
category: Filter by category
|
|
tags: Filter by tags
|
|
|
|
Returns:
|
|
List of matching plugins
|
|
"""
|
|
registry = self.fetch_registry()
|
|
plugins = registry.get('plugins', [])
|
|
|
|
results = []
|
|
for plugin in plugins:
|
|
# Category filter
|
|
if category and plugin.get('category') != category:
|
|
continue
|
|
|
|
# Tags filter
|
|
if tags and not any(tag in plugin.get('tags', []) for tag in tags):
|
|
continue
|
|
|
|
# Query search
|
|
if query:
|
|
query_lower = query.lower()
|
|
if not any([
|
|
query_lower in plugin.get('name', '').lower(),
|
|
query_lower in plugin.get('description', '').lower(),
|
|
query_lower in plugin.get('id', '').lower()
|
|
]):
|
|
continue
|
|
|
|
results.append(plugin)
|
|
|
|
return results
|
|
|
|
def install_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Install a plugin from GitHub.
|
|
Always clones or downloads the latest commit from the repository's default branch.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if installed successfully
|
|
"""
|
|
registry = self.fetch_registry()
|
|
plugin_info = next((p for p in registry['plugins'] if p['id'] == plugin_id), None)
|
|
|
|
if not plugin_info:
|
|
self.logger.error(f"Plugin not found in registry: {plugin_id}")
|
|
return False
|
|
|
|
try:
|
|
# Get version info
|
|
if version == "latest":
|
|
version_info = plugin_info['versions'][0] # First is latest
|
|
else:
|
|
version_info = next((v for v in plugin_info['versions'] if v['version'] == version), None)
|
|
if not version_info:
|
|
self.logger.error(f"Version not found: {version}")
|
|
return False
|
|
|
|
# Get repo URL
|
|
repo_url = plugin_info['repo']
|
|
|
|
# Clone or download
|
|
plugin_path = self.plugins_dir / plugin_id
|
|
|
|
if plugin_path.exists():
|
|
self.logger.warning(f"Plugin directory already exists: {plugin_id}")
|
|
shutil.rmtree(plugin_path)
|
|
|
|
# Try git clone first
|
|
try:
|
|
subprocess.run(
|
|
['git', 'clone', '--depth', '1', '--branch', version_info['version'],
|
|
repo_url, str(plugin_path)],
|
|
check=True,
|
|
capture_output=True
|
|
)
|
|
self.logger.info(f"Cloned plugin {plugin_id} v{version_info['version']}")
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
# Fall back to download
|
|
self.logger.info("Git not available, downloading zip...")
|
|
download_url = version_info['download_url']
|
|
response = requests.get(download_url, timeout=30)
|
|
response.raise_for_status()
|
|
|
|
# Extract zip (implementation needed)
|
|
# ...
|
|
|
|
# Install Python dependencies
|
|
requirements_file = plugin_path / "requirements.txt"
|
|
if requirements_file.exists():
|
|
subprocess.run(
|
|
['pip3', 'install', '--break-system-packages', '-r', str(requirements_file)],
|
|
check=True
|
|
)
|
|
self.logger.info(f"Installed dependencies for {plugin_id}")
|
|
|
|
self.logger.info(f"Successfully installed plugin: {plugin_id}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error installing plugin {plugin_id}: {e}")
|
|
return False
|
|
|
|
def uninstall_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Uninstall a plugin.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if uninstalled successfully
|
|
"""
|
|
plugin_path = self.plugins_dir / plugin_id
|
|
|
|
if not plugin_path.exists():
|
|
self.logger.warning(f"Plugin not found: {plugin_id}")
|
|
return False
|
|
|
|
try:
|
|
shutil.rmtree(plugin_path)
|
|
self.logger.info(f"Uninstalled plugin: {plugin_id}")
|
|
return True
|
|
except Exception as e:
|
|
self.logger.error(f"Error uninstalling plugin {plugin_id}: {e}")
|
|
return False
|
|
|
|
def update_plugin(self, plugin_id: str) -> bool:
|
|
"""
|
|
Update a plugin to the latest version.
|
|
|
|
Args:
|
|
plugin_id: Plugin identifier
|
|
|
|
Returns:
|
|
True if updated successfully
|
|
"""
|
|
plugin_path = self.plugins_dir / plugin_id
|
|
|
|
if not plugin_path.exists():
|
|
self.logger.error(f"Plugin not installed: {plugin_id}")
|
|
return False
|
|
|
|
try:
|
|
# Try git pull first
|
|
git_dir = plugin_path / ".git"
|
|
if git_dir.exists():
|
|
result = subprocess.run(
|
|
['git', '-C', str(plugin_path), 'pull'],
|
|
capture_output=True,
|
|
text=True
|
|
)
|
|
if result.returncode == 0:
|
|
self.logger.info(f"Updated plugin {plugin_id} via git pull")
|
|
return True
|
|
|
|
# Fall back to re-download
|
|
self.logger.info(f"Re-downloading plugin {plugin_id}")
|
|
return self.install_plugin(plugin_id)
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error updating plugin {plugin_id}: {e}")
|
|
return False
|
|
|
|
def install_from_url(self, repo_url: str, plugin_id: str = None) -> bool:
|
|
"""
|
|
Install a plugin directly from a GitHub URL (for custom/unlisted plugins).
|
|
|
|
Args:
|
|
repo_url: GitHub repository URL
|
|
plugin_id: Optional custom plugin ID (extracted from manifest if not provided)
|
|
|
|
Returns:
|
|
True if installed successfully
|
|
"""
|
|
try:
|
|
# Clone to temporary location
|
|
temp_dir = self.plugins_dir / ".temp_install"
|
|
if temp_dir.exists():
|
|
shutil.rmtree(temp_dir)
|
|
|
|
subprocess.run(
|
|
['git', 'clone', '--depth', '1', repo_url, str(temp_dir)],
|
|
check=True,
|
|
capture_output=True
|
|
)
|
|
|
|
# Read manifest to get plugin ID
|
|
manifest_path = temp_dir / "manifest.json"
|
|
if not manifest_path.exists():
|
|
self.logger.error("No manifest.json found in repository")
|
|
shutil.rmtree(temp_dir)
|
|
return False
|
|
|
|
with open(manifest_path, 'r') as f:
|
|
manifest = json.load(f)
|
|
|
|
plugin_id = plugin_id or manifest.get('id')
|
|
if not plugin_id:
|
|
self.logger.error("No plugin ID found in manifest")
|
|
shutil.rmtree(temp_dir)
|
|
return False
|
|
|
|
# Move to plugins directory
|
|
final_path = self.plugins_dir / plugin_id
|
|
if final_path.exists():
|
|
shutil.rmtree(final_path)
|
|
|
|
shutil.move(str(temp_dir), str(final_path))
|
|
|
|
# Install dependencies
|
|
requirements_file = final_path / "requirements.txt"
|
|
if requirements_file.exists():
|
|
subprocess.run(
|
|
['pip3', 'install', '--break-system-packages', '-r', str(requirements_file)],
|
|
check=True
|
|
)
|
|
|
|
self.logger.info(f"Installed plugin from URL: {plugin_id}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"Error installing from URL: {e}")
|
|
if temp_dir.exists():
|
|
shutil.rmtree(temp_dir)
|
|
return False
|
|
```
|
|
|
|
---
|
|
|
|
## 4. Web UI Transformation
|
|
|
|
### New Web UI Structure
|
|
|
|
The web UI needs significant updates to support dynamic plugin management:
|
|
|
|
**New Sections:**
|
|
1. **Plugin Store** - Browse, search, install plugins
|
|
2. **Plugin Manager** - View installed, enable/disable, configure
|
|
3. **Display Rotation** - Drag-and-drop ordering of active displays
|
|
4. **Plugin Settings** - Dynamic configuration UI generated from schemas
|
|
|
|
### Plugin Store UI (React Component Structure)
|
|
|
|
```javascript
|
|
// New: templates/src/components/PluginStore.jsx
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
export default function PluginStore() {
|
|
const [plugins, setPlugins] = useState([]);
|
|
const [search, setSearch] = useState('');
|
|
const [category, setCategory] = useState('all');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
useEffect(() => {
|
|
fetchPlugins();
|
|
}, []);
|
|
|
|
const fetchPlugins = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const response = await fetch('/api/plugins/store/list');
|
|
const data = await response.json();
|
|
setPlugins(data.plugins);
|
|
} catch (error) {
|
|
console.error('Error fetching plugins:', error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const installPlugin = async (pluginId) => {
|
|
try {
|
|
const response = await fetch('/api/plugins/install', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ plugin_id: pluginId })
|
|
});
|
|
|
|
if (response.ok) {
|
|
alert('Plugin installed successfully!');
|
|
// Refresh plugin list
|
|
fetchPlugins();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error installing plugin:', error);
|
|
}
|
|
};
|
|
|
|
const filteredPlugins = plugins.filter(plugin => {
|
|
const matchesSearch = search === '' ||
|
|
plugin.name.toLowerCase().includes(search.toLowerCase()) ||
|
|
plugin.description.toLowerCase().includes(search.toLowerCase());
|
|
|
|
const matchesCategory = category === 'all' || plugin.category === category;
|
|
|
|
return matchesSearch && matchesCategory;
|
|
});
|
|
|
|
return (
|
|
<div className="plugin-store">
|
|
<div className="store-header">
|
|
<h1>Plugin Store</h1>
|
|
<div className="store-controls">
|
|
<input
|
|
type="text"
|
|
placeholder="Search plugins..."
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
className="search-input"
|
|
/>
|
|
<select
|
|
value={category}
|
|
onChange={(e) => setCategory(e.target.value)}
|
|
className="category-select"
|
|
>
|
|
<option value="all">All Categories</option>
|
|
<option value="time">Time</option>
|
|
<option value="weather">Weather</option>
|
|
<option value="sports">Sports</option>
|
|
<option value="finance">Finance</option>
|
|
<option value="entertainment">Entertainment</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="loading">Loading plugins...</div>
|
|
) : (
|
|
<div className="plugin-grid">
|
|
{filteredPlugins.map(plugin => (
|
|
<PluginCard
|
|
key={plugin.id}
|
|
plugin={plugin}
|
|
onInstall={installPlugin}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function PluginCard({ plugin, onInstall }) {
|
|
return (
|
|
<div className="plugin-card">
|
|
<div className="plugin-header">
|
|
<h3>{plugin.name}</h3>
|
|
{plugin.verified && <span className="verified-badge">✓ Verified</span>}
|
|
</div>
|
|
<p className="plugin-author">by {plugin.author}</p>
|
|
<p className="plugin-description">{plugin.description}</p>
|
|
<div className="plugin-meta">
|
|
<span className="meta-item">⭐ {plugin.stars}</span>
|
|
<span className="meta-item">📥 {plugin.downloads}</span>
|
|
<span className="meta-item">{plugin.category}</span>
|
|
</div>
|
|
<div className="plugin-tags">
|
|
{plugin.tags.map(tag => (
|
|
<span key={tag} className="tag">{tag}</span>
|
|
))}
|
|
</div>
|
|
<div className="plugin-actions">
|
|
<button
|
|
className="btn-primary"
|
|
onClick={() => onInstall(plugin.id)}
|
|
>
|
|
Install
|
|
</button>
|
|
<a
|
|
href={plugin.repo}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="btn-secondary"
|
|
>
|
|
View on GitHub
|
|
</a>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
```
|
|
|
|
### Plugin Manager UI
|
|
|
|
```javascript
|
|
// New: templates/src/components/PluginManager.jsx
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
|
|
|
|
export default function PluginManager() {
|
|
const [installedPlugins, setInstalledPlugins] = useState([]);
|
|
const [rotationOrder, setRotationOrder] = useState([]);
|
|
|
|
useEffect(() => {
|
|
fetchInstalledPlugins();
|
|
}, []);
|
|
|
|
const fetchInstalledPlugins = async () => {
|
|
try {
|
|
const response = await fetch('/api/plugins/installed');
|
|
const data = await response.json();
|
|
setInstalledPlugins(data.plugins);
|
|
setRotationOrder(data.rotation_order || []);
|
|
} catch (error) {
|
|
console.error('Error fetching installed plugins:', error);
|
|
}
|
|
};
|
|
|
|
const togglePlugin = async (pluginId, enabled) => {
|
|
try {
|
|
await fetch('/api/plugins/toggle', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ plugin_id: pluginId, enabled })
|
|
});
|
|
fetchInstalledPlugins();
|
|
} catch (error) {
|
|
console.error('Error toggling plugin:', error);
|
|
}
|
|
};
|
|
|
|
const uninstallPlugin = async (pluginId) => {
|
|
if (!confirm(`Are you sure you want to uninstall ${pluginId}?`)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await fetch('/api/plugins/uninstall', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ plugin_id: pluginId })
|
|
});
|
|
fetchInstalledPlugins();
|
|
} catch (error) {
|
|
console.error('Error uninstalling plugin:', error);
|
|
}
|
|
};
|
|
|
|
const handleDragEnd = async (result) => {
|
|
if (!result.destination) return;
|
|
|
|
const newOrder = Array.from(rotationOrder);
|
|
const [removed] = newOrder.splice(result.source.index, 1);
|
|
newOrder.splice(result.destination.index, 0, removed);
|
|
|
|
setRotationOrder(newOrder);
|
|
|
|
try {
|
|
await fetch('/api/plugins/rotation-order', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ order: newOrder })
|
|
});
|
|
} catch (error) {
|
|
console.error('Error saving rotation order:', error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="plugin-manager">
|
|
<h1>Installed Plugins</h1>
|
|
|
|
<div className="plugins-list">
|
|
{installedPlugins.map(plugin => (
|
|
<div key={plugin.id} className="plugin-item">
|
|
<div className="plugin-info">
|
|
<h3>{plugin.name}</h3>
|
|
<p>{plugin.description}</p>
|
|
<span className="version">v{plugin.version}</span>
|
|
</div>
|
|
<div className="plugin-controls">
|
|
<label className="toggle-switch">
|
|
<input
|
|
type="checkbox"
|
|
checked={plugin.enabled}
|
|
onChange={(e) => togglePlugin(plugin.id, e.target.checked)}
|
|
/>
|
|
<span className="slider"></span>
|
|
</label>
|
|
<button
|
|
className="btn-config"
|
|
onClick={() => openPluginConfig(plugin.id)}
|
|
>
|
|
⚙️ Configure
|
|
</button>
|
|
<button
|
|
className="btn-danger"
|
|
onClick={() => uninstallPlugin(plugin.id)}
|
|
>
|
|
🗑️ Uninstall
|
|
</button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<h2>Display Rotation Order</h2>
|
|
<DragDropContext onDragEnd={handleDragEnd}>
|
|
<Droppable droppableId="rotation">
|
|
{(provided) => (
|
|
<div
|
|
{...provided.droppableProps}
|
|
ref={provided.innerRef}
|
|
className="rotation-list"
|
|
>
|
|
{rotationOrder.map((pluginId, index) => {
|
|
const plugin = installedPlugins.find(p => p.id === pluginId);
|
|
if (!plugin || !plugin.enabled) return null;
|
|
|
|
return (
|
|
<Draggable
|
|
key={pluginId}
|
|
draggableId={pluginId}
|
|
index={index}
|
|
>
|
|
{(provided) => (
|
|
<div
|
|
ref={provided.innerRef}
|
|
{...provided.draggableProps}
|
|
{...provided.dragHandleProps}
|
|
className="rotation-item"
|
|
>
|
|
<span className="drag-handle">⋮⋮</span>
|
|
<span>{plugin.name}</span>
|
|
<span className="duration">{plugin.display_duration}s</span>
|
|
</div>
|
|
)}
|
|
</Draggable>
|
|
);
|
|
})}
|
|
{provided.placeholder}
|
|
</div>
|
|
)}
|
|
</Droppable>
|
|
</DragDropContext>
|
|
</div>
|
|
);
|
|
}
|
|
```
|
|
|
|
### API Endpoints for Web UI
|
|
|
|
```python
|
|
# New endpoints in web_interface_v2.py
|
|
|
|
@app.route('/api/plugins/store/list', methods=['GET'])
|
|
def api_plugin_store_list():
|
|
"""Get list of available plugins from store."""
|
|
try:
|
|
store_manager = PluginStoreManager()
|
|
registry = store_manager.fetch_registry()
|
|
return jsonify({
|
|
'status': 'success',
|
|
'plugins': registry.get('plugins', [])
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/install', methods=['POST'])
|
|
def api_plugin_install():
|
|
"""Install a plugin from the store."""
|
|
try:
|
|
data = request.get_json()
|
|
plugin_id = data.get('plugin_id')
|
|
version = data.get('version', 'latest')
|
|
|
|
store_manager = PluginStoreManager()
|
|
success = store_manager.install_plugin(plugin_id)
|
|
|
|
if success:
|
|
# Reload plugin manager to discover new plugin
|
|
global plugin_manager
|
|
plugin_manager.discover_plugins()
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': f'Plugin {plugin_id} installed successfully'
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': f'Failed to install plugin {plugin_id}'
|
|
}), 500
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/installed', methods=['GET'])
|
|
def api_plugins_installed():
|
|
"""Get list of installed plugins."""
|
|
try:
|
|
global plugin_manager
|
|
plugins = []
|
|
|
|
for plugin_id, plugin in plugin_manager.get_all_plugins().items():
|
|
manifest = plugin_manager.plugin_manifests.get(plugin_id, {})
|
|
plugins.append({
|
|
'id': plugin_id,
|
|
'name': manifest.get('name', plugin_id),
|
|
'version': manifest.get('version', ''),
|
|
'description': manifest.get('description', ''),
|
|
'author': manifest.get('author', ''),
|
|
'enabled': plugin.enabled,
|
|
'display_duration': plugin.get_display_duration()
|
|
})
|
|
|
|
# Get rotation order from config
|
|
config = config_manager.load_config()
|
|
rotation_order = config.get('display', {}).get('plugin_rotation_order', [])
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'plugins': plugins,
|
|
'rotation_order': rotation_order
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/toggle', methods=['POST'])
|
|
def api_plugin_toggle():
|
|
"""Enable or disable a plugin."""
|
|
try:
|
|
data = request.get_json()
|
|
plugin_id = data.get('plugin_id')
|
|
enabled = data.get('enabled', True)
|
|
|
|
# Update config
|
|
config = config_manager.load_config()
|
|
if plugin_id not in config:
|
|
config[plugin_id] = {}
|
|
config[plugin_id]['enabled'] = enabled
|
|
config_manager.save_config(config)
|
|
|
|
# Reload plugin
|
|
global plugin_manager
|
|
if enabled:
|
|
plugin_manager.load_plugin(plugin_id)
|
|
else:
|
|
plugin_manager.unload_plugin(plugin_id)
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': f'Plugin {plugin_id} {"enabled" if enabled else "disabled"}'
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/uninstall', methods=['POST'])
|
|
def api_plugin_uninstall():
|
|
"""Uninstall a plugin."""
|
|
try:
|
|
data = request.get_json()
|
|
plugin_id = data.get('plugin_id')
|
|
|
|
# Unload first
|
|
global plugin_manager
|
|
plugin_manager.unload_plugin(plugin_id)
|
|
|
|
# Uninstall
|
|
store_manager = PluginStoreManager()
|
|
success = store_manager.uninstall_plugin(plugin_id)
|
|
|
|
if success:
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': f'Plugin {plugin_id} uninstalled successfully'
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': f'Failed to uninstall plugin {plugin_id}'
|
|
}), 500
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/rotation-order', methods=['POST'])
|
|
def api_plugin_rotation_order():
|
|
"""Update plugin rotation order."""
|
|
try:
|
|
data = request.get_json()
|
|
order = data.get('order', [])
|
|
|
|
# Update config
|
|
config = config_manager.load_config()
|
|
if 'display' not in config:
|
|
config['display'] = {}
|
|
config['display']['plugin_rotation_order'] = order
|
|
config_manager.save_config(config)
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Rotation order updated'
|
|
})
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
|
|
@app.route('/api/plugins/install-from-url', methods=['POST'])
|
|
def api_plugin_install_from_url():
|
|
"""Install a plugin from a custom GitHub URL."""
|
|
try:
|
|
data = request.get_json()
|
|
repo_url = data.get('repo_url')
|
|
|
|
if not repo_url:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': 'repo_url is required'
|
|
}), 400
|
|
|
|
store_manager = PluginStoreManager()
|
|
success = store_manager.install_from_url(repo_url)
|
|
|
|
if success:
|
|
# Reload plugin manager
|
|
global plugin_manager
|
|
plugin_manager.discover_plugins()
|
|
|
|
return jsonify({
|
|
'status': 'success',
|
|
'message': 'Plugin installed from URL successfully'
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': 'Failed to install plugin from URL'
|
|
}), 500
|
|
except Exception as e:
|
|
return jsonify({
|
|
'status': 'error',
|
|
'message': str(e)
|
|
}), 500
|
|
```
|
|
|
|
---
|
|
|
|
## 5. Migration Strategy
|
|
|
|
### Phase 1: Core Plugin Infrastructure (v2.0.0)
|
|
|
|
**Goal**: Build plugin system alongside existing managers
|
|
|
|
**Changes**:
|
|
1. Create `src/plugin_system/` module
|
|
2. Implement `BasePlugin`, `PluginManager`, `PluginStoreManager`
|
|
3. Add `plugins/` directory support
|
|
4. Modify `display_controller.py` to load both legacy and plugins
|
|
5. Update web UI to show plugin store tab
|
|
|
|
**Backward Compatibility**: 100% - all existing managers still work
|
|
|
|
### Phase 2: Example Plugins (v2.1.0)
|
|
|
|
**Goal**: Create reference plugins and migration examples
|
|
|
|
**Create Official Plugins**:
|
|
1. `ledmatrix-clock-simple` - Simple clock (migrated from existing)
|
|
2. `ledmatrix-weather-basic` - Basic weather display
|
|
3. `ledmatrix-stocks-ticker` - Stock ticker
|
|
4. `ledmatrix-nhl-scores` - NHL scoreboard
|
|
|
|
**Changes**:
|
|
- Document plugin creation process
|
|
- Create plugin templates
|
|
- Update wiki with plugin development guide
|
|
|
|
**Backward Compatibility**: 100% - plugins are additive
|
|
|
|
### Phase 3: Migration Tools (v2.2.0)
|
|
|
|
**Goal**: Provide tools to migrate existing setups
|
|
|
|
**Migration Script**:
|
|
```python
|
|
# scripts/migrate_to_plugins.py
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
def migrate_config():
|
|
"""
|
|
Migrate existing config.json to plugin-based format.
|
|
"""
|
|
config_path = Path("config/config.json")
|
|
with open(config_path, 'r') as f:
|
|
config = json.load(f)
|
|
|
|
# Create migration plan
|
|
migration_map = {
|
|
'clock': 'clock-simple',
|
|
'weather': 'weather-basic',
|
|
'stocks': 'stocks-ticker',
|
|
'nhl_scoreboard': 'nhl-scores',
|
|
# ... etc
|
|
}
|
|
|
|
# Install recommended plugins
|
|
from src.plugin_system.store_manager import PluginStoreManager
|
|
store = PluginStoreManager()
|
|
|
|
for legacy_key, plugin_id in migration_map.items():
|
|
if config.get(legacy_key, {}).get('enabled', False):
|
|
print(f"Migrating {legacy_key} to plugin {plugin_id}")
|
|
store.install_plugin(plugin_id)
|
|
|
|
# Migrate config section
|
|
if legacy_key in config:
|
|
config[plugin_id] = config[legacy_key]
|
|
|
|
# Save migrated config
|
|
with open("config/config.json.migrated", 'w') as f:
|
|
json.dump(config, f, indent=2)
|
|
|
|
print("Migration complete! Review config.json.migrated")
|
|
|
|
if __name__ == "__main__":
|
|
migrate_config()
|
|
```
|
|
|
|
**User Instructions**:
|
|
```bash
|
|
# 1. Backup existing config
|
|
cp config/config.json config/config.json.backup
|
|
|
|
# 2. Run migration script
|
|
python3 scripts/migrate_to_plugins.py
|
|
|
|
# 3. Review migrated config
|
|
cat config/config.json.migrated
|
|
|
|
# 4. Apply migration
|
|
mv config/config.json.migrated config/config.json
|
|
|
|
# 5. Restart service
|
|
sudo systemctl restart ledmatrix
|
|
```
|
|
|
|
### Phase 4: Deprecation (v2.5.0)
|
|
|
|
**Goal**: Mark legacy managers as deprecated
|
|
|
|
**Changes**:
|
|
- Add deprecation warnings to legacy managers
|
|
- Update documentation to recommend plugins
|
|
- Create migration guide in wiki
|
|
|
|
**Backward Compatibility**: 95% - legacy still works but shows warnings
|
|
|
|
### Phase 5: Plugin-Only (v3.0.0)
|
|
|
|
**Goal**: Remove legacy managers from core
|
|
|
|
**Breaking Changes**:
|
|
- Remove hardcoded manager imports from `display_controller.py`
|
|
- Remove legacy manager files from `src/`
|
|
- Package legacy managers as official plugins
|
|
- Update config template to plugin-based format
|
|
|
|
**Migration Required**: Users must run migration script
|
|
|
|
---
|
|
|
|
## 6. Plugin Developer Guidelines
|
|
|
|
### Creating a New Plugin
|
|
|
|
#### Step 1: Plugin Structure
|
|
|
|
```bash
|
|
# Create plugin directory
|
|
mkdir -p plugins/my-plugin
|
|
cd plugins/my-plugin
|
|
|
|
# Create required files
|
|
touch manifest.json
|
|
touch manager.py
|
|
touch requirements.txt
|
|
touch config_schema.json
|
|
touch README.md
|
|
```
|
|
|
|
#### Step 2: Manifest
|
|
|
|
```json
|
|
{
|
|
"id": "my-plugin",
|
|
"name": "My Custom Display",
|
|
"version": "1.0.0",
|
|
"author": "YourName",
|
|
"description": "A custom display for LEDMatrix",
|
|
"homepage": "https://github.com/YourName/ledmatrix-my-plugin",
|
|
"entry_point": "manager.py",
|
|
"class_name": "MyPluginManager",
|
|
"category": "custom",
|
|
"tags": ["custom", "example"],
|
|
"compatible_versions": [">=2.0.0"],
|
|
"min_ledmatrix_version": "2.0.0",
|
|
"max_ledmatrix_version": "3.0.0",
|
|
"requires": {
|
|
"python": ">=3.9",
|
|
"display_size": {
|
|
"min_width": 64,
|
|
"min_height": 32
|
|
}
|
|
},
|
|
"config_schema": "config_schema.json",
|
|
"assets": {},
|
|
"update_interval": 60,
|
|
"default_duration": 15,
|
|
"display_modes": ["my-plugin"],
|
|
"api_requirements": []
|
|
}
|
|
```
|
|
|
|
#### Step 3: Manager Implementation
|
|
|
|
```python
|
|
# manager.py
|
|
|
|
from src.plugin_system.base_plugin import BasePlugin
|
|
import time
|
|
|
|
class MyPluginManager(BasePlugin):
|
|
"""
|
|
Example plugin that displays custom content.
|
|
"""
|
|
|
|
def __init__(self, plugin_id, config, display_manager, cache_manager, plugin_manager):
|
|
super().__init__(plugin_id, config, display_manager, cache_manager, plugin_manager)
|
|
|
|
# Plugin-specific initialization
|
|
self.message = config.get('message', 'Hello, World!')
|
|
self.color = tuple(config.get('color', [255, 255, 255]))
|
|
self.last_update = 0
|
|
|
|
def update(self):
|
|
"""
|
|
Update plugin data.
|
|
Called based on update_interval in manifest.
|
|
"""
|
|
# Fetch or update data here
|
|
self.last_update = time.time()
|
|
self.logger.info(f"Updated {self.plugin_id}")
|
|
|
|
def display(self, force_clear=False):
|
|
"""
|
|
Render the plugin display.
|
|
"""
|
|
if force_clear:
|
|
self.display_manager.clear()
|
|
|
|
# Get display dimensions
|
|
width = self.display_manager.width
|
|
height = self.display_manager.height
|
|
|
|
# Draw custom content
|
|
self.display_manager.draw_text(
|
|
self.message,
|
|
x=width // 2,
|
|
y=height // 2,
|
|
color=self.color,
|
|
centered=True
|
|
)
|
|
|
|
# Update the physical display
|
|
self.display_manager.update_display()
|
|
|
|
self.logger.debug(f"Displayed {self.plugin_id}")
|
|
```
|
|
|
|
#### Step 4: Configuration Schema
|
|
|
|
```json
|
|
{
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
"type": "object",
|
|
"properties": {
|
|
"enabled": {
|
|
"type": "boolean",
|
|
"default": true,
|
|
"description": "Enable or disable this plugin"
|
|
},
|
|
"message": {
|
|
"type": "string",
|
|
"default": "Hello, World!",
|
|
"description": "Message to display"
|
|
},
|
|
"color": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "integer",
|
|
"minimum": 0,
|
|
"maximum": 255
|
|
},
|
|
"minItems": 3,
|
|
"maxItems": 3,
|
|
"default": [255, 255, 255],
|
|
"description": "RGB color for text"
|
|
},
|
|
"display_duration": {
|
|
"type": "number",
|
|
"default": 15,
|
|
"minimum": 1,
|
|
"description": "How long to display in seconds"
|
|
}
|
|
},
|
|
"required": ["enabled"]
|
|
}
|
|
```
|
|
|
|
#### Step 5: README
|
|
|
|
```markdown
|
|
# My Custom Display Plugin
|
|
|
|
A custom display plugin for LEDMatrix.
|
|
|
|
## Installation
|
|
|
|
From the LEDMatrix web UI:
|
|
1. Go to Plugin Store
|
|
2. Search for "My Custom Display"
|
|
3. Click Install
|
|
|
|
Or install from command line:
|
|
```bash
|
|
cd /path/to/LEDMatrix
|
|
python3 -c "from src.plugin_system.store_manager import PluginStoreManager; PluginStoreManager().install_plugin('my-plugin')"
|
|
```
|
|
|
|
## Configuration
|
|
|
|
Add to `config/config.json`:
|
|
|
|
```json
|
|
{
|
|
"my-plugin": {
|
|
"enabled": true,
|
|
"message": "Hello, World!",
|
|
"color": [255, 255, 255],
|
|
"display_duration": 15
|
|
}
|
|
}
|
|
```
|
|
|
|
## Options
|
|
|
|
- `message` (string): Text to display
|
|
- `color` (array): RGB color [R, G, B]
|
|
- `display_duration` (number): Display time in seconds
|
|
|
|
## License
|
|
|
|
MIT
|
|
```
|
|
|
|
### Publishing a Plugin
|
|
|
|
#### Step 1: Create GitHub Repository
|
|
|
|
```bash
|
|
# Initialize git
|
|
git init
|
|
git add .
|
|
git commit -m "Initial commit"
|
|
|
|
# Create on GitHub and push
|
|
git remote add origin https://github.com/YourName/ledmatrix-my-plugin.git
|
|
git push -u origin main
|
|
```
|
|
|
|
#### Step 2: Create Release
|
|
|
|
```bash
|
|
# Tag version
|
|
git tag -a v1.0.0 -m "Version 1.0.0"
|
|
git push origin v1.0.0
|
|
```
|
|
|
|
Create release on GitHub with:
|
|
- Release notes
|
|
- Installation instructions
|
|
- Screenshots/GIFs
|
|
|
|
#### Step 3: Submit to Registry
|
|
|
|
Create pull request to `ChuckBuilds/ledmatrix-plugin-registry` adding your plugin:
|
|
|
|
```json
|
|
{
|
|
"id": "my-plugin",
|
|
"name": "My Custom Display",
|
|
"description": "A custom display for LEDMatrix",
|
|
"author": "YourName",
|
|
"category": "custom",
|
|
"tags": ["custom", "example"],
|
|
"repo": "https://github.com/YourName/ledmatrix-my-plugin",
|
|
"branch": "main",
|
|
"versions": [
|
|
{
|
|
"version": "1.0.0",
|
|
"ledmatrix_min_version": "2.0.0",
|
|
"released": "2025-01-15",
|
|
"download_url": "https://github.com/YourName/ledmatrix-my-plugin/archive/refs/tags/v1.0.0.zip"
|
|
}
|
|
],
|
|
"verified": false
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 7. Technical Implementation Details
|
|
|
|
### Configuration Management
|
|
|
|
**Old Way** (monolithic):
|
|
```json
|
|
{
|
|
"clock": { "enabled": true },
|
|
"weather": { "enabled": true },
|
|
"nhl_scoreboard": { "enabled": true }
|
|
}
|
|
```
|
|
|
|
**New Way** (plugin-based):
|
|
```json
|
|
{
|
|
"plugins": {
|
|
"clock-simple": { "enabled": true },
|
|
"weather-basic": { "enabled": true },
|
|
"nhl-scores": { "enabled": true }
|
|
},
|
|
"display": {
|
|
"plugin_rotation_order": [
|
|
"clock-simple",
|
|
"weather-basic",
|
|
"nhl-scores"
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
### Dependency Management
|
|
|
|
Each plugin manages its own dependencies via `requirements.txt`:
|
|
|
|
```txt
|
|
# plugins/nhl-scores/requirements.txt
|
|
requests>=2.28.0
|
|
pytz>=2022.1
|
|
```
|
|
|
|
During installation:
|
|
```python
|
|
subprocess.run([
|
|
'pip3', 'install',
|
|
'--break-system-packages',
|
|
'-r', 'plugins/nhl-scores/requirements.txt'
|
|
])
|
|
```
|
|
|
|
### Asset Management
|
|
|
|
Plugins can include their own assets:
|
|
|
|
```
|
|
plugins/nhl-scores/
|
|
├── assets/
|
|
│ ├── logos/
|
|
│ │ ├── TB.png
|
|
│ │ └── DAL.png
|
|
│ └── fonts/
|
|
│ └── sports.bdf
|
|
```
|
|
|
|
Access in plugin:
|
|
```python
|
|
def get_asset_path(self, relative_path):
|
|
"""Get absolute path to plugin asset."""
|
|
plugin_dir = Path(__file__).parent
|
|
return plugin_dir / "assets" / relative_path
|
|
|
|
# Usage
|
|
logo_path = self.get_asset_path("logos/TB.png")
|
|
```
|
|
|
|
### Caching Integration
|
|
|
|
Plugins use the shared cache manager:
|
|
|
|
```python
|
|
def update(self):
|
|
cache_key = f"{self.plugin_id}_data"
|
|
|
|
# Try to get cached data
|
|
cached = self.cache_manager.get(cache_key, max_age=3600)
|
|
if cached:
|
|
self.data = cached
|
|
return
|
|
|
|
# Fetch fresh data
|
|
self.data = self._fetch_from_api()
|
|
|
|
# Cache it
|
|
self.cache_manager.set(cache_key, self.data)
|
|
```
|
|
|
|
### Inter-Plugin Communication
|
|
|
|
Plugins can communicate through the plugin manager:
|
|
|
|
```python
|
|
# In plugin A
|
|
other_plugin = self.plugin_manager.get_plugin('plugin-b')
|
|
if other_plugin:
|
|
data = other_plugin.get_shared_data()
|
|
|
|
# In plugin B
|
|
def get_shared_data(self):
|
|
return {'temperature': 72, 'conditions': 'sunny'}
|
|
```
|
|
|
|
### Error Handling
|
|
|
|
Plugins should handle errors gracefully:
|
|
|
|
```python
|
|
def display(self, force_clear=False):
|
|
try:
|
|
# Plugin logic
|
|
self._render_content()
|
|
except Exception as e:
|
|
self.logger.error(f"Error in display: {e}", exc_info=True)
|
|
# Show error message on display
|
|
self.display_manager.clear()
|
|
self.display_manager.draw_text(
|
|
f"Error: {self.plugin_id}",
|
|
x=5, y=15,
|
|
color=(255, 0, 0)
|
|
)
|
|
self.display_manager.update_display()
|
|
```
|
|
|
|
---
|
|
|
|
## 8. Best Practices & Standards
|
|
|
|
### Plugin Best Practices
|
|
|
|
1. **Follow BasePlugin Interface**: Always extend `BasePlugin` and implement required methods
|
|
2. **Validate Configuration**: Use config schemas to validate user settings
|
|
3. **Handle Errors Gracefully**: Never crash the entire system
|
|
4. **Use Logging**: Log important events and errors
|
|
5. **Cache Appropriately**: Use cache manager for API responses
|
|
6. **Clean Up Resources**: Implement `cleanup()` for resource disposal
|
|
7. **Document Everything**: Provide clear README and code comments
|
|
8. **Test on Hardware**: Test on actual Raspberry Pi with LED matrix
|
|
9. **Version Properly**: Use semantic versioning
|
|
10. **Respect Resources**: Be mindful of CPU, memory, and API quotas
|
|
|
|
### Coding Standards
|
|
|
|
```python
|
|
# Good: Clear, documented, error-handled
|
|
class MyPlugin(BasePlugin):
|
|
"""
|
|
Custom plugin that displays messages.
|
|
|
|
Configuration:
|
|
message (str): Message to display
|
|
color (tuple): RGB color tuple
|
|
"""
|
|
|
|
def __init__(self, plugin_id, config, display_manager, cache_manager, plugin_manager):
|
|
super().__init__(plugin_id, config, display_manager, cache_manager, plugin_manager)
|
|
self.message = config.get('message', 'Default')
|
|
self.validate_color(config.get('color', (255, 255, 255)))
|
|
|
|
def validate_color(self, color):
|
|
"""Validate color is proper RGB tuple."""
|
|
if not isinstance(color, (list, tuple)) or len(color) != 3:
|
|
raise ValueError("Color must be RGB tuple")
|
|
if not all(0 <= c <= 255 for c in color):
|
|
raise ValueError("Color values must be 0-255")
|
|
self.color = tuple(color)
|
|
|
|
def update(self):
|
|
"""Update plugin data."""
|
|
try:
|
|
# Update logic
|
|
pass
|
|
except Exception as e:
|
|
self.logger.error(f"Update failed: {e}")
|
|
|
|
def display(self, force_clear=False):
|
|
"""Display plugin content."""
|
|
try:
|
|
if force_clear:
|
|
self.display_manager.clear()
|
|
|
|
self.display_manager.draw_text(
|
|
self.message,
|
|
x=5, y=15,
|
|
color=self.color
|
|
)
|
|
self.display_manager.update_display()
|
|
except Exception as e:
|
|
self.logger.error(f"Display failed: {e}")
|
|
```
|
|
|
|
### Testing Guidelines
|
|
|
|
```python
|
|
# test/test_my_plugin.py
|
|
|
|
import unittest
|
|
from unittest.mock import Mock, MagicMock
|
|
import sys
|
|
sys.path.insert(0, 'plugins/my-plugin')
|
|
from manager import MyPluginManager
|
|
|
|
class TestMyPlugin(unittest.TestCase):
|
|
def setUp(self):
|
|
"""Set up test fixtures."""
|
|
self.config = {
|
|
'enabled': True,
|
|
'message': 'Test',
|
|
'color': [255, 0, 0]
|
|
}
|
|
self.display_manager = Mock()
|
|
self.cache_manager = Mock()
|
|
self.plugin_manager = Mock()
|
|
|
|
self.plugin = MyPluginManager(
|
|
plugin_id='my-plugin',
|
|
config=self.config,
|
|
display_manager=self.display_manager,
|
|
cache_manager=self.cache_manager,
|
|
plugin_manager=self.plugin_manager
|
|
)
|
|
|
|
def test_initialization(self):
|
|
"""Test plugin initializes correctly."""
|
|
self.assertEqual(self.plugin.message, 'Test')
|
|
self.assertEqual(self.plugin.color, (255, 0, 0))
|
|
|
|
def test_display_calls_manager(self):
|
|
"""Test display method calls display manager."""
|
|
self.plugin.display()
|
|
self.display_manager.draw_text.assert_called_once()
|
|
self.display_manager.update_display.assert_called_once()
|
|
|
|
def test_invalid_color_raises_error(self):
|
|
"""Test invalid color configuration raises error."""
|
|
bad_config = {'color': [300, 0, 0]}
|
|
with self.assertRaises(ValueError):
|
|
MyPluginManager(
|
|
'test', bad_config,
|
|
self.display_manager,
|
|
self.cache_manager,
|
|
self.plugin_manager
|
|
)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
```
|
|
|
|
---
|
|
|
|
## 9. Security Considerations
|
|
|
|
### Plugin Verification
|
|
|
|
**Verified Plugins**:
|
|
- Reviewed by maintainers
|
|
- Follow best practices
|
|
- No known security issues
|
|
- Marked with ✓ badge in store
|
|
|
|
**Unverified Plugins**:
|
|
- User-contributed
|
|
- Not reviewed
|
|
- Install at own risk
|
|
- Show warning before installation
|
|
|
|
### Code Review Process
|
|
|
|
Before marking a plugin as verified:
|
|
|
|
1. **Code Review**: Manual inspection of code
|
|
2. **Dependency Audit**: Check all requirements
|
|
3. **Permission Check**: Verify minimal permissions
|
|
4. **API Key Safety**: Ensure no hardcoded secrets
|
|
5. **Resource Usage**: Check for excessive CPU/memory use
|
|
6. **Testing**: Test on actual hardware
|
|
|
|
### Sandboxing Considerations
|
|
|
|
Current implementation has NO sandboxing. Plugins run with same permissions as main process.
|
|
|
|
**Future Enhancement** (v3.x):
|
|
- Run plugins in separate processes
|
|
- Limit file system access
|
|
- Rate limit API calls
|
|
- Monitor resource usage
|
|
- Kill misbehaving plugins
|
|
|
|
### User Guidelines
|
|
|
|
**For Users**:
|
|
1. Only install plugins from trusted sources
|
|
2. Review plugin permissions before installing
|
|
3. Check plugin ratings and reviews
|
|
4. Keep plugins updated
|
|
5. Report suspicious plugins
|
|
|
|
**For Developers**:
|
|
1. Never include hardcoded API keys
|
|
2. Minimize required permissions
|
|
3. Use secure API practices
|
|
4. Validate all user inputs
|
|
5. Handle errors gracefully
|
|
|
|
---
|
|
|
|
## 10. Implementation Roadmap
|
|
|
|
### Timeline
|
|
|
|
**Phase 1: Foundation (Weeks 1-3)**
|
|
- Create plugin system infrastructure
|
|
- Implement BasePlugin, PluginManager, StoreManager
|
|
- Update display_controller for plugin support
|
|
- Basic web UI for plugin management
|
|
|
|
**Phase 2: Example Plugins (Weeks 4-5)**
|
|
- Create 4-5 reference plugins
|
|
- Migrate existing managers as examples
|
|
- Write developer documentation
|
|
- Create plugin templates
|
|
|
|
**Phase 3: Store Integration (Weeks 6-7)**
|
|
- Set up plugin registry repo
|
|
- Implement store API
|
|
- Build web UI for store
|
|
- Add search and filtering
|
|
|
|
**Phase 4: Migration Tools (Weeks 8-9)**
|
|
- Create migration script
|
|
- Test with existing installations
|
|
- Write migration guide
|
|
- Update documentation
|
|
|
|
**Phase 5: Testing & Polish (Weeks 10-12)**
|
|
- Comprehensive testing on Pi hardware
|
|
- Bug fixes
|
|
- Performance optimization
|
|
- Documentation improvements
|
|
|
|
**Phase 6: Release v2.0.0 (Week 13)**
|
|
- Tag release
|
|
- Publish documentation
|
|
- Announce to community
|
|
- Gather feedback
|
|
|
|
### Success Metrics
|
|
|
|
**Technical**:
|
|
- 100% backward compatibility in v2.0
|
|
- <100ms plugin load time
|
|
- <5% performance overhead
|
|
- Zero critical bugs in first month
|
|
|
|
**User Adoption**:
|
|
- 10+ community-created plugins in 3 months
|
|
- 50%+ of users install at least one plugin
|
|
- Positive feedback on ease of use
|
|
|
|
**Developer Experience**:
|
|
- Clear documentation
|
|
- Responsive to plugin dev questions
|
|
- Regular updates to plugin system
|
|
|
|
---
|
|
|
|
## Appendix A: File Structure Comparison
|
|
|
|
### Before (v1.x)
|
|
|
|
```
|
|
LEDMatrix/
|
|
├── src/
|
|
│ ├── clock.py
|
|
│ ├── weather_manager.py
|
|
│ ├── stock_manager.py
|
|
│ ├── nhl_managers.py
|
|
│ ├── nba_managers.py
|
|
│ ├── mlb_manager.py
|
|
│ └── ... (40+ manager files)
|
|
├── config/
|
|
│ ├── config.json (650+ lines)
|
|
│ └── config.template.json
|
|
└── web_interface_v2.py (hardcoded imports)
|
|
```
|
|
|
|
### After (v2.0+)
|
|
|
|
```
|
|
LEDMatrix/
|
|
├── src/
|
|
│ ├── plugin_system/
|
|
│ │ ├── __init__.py
|
|
│ │ ├── base_plugin.py
|
|
│ │ ├── plugin_manager.py
|
|
│ │ └── store_manager.py
|
|
│ ├── display_controller.py (plugin-aware)
|
|
│ └── ... (core components only)
|
|
├── plugins/
|
|
│ ├── clock-simple/
|
|
│ ├── weather-basic/
|
|
│ ├── nhl-scores/
|
|
│ └── ... (user-installed plugins)
|
|
├── config/
|
|
│ └── config.json (minimal core config)
|
|
└── web_interface_v2.py (dynamic plugin loading)
|
|
```
|
|
|
|
---
|
|
|
|
## Appendix B: Example Plugin: NHL Scoreboard
|
|
|
|
Complete example of migrating NHL scoreboard to plugin:
|
|
|
|
**Directory Structure**:
|
|
```
|
|
plugins/nhl-scores/
|
|
├── manifest.json
|
|
├── manager.py
|
|
├── requirements.txt
|
|
├── config_schema.json
|
|
├── assets/
|
|
│ └── logos/
|
|
│ ├── TB.png
|
|
│ └── ... (NHL team logos)
|
|
└── README.md
|
|
```
|
|
|
|
**manifest.json**:
|
|
```json
|
|
{
|
|
"id": "nhl-scores",
|
|
"name": "NHL Scoreboard",
|
|
"version": "1.0.0",
|
|
"author": "ChuckBuilds",
|
|
"description": "Display NHL game scores and schedules",
|
|
"homepage": "https://github.com/ChuckBuilds/ledmatrix-nhl-scores",
|
|
"entry_point": "manager.py",
|
|
"class_name": "NHLScoresPlugin",
|
|
"category": "sports",
|
|
"tags": ["nhl", "hockey", "sports", "scores"],
|
|
"compatible_versions": [">=2.0.0"],
|
|
"requires": {
|
|
"python": ">=3.9",
|
|
"display_size": {
|
|
"min_width": 64,
|
|
"min_height": 32
|
|
}
|
|
},
|
|
"config_schema": "config_schema.json",
|
|
"assets": {
|
|
"logos": "assets/logos/"
|
|
},
|
|
"update_interval": 60,
|
|
"default_duration": 30,
|
|
"display_modes": ["nhl_live", "nhl_recent", "nhl_upcoming"],
|
|
"api_requirements": ["ESPN API"]
|
|
}
|
|
```
|
|
|
|
**requirements.txt**:
|
|
```txt
|
|
requests>=2.28.0
|
|
pytz>=2022.1
|
|
```
|
|
|
|
**manager.py** (abbreviated):
|
|
```python
|
|
from src.plugin_system.base_plugin import BasePlugin
|
|
import requests
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
class NHLScoresPlugin(BasePlugin):
|
|
"""NHL Scoreboard plugin for LEDMatrix."""
|
|
|
|
ESPN_NHL_URL = "https://site.api.espn.com/apis/site/v2/sports/hockey/nhl/scoreboard"
|
|
|
|
def __init__(self, plugin_id, config, display_manager, cache_manager, plugin_manager):
|
|
super().__init__(plugin_id, config, display_manager, cache_manager, plugin_manager)
|
|
|
|
self.favorite_teams = config.get('favorite_teams', [])
|
|
self.show_favorite_only = config.get('show_favorite_teams_only', True)
|
|
self.games = []
|
|
|
|
def update(self):
|
|
"""Fetch NHL games from ESPN API."""
|
|
cache_key = f"{self.plugin_id}_games"
|
|
|
|
# Try cache first
|
|
cached = self.cache_manager.get(cache_key, max_age=60)
|
|
if cached:
|
|
self.games = cached
|
|
self.logger.debug("Using cached NHL data")
|
|
return
|
|
|
|
try:
|
|
response = requests.get(self.ESPN_NHL_URL, timeout=10)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
|
|
self.games = self._process_games(data.get('events', []))
|
|
|
|
# Cache the results
|
|
self.cache_manager.set(cache_key, self.games)
|
|
|
|
self.logger.info(f"Fetched {len(self.games)} NHL games")
|
|
except Exception as e:
|
|
self.logger.error(f"Error fetching NHL data: {e}")
|
|
|
|
def _process_games(self, events):
|
|
"""Process raw ESPN data into game objects."""
|
|
games = []
|
|
for event in events:
|
|
# Extract game info
|
|
# ... (implementation)
|
|
pass
|
|
return games
|
|
|
|
def display(self, force_clear=False):
|
|
"""Display NHL scores."""
|
|
if force_clear:
|
|
self.display_manager.clear()
|
|
|
|
if not self.games:
|
|
self._show_no_games()
|
|
return
|
|
|
|
# Show first game (or cycle through)
|
|
game = self.games[0]
|
|
self._display_game(game)
|
|
|
|
self.display_manager.update_display()
|
|
|
|
def _display_game(self, game):
|
|
"""Render a single game."""
|
|
# Load team logos
|
|
away_logo = self._get_logo(game['away_team'])
|
|
home_logo = self._get_logo(game['home_team'])
|
|
|
|
# Draw logos and scores
|
|
# ... (implementation)
|
|
|
|
def _get_logo(self, team_abbr):
|
|
"""Get team logo from assets."""
|
|
logo_path = Path(__file__).parent / "assets" / "logos" / f"{team_abbr}.png"
|
|
if logo_path.exists():
|
|
return logo_path
|
|
return None
|
|
|
|
def _show_no_games(self):
|
|
"""Show 'no games' message."""
|
|
self.display_manager.draw_text(
|
|
"No NHL games",
|
|
x=5, y=15,
|
|
color=(255, 255, 255)
|
|
)
|
|
```
|
|
|
|
---
|
|
|
|
## Conclusion
|
|
|
|
This specification outlines a comprehensive transformation of the LEDMatrix project into a modular, extensible platform. The plugin architecture enables:
|
|
|
|
- **User Extensibility**: Anyone can create custom displays
|
|
- **Easy Distribution**: GitHub-based store for discovery and installation
|
|
- **Backward Compatibility**: Gradual migration path for existing users
|
|
- **Community Growth**: Lower barrier to contribution
|
|
- **Better Maintenance**: Smaller core, cleaner codebase
|
|
|
|
The gradual migration approach ensures existing users aren't disrupted while new users benefit from the improved architecture.
|
|
|
|
**Next Steps**:
|
|
1. Review and refine this specification
|
|
2. Begin Phase 1 implementation
|
|
3. Create prototype plugins for testing
|
|
4. Gather community feedback
|
|
5. Iterate and improve
|
|
|
|
---
|
|
|
|
**Document Version**: 1.0.0
|
|
**Last Updated**: 2025-01-09
|
|
**Author**: AI Assistant (Claude)
|
|
**Status**: Draft for Review
|
|
|