mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-09-20 08:58:03 +00:00
example: 演示插件 onRemove 清理补齐(memo/rss/weather)
- memo: 卸载时删除 memos.json 数据文件 - rss: 卸载时清理 .homeagent/rss 订阅数据目录 - weather: 卸载时清理 .homeagent/weather 缓存目录 - 与 calendar(events.json)一致:仅删除触发、重载不触发; files(filesDir 为用户配置的访问根目录)、bili/qq(用户下载资产)、 ocr(临时目录函数内自清理)按语义不加入删除回调
This commit is contained in:
@ -46,6 +46,9 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
p.filePath = filepath.Join(fmt.Sprint(dataDirVal), "memos.json")
|
||||
p.load()
|
||||
|
||||
// 卸载(删除)时清理备忘数据文件;重载不触发
|
||||
s.RegisterOnRemoveHandler(p.cleanupData)
|
||||
|
||||
s.RegisterTool(p.tp+"create", sdk.ToolDef{
|
||||
Name: p.tp + "create",
|
||||
Description: "创建一条备忘条目。备忘内容应包含具体事项的完整描述。",
|
||||
@ -272,3 +275,10 @@ func errorResult(msg string) map[string]interface{} {
|
||||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
return &Plugin{name: name}, nil
|
||||
}
|
||||
|
||||
// cleanupData 卸载时清理备忘数据文件
|
||||
func (p *Plugin) cleanupData() {
|
||||
if p.filePath != "" {
|
||||
os.Remove(p.filePath)
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,6 +113,9 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
os.MkdirAll(p.dataDir, 0755)
|
||||
p.loadData()
|
||||
|
||||
// 卸载(删除)时清理订阅数据目录;重载不触发
|
||||
s.RegisterOnRemoveHandler(p.cleanupData)
|
||||
|
||||
s.Settings().RegisterDef(sdk.ConfigDef{
|
||||
Key: "poll_interval", Default: "30", Type: "string",
|
||||
DisplayName: "Poll Interval", Description: "Default polling interval in minutes (default: 30)",
|
||||
@ -437,4 +440,11 @@ func (p *Plugin) saveData() {
|
||||
os.WriteFile(p.dataFile(), b, 0644)
|
||||
}
|
||||
|
||||
// cleanupData 卸载时清理订阅数据目录(feeds.json 等)
|
||||
func (p *Plugin) cleanupData() {
|
||||
if p.dataDir != "" {
|
||||
os.RemoveAll(p.dataDir)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@ type Plugin struct {
|
||||
sdk *sdk.PluginSDK
|
||||
client *http.Client
|
||||
defaultLoc string
|
||||
dataDir string
|
||||
}
|
||||
|
||||
func NewPluginFactory(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
@ -48,7 +49,11 @@ func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
if dataHome == "" {
|
||||
dataHome = "/tmp"
|
||||
}
|
||||
os.MkdirAll(filepath.Join(dataHome, ".homeagent", "weather"), 0755)
|
||||
p.dataDir = filepath.Join(dataHome, ".homeagent", "weather")
|
||||
os.MkdirAll(p.dataDir, 0755)
|
||||
|
||||
// 卸载(删除)时清理天气缓存目录;重载不触发
|
||||
s.RegisterOnRemoveHandler(p.cleanupData)
|
||||
|
||||
tp := p.name + "_"
|
||||
s.RegisterTool(tp+"current", sdk.ToolDef{
|
||||
@ -423,3 +428,10 @@ func (p *Plugin) handleSetLocation(args map[string]interface{}) (interface{}, er
|
||||
p.defaultLoc = loc
|
||||
return map[string]interface{}{"content": fmt.Sprintf("Default location set to: %s", loc)}, nil
|
||||
}
|
||||
|
||||
// cleanupData 卸载时清理天气缓存目录
|
||||
func (p *Plugin) cleanupData() {
|
||||
if p.dataDir != "" {
|
||||
os.RemoveAll(p.dataDir)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user