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

72 lines
2.2 KiB
Go
Raw Normal View History

package memory
import (
2023-09-28 18:24:36 +08:00
"fmt"
2024-01-18 14:11:47 +08:00
"unsafe"
2023-09-28 18:24:36 +08:00
"joylink.club/bj-rtsts-server/dto/data_proto"
2023-10-13 17:43:48 +08:00
"joylink.club/bj-rtsts-server/dto/request_proto"
2024-01-18 14:11:47 +08:00
appcomponent "joylink.club/bj-rtsts-server/ts/simulation/app_component"
2024-01-17 17:49:11 +08:00
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
2023-10-13 17:43:48 +08:00
// 处理道岔操作
2024-01-17 17:49:11 +08:00
func HandlePointsOperation(simulation *VerifySimulation, req *request_proto.PointsOperationReq) error {
uid := QueryUidByMidAndComId(req.MapId, req.DeviceId, &data_proto.Turnout{})
2024-01-17 17:49:11 +08:00
// 道岔强制位置
switch req.Operation {
case request_proto.Points_SetParams:
2024-01-18 14:11:47 +08:00
return setTurnoutParam(simulation.World, uid, req.Param)
default:
return fmt.Errorf("未知的道岔操作:%s", req.Operation)
}
2023-10-13 17:43:48 +08:00
}
2024-01-17 17:49:11 +08:00
2024-01-18 14:11:47 +08:00
func setTurnoutParam(w ecs.World, uid string, param *request_proto.PointsParam) error {
force := param.ForcePosition
2024-01-17 17:49:11 +08:00
return handleEntityState(w, uid, func(e *ecs.Entry) error {
2024-01-18 14:11:47 +08:00
e.AddComponent(appcomponent.PointsParamType, unsafe.Pointer(param))
2024-01-17 17:49:11 +08:00
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
}