package memory import ( "fmt" "unsafe" "joylink.club/bj-rtsts-server/dto/data_proto" "joylink.club/bj-rtsts-server/dto/request_proto" appcomponent "joylink.club/bj-rtsts-server/ts/simulation/app_component" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" ) // 处理道岔操作 func HandlePointsOperation(simulation *VerifySimulation, req *request_proto.PointsOperationReq) error { uid := QueryUidByMidAndComId(req.MapId, req.DeviceId, &data_proto.Turnout{}) // 道岔强制位置 switch req.Operation { case request_proto.Points_SetParams: return setTurnoutParam(simulation.World, uid, req.Param) default: return fmt.Errorf("未知的道岔操作:%s", req.Operation) } } func setTurnoutParam(w ecs.World, uid string, param *request_proto.PointsParam) error { force := param.ForcePosition return handleEntityState(w, uid, func(e *ecs.Entry) error { e.AddComponent(appcomponent.PointsParamType, unsafe.Pointer(param)) if e.HasComponent(component.Zdj9TwoElectronicType) { // ZDJ9双机牵引道岔 zdj92 := component.Zdj9TwoElectronicType.Get(e) switch force { case request_proto.Points_FP_DW: setRelayForce(zdj92.TDC_ZFBJ, false) setRelayForce(zdj92.TDC_ZDBJ, true) case request_proto.Points_FP_FW: setRelayForce(zdj92.TDC_ZDBJ, false) setRelayForce(zdj92.TDC_ZFBJ, true) case request_proto.Points_FP_SB: setRelayForce(zdj92.TDC_ZDBJ, false) setRelayForce(zdj92.TDC_ZFBJ, false) case request_proto.Points_FP_NONE: cancelRelayForce(zdj92.TDC_ZDBJ) cancelRelayForce(zdj92.TDC_ZFBJ) default: return fmt.Errorf("未知的道岔强制位置参数:%s", force) } } else { return fmt.Errorf("未知的道岔牵引类型,无法设置强制位置") } return nil }) } func handleEntityState(w ecs.World, uid string, handler func(e *ecs.Entry) error) error { result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] { wd := entity.GetWorldData(w) entry, ok := wd.EntityMap[uid] if ok { err := handler(entry) if err != nil { return ecs.NewErrResult(err) } } else { return ecs.NewErrResult(fmt.Errorf("未找到id=%s的实体", uid)) } return ecs.NewOkEmptyResult() }) return result.Err }