Phase 3: PluginWebMixin SDK + web plugin control panels
- New: sdk/plugin_web.py (PluginWebMixin: web pages, API routes, SSE) - New: services/web_panel/routes/plugin_web.py (3 endpoints) - New: plugins/example_plugin/dashboard.html (counter + SSE demo) - Updated: example_plugin uses PluginWebMixin - Tests: 28/28 passing
This commit is contained in:
@@ -1,22 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
import logging
|
||||
import logging, os
|
||||
from typing import Dict
|
||||
from aiohttp import web
|
||||
try:
|
||||
from sdk.plugin_command_decorator import plugin_command, command
|
||||
from sdk.plugin_web import PluginWebMixin
|
||||
except ImportError:
|
||||
def plugin_command(n=None,d=None,p=None):
|
||||
def deco(f):
|
||||
f._is_plugin_command=True;f._command_name=n or f.__name__
|
||||
f._command_description=d or (f.__doc__ or "").strip();return f
|
||||
def deco(f):f._is_plugin_command=True;f._command_name=n or f.__name__;return f
|
||||
return deco
|
||||
command=plugin_command
|
||||
class PluginWebMixin:
|
||||
def register_web_page(self,*a,**k):pass
|
||||
def register_api_route(self,*a,**k):pass
|
||||
|
||||
logger=logging.getLogger(__name__)
|
||||
|
||||
class Plugin:
|
||||
def __init__(self, plugin_name=None, config=None, bridge=None, n=None, c=None):
|
||||
self.plugin_name=plugin_name or n;self.config=config or c;self.bridge=bridge
|
||||
self.network_bridge=None;self.is_running=False
|
||||
class Plugin(PluginWebMixin):
|
||||
def __init__(self, plugin_name=None, config=None, bridge=None):
|
||||
PluginWebMixin.__init__(self)
|
||||
self.plugin_name=plugin_name or "example"
|
||||
self.config=config or {}
|
||||
self.bridge=bridge
|
||||
self.network_bridge=None
|
||||
self.is_running=False
|
||||
|
||||
async def initialize(self):
|
||||
logger.info(f"init: {self.plugin_name}")
|
||||
@@ -28,17 +34,21 @@ class Plugin:
|
||||
"/api/example/info",self._api_info,methods=["GET"],require_auth=False)
|
||||
except Exception as e:
|
||||
logger.warning(f"network skip: {e}")
|
||||
# Load dashboard HTML from file
|
||||
html_path=os.path.join(os.path.dirname(__file__),"dashboard.html")
|
||||
if os.path.exists(html_path):
|
||||
with open(html_path) as hf:
|
||||
self.register_web_page("example_plugin","Example Plugin",hf.read(),icon="P")
|
||||
self.is_running=True
|
||||
|
||||
async def _api_info(self,req):
|
||||
from aiohttp import web
|
||||
return web.json_response({"plugin":self.plugin_name,"status":"running"})
|
||||
|
||||
@plugin_command(name="echo",description="echo input")
|
||||
async def cmd_echo(self,*args):
|
||||
return " ".join(args) if args else "echo: no input"
|
||||
|
||||
@plugin_command(name="plugin_status",description="show plugin status")
|
||||
@plugin_command(name="plugin_status",description="show status")
|
||||
async def cmd_status(self,*args):
|
||||
return f"{self.plugin_name} v{self.config.get('version','?')} - running"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user