rts-sim-testing-service/ts/simulation/wayside/memory/wayside_memory_psd.go

79 lines
2.7 KiB
Go

package memory
import (
"fmt"
"joylink.club/bj-rtsts-server/dto/data_proto"
"joylink.club/bj-rtsts-server/dto/request_proto"
"joylink.club/bj-rtsts-server/sys_error"
appcomponent "joylink.club/bj-rtsts-server/ts/simulation/app_component"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
"unsafe"
)
// HandlePsdOperation 处理屏蔽门操作
func HandlePsdOperation(simulation *VerifySimulation, req *request_proto.PsdOperationReq) error {
switch req.Operation {
case request_proto.Psd_SetParams:
return setPsdParams(simulation, req)
default:
panic(fmt.Sprintf("未知的屏蔽门操作:%s", req.Operation))
}
return nil
}
func setPsdParams(simulation *VerifySimulation, req *request_proto.PsdOperationReq) error {
uid := QueryUidByMidAndComId(req.MapId, req.DeviceId, &data_proto.ScreenDoor{})
result := <-ecs.Request[ecs.EmptyType](simulation.World, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(simulation.World)
entry, ok := wd.EntityMap[uid]
if ok {
if !entry.HasComponent(component.AsdListType) {
return ecs.NewErrResult(fmt.Errorf("屏蔽门[%s]没有滑动门", uid))
}
err := getEcsRequest(req)(entry)
if err != nil {
return ecs.NewErrResult(err)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的实体", uid))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
func getEcsRequest(req *request_proto.PsdOperationReq) func(e *ecs.Entry) error {
return func(e *ecs.Entry) error {
e.AddComponent(appcomponent.PsdParamType, unsafe.Pointer(req.Param))
asdList := component.AsdListType.Get(e)
asdMap := make(map[int32]bool)
for _, code := range req.Param.AsdCodes {
asdMap[code] = true
}
for i, asdEntry := range asdList.List {
if asdMap[int32(i+1)] == true {
if request_proto.Psd_FA_Obstacle == req.Param.Fault {
asdEntry.AddComponent(component.AsdHasObstacleTag)
} else if request_proto.Psd_FA_NONE == req.Param.Fault {
asdEntry.RemoveComponent(component.AsdHasObstacleTag)
} else {
return sys_error.New(fmt.Sprintf("未知的屏蔽门故障设置[%s]", req.Param.Fault.String()))
}
if request_proto.Psd_F_ASD_KM == req.Param.Force {
asdEntry.AddComponent(component.AsdForceType, unsafe.Pointer(&component.BitState{Val: true}))
} else if request_proto.Psd_F_ASD_GM == req.Param.Force {
asdEntry.AddComponent(component.AsdForceType, unsafe.Pointer(&component.BitState{Val: false}))
} else if request_proto.Psd_F_NONE == req.Param.Force {
asdEntry.RemoveComponent(component.AsdForceType)
}
} else {
asdEntry.RemoveComponent(component.AsdHasObstacleTag)
asdEntry.RemoveComponent(component.AsdForceType)
}
}
return nil
}
}