Skip to content

Commit

Permalink
fix(核心缓存): 优化添加设备逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xcocx committed Jun 6, 2024
1 parent 8797753 commit 0560a44
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 19 deletions.
18 changes: 17 additions & 1 deletion driverbox/helper/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ func (c *cache) AddOrUpdateDevice(device config.Device) error {
if Logger != nil {
Logger.Info("core cache add device", zap.Any("device", device))
}
// 查找模型信息
model, ok := cmanager.GetModel(device.ModelName)
if !ok {
Logger.Error("model not found", zap.String("modelName", device.ModelName))
return fmt.Errorf("model %s not found", device.ModelName)
}
// 查找配置 key
key := cmanager.GetConfigKeyByModel(model.Name)
if key == "" {
Logger.Error("config key not found", zap.String("modelName", model.Name))
return fmt.Errorf("config key not found for model %s", model.Name)
}
// 更新 devicePointPlugins
for _, pointMap := range model.DevicePoints {
c.devicePointPlugins.Store(fmt.Sprintf("%s_%s", device.ID, pointMap.ToPoint().Name), key)
}
// 自动补全设备描述
if device.Description == "" {
device.Description = device.ID
Expand Down Expand Up @@ -453,7 +469,7 @@ func (c *cache) GetConnection(key string) (any, error) {

// GetConnectionPluginName 获取连接所属的插件名称
func (c *cache) GetConnectionPluginName(key string) string {
return cmanager.GetConnectionPluginName(key)
return cmanager.GetPluginNameByConnection(key)
}

// AddModel 新增模型
Expand Down
96 changes: 78 additions & 18 deletions driverbox/helper/cmanager/cmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type Manager interface {
GetConfigs() map[string]config.Config
// AddConfig 新增配置
AddConfig(c config.Config) error
// GetConfig 根据 key 获取配置
GetConfig(key string) (config.Config, bool)
// GetConfigKeyByModel 根据模型名获取配置 key
GetConfigKeyByModel(modelName string) string

// -------------------- 模型相关 --------------------

Expand Down Expand Up @@ -79,8 +83,13 @@ type Manager interface {
AddConnection(plugin string, key string, conn any) error
// GetConnection 获取连接信息
GetConnection(key string) (any, error)
// GetConnectionPluginName 获取连接所属的插件名称
GetConnectionPluginName(key string) string

// -------------------- 插件相关 --------------------

// GetPluginNameByModel 获取模型所属的插件名称
GetPluginNameByModel(modelName string) string
// GetPluginNameByConnection 获取连接所属的插件名称
GetPluginNameByConnection(key string) string
}

// manager 实现配置管理器接口
Expand Down Expand Up @@ -127,6 +136,14 @@ func AddConfig(c config.Config) error {
return instance.AddConfig(c)
}

func GetConfig(key string) (config.Config, bool) {
return instance.GetConfig(key)
}

func GetConfigKeyByModel(modelName string) string {
return instance.GetConfigKeyByModel(modelName)
}

func GetModel(modelName string) (config.DeviceModel, bool) {
return instance.GetModel(modelName)
}
Expand Down Expand Up @@ -163,8 +180,12 @@ func GetConnection(key string) (any, error) {
return instance.GetConnection(key)
}

func GetConnectionPluginName(key string) string {
return instance.GetConnectionPluginName(key)
func GetPluginNameByModel(modelName string) string {
return instance.GetPluginNameByModel(modelName)
}

func GetPluginNameByConnection(key string) string {
return instance.GetPluginNameByConnection(key)
}

// SetConfigPath 设置配置目录
Expand Down Expand Up @@ -241,6 +262,31 @@ func (m *manager) GetConfigs() map[string]config.Config {
return m.configs
}

// GetConfig 根据 key 获取配置
func (m *manager) GetConfig(key string) (config.Config, bool) {
m.mux.RLock()
defer m.mux.RUnlock()

if c, ok := m.configs[key]; ok {
return c, true
}
return config.Config{}, false
}

// GetConfigKeyByModel 通过模型名称获取配置 key
func (m *manager) GetConfigKeyByModel(modelName string) string {
m.mux.RLock()
defer m.mux.RUnlock()

for key, conf := range m.configs {
if _, ok := conf.GetModelIndexes()[modelName]; ok {
return key
}
}

return ""
}

// GetModel 获取模型
func (m *manager) GetModel(modelName string) (config.DeviceModel, bool) {
m.mux.RLock()
Expand Down Expand Up @@ -459,20 +505,6 @@ func (m *manager) GetConnection(key string) (any, error) {
return nil, nil
}

// GetConnectionPluginName 获取连接所属的插件名称
func (m *manager) GetConnectionPluginName(key string) string {
m.mux.RLock()
defer m.mux.RUnlock()

for _, conf := range m.configs {
if _, ok := conf.Connections[key]; ok {
return conf.ProtocolName
}
}

return ""
}

// addConfig 新增配置
func (m *manager) addConfig(c config.Config) error {
for s, conf := range m.configs {
Expand Down Expand Up @@ -610,3 +642,31 @@ func (m *manager) mergeDevices(arr1 []config.Device, arr2 []config.Device) []con

return result
}

// GetPluginNameByModel 获取模型所属的插件名称
func (m *manager) GetPluginNameByModel(modelName string) string {
m.mux.RLock()
defer m.mux.RUnlock()

for _, conf := range m.configs {
if _, ok := conf.GetModelIndexes()[modelName]; ok {
return conf.ProtocolName
}
}

return ""
}

// GetPluginNameByConnection 获取连接所属的插件名称
func (m *manager) GetPluginNameByConnection(key string) string {
m.mux.RLock()
defer m.mux.RUnlock()

for _, conf := range m.configs {
if _, ok := conf.Connections[key]; ok {
return conf.ProtocolName
}
}

return ""
}

0 comments on commit 0560a44

Please sign in to comment.