forked from ProjectWIND/ProjectWIND
将数据库api导入到js插件
This commit is contained in:
parent
8a49ded455
commit
0a3a260eda
219
core/api.go
219
core/api.go
@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"ProjectWIND/LOG"
|
||||
"ProjectWIND/database"
|
||||
"ProjectWIND/wba"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
@ -818,108 +819,133 @@ func (a *apiInfo) Log(content string, args ...interface{}) {
|
||||
}
|
||||
|
||||
//database模块
|
||||
// //数据库部分允许字符串变量的读写操作,允许获取配置项操作
|
||||
// func (ai *AppInfo) VarSet(dataMap string, unit string, id string, key string, value string) {
|
||||
// if ai.dbHandler != nil {
|
||||
// ai.dbHandler.Set(ai.Name, dataMap, unit, id, key, value)
|
||||
// }
|
||||
// }
|
||||
// //数据库部分允许字符串变量的读写操作,允许读取配置项操作
|
||||
|
||||
// // VarGet 获取变量
|
||||
// func (ai *AppInfo) VarGet(dataMap string, unit string, id string, key string) (string, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, unit, id, key, false)
|
||||
// if !ok {
|
||||
// return "", false
|
||||
// }
|
||||
// resStr, ok := res.(string)
|
||||
// if !ok {
|
||||
// return "", false
|
||||
// }
|
||||
// return resStr, true
|
||||
// }
|
||||
// return "", false
|
||||
// }
|
||||
type databaseInfo struct {}
|
||||
|
||||
// // GetIntConfig 获取整数配置
|
||||
// func (ai *AppInfo) GetIntConfig(dataMap string, key string) (int64, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "number", key, true)
|
||||
// if !ok {
|
||||
// return 0, false
|
||||
// }
|
||||
// resInt, ok := res.(int64)
|
||||
// if !ok {
|
||||
// return 0, false
|
||||
// }
|
||||
// return resInt, true
|
||||
// }
|
||||
// return 0, false
|
||||
// }
|
||||
func (dbi *databaseInfo) varSet(app wba.AppInfo, datamap string, unit string, id string, key string, value string) {
|
||||
database.Set(app.Name, datamap, unit, id, key, value)
|
||||
}
|
||||
|
||||
// // GetStringConfig 获取字符串配置
|
||||
// func (ai *AppInfo) GetStringConfig(dataMap string, key string) (string, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "string", key, true)
|
||||
// if !ok {
|
||||
// return "", false
|
||||
// }
|
||||
// resStr, ok := res.(string)
|
||||
// if !ok {
|
||||
// return "", false
|
||||
// }
|
||||
// return resStr, true
|
||||
// }
|
||||
// return "", false
|
||||
// }
|
||||
func (dbi *databaseInfo) SetUserVariable(app wba.AppInfo, id string, key string, value string) {
|
||||
dbi.varSet(app, app.Name, "user", id, key, value)
|
||||
}
|
||||
|
||||
// // GetFloatConfig 获取浮点数配置
|
||||
// func (ai *AppInfo) GetFloatConfig(dataMap string, key string) (float64, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "float", key, true)
|
||||
// if !ok {
|
||||
// return 0, false
|
||||
// }
|
||||
// resFloat, ok := res.(float64)
|
||||
// if !ok {
|
||||
// return 0, false
|
||||
// }
|
||||
// return resFloat, true
|
||||
// }
|
||||
// return 0, false
|
||||
// }
|
||||
func (dbi *databaseInfo) SetGroupVariable(app wba.AppInfo, id string, key string, value string) {
|
||||
dbi.varSet(app, app.Name, "group", id, key, value)
|
||||
}
|
||||
|
||||
// // GetIntSliceConfig 获取整数切片配置
|
||||
// func (ai *AppInfo) GetIntSliceConfig(dataMap string, key string) ([]int64, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "number_slice", key, true)
|
||||
// if !ok {
|
||||
// return nil, false
|
||||
// }
|
||||
// resSlice, ok := res.([]int64)
|
||||
// if !ok {
|
||||
// return nil, false
|
||||
// }
|
||||
// return resSlice, true
|
||||
// }
|
||||
// return nil, false
|
||||
// }
|
||||
func (dbi *databaseInfo) SetGlobalVariable(app wba.AppInfo, id string, key string, value string) {
|
||||
dbi.varSet(app, app.Name, "global", id, key, value)
|
||||
}
|
||||
|
||||
// // GetStringSliceConfig 获取字符串切片配置
|
||||
// func (ai *AppInfo) GetStringSliceConfig(dataMap string, key string) ([]string, bool) {
|
||||
// if ai.dbHandler != nil {
|
||||
// res, ok := ai.dbHandler.Get(ai.Name, dataMap, "config", "string_slice", key, true)
|
||||
// if !ok {
|
||||
// return nil, false
|
||||
// }
|
||||
// resSlice, ok := res.([]string)
|
||||
// if !ok {
|
||||
// return nil, false
|
||||
// }
|
||||
// return resSlice, true
|
||||
// }
|
||||
// return nil, false
|
||||
// }
|
||||
func (dbi *databaseInfo) SetOutUserVariable(app wba.AppInfo, datamap string, id string, key string, value string) {
|
||||
dbi.varSet(app, datamap, "user", id, key, value)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) SetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string, value string) {
|
||||
dbi.varSet(app, datamap, "group", id, key, value)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) SetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string, value string) {
|
||||
dbi.varSet(app, datamap, "global", id, key, value)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) varGet(app wba.AppInfo, datamap string, unit string, id string, key string) (string, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, unit, id, key, false)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
resStr, ok := res.(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
return resStr, true
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetUserVariable(app wba.AppInfo, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, app.Name, "user", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetGroupVariable(app wba.AppInfo, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, app.Name, "group", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetGlobalVariable(app wba.AppInfo, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, app.Name, "global", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetOutUserVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, datamap, "user", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetOutGroupVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, datamap, "group", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetOutGlobalVariable(app wba.AppInfo, datamap string, id string, key string) (string, bool) {
|
||||
return dbi.varGet(app, datamap, "global", id, key)
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetIntConfig(app wba.AppInfo, datamap string, key string) (int64, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, "config", "number", key, true)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
resInt, ok := res.(int64)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return resInt, true
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetStringConfig(app wba.AppInfo, datamap string, key string) (string, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, "config", "string", key, true)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
resStr, ok := res.(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
return resStr, true
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetFloatConfig(app wba.AppInfo, datamap string, key string) (float64, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, "config", "float", key, true)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
resFloat, ok := res.(float64)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return resFloat, true
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetIntSliceConfig(app wba.AppInfo, datamap string, key string) ([]int64, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, "config", "number_slice", key, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
resSlice, ok := res.([]int64)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return resSlice, true
|
||||
}
|
||||
|
||||
func (dbi *databaseInfo) GetStringSliceConfig(app wba.AppInfo, datamap string, key string) ([]string, bool) {
|
||||
res, ok := database.Get(app.Name, datamap, "config", "string_slice", key, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
resSlice, ok := res.([]string)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return resSlice, true
|
||||
}
|
||||
|
||||
// 文件管理模块
|
||||
//TODO: 文件管理模块待实现
|
||||
@ -930,6 +956,7 @@ func (a *apiInfo) Log(content string, args ...interface{}) {
|
||||
//核心信息调用模块
|
||||
|
||||
var AppApi apiInfo
|
||||
var DatabaseApi databaseInfo
|
||||
|
||||
func GenerateUUID() (string, error) {
|
||||
uuid := make([]byte, 16)
|
||||
|
@ -58,6 +58,11 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
LOG.Error("初始化应用 %s 失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
err = app.InitWSD(&DatabaseApi)
|
||||
if err != nil {
|
||||
LOG.Error("初始化应用 %s 数据库失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
CmdMap = mergeMaps(CmdMap, app.Get().CmdMap)
|
||||
ScheduledTasks := app.Get().ScheduledTasks
|
||||
for _, task := range ScheduledTasks {
|
||||
@ -84,6 +89,7 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
// 创建JS可用的wbaObj对象
|
||||
wbaObj := runtime.NewObject()
|
||||
wsp := runtime.NewObject()
|
||||
wsd := runtime.NewObject()
|
||||
_ = runtime.Set("wba", wbaObj)
|
||||
_ = wbaObj.Set("NewApp", wba.NewApp)
|
||||
_ = wbaObj.Set("NewCmd", wba.NewCmd)
|
||||
@ -97,6 +103,7 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
_ = wbaObj.Set("WithAppType", wba.WithAppType)
|
||||
_ = wbaObj.Set("WithRule", wba.WithRule)
|
||||
_ = wbaObj.Set("WSP", wsp)
|
||||
_ = wbaObj.Set("WSD", wsd)
|
||||
_ = wsp.Set("UnsafelySendMsg", AppApi.UnsafelySendMsg)
|
||||
_ = wsp.Set("UnsafelySendPrivateMsg", AppApi.UnsafelySendPrivateMsg)
|
||||
_ = wsp.Set("UnsafelySendGroupMsg", AppApi.UnsafelySendGroupMsg)
|
||||
@ -139,6 +146,23 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
_ = wsp.Set("CleanCache", AppApi.CleanCache)
|
||||
_ = wsp.Set("GetLoginInfo", AppApi.LogWith)
|
||||
_ = wsp.Set("GetVersionInfo", AppApi.GetVersionInfo)
|
||||
_ = wsd.Set("SetUserVariable", DatabaseApi.SetUserVariable)
|
||||
_ = wsd.Set("SetGroupVariable", DatabaseApi.SetGroupVariable)
|
||||
_ = wsd.Set("SetGlobalVariable", DatabaseApi.SetGlobalVariable)
|
||||
_ = wsd.Set("SetOutUserVariable", DatabaseApi.SetOutUserVariable)
|
||||
_ = wsd.Set("SetOutGroupVariable", DatabaseApi.SetOutGroupVariable)
|
||||
_ = wsd.Set("SetOutGlobalVariable", DatabaseApi.SetOutGlobalVariable)
|
||||
_ = wsd.Set("GetUserVariable", DatabaseApi.GetUserVariable)
|
||||
_ = wsd.Set("GetGroupVariable", DatabaseApi.GetGroupVariable)
|
||||
_ = wsd.Set("GetGlobalVariable", DatabaseApi.GetGlobalVariable)
|
||||
_ = wsd.Set("GetOutUserVariable", DatabaseApi.GetOutUserVariable)
|
||||
_ = wsd.Set("GetOutGroupVariable", DatabaseApi.GetOutGroupVariable)
|
||||
_ = wsd.Set("GetOutGlobalVariable", DatabaseApi.GetOutGlobalVariable)
|
||||
_ = wsd.Set("GetIntConfig", DatabaseApi.GetIntConfig)
|
||||
_ = wsd.Set("GetFloatConfig", DatabaseApi.GetFloatConfig)
|
||||
_ = wsd.Set("GetStringConfig", DatabaseApi.GetStringConfig)
|
||||
_ = wsd.Set("GetIntSliceConfig", DatabaseApi.GetIntSliceConfig)
|
||||
_ = wsd.Set("GetStringSliceConfig", DatabaseApi.GetStringSliceConfig)
|
||||
|
||||
// 获取AppInit函数
|
||||
appInitVal := runtime.Get("AppInit")
|
||||
|
@ -72,6 +72,11 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
LOG.Error("初始化应用 %s 失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
err = app.InitWSD(&DatabaseApi)
|
||||
if err != nil {
|
||||
LOG.Error("初始化应用 %s 数据库失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
CmdMap = mergeMaps(CmdMap, app.Get().CmdMap)
|
||||
LOG.Info("应用 %s 加载成功", pluginPath)
|
||||
return 1, 1
|
||||
@ -95,6 +100,7 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
// 创建JS可用的wbaObj对象
|
||||
wbaObj := runtime.NewObject()
|
||||
wsp := runtime.NewObject()
|
||||
wsd := runtime.NewObject()
|
||||
_ = runtime.Set("wba", wbaObj)
|
||||
_ = wbaObj.Set("NewApp", wba.NewApp)
|
||||
_ = wbaObj.Set("NewCmd", wba.NewCmd)
|
||||
@ -108,6 +114,7 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
_ = wbaObj.Set("WithAppType", wba.WithAppType)
|
||||
_ = wbaObj.Set("WithRule", wba.WithRule)
|
||||
_ = wbaObj.Set("WSP", wsp)
|
||||
_ = wbaObj.Set("WSD", wsd)
|
||||
_ = wsp.Set("UnsafelySendMsg", AppApi.UnsafelySendMsg)
|
||||
_ = wsp.Set("UnsafelySendPrivateMsg", AppApi.UnsafelySendPrivateMsg)
|
||||
_ = wsp.Set("UnsafelySendGroupMsg", AppApi.UnsafelySendGroupMsg)
|
||||
@ -150,6 +157,23 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
_ = wsp.Set("CleanCache", AppApi.CleanCache)
|
||||
_ = wsp.Set("GetLoginInfo", AppApi.LogWith)
|
||||
_ = wsp.Set("GetVersionInfo", AppApi.GetVersionInfo)
|
||||
_ = wsd.Set("SetUserVariable", DatabaseApi.SetUserVariable)
|
||||
_ = wsd.Set("SetGroupVariable", DatabaseApi.SetGroupVariable)
|
||||
_ = wsd.Set("SetGlobalVariable", DatabaseApi.SetGlobalVariable)
|
||||
_ = wsd.Set("SetOutUserVariable", DatabaseApi.SetOutUserVariable)
|
||||
_ = wsd.Set("SetOutGroupVariable", DatabaseApi.SetOutGroupVariable)
|
||||
_ = wsd.Set("SetOutGlobalVariable", DatabaseApi.SetOutGlobalVariable)
|
||||
_ = wsd.Set("GetUserVariable", DatabaseApi.GetUserVariable)
|
||||
_ = wsd.Set("GetGroupVariable", DatabaseApi.GetGroupVariable)
|
||||
_ = wsd.Set("GetGlobalVariable", DatabaseApi.GetGlobalVariable)
|
||||
_ = wsd.Set("GetOutUserVariable", DatabaseApi.GetOutUserVariable)
|
||||
_ = wsd.Set("GetOutGroupVariable", DatabaseApi.GetOutGroupVariable)
|
||||
_ = wsd.Set("GetOutGlobalVariable", DatabaseApi.GetOutGlobalVariable)
|
||||
_ = wsd.Set("GetIntConfig", DatabaseApi.GetIntConfig)
|
||||
_ = wsd.Set("GetFloatConfig", DatabaseApi.GetFloatConfig)
|
||||
_ = wsd.Set("GetStringConfig", DatabaseApi.GetStringConfig)
|
||||
_ = wsd.Set("GetIntSliceConfig", DatabaseApi.GetIntSliceConfig)
|
||||
_ = wsd.Set("GetStringSliceConfig", DatabaseApi.GetStringSliceConfig)
|
||||
|
||||
// 获取AppInit函数
|
||||
appInitVal := runtime.Get("AppInit")
|
||||
|
@ -29,6 +29,10 @@ func (app *AppInfo) Init(Api wba.WindStandardProtocolAPI) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *AppInfo) InitWSD(Api wba.WindStandardDataBaseAPI) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *AppInfo) GetCmd() map[string]wba.Cmd {
|
||||
return app.CmdMap
|
||||
}
|
||||
|
@ -538,16 +538,6 @@ func Set(appName string, datamap string, unit string, id string, key string, val
|
||||
dataSet(appName, unit, id, key, value, true)
|
||||
}
|
||||
|
||||
type DatabaseHandlerImpl struct{}
|
||||
|
||||
func (dbh *DatabaseHandlerImpl) Set(appName string, datamap string, unit string, id string, key string, value interface{}) {
|
||||
Set(appName, datamap, unit, id, key, value)
|
||||
}
|
||||
|
||||
func (dbh *DatabaseHandlerImpl) Get(appName string, datamap string, unit string, id string, key string, isGettingConfig bool) (interface{}, bool) {
|
||||
return Get(appName, datamap, unit, id, key, isGettingConfig)
|
||||
}
|
||||
|
||||
// func VarSet(app wba.AppInfo, datamap string, unit string, id string, key string, value string) {
|
||||
// Set(app.Name, datamap, unit, id, key, value)
|
||||
// }
|
||||
|
@ -1 +0,0 @@
|
||||
{"Id":"datamap","Users":{},"Groups":{},"Global":{}}
|
@ -1,40 +0,0 @@
|
||||
```go
|
||||
func Start() //启动默认数据库
|
||||
```
|
||||
|
||||
该函数用于启动数据库,加载数据,启动自动存储
|
||||
|
||||
Tips:需要异步启动数据库,否则进程会被阻塞
|
||||
|
||||
Tips:需要在启动数据库后等待其初始化,建议使用time.Sleep()函数等待至少1秒
|
||||
|
||||
```go
|
||||
func Get(database *database, category string, id string, key string) (string,bool) //获取变量,示例用法:// database.Get("user", "1001", "age") 表示查询db数据库中id为1001的用户个人变量age
|
||||
```
|
||||
|
||||
该函数用于查询设定的变量
|
||||
|
||||
——category部分可以填入"user","group","global",分别表示个人变量,群变量,全局变量
|
||||
|
||||
——id为用户id或群id,全局变量使用时,id可以理解为命名空间
|
||||
|
||||
——key为要查询的变量名
|
||||
|
||||
返回值类型为string,bool,第一个返回值为查询到的变量,第二个返回值表示是否返回成功
|
||||
|
||||
```go
|
||||
func Set(category string, id string, key string, value string) //修改变量,示例用法:
|
||||
// database.Set("user", "1001", "age", "18") 表示将db数据库中id为1001的用户个人变量age设置为"18"
|
||||
// 注意:变量目前只支持string类型,如果需要储存数据或对象,请将它们转化为string类型再进行储存
|
||||
// 该数据库的所有变量将会存放在/data/database/datamap.txt中,请不要乱动这个文件
|
||||
```
|
||||
|
||||
该函数用于新建或修改变量
|
||||
|
||||
——category部分可以填入"user","group","global",分别表示个人变量,群变量,全局变量
|
||||
|
||||
——id为用户id或群id,全局变量使用时,id可以理解为命名空间
|
||||
|
||||
——key为要修改的变量名
|
||||
|
||||
——value为要修改的变量值
|
25
utils.go
25
utils.go
@ -360,30 +360,5 @@ func ReloadApps() {
|
||||
func startDatabase() {
|
||||
go database.Start()
|
||||
time.Sleep(time.Second * 1)
|
||||
// 读写测试
|
||||
// for i := 0; i < 10; i++ {
|
||||
// data, ok := database.Get("go", "go", "user", "test", "test"+fmt.Sprintf("%d", i), false)
|
||||
// if !ok {
|
||||
// LOG.Error("Failed to get data from database")
|
||||
// continue
|
||||
// }
|
||||
// LOG.Info("Get data from database: %v", data)
|
||||
// time.Sleep(time.Second * 1)
|
||||
// }
|
||||
// time.Sleep(time.Second * 1)
|
||||
// for i := 0; i < 10; i++ {
|
||||
// database.Set("go", "go", "user", "test", "test"+fmt.Sprintf("%d", i), "test"+fmt.Sprintf("%d", 1000+i))
|
||||
// time.Sleep(time.Second * 1)
|
||||
// }
|
||||
// time.Sleep(time.Second * 1)
|
||||
// for i := 0; i < 10; i++ {
|
||||
// data, ok := database.Get("go", "go", "user", "test", "test"+fmt.Sprintf("%d", i), false)
|
||||
// if !ok {
|
||||
// LOG.Error("Failed to get data from database")
|
||||
// continue
|
||||
// }
|
||||
// LOG.Info("Get data from database: %v", data)
|
||||
// time.Sleep(time.Second * 1)
|
||||
// }
|
||||
select {}
|
||||
}
|
||||
|
191
wba/wind.go
191
wba/wind.go
@ -7,6 +7,7 @@ import (
|
||||
type APP interface {
|
||||
Get() AppInfo
|
||||
Init(api WindStandardProtocolAPI) error
|
||||
InitWSD(api WindStandardDataBaseAPI) error
|
||||
}
|
||||
|
||||
// WindStandardProtocolAPI Wind标准协议API,提供了onebot11标准中的API接口。
|
||||
@ -265,9 +266,84 @@ type WindStandardProtocolAPI interface {
|
||||
Log(log string, args ...interface{})
|
||||
}
|
||||
|
||||
type DataBaseHandler interface {
|
||||
Set(appName string, dataMap string, unit string, id string, key string, value interface{})
|
||||
Get(appName string, dataMap string, unit string, id string, key string, isGettingConfig bool) (interface{}, bool)
|
||||
type WindStandardDataBaseAPI interface {
|
||||
// SetUserVariable 设置用户变量
|
||||
// SetGroupVariable 设置群组变量
|
||||
// SetGlobalVariable 设置全局变量
|
||||
// SetOutUserVarialbe 设置其他数据库中的用户变量(需要权限)
|
||||
// SetOutGroupVarialbe 设置其他数据库中的群组变量(需要权限)
|
||||
// SetOutGlobalVarialbe 设置其他数据库中的全局变量(需要权限)
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - id: 数据单元 ID。
|
||||
// - key: 变量名称。
|
||||
// - value: 变量值。
|
||||
// - datamap: 数据表名称。
|
||||
SetUserVariable(app AppInfo, id string, key string, value string)
|
||||
SetGroupVariable(app AppInfo, id string, key string, value string)
|
||||
SetGlobalVariable(app AppInfo, id string, key string, value string)
|
||||
SetOutUserVariable(app AppInfo, datamap string, id string, key string, value string)
|
||||
SetOutGroupVariable(app AppInfo, datamap string, id string, key string, value string)
|
||||
SetOutGlobalVariable(app AppInfo, datamap string, id string, key string, value string)
|
||||
|
||||
// GetUserVariable 获取用户变量
|
||||
// GetGroupVariable 获取群组变量
|
||||
// GetGlobalVariable 获取全局变量
|
||||
// GetOutUserVariable 获取其他数据库中的用户变量(需要权限)
|
||||
// GetOutGroupVariable 获取其他数据库中的群组变量(需要权限)
|
||||
// GetOutGlobalVariable 获取其他数据库中的全局变量(需要权限)
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - id: 数据单元 ID。
|
||||
// - key: 变量名称。
|
||||
// - datamap:数据表名称。
|
||||
// 返回: 变量值,是否存在。
|
||||
GetUserVariable(app AppInfo, id string, key string) (string, bool)
|
||||
GetGroupVariable(app AppInfo, id string, key string) (string, bool)
|
||||
GetGlobalVariable(app AppInfo, id string, key string) (string, bool)
|
||||
GetOutUserVariable(app AppInfo, datamap string, id string, key string) (string, bool)
|
||||
GetOutGroupVariable(app AppInfo, datamap string, id string, key string) (string, bool)
|
||||
GetOutGlobalVariable(app AppInfo, datamap string, id string, key string) (string, bool)
|
||||
|
||||
// GetIntConfig 获取指定数据单元的整数型配置。
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - datamap: 数据单元名称。
|
||||
// - key: 配置名称。
|
||||
// 返回: 配置值,是否存在。
|
||||
GetIntConfig(app AppInfo, datamap string, key string) (int64, bool)
|
||||
|
||||
// GetStringConfig 获取指定数据单元的字符串型配置。
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - datamap: 数据单元名称。
|
||||
// - key: 配置名称。
|
||||
// 返回: 配置值,是否存在。
|
||||
GetStringConfig(app AppInfo, datamap string, key string) (string, bool)
|
||||
|
||||
// GetFloatConfig 获取指定数据单元的浮点型配置。
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - datamap: 数据单元名称。
|
||||
// - key: 配置名称。
|
||||
// 返回: 配置值,是否存在。
|
||||
GetFloatConfig(app AppInfo, datamap string, key string) (float64, bool)
|
||||
|
||||
// GetIntSliceConfig 获取指定数据单元的整数型切片配置。
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - datamap: 数据单元名称。
|
||||
// - key: 配置名称。
|
||||
// 返回: 配置值,是否存在。
|
||||
GetIntSliceConfig(app AppInfo, datamap string, key string) ([]int64, bool)
|
||||
|
||||
// GetStringSliceConfig 获取指定数据单元的字符串型切片配置。
|
||||
// 参数:
|
||||
// - app: 应用信息。
|
||||
// - datamap: 数据单元名称。
|
||||
// - key: 配置名称。
|
||||
// 返回: 配置值,是否存在。
|
||||
GetStringSliceConfig(app AppInfo, datamap string, key string) ([]string, bool)
|
||||
}
|
||||
|
||||
type AppInfo struct {
|
||||
@ -287,7 +363,6 @@ type AppInfo struct {
|
||||
MetaEventHandler func(msg MetaEventInfo)
|
||||
ScheduledTasks map[string]ScheduledTaskInfo
|
||||
API map[string]interface{}
|
||||
DbHandler DataBaseHandler
|
||||
}
|
||||
|
||||
func (ai AppInfo) Get() AppInfo {
|
||||
@ -299,6 +374,11 @@ func (ai *AppInfo) Init(api WindStandardProtocolAPI) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ai *AppInfo) InitWSD(api WindStandardDataBaseAPI) error {
|
||||
WSD = api
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ai *AppInfo) AddCmd(name string, cmd Cmd) {
|
||||
ai.CmdMap[name] = cmd
|
||||
}
|
||||
@ -311,108 +391,6 @@ func (ai *AppInfo) AddScheduledTask(task ScheduledTaskInfo) {
|
||||
ai.ScheduledTasks[task.Name] = task
|
||||
}
|
||||
|
||||
func (ai *AppInfo) VarSet(dataMap string, unit string, id string, key string, value string) {
|
||||
if ai.DbHandler != nil {
|
||||
ai.DbHandler.Set(ai.Name, dataMap, unit, id, key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// VarGet 获取变量
|
||||
func (ai *AppInfo) VarGet(dataMap string, unit string, id string, key string) (string, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, unit, id, key, false)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
resStr, ok := res.(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
return resStr, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetIntConfig 获取整数配置
|
||||
func (ai *AppInfo) GetIntConfig(dataMap string, key string) (int64, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, "config", "number", key, true)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
resInt, ok := res.(int64)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return resInt, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetStringConfig 获取字符串配置
|
||||
func (ai *AppInfo) GetStringConfig(dataMap string, key string) (string, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, "config", "string", key, true)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
resStr, ok := res.(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
return resStr, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetFloatConfig 获取浮点数配置
|
||||
func (ai *AppInfo) GetFloatConfig(dataMap string, key string) (float64, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, "config", "float", key, true)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
resFloat, ok := res.(float64)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
return resFloat, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetIntSliceConfig 获取整数切片配置
|
||||
func (ai *AppInfo) GetIntSliceConfig(dataMap string, key string) ([]int64, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, "config", "number_slice", key, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
resSlice, ok := res.([]int64)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return resSlice, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// GetStringSliceConfig 获取字符串切片配置
|
||||
func (ai *AppInfo) GetStringSliceConfig(dataMap string, key string) ([]string, bool) {
|
||||
if ai.DbHandler != nil {
|
||||
res, ok := ai.DbHandler.Get(ai.Name, dataMap, "config", "string_slice", key, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
resSlice, ok := res.([]string)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return resSlice, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
type AppInfoOption func(ei *AppInfo)
|
||||
|
||||
func WithName(name string) AppInfoOption {
|
||||
@ -745,3 +723,4 @@ type ScheduledTaskInfo struct {
|
||||
}
|
||||
|
||||
var WSP WindStandardProtocolAPI
|
||||
var WSD WindStandardDataBaseAPI
|
||||
|
Loading…
x
Reference in New Issue
Block a user