diff --git a/examples/signal1/main.go b/examples/signal1/main.go index e2e9600..ff0b7ac 100644 --- a/examples/signal1/main.go +++ b/examples/signal1/main.go @@ -1,18 +1,55 @@ package main import ( + "fmt" + "joylink.club/ecs" rtss_simulation "joylink.club/rtsssimulation" + "joylink.club/rtsssimulation/consts" + "joylink.club/rtsssimulation/entity" + "joylink.club/rtsssimulation/examples/signal1/sigSys" "joylink.club/rtsssimulation/repository" + "joylink.club/rtsssimulation/repository/model/proto" + "joylink.club/rtsssimulation/sys/circuit_sys" + "time" ) // 信号机测试 func main() { - r := &repository.Repository{} - addSignal2XH1(r) - sim := rtss_simulation.NewSimulation(r) + proto := &proto.Repository{} + proto.Id = "test-for-signal" + proto.Version = "v1.0" + addProtoSignal2XH1(proto) + repo := repository.BuildRepositoryForSignalTest(proto) + sim := rtss_simulation.NewSimulation(repo) + loadEntities(sim, repo) + sim.SetSpeed(0.1) + sim.AddSystem(circuit_sys.NewSignal2XH1System()) + sim.AddSystem(sigSys.NewSignalDebugSystem()) sim.StartUp() + // + time.Sleep(10 * time.Second) + sim.Close() } -func addSignal2XH1(r *repository.Repository) { - //signal := repository.NewSignal("signal1-2xh1", &proto.Kilometer{}) - +func addProtoSignal2XH1(r *proto.Repository) { + //相关继电器 + r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-ddj", Code: consts.SIGNAL_DDJ}) + r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-dj", Code: consts.SIGNAL_DJ}) + r.Relays = append(r.Relays, &proto.Relay{Id: "2xh1-lxj", Code: consts.SIGNAL_LXJ}) + // + signal := &proto.Signal{} + signal.Id = "signal1-2xh1" + signal.Km = &proto.Kilometer{} + // + group := &proto.ElectronicComponentGroup{Code: consts.SIGNAL_2XH1, ComponentIds: []string{"2xh1-ddj", "2xh1-dj", "2xh1-lxj"}} + // + signal.ElectronicComponentGroups = append(signal.ElectronicComponentGroups, group) + r.Signals = append(r.Signals, signal) +} +func loadEntities(w ecs.World, repo *repository.Repository) { + // 初始化世界数据单例组件 + entity.LoadWorldData(w, repo) + // + if le := entity.LoadSignals(w); le != nil { + fmt.Println("===>>LoadSignals error : ", le.Error()) + } } diff --git a/examples/signal1/sigSys/debug_sys.go b/examples/signal1/sigSys/debug_sys.go new file mode 100644 index 0000000..21d49b3 --- /dev/null +++ b/examples/signal1/sigSys/debug_sys.go @@ -0,0 +1,26 @@ +package sigSys + +import ( + "joylink.club/ecs" + "joylink.club/ecs/filter" + "joylink.club/rtsssimulation/component" + "log" +) + +type SignalDebugSystem struct { + query *ecs.Query +} + +func NewSignalDebugSystem() *SignalDebugSystem { + return &SignalDebugSystem{query: ecs.NewQuery(filter.Contains(component.Signal2XH1ElectronicType, component.Signal2XH1FilamentType))} +} + +// Update world 执行 +func (s *SignalDebugSystem) Update(w ecs.World) { + s.query.Each(w, func(entry *ecs.Entry) { + uid := component.UidType.Get(entry) + //state := component.Signal2XH1ElectronicType.Get(entry) + filament := component.Signal2XH1FilamentType.Get(entry) + log.Println("===>> uid = ", uid.Id, " 绿灯 = ", filament.L, " 红灯 = ", filament.H) + }) +} diff --git a/jl-ecs-go b/jl-ecs-go index 47f78cb..6b6259e 160000 --- a/jl-ecs-go +++ b/jl-ecs-go @@ -1 +1 @@ -Subproject commit 47f78cb54ddc2673b87d3f85eea23e3769e417ad +Subproject commit 6b6259e2eb95db625111b539ef9b526c8d3862f4 diff --git a/repository/repository_ceshi.go b/repository/repository_ceshi.go new file mode 100644 index 0000000..0ece898 --- /dev/null +++ b/repository/repository_ceshi.go @@ -0,0 +1,17 @@ +package repository + +import ( + "fmt" + "joylink.club/rtsssimulation/repository/model/proto" +) + +//测试 + +func BuildRepositoryForSignalTest(source *proto.Repository) *Repository { + repository := newRepository(source.Id, source.Version) + buildModels(source, repository) + if err := buildSignalRelationShip(source, repository); err != nil { + fmt.Println("===>>buildSignalRelationShip error : ", err.Error()) + } + return repository +} diff --git a/repository/repository_manager.go b/repository/repository_manager.go index cfceb0d..72e5396 100644 --- a/repository/repository_manager.go +++ b/repository/repository_manager.go @@ -38,7 +38,6 @@ func BuildRepository(source *proto.Repository) (*Repository, error) { repositoryMap[buildRepositoryKey(source.Id, source.Version)] = repository return repository, err } - func FindRepository(id string, version string) *Repository { return repositoryMap[buildRepositoryKey(id, version)] } @@ -241,6 +240,19 @@ func buildSignalRelationShip(source *proto.Repository, repository *Repository) e } interrelateSignalAndTurnout(signal, repository.turnoutMap[tp.DeviceId], tp.Port) } + //关联继电器组 + for _, group := range protoData.ElectronicComponentGroups { + var components []IGroupedElectronicComponent + for _, id := range group.GetComponentIds() { + if relay := repository.relayMap[id]; relay != nil { + components = append(components, relay) + } + } + signal.componentGroups = append(signal.componentGroups, &ElectronicComponentGroup{ + code: group.Code, + components: components, + }) + } } return nil }