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

feat(capabilities): capabilities topic #98

Open
wants to merge 1 commit into
base: master
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: 7 additions & 0 deletions lib/astarte_vmq_plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
control_path = "/" <> Enum.join(control_path_tokens, "/")
publish_control_message(realm, device_id, control_path, payload, timestamp)

[^realm, ^device_id, "capabilities"] ->
publish_capabilities(realm, device_id, payload, timestamp)

[^realm, ^device_id, interface | path_tokens] ->
path = "/" <> Enum.join(path_tokens, "/")
publish_data(realm, device_id, interface, path, payload, timestamp)
Expand Down Expand Up @@ -190,7 +193,7 @@
end

defp randomize_interval(interval, tolerance) do
multiplier = 1 + (tolerance * 2 * :random.uniform() - tolerance)

Check warning on line 196 in lib/astarte_vmq_plugin.ex

View workflow job for this annotation

GitHub Actions / Check Dialyzer

:random.uniform/0 is deprecated. Use the 'rand' module instead

Check warning on line 196 in lib/astarte_vmq_plugin.ex

View workflow job for this annotation

GitHub Actions / Build and Test (rabbitmq:3.12.0-management, scylladb/scylla:5.2.2)

:random.uniform/0 is deprecated. Use the 'rand' module instead

Check warning on line 196 in lib/astarte_vmq_plugin.ex

View workflow job for this annotation

GitHub Actions / Build and Test (rabbitmq:3.12.0-management, cassandra:3.11.15)

:random.uniform/0 is deprecated. Use the 'rand' module instead

(interval * multiplier)
|> Float.round()
Expand All @@ -207,6 +210,10 @@
publish(realm, device_id, payload, "data", timestamp, additional_headers)
end

defp publish_capabilities(realm, device_id, payload, timestamp) do
publish(realm, device_id, payload, "capabilities", timestamp)
end

defp publish_control_message(realm, device_id, control_path, payload, timestamp) do
additional_headers = [x_astarte_control_path: control_path]

Expand Down
26 changes: 26 additions & 0 deletions test/astarte_vmq_plugin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,32 @@ defmodule Astarte.VMQ.PluginTest do
assert String.starts_with?(message_id, message_id_prefix(@realm, @device_id, timestamp))
end

test "device capabilities on_publish" do
payload = "payload"
capabilities_topic = [@realm, @device_id, "capabilities"]

Plugin.on_publish(
:dontcare,
{:dontcare, @device_base_path},
:dontcare,
capabilities_topic,
payload,
:dontcare
)

assert_receive {:amqp_msg, ^payload, %{headers: headers, timestamp: timestamp} = _metadata}

# 5 seconds
assert_in_delta timestamp, now_us_x10_timestamp(), 50_000_000

assert %{
"x_astarte_vmqamqp_proto_ver" => 1,
"x_astarte_msg_type" => "capabilities",
"x_astarte_realm" => @realm,
"x_astarte_device_id" => @device_id
} = amqp_headers_to_map(headers)
end

test "device data on_publish" do
path = "/some/data/path"
interface = "com.my.Interface"
Expand Down
Loading