2023-08-24 10:27:50 +08:00
|
|
|
|
package tstorages
|
2023-08-18 18:04:53 +08:00
|
|
|
|
|
2023-08-21 15:12:57 +08:00
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-09-22 10:53:51 +08:00
|
|
|
|
"joylink.club/rtsssimulation/system"
|
2023-08-21 15:12:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-08-21 15:46:25 +08:00
|
|
|
|
// 仿真模型数据定义
|
2023-09-22 10:53:51 +08:00
|
|
|
|
type ModelData = system.IDeviceModel
|
2023-08-21 15:12:57 +08:00
|
|
|
|
|
|
|
|
|
// 模型存储、管理仓库
|
2023-08-22 11:00:14 +08:00
|
|
|
|
type ModelStorage struct {
|
2023-08-21 15:12:57 +08:00
|
|
|
|
//key-模型id,value-模型指针
|
|
|
|
|
idModelMap map[string]ModelData
|
|
|
|
|
//key-设备类型,value-对应设备类型的所有模型数据的指针列表
|
2023-09-22 10:53:51 +08:00
|
|
|
|
typeModelMap map[system.DeviceType][]ModelData
|
2023-08-22 11:00:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 10:27:50 +08:00
|
|
|
|
func NewModelStorage() *ModelStorage {
|
2023-09-22 10:53:51 +08:00
|
|
|
|
return &ModelStorage{idModelMap: make(map[string]ModelData, 2048), typeModelMap: make(map[system.DeviceType][]ModelData, 128)}
|
2023-08-21 15:12:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加模型数据
|
|
|
|
|
// m 为具体模型数据的指针
|
2023-08-22 11:00:14 +08:00
|
|
|
|
func (me *ModelStorage) AddModel(m ModelData) error {
|
2023-09-21 16:23:03 +08:00
|
|
|
|
_, ok := me.idModelMap[m.Id()]
|
2023-08-21 15:12:57 +08:00
|
|
|
|
if ok {
|
2023-09-21 16:23:03 +08:00
|
|
|
|
return fmt.Errorf("模型[%s]已经存在", m.Id())
|
2023-08-21 15:12:57 +08:00
|
|
|
|
} else {
|
2023-09-21 16:23:03 +08:00
|
|
|
|
me.idModelMap[m.Id()] = m
|
2023-08-21 15:12:57 +08:00
|
|
|
|
//
|
2023-09-21 16:23:03 +08:00
|
|
|
|
_, mdOk := me.typeModelMap[m.Type()]
|
2023-08-21 15:12:57 +08:00
|
|
|
|
if !mdOk {
|
2023-09-21 16:23:03 +08:00
|
|
|
|
me.typeModelMap[m.Type()] = make([]ModelData, 0, 512)
|
2023-08-21 15:12:57 +08:00
|
|
|
|
}
|
2023-09-21 16:23:03 +08:00
|
|
|
|
me.typeModelMap[m.Type()] = append(me.typeModelMap[m.Type()], m)
|
2023-08-21 15:12:57 +08:00
|
|
|
|
//
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据设备类型获取该类型的所有设备数据
|
2023-09-22 10:53:51 +08:00
|
|
|
|
func (me *ModelStorage) FindModelsByType(deviceType system.DeviceType) []ModelData {
|
2023-08-22 11:00:14 +08:00
|
|
|
|
models, ok := me.typeModelMap[deviceType]
|
2023-08-21 15:12:57 +08:00
|
|
|
|
if ok {
|
|
|
|
|
return models
|
|
|
|
|
} else {
|
|
|
|
|
return []ModelData{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据设备id获取对应模型数据
|
|
|
|
|
// 如果不存在返回nil
|
2023-08-22 11:00:14 +08:00
|
|
|
|
func (me *ModelStorage) FindModelById(id string) ModelData {
|
|
|
|
|
md, ok := me.idModelMap[id]
|
2023-08-21 15:12:57 +08:00
|
|
|
|
if ok {
|
|
|
|
|
return md
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遍历某个类型的所有设备
|
2023-09-22 10:53:51 +08:00
|
|
|
|
func (me *ModelStorage) ForEachModelsByType(deviceType system.DeviceType, callback func(md ModelData)) {
|
2023-08-22 11:00:14 +08:00
|
|
|
|
mds := me.FindModelsByType(deviceType)
|
2023-08-21 15:12:57 +08:00
|
|
|
|
for _, modelData := range mds {
|
|
|
|
|
callback(modelData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据id检测设备模型数据是否存在
|
2023-08-22 11:00:14 +08:00
|
|
|
|
func (me *ModelStorage) HasModelById(id string) bool {
|
|
|
|
|
_, ok := me.idModelMap[id]
|
2023-08-21 15:12:57 +08:00
|
|
|
|
return ok
|
2023-08-18 18:04:53 +08:00
|
|
|
|
}
|
2023-08-21 18:09:41 +08:00
|
|
|
|
|
|
|
|
|
// 获取当前模型数据总数量
|
2023-08-22 11:00:14 +08:00
|
|
|
|
func (me *ModelStorage) AmountOfModels() int {
|
|
|
|
|
return len(me.idModelMap)
|
2023-08-21 18:09:41 +08:00
|
|
|
|
}
|
2023-08-22 11:00:14 +08:00
|
|
|
|
func (me *ModelStorage) AmountOfDeviceTypes() int {
|
|
|
|
|
return len(me.typeModelMap)
|
2023-08-21 18:09:41 +08:00
|
|
|
|
}
|