Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/20240409_0.8.0_dev' into 2024040…
Browse files Browse the repository at this point in the history
…9_0.8.0_dev
  • Loading branch information
smthing committed Jun 3, 2024
2 parents 3ff2b45 + 3dc0795 commit 9c342be
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
1 change: 0 additions & 1 deletion driverbox/helper/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ type coreCache interface {
DeleteDevice(id string) // 删除设备
UpdateDeviceDesc(id string, desc string) // 更新设备描述
Reset()
AddOrUpdateDevice(device config.Device) error // 添加或更新设备

// businessPropCache 业务属性接口
businessPropCache
Expand Down
25 changes: 22 additions & 3 deletions driverbox/helper/cmanager/cmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
// DefaultConfigPath is the default path to the config directory.
DefaultConfigPath string = "./res/drivers"
DefaultConfigPath string = "./res/driver"
// DefaultConfigName is the default name of the config file.
DefaultConfigName string = "config.json"
// DefaultScriptName is the default name of the script file.
Expand Down Expand Up @@ -480,8 +480,8 @@ func (m *manager) addConfig(c config.Config) error {
// 合并模型、设备
for _, model := range c.DeviceModels {
if modelIndex, ok := conf.GetModelIndexes()[model.Name]; ok {
// 更新
conf.DeviceModels[modelIndex] = model
// 合并设备
conf.DeviceModels[modelIndex].Devices = m.mergeDevices(conf.DeviceModels[modelIndex].Devices, model.Devices)
} else {
// 新增
conf.DeviceModels = append(conf.DeviceModels, model)
Expand Down Expand Up @@ -591,3 +591,22 @@ func (m *manager) createConfig(protocolName string) config.Config {
ProtocolName: protocolName,
}
}

// mergeDevice 合并设备
// 以 arr1 为主,arr2 为辅,合并 arr1 与 arr2 相同的设备,并返回合并后的设备列表
func (m *manager) mergeDevices(arr1 []config.Device, arr2 []config.Device) []config.Device {
ids := make(map[string]struct{})
var result []config.Device

all := append(arr1, arr2...)
for _, device := range all {
if _, exist := ids[device.ID]; exist {
continue
}

ids[device.ID] = struct{}{}
result = append(result, device)
}

return result
}
73 changes: 73 additions & 0 deletions driverbox/helper/cmanager/cmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
)

func TestAddConnection(t *testing.T) {
if err := LoadConfig(); err != nil {
t.Error(err)
}

conn := map[string]any{
"address": "/dev/ttyS5",
"discover": true,
Expand All @@ -16,6 +20,10 @@ func TestAddConnection(t *testing.T) {
}

func TestAddModel(t *testing.T) {
if err := LoadConfig(); err != nil {
t.Error(err)
}

model := config.DeviceModel{
ModelBase: config.ModelBase{
Name: "test_model_001",
Expand All @@ -36,6 +44,10 @@ func TestAddModel(t *testing.T) {
}

func TestAddDevice(t *testing.T) {
if err := LoadConfig(); err != nil {
t.Error(err)
}

device := config.Device{
ID: "device_1",
ModelName: "test_model_001",
Expand All @@ -51,6 +63,67 @@ func TestAddDevice(t *testing.T) {
}
}

func TestAddConfig(t *testing.T) {
if err := LoadConfig(); err != nil {
t.Error(err)
}

c := config.Config{
DeviceModels: []config.DeviceModel{
{
ModelBase: config.ModelBase{
Name: "test_model_001",
ModelID: "test_model_001",
Description: "测试模型",
},
DevicePoints: []config.PointMap{
{
"name": "onOff",
"type": "int",
},
},
Devices: []config.Device{
{
ID: "device_2",
ModelName: "test_model_001",
Description: "测试设备2",
Ttl: "",
Tags: nil,
ConnectionKey: "/dev/ttyS0",
Properties: nil,
DriverKey: "",
},
},
},
{
ModelBase: config.ModelBase{
Name: "test_model_002",
ModelID: "test_model_002",
Description: "测试模型2",
},
DevicePoints: []config.PointMap{
{
"name": "onOff",
"type": "int",
},
},
Devices: nil,
},
},
Connections: map[string]any{
"/dev/ttyS0": map[string]any{
"address": "/dev/ttyS0",
"discover": true,
},
},
ProtocolName: "modbus",
}

if err := AddConfig(c); err != nil {
t.Error(err)
}
}

func BenchmarkAddConnection(b *testing.B) {
for i := 0; i < b.N; i++ {
conn := map[string]any{
Expand Down

0 comments on commit 9c342be

Please sign in to comment.