Skip to content

Commit db621ab

Browse files
committed
update.2025.08.09
1 parent ae57be6 commit db621ab

File tree

5 files changed

+149
-12
lines changed

5 files changed

+149
-12
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ app/plugin/
3737
"description": "插件功能描述",
3838
"author": "插件作者",
3939
"entry_point": "main.py",
40-
"min_app_version": "1.0.0",
40+
"background_service": "service.py",
41+
"min_app_version": "1.0.0.0",
4142
"dependencies": [],
42-
"enabled": true
43+
"enabled": true,
44+
"autostart": false
4345
}
4446
```
4547

46-
### 3. 插件主程序结构
48+
### 3. 插件主程序 (main.py)
4749

4850
```python
4951
# 导入必要的库
@@ -96,6 +98,25 @@ def get_plugin_info() -> Dict:
9698
return plugin.get_info()
9799
```
98100

101+
### 4. 插件服务
102+
103+
如果插件需要在后台运行服务,应在 `service.py` 文件中实现:
104+
105+
```python
106+
class MyPluginService:
107+
def __init__(self):
108+
# 初始化服务
109+
pass
110+
111+
def start(self):
112+
# 启动服务
113+
pass
114+
115+
def stop(self):
116+
# 停止服务
117+
pass
118+
```
119+
99120
## 插件系统特性
100121

101122
### 核心优势
@@ -128,9 +149,11 @@ def get_plugin_info() -> Dict:
128149
1. **创建插件目录**: 在 `app/plugin/` 下创建插件目录
129150
2. **编写配置文件**: 创建 `plugin.json` 定义插件信息
130151
3. **实现主程序**: 编写 `main.py` 实现插件功能
131-
4. **添加界面**: 创建图形界面(如需要)
132-
5. **编写文档**: 创建 `README.md` 说明插件用法
133-
6. **测试调试**: 测试插件功能并调试问题
152+
4. **实现服务**: 如果需要后台服务,编写 `service.py`
153+
5. **添加界面**: 创建图形界面(如需要)
154+
6. **编写文档**: 创建 `README.md` 说明插件用法
155+
7. **测试调试**: 测试插件功能并调试问题
156+
8. **打包发布**: 打包插件为 zip 文件,发布到插件广场
134157

135158
### 3. 最佳实践
136159

example_plugin/README.md renamed to SecRandom-plugins/README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,25 @@ app/plugin/example_plugin/
5353
"description": "插件描述",
5454
"author": "作者",
5555
"entry_point": "入口文件",
56+
"background_service": "后台服务文件",
5657
"min_app_version": "最低应用版本",
5758
"dependencies": [],
58-
"enabled": true
59+
"enabled": true,
60+
"autostart": false
5961
}
6062
```
6163

6264
**配置项说明:**
6365
- `name`: 插件显示名称
64-
- `version`: 插件版本号(语义化版本)
66+
- `version`: 插件版本号(语义化版本)(格式:v1.0.0(就建议这种格式的) 或者是跟你的GitHub tag一致)
6567
- `description`: 插件功能描述
6668
- `author`: 插件作者
6769
- `entry_point`: 插件入口文件(通常是 main.py)
68-
- `min_app_version`: 兼容的最低应用版本
70+
- `background_service`: 插件后台服务文件(可选)
71+
- `min_app_version`: 兼容的最低应用版本(格式:v0.0.0.0(必须是这个!))
6972
- `dependencies`: 依赖列表(可选)
7073
- `enabled`: 是否默认启用
74+
- `autostart`: 是否自动启动(一般来说默认最好是false)
7175

7276
### 2. 插件主程序 (main.py)
7377

@@ -168,9 +172,28 @@ except Exception as e:
168172
# 返回错误信息或显示错误提示
169173
```
170174

175+
### 6. 插件服务
176+
177+
如果插件需要在后台运行服务,应在 `service.py` 文件中实现:
178+
179+
```python
180+
class ExampleService:
181+
def __init__(self):
182+
# 初始化服务
183+
pass
184+
185+
def start(self):
186+
# 启动服务
187+
pass
188+
189+
def stop(self):
190+
# 停止服务
191+
pass
192+
```
193+
171194
## 插件集成要点
172195

173-
### 1. 与主系统通信
196+
### 1. 与主系统通信(暂时不支持)
174197

175198
插件可以通过以下方式与主系统集成:
176199

File renamed without changes.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "示例插件",
3-
"version": "v1.0.2",
3+
"version": "v1.0.3",
44
"description": "这是一个简单的示例插件,展示插件系统的基本功能",
55
"author": "SecRandom",
66
"entry_point": "main.py",
7+
"background_service": "service.py",
78
"min_app_version": "v0.0.0.0",
89
"dependencies": [],
9-
"enabled": true
10+
"enabled": true,
11+
"autostart": false
1012
}

SecRandom-plugins/service.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import os
2+
import sys
3+
import time
4+
import threading
5+
import subprocess
6+
from loguru import logger
7+
8+
class BackgroundService:
9+
def __init__(self, plugin_path):
10+
self.plugin_path = plugin_path
11+
self.running = False
12+
self.thread = None
13+
14+
def start(self):
15+
"""启动后台服务"""
16+
if self.running:
17+
return
18+
19+
self.running = True
20+
self.thread = threading.Thread(target=self._run_service, daemon=True)
21+
self.thread.start()
22+
logger.info(f"插件 {os.path.basename(self.plugin_path)} 后台服务已启动")
23+
24+
def stop(self):
25+
"""停止后台服务"""
26+
self.running = False
27+
if self.thread and self.thread.is_alive():
28+
self.thread.join(timeout=5)
29+
logger.info(f"插件 {os.path.basename(self.plugin_path)} 后台服务已停止")
30+
31+
def _run_service(self):
32+
"""运行后台服务主循环"""
33+
logger.info(f"插件 {os.path.basename(self.plugin_path)} 后台服务开始运行")
34+
35+
try:
36+
# 这里可以添加插件特定的后台逻辑
37+
# 例如:定时任务、监听事件、数据处理等
38+
while self.running:
39+
# 示例:每60秒执行一次任务
40+
time.sleep(60)
41+
42+
# 在这里添加插件的后台逻辑
43+
self._execute_background_task()
44+
45+
except Exception as e:
46+
logger.error(f"插件 {os.path.basename(self.plugin_path)} 后台服务运行错误: {e}")
47+
48+
def _execute_background_task(self):
49+
"""执行后台任务(可由插件开发者重写此方法)"""
50+
# 默认实现:记录日志
51+
logger.info(f"插件 {os.path.basename(self.plugin_path)} 后台任务执行中...")
52+
53+
# 插件开发者可以在这里添加自己的后台逻辑
54+
# 例如:
55+
# - 数据同步
56+
# - 定时检查
57+
# - 消息推送
58+
# - 系统监控
59+
# 等等
60+
61+
# 全局服务实例
62+
service_instance = None
63+
64+
def start_background_service(plugin_path):
65+
"""启动后台服务"""
66+
global service_instance
67+
if service_instance is None:
68+
service_instance = BackgroundService(plugin_path)
69+
service_instance.start()
70+
return service_instance
71+
72+
def stop_background_service():
73+
"""停止后台服务"""
74+
global service_instance
75+
if service_instance:
76+
service_instance.stop()
77+
service_instance = None
78+
79+
if __name__ == "__main__":
80+
# 当作为独立脚本运行时的测试代码
81+
plugin_path = os.path.dirname(os.path.abspath(__file__))
82+
service = start_background_service(plugin_path)
83+
84+
try:
85+
# 保持运行
86+
while True:
87+
time.sleep(1)
88+
except KeyboardInterrupt:
89+
stop_background_service()

0 commit comments

Comments
 (0)