Ai
43 Star 347 Fork 190

GVPiBUILDING-X/driver-box

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
library.go 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
三刀 提交于 2025-04-04 20:58 +08:00 . 新增rest接口
package library
import (
"encoding/json"
"fmt"
"github.com/ibuilding-x/driver-box/driverbox/common"
"github.com/ibuilding-x/driver-box/driverbox/config"
glua "github.com/yuin/gopher-lua"
"os"
"path"
"sync"
)
type Type string
const (
baseDir = "library"
//设备层驱动
deviceDriver Type = "driver"
//物模型
deviceModel Type = "model"
//协议层驱动
protocolDriver Type = "protocol"
//镜像设备模版
mirrorTemplate Type = "mirror_tpl"
ProtocolConfigKey = "protocolKey"
DriverConfigKey = "driverKey"
)
var driverOnce = &sync.Once{}
var mirrorOnce = &sync.Once{}
var protocolOnce = &sync.Once{}
var modelOnce = &sync.Once{}
var driver *DeviceDriver
var mirror *MirrorTemplate
var protocol *ProtocolDriver
var model *DeviceModel
// 设备驱动库
func Driver() *DeviceDriver {
driverOnce.Do(func() {
driver = &DeviceDriver{
drivers: &sync.Map{},
}
})
return driver
}
// 镜像模版库
func Mirror() *MirrorTemplate {
mirrorOnce.Do(func() {
mirror = &MirrorTemplate{}
})
return mirror
}
// 通信协议层驱动库
func Protocol() *ProtocolDriver {
protocolOnce.Do(func() {
protocol = &ProtocolDriver{
drivers: make(map[string]*glua.LState),
}
})
return protocol
}
// 设备模型库
func Model() *DeviceModel {
modelOnce.Do(func() {
model = &DeviceModel{}
})
return model
}
// 加载library中的内容
func LoadContent(library string, fileName string) ([]byte, error) {
filePath := path.Join(config.ResourcePath, baseDir, library, fileName)
if !common.FileExists(filePath) {
return []byte{}, fmt.Errorf("library not found: %s/%s", library, fileName)
}
//读取filePath中的文件内容
return common.ReadFileBytes(filePath)
}
// 加载library中的内容,并填充至结构体
func LoadLibrary(library string, key string, v any) error {
bytes, err := LoadContent(library, key)
if err != nil {
return err
}
return json.Unmarshal(bytes, v)
}
// 保存library中的内容
func SaveContent(library string, fileName string, data string) error {
libraryPath := path.Join(config.ResourcePath, baseDir, library)
if !common.FileExists(libraryPath) {
err := os.MkdirAll(libraryPath, 0755)
if err != nil {
return err
}
}
filePath := path.Join(libraryPath, fileName)
if common.FileExists(filePath) {
return fmt.Errorf("library is exists: %s/%s", library, fileName)
}
//将data写入filePath中
return os.WriteFile(filePath, []byte(data), 0644)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/iBUILDING-X/driver-box.git
git@gitee.com:iBUILDING-X/driver-box.git
iBUILDING-X
driver-box
driver-box
master

搜索帮助