Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make vehicle class configurable #82

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/tablespoon/communicator/btd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Tablespoon.Communicator.Btd do
- id: always 0
- id in message: increases with each request, up to 255 where it wraps back to 1
- vehicle_id: the vehicle's ID
- vehicle_class: always 2
- vehicle_class: configurable, default to 2
- vehicle_class_level: always 0
- strategy: 1 - North, 2 - East, 3 - South, 4 - West
- time_of_service_desired: always 0
Expand All @@ -23,7 +23,8 @@ defmodule Tablespoon.Communicator.Btd do
require Logger

@enforce_keys [:transport, :group, :intersection_id, :ref]
defstruct @enforce_keys ++ [timeout: 5_000, next_id: 1, in_flight: %{}]
defstruct @enforce_keys ++
[source_to_vehicle_class: %{}, timeout: 5_000, next_id: 1, in_flight: %{}]

@impl Tablespoon.Communicator
def new(transport, opts) do
Expand Down Expand Up @@ -93,7 +94,7 @@ defmodule Tablespoon.Communicator.Btd do
%NTCIP.PriorityRequest{
id: comm.next_id,
vehicle_id: q.vehicle_id,
vehicle_class: 2,
vehicle_class: Map.get(comm.source_to_vehicle_class, q.source, 2),
vehicle_class_level: 0,
strategy: ntcip_strategy(q.approach),
time_of_service_desired: 0,
Expand Down
2 changes: 2 additions & 0 deletions lib/tablespoon/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Tablespoon.Query do
"""
@enforce_keys [
:id,
:source,
:type,
:vehicle_id,
:intersection_alias,
Expand All @@ -15,6 +16,7 @@ defmodule Tablespoon.Query do

@type t :: %__MODULE__{
id: id,
source: atom,
type: query_type,
vehicle_id: vehicle_id,
vehicle_latitude: float | nil,
Expand Down
1 change: 1 addition & 0 deletions lib/tablespoon_tcp/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ defmodule TablespoonTcp.Protocol do

Tablespoon.Query.new(
id: tm.id,
source: __MODULE__,
type: type,
intersection_alias: intersection_alias,
approach: approach,
Expand Down
1 change: 1 addition & 0 deletions lib/tablespoon_web/controllers/priority_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule TablespoonWeb.PriorityController do
q =
Tablespoon.Query.new(
id: id,
source: __MODULE__,
type: type,
intersection_alias: intersection_alias,
approach: approach,
Expand Down
79 changes: 48 additions & 31 deletions test/tablespoon/communicator/btd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,14 @@ defmodule Tablespoon.Communicator.BtdTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
approach: :south,
event_time: System.system_time()
)

comm =
Btd.new(
FakeTransport.new(),
group: @group,
intersection_id: @intersection_id
)

{:ok, comm, []} = Btd.connect(comm)
{:ok, comm, []} = Btd.send(comm, query)

ntcip_message = %NTCIP.PriorityRequest{
id: 1,
vehicle_id: "1",
Expand All @@ -58,42 +49,49 @@ defmodule Tablespoon.Communicator.BtdTest do
intersection_id: @intersection_id
}

ntcip =
NTCIP.encode(%NTCIP{
group: @group,
pdu_type: :response,
request_id: 0,
message: ntcip_message
})
assert_sent_query_and_got_message(query, ntcip_message)
end

{:ok, comm, [sent: ^query]} = Btd.stream(comm, ntcip)
[sent_packet] = comm.transport.sent
test "can override the vehicle class based on the source" do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
approach: :south,
event_time: System.system_time()
)

assert {:ok, %NTCIP{group: @group, pdu_type: :set, message: ^ntcip_message}} =
NTCIP.decode(sent_packet)
ntcip_message = %NTCIP.PriorityRequest{
id: 1,
vehicle_id: "1",
vehicle_class: 1,
vehicle_class_level: 0,
strategy: 3,
time_of_service_desired: 0,
time_of_estimated_departure: 0,
intersection_id: @intersection_id
}

assert_sent_query_and_got_message(query, ntcip_message,
source_to_vehicle_class: %{testing: 1}
)
end

test "sends a NTCIP1211 cancel query and receives an ack" do
query =
Query.new(
id: 1,
source: :testing,
type: :cancel,
vehicle_id: "1",
intersection_alias: "int",
approach: :south,
event_time: System.system_time()
)

comm =
Btd.new(
FakeTransport.new(),
group: @group,
intersection_id: @intersection_id
)

{:ok, comm, []} = Btd.connect(comm)
{:ok, comm, []} = Btd.send(comm, query)

ntcip_message = %NTCIP.PriorityCancel{
id: 1,
vehicle_id: "1",
Expand All @@ -103,6 +101,24 @@ defmodule Tablespoon.Communicator.BtdTest do
intersection_id: @intersection_id
}

assert_sent_query_and_got_message(query, ntcip_message)
end

defp assert_sent_query_and_got_message(query, ntcip_message, opts \\ []) do
opts =
opts
|> Keyword.put_new(:group, @group)
|> Keyword.put_new(:intersection_id, @intersection_id)

comm =
Btd.new(
FakeTransport.new(),
opts
)

{:ok, comm, []} = Btd.connect(comm)
{:ok, comm, []} = Btd.send(comm, query)

ntcip =
NTCIP.encode(%NTCIP{
group: @group,
Expand Down Expand Up @@ -230,6 +246,7 @@ defmodule Tablespoon.Communicator.BtdTest do
) do
Query.new(
id: 1,
source: :testing,
type: type,
vehicle_id: "1",
intersection_alias: "int",
Expand Down
13 changes: 13 additions & 0 deletions test/tablespoon/communicator/modem_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand All @@ -32,6 +33,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand Down Expand Up @@ -61,6 +63,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand Down Expand Up @@ -92,6 +95,7 @@ defmodule Tablespoon.Communicator.ModemTest do
request =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand All @@ -102,6 +106,7 @@ defmodule Tablespoon.Communicator.ModemTest do
cancel =
Query.new(
id: 1,
source: :testing,
type: :cancel,
vehicle_id: "1",
intersection_alias: "int",
Expand All @@ -126,6 +131,7 @@ defmodule Tablespoon.Communicator.ModemTest do
vehicle_id <- vehicle_ids do
Query.new(
id: :erlang.unique_integer([:monotonic]),
source: :testing,
type: type,
vehicle_id: vehicle_id,
intersection_alias: "int",
Expand All @@ -152,6 +158,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: :erlang.unique_integer([:monotonic]),
source: :testing,
type: :cancel,
vehicle_id: "1234",
intersection_alias: "int",
Expand All @@ -173,6 +180,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: :erlang.unique_integer([:monotonic]),
source: :testing,
type: :request,
vehicle_id: "1234",
intersection_alias: "int",
Expand All @@ -196,6 +204,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand Down Expand Up @@ -225,6 +234,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :cancel,
vehicle_id: "1",
intersection_alias: "int",
Expand All @@ -246,6 +256,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand All @@ -270,6 +281,7 @@ defmodule Tablespoon.Communicator.ModemTest do
query =
Query.new(
id: 1,
source: :testing,
type: :request,
vehicle_id: "1",
intersection_alias: "int",
Expand Down Expand Up @@ -370,6 +382,7 @@ defmodule Tablespoon.Communicator.ModemTest do
) do
Query.new(
id: 1,
source: :testing,
type: type,
vehicle_id: "1",
intersection_alias: "int",
Expand Down
6 changes: 6 additions & 0 deletions test/tablespoon/intersection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Tablespoon.IntersectionTest do
query =
Query.new(
id: "test_query_id",
source: :testing,
type: :cancel,
intersection_alias: @alias,
approach: :south,
Expand Down Expand Up @@ -56,6 +57,7 @@ defmodule Tablespoon.IntersectionTest do
query =
Query.new(
id: "test_response_id",
source: :testing,
type: :request,
intersection_alias: @alias,
approach: :north,
Expand Down Expand Up @@ -87,6 +89,7 @@ defmodule Tablespoon.IntersectionTest do
query =
Query.new(
id: "test_invalid_alias",
source: :testing,
type: :request,
intersection_alias: @alias <> "_invalid",
approach: :north,
Expand Down Expand Up @@ -125,6 +128,7 @@ defmodule Tablespoon.IntersectionTest do
query =
Query.new(
id: "test_response_id",
source: :testing,
type: :request,
intersection_alias: @alias,
approach: :north,
Expand Down Expand Up @@ -241,6 +245,7 @@ defmodule Tablespoon.IntersectionTest do
query =
Query.new(
id: "test_failure_id",
source: :testing,
type: :request,
intersection_alias: @alias,
approach: :north,
Expand Down Expand Up @@ -287,6 +292,7 @@ defmodule Tablespoon.IntersectionTest do
for id <- 1..3 do
Query.new(
id: id,
source: :testing,
type: :request,
intersection_alias: intersection_alias,
approach: :north,
Expand Down
2 changes: 2 additions & 0 deletions test/tablespoon/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Tablespoon.QueryTest do
query =
new(
id: "id",
source: :testing,
type: :request,
vehicle_id: "veh",
intersection_alias: "int",
Expand All @@ -26,6 +27,7 @@ defmodule Tablespoon.QueryTest do
query =
new(
id: "id",
source: :testing,
type: :request,
vehicle_id: "veh",
intersection_alias: "int",
Expand Down