Allow operation.py to ignore TACAN rules

This commit is contained in:
Magnus Wolffelt 2021-08-18 00:06:34 +02:00
parent f63a35b1fa
commit 34ff5fbc6a
3 changed files with 22 additions and 4 deletions

View File

@ -229,7 +229,9 @@ class Operation:
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.reserve(
beacon.tacan_channel, TacanUsage.TransmitReceive beacon.tacan_channel,
TacanUsage.TransmitReceive,
ignore_rules=True,
) )
@classmethod @classmethod

View File

@ -103,7 +103,12 @@ class TacanRegistry:
except StopIteration: except StopIteration:
raise OutOfTacanChannelsError(band) raise OutOfTacanChannelsError(band)
def reserve(self, channel: TacanChannel, intended_usage: TacanUsage) -> None: def reserve(
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.
@ -111,13 +116,15 @@ class TacanRegistry:
Args: Args:
channel: The channel to reserve. channel: The channel to reserve.
intended_usage: What the caller intends to use the tacan channel for. 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. TacanChannelForbiddenError: The given channel is forbidden.
""" """
if channel.number in UNAVAILABLE[intended_usage][channel.band]: if not ignore_rules:
raise TacanChannelForbiddenError(channel) 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)

View File

@ -24,6 +24,15 @@ 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()