mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Simplify and rename TACAN registry reserve function (#1559)
* Simplify and rename TACAN registry reserve function * Remove unused tacan error
This commit is contained in:
parent
0cb10e4224
commit
056e6b28da
@ -228,11 +228,7 @@ class Operation:
|
|||||||
if beacon.channel is None:
|
if beacon.channel is None:
|
||||||
logging.error(f"TACAN beacon has no channel: {beacon.callsign}")
|
logging.error(f"TACAN beacon has no channel: {beacon.callsign}")
|
||||||
else:
|
else:
|
||||||
cls.tacan_registry.reserve(
|
cls.tacan_registry.mark_unavailable(beacon.tacan_channel)
|
||||||
beacon.tacan_channel,
|
|
||||||
TacanUsage.TransmitReceive,
|
|
||||||
ignore_rules=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _create_radio_registry(
|
def _create_radio_registry(
|
||||||
|
|||||||
20
gen/tacan.py
20
gen/tacan.py
@ -60,13 +60,6 @@ class TacanChannelInUseError(RuntimeError):
|
|||||||
super().__init__(f"{channel} is already in use")
|
super().__init__(f"{channel} is already in use")
|
||||||
|
|
||||||
|
|
||||||
class TacanChannelForbiddenError(RuntimeError):
|
|
||||||
"""Raised when attempting to reserve a, for technical reasons, forbidden channel."""
|
|
||||||
|
|
||||||
def __init__(self, channel: TacanChannel) -> None:
|
|
||||||
super().__init__(f"{channel} is forbidden")
|
|
||||||
|
|
||||||
|
|
||||||
class TacanRegistry:
|
class TacanRegistry:
|
||||||
"""Manages allocation of TACAN channels."""
|
"""Manages allocation of TACAN channels."""
|
||||||
|
|
||||||
@ -103,28 +96,17 @@ class TacanRegistry:
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
raise OutOfTacanChannelsError(band)
|
raise OutOfTacanChannelsError(band)
|
||||||
|
|
||||||
def reserve(
|
def mark_unavailable(self, channel: TacanChannel) -> None:
|
||||||
self,
|
|
||||||
channel: TacanChannel,
|
|
||||||
intended_usage: TacanUsage,
|
|
||||||
ignore_rules: bool = False,
|
|
||||||
) -> None:
|
|
||||||
"""Reserves the given channel.
|
"""Reserves the given channel.
|
||||||
|
|
||||||
Reserving a channel ensures that it will not be allocated in the future.
|
Reserving a channel ensures that it will not be allocated in the future.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
channel: The channel to reserve.
|
channel: The channel to reserve.
|
||||||
intended_usage: What the caller intends to use the tacan channel for.
|
|
||||||
ignore_rules: Whether to reserve regardless of recommended rules.
|
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
TacanChannelInUseError: The given channel is already in use.
|
TacanChannelInUseError: The given channel is already in use.
|
||||||
TacanChannelForbiddenError: The given channel is forbidden.
|
|
||||||
"""
|
"""
|
||||||
if not ignore_rules:
|
|
||||||
if channel.number in UNAVAILABLE[intended_usage][channel.band]:
|
|
||||||
raise TacanChannelForbiddenError(channel)
|
|
||||||
if channel in self.allocated_channels:
|
if channel in self.allocated_channels:
|
||||||
raise TacanChannelInUseError(channel)
|
raise TacanChannelInUseError(channel)
|
||||||
self.allocated_channels.add(channel)
|
self.allocated_channels.add(channel)
|
||||||
|
|||||||
@ -2,7 +2,6 @@ from gen.tacan import (
|
|||||||
OutOfTacanChannelsError,
|
OutOfTacanChannelsError,
|
||||||
TacanBand,
|
TacanBand,
|
||||||
TacanChannel,
|
TacanChannel,
|
||||||
TacanChannelForbiddenError,
|
|
||||||
TacanChannelInUseError,
|
TacanChannelInUseError,
|
||||||
TacanRegistry,
|
TacanRegistry,
|
||||||
TacanUsage,
|
TacanUsage,
|
||||||
@ -24,15 +23,6 @@ def test_allocate_first_few_channels() -> None:
|
|||||||
assert chan3 == TacanChannel(32, TacanBand.X)
|
assert chan3 == TacanChannel(32, TacanBand.X)
|
||||||
|
|
||||||
|
|
||||||
def test_reserve_ignoring_rules() -> None:
|
|
||||||
registry = TacanRegistry()
|
|
||||||
with pytest.raises(TacanChannelForbiddenError):
|
|
||||||
registry.reserve(TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive)
|
|
||||||
registry.reserve(
|
|
||||||
TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive, ignore_rules=True
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_allocate_different_usages() -> None:
|
def test_allocate_different_usages() -> None:
|
||||||
"""Make sure unallocated channels for one use don't make channels unavailable for other usage"""
|
"""Make sure unallocated channels for one use don't make channels unavailable for other usage"""
|
||||||
registry = TacanRegistry()
|
registry = TacanRegistry()
|
||||||
@ -53,7 +43,7 @@ def test_reserve_all_valid_transmit_receive() -> None:
|
|||||||
print("All valid x", ALL_VALID_X_TR)
|
print("All valid x", ALL_VALID_X_TR)
|
||||||
|
|
||||||
for num in ALL_VALID_X_TR:
|
for num in ALL_VALID_X_TR:
|
||||||
registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.TransmitReceive)
|
registry.mark_unavailable(TacanChannel(num, TacanBand.X))
|
||||||
|
|
||||||
with pytest.raises(OutOfTacanChannelsError):
|
with pytest.raises(OutOfTacanChannelsError):
|
||||||
registry.alloc_for_band(TacanBand.X, TacanUsage.TransmitReceive)
|
registry.alloc_for_band(TacanBand.X, TacanUsage.TransmitReceive)
|
||||||
@ -69,7 +59,7 @@ def test_reserve_all_valid_a2a() -> None:
|
|||||||
print("All valid x", ALL_VALID_X_A2A)
|
print("All valid x", ALL_VALID_X_A2A)
|
||||||
|
|
||||||
for num in ALL_VALID_X_A2A:
|
for num in ALL_VALID_X_A2A:
|
||||||
registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.AirToAir)
|
registry.mark_unavailable(TacanChannel(num, TacanBand.X))
|
||||||
|
|
||||||
with pytest.raises(OutOfTacanChannelsError):
|
with pytest.raises(OutOfTacanChannelsError):
|
||||||
registry.alloc_for_band(TacanBand.X, TacanUsage.AirToAir)
|
registry.alloc_for_band(TacanBand.X, TacanUsage.AirToAir)
|
||||||
@ -80,47 +70,8 @@ def test_reserve_all_valid_a2a() -> None:
|
|||||||
assert chanTR == TacanChannel(1, TacanBand.X)
|
assert chanTR == TacanChannel(1, TacanBand.X)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip(reason="TODO")
|
|
||||||
def test_allocate_all() -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def test_reserve_invalid_tr_channels() -> None:
|
|
||||||
registry = TacanRegistry()
|
|
||||||
some_invalid_channels = [
|
|
||||||
TacanChannel(2, TacanBand.X),
|
|
||||||
TacanChannel(30, TacanBand.X),
|
|
||||||
TacanChannel(47, TacanBand.X),
|
|
||||||
TacanChannel(63, TacanBand.X),
|
|
||||||
TacanChannel(2, TacanBand.Y),
|
|
||||||
TacanChannel(30, TacanBand.Y),
|
|
||||||
TacanChannel(64, TacanBand.Y),
|
|
||||||
TacanChannel(92, TacanBand.Y),
|
|
||||||
]
|
|
||||||
for chan in some_invalid_channels:
|
|
||||||
with pytest.raises(TacanChannelForbiddenError):
|
|
||||||
registry.reserve(chan, TacanUsage.TransmitReceive)
|
|
||||||
|
|
||||||
|
|
||||||
def test_reserve_invalid_a2a_channels() -> None:
|
|
||||||
registry = TacanRegistry()
|
|
||||||
some_invalid_channels = [
|
|
||||||
TacanChannel(1, TacanBand.X),
|
|
||||||
TacanChannel(36, TacanBand.X),
|
|
||||||
TacanChannel(64, TacanBand.X),
|
|
||||||
TacanChannel(99, TacanBand.X),
|
|
||||||
TacanChannel(1, TacanBand.Y),
|
|
||||||
TacanChannel(36, TacanBand.Y),
|
|
||||||
TacanChannel(64, TacanBand.Y),
|
|
||||||
TacanChannel(99, TacanBand.Y),
|
|
||||||
]
|
|
||||||
for chan in some_invalid_channels:
|
|
||||||
with pytest.raises(TacanChannelForbiddenError):
|
|
||||||
registry.reserve(chan, TacanUsage.AirToAir)
|
|
||||||
|
|
||||||
|
|
||||||
def test_reserve_again() -> None:
|
def test_reserve_again() -> None:
|
||||||
registry = TacanRegistry()
|
registry = TacanRegistry()
|
||||||
with pytest.raises(TacanChannelInUseError):
|
with pytest.raises(TacanChannelInUseError):
|
||||||
registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive)
|
registry.mark_unavailable(TacanChannel(1, TacanBand.X))
|
||||||
registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive)
|
registry.mark_unavailable(TacanChannel(1, TacanBand.X))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user