2023-07-31 16:48:10 +08:00
|
|
|
|
package memory
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-31 17:35:07 +08:00
|
|
|
|
"sync"
|
2023-07-31 16:48:10 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-07-31 17:35:07 +08:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-07-31 16:48:10 +08:00
|
|
|
|
// 轨旁仿真模型状态
|
|
|
|
|
type VerifyStatus struct {
|
2023-08-01 14:54:11 +08:00
|
|
|
|
//道岔状态,key为道岔id即索引 state.SwitchState
|
|
|
|
|
SwitchStateMap sync.Map
|
|
|
|
|
//轨道状态,key为轨道id即索引 state.LinkState
|
|
|
|
|
LinkStateMap sync.Map
|
|
|
|
|
//列车状态,key为列车id即索引 state.TrainState
|
|
|
|
|
TrainStateMap sync.Map
|
|
|
|
|
//计轴区段状态,key为计轴区段的id即索引 state.SectionState
|
|
|
|
|
AxleSectionStateMap sync.Map
|
|
|
|
|
//物理区段状态,key为物理区段id即索引 state.SectionState
|
|
|
|
|
PhysicalSectionStateMap sync.Map
|
|
|
|
|
//逻辑区段状态,key为逻辑区段id即索引 state.SectionState
|
|
|
|
|
LogicSectionStateMap sync.Map
|
|
|
|
|
//信号机状态,key为信号机id,即索引 state.SignalState
|
|
|
|
|
SignalStateMap sync.Map
|
2023-07-31 16:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 14:54:11 +08:00
|
|
|
|
// 轨旁仿真模型状态(变更)
|
|
|
|
|
type ChangeVerifyStatus struct {
|
|
|
|
|
VerifyStatus
|
|
|
|
|
//删除的列车ID列表
|
|
|
|
|
RemoveTrainId []string
|
2023-07-31 17:35:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-07-31 16:48:10 +08:00
|
|
|
|
// 轨旁仿真内存模型
|
|
|
|
|
type WaysideMemory struct {
|
2023-08-01 14:54:11 +08:00
|
|
|
|
//可变状态数据:轨旁仿真模型状态(全量数据)
|
2023-07-31 16:48:10 +08:00
|
|
|
|
Status *VerifyStatus
|
2023-08-01 14:54:11 +08:00
|
|
|
|
// 要变更的状态:用户操作过的状态记录在这里,增量推送次数据
|
|
|
|
|
ChangeStatus *ChangeVerifyStatus
|
2023-07-31 17:35:07 +08:00
|
|
|
|
//状态保护锁
|
|
|
|
|
rwLock *sync.RWMutex
|
2023-07-31 16:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化轨旁仿真内存模型
|
2023-07-31 17:35:07 +08:00
|
|
|
|
func (memory *WaysideMemory) Create() *WaysideMemory {
|
2023-08-01 14:54:11 +08:00
|
|
|
|
memory.Status = new(VerifyStatus)
|
|
|
|
|
memory.ChangeStatus = &ChangeVerifyStatus{}
|
2023-07-31 17:35:07 +08:00
|
|
|
|
memory.rwLock = new(sync.RWMutex)
|
2023-07-31 16:48:10 +08:00
|
|
|
|
return memory
|
|
|
|
|
}
|
2023-07-31 17:35:07 +08:00
|
|
|
|
|
|
|
|
|
// 状态读保护操作
|
|
|
|
|
func (me *WaysideMemory) SafeOptRead(opt func(memory *WaysideMemory) any) any {
|
|
|
|
|
me.rwLock.RLock()
|
|
|
|
|
defer me.rwLock.RUnlock()
|
|
|
|
|
var rt any = opt(me)
|
|
|
|
|
return rt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 状态写保护操作
|
|
|
|
|
func (me *WaysideMemory) SafeOptWrite(opt func(memory *WaysideMemory) any) any {
|
|
|
|
|
me.rwLock.Lock()
|
|
|
|
|
defer me.rwLock.Unlock()
|
|
|
|
|
var rt any = opt(me)
|
|
|
|
|
return rt
|
|
|
|
|
}
|