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