package fi import ( "fmt" "joylink.club/ecs" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/entity" ) // 继电器功能接口 // 控制有极继电器励磁 // id - 继电器uid // td - 是否通电励磁 // xq - 是否到吸起位置(有极继电器) func driveYjRelay(w ecs.World, entry *ecs.Entry, td bool, xq bool) { rd := component.RelayDriveType.Get(entry) rd.Td = td rd.Xq = xq } // 控制无极继电器励磁 // id - 继电器uid // td - 是否通电励磁 // xq - 是否到吸起位置(有极继电器) func driveWjRelay(w ecs.World, entry *ecs.Entry, td bool) { rd := component.RelayDriveType.Get(entry) rd.Td = td } // 驱动继电器到吸起位置 func DriveRelayUp(w ecs.World, id string) error { result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] { wd := entity.GetWorldData(w) entry, ok := wd.EntityMap[id] if ok { if entry.HasComponent(component.YjRelayTag) { driveYjRelay(w, entry, true, true) } else { driveWjRelay(w, entry, true) } } else { return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id)) } return ecs.NewOkEmptyResult() }) return result.Err } // 驱动继电器到落下位置 func DriveRelayDown(w ecs.World, id string) error { result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] { wd := entity.GetWorldData(w) entry, ok := wd.EntityMap[id] if ok { if entry.HasComponent(component.YjRelayTag) { driveYjRelay(w, entry, true, false) } else { driveWjRelay(w, entry, false) } } else { return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id)) } return ecs.NewOkEmptyResult() }) return result.Err } // 设置继电器强制故障 func SetRelayFaultForce(w ecs.World, id string, q bool) error { return updateRelayFault(w, id, func(entry *ecs.Entry) { component.AddOrUpdateRelayFaultForce(entry, q) }) } // 取消继电器强制故障 func CancelRelayFaultForce(w ecs.World, id string) error { return updateRelayFault(w, id, func(entry *ecs.Entry) { entry.RemoveComponent(component.RelayFaultForceType) }) } func updateRelayFault(w ecs.World, id string, faultHandle func(entry *ecs.Entry)) error { result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] { wd := entity.GetWorldData(w) entry, ok := wd.EntityMap[id] if ok { faultHandle(entry) } else { return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id)) } return ecs.NewOkEmptyResult() }) return result.Err }