80 lines
3.1 KiB
Go
80 lines
3.1 KiB
Go
package entity
|
||
|
||
import (
|
||
"fmt"
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/component"
|
||
"joylink.club/rtsssimulation/repository"
|
||
"strings"
|
||
)
|
||
|
||
// LoadAxlePhysicalSections 加载计轴区段
|
||
func LoadAxlePhysicalSections(w ecs.World) error {
|
||
data := GetWorldData(w)
|
||
sections := data.Repo.PhysicalSectionList()
|
||
for _, section := range sections {
|
||
if is, se := section.IsAxleSection(); se == nil && is {
|
||
if len(strings.TrimSpace(section.CentralizedStation())) == 0 {
|
||
return fmt.Errorf("区段[%s]未设置所属集中站", section.Id())
|
||
}
|
||
//fmt.Println("=====>>>>>计轴区段:", section.Id())
|
||
fadcDeviceId := CreateFaDcAxleDeviceId(section.CentralizedStation())
|
||
fadcDeviceEntry, find := data.EntityMap[fadcDeviceId]
|
||
if !find {
|
||
fadcDeviceEntry = createFadcDeviceEntity(w, section, data)
|
||
data.EntityMap[fadcDeviceId] = fadcDeviceEntry
|
||
}
|
||
fadcDevice := component.FaDcAxleDeviceType.Get(fadcDeviceEntry)
|
||
counter := createAxleCounterEntity(w, section, data)
|
||
axleSec := createAxleSectionEntity(w, section, data)
|
||
fadcDevice.AddAxleSection(counter, axleSec)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// 联锁集中站计轴管理设备实体
|
||
func createFadcDeviceEntity(w ecs.World, axleSection *repository.PhysicalSection, worldData *component.WorldData) *ecs.Entry {
|
||
uid := CreateFaDcAxleDeviceId(axleSection.CentralizedStation())
|
||
entry, ok := worldData.EntityMap[uid]
|
||
if !ok {
|
||
entry = w.Entry(w.Create(component.UidType, component.FaDcAxleDeviceType))
|
||
//
|
||
component.UidType.SetValue(entry, component.Uid{Id: uid})
|
||
component.FaDcAxleDeviceType.Set(entry, &component.FaDcAxleDevice{CounterMap: make(map[string]*ecs.Entry)})
|
||
//
|
||
worldData.EntityMap[uid] = entry
|
||
}
|
||
return entry
|
||
}
|
||
|
||
// 计轴区段实体
|
||
func createAxleSectionEntity(w ecs.World, axleSection *repository.PhysicalSection, worldData *component.WorldData) *ecs.Entry {
|
||
uid := axleSection.Id()
|
||
entry, ok := worldData.EntityMap[uid]
|
||
if !ok {
|
||
entry = w.Entry(w.Create(component.UidType, component.AxleSectionType, component.AxleSectionFaultType))
|
||
//
|
||
component.UidType.SetValue(entry, component.Uid{Id: uid})
|
||
component.AxleSectionType.Set(entry, &component.AxleSection{Occ: false})
|
||
component.AxleSectionFaultType.Set(entry, &component.AxleSectionFault{SectionFault: false})
|
||
//
|
||
worldData.EntityMap[uid] = entry
|
||
}
|
||
return entry
|
||
}
|
||
|
||
// 计轴器实体
|
||
func createAxleCounterEntity(w ecs.World, axleSection *repository.PhysicalSection, worldData *component.WorldData) *ecs.Entry {
|
||
entry := w.Entry(w.Create(component.AxleCounterType, component.AxleCounterRuntimeType, component.AxleSectionFlag))
|
||
component.AxleCounterType.Set(entry, component.NewAxleCounter())
|
||
component.AxleCounterRuntimeType.Set(entry, &component.AxleCounterRuntime{Rac: false, Rjo: false, Rjt: false, Drst: false, Pdrst: false, DoingPdrst: false})
|
||
component.AxleSectionFlag.SetValue(entry, component.Uid{Id: axleSection.Id()})
|
||
return entry
|
||
}
|
||
|
||
// CreateFaDcAxleDeviceId 设备集中站计轴管理设备id(通过联锁集中站centralizedStation来构建)
|
||
func CreateFaDcAxleDeviceId(centralizedStation string) string {
|
||
return fmt.Sprintf("fadc-axle-device-%s", centralizedStation)
|
||
}
|