-
Notifications
You must be signed in to change notification settings - Fork 70
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
Start support for AWS IoT setup #1103
Open
jjcarstens
wants to merge
1
commit into
main
Choose a base branch
from
mqtt-sqs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
defmodule NervesHub.AWSIoT do | ||
@moduledoc """ | ||
Support for common AWS IOT infrastructure including MQTT and SQS | ||
|
||
Requires `:queues` to be defined in the application config or | ||
the supervisor is simply ignored | ||
|
||
See docs.nerves-hub.org for a general overview of the architecture | ||
""" | ||
use Supervisor | ||
|
||
alias NervesHub.Tracker | ||
|
||
@type opt :: {:queues, [keyword()]} | ||
@spec start_link([opt]) :: Supervisor.on_start() | ||
def start_link(opts) do | ||
Supervisor.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@impl Supervisor | ||
def init(opts) do | ||
opts = | ||
Application.get_env(:nerves_hub, __MODULE__, []) | ||
|> Keyword.merge(opts) | ||
|
||
case opts[:queues] do | ||
queues when is_list(queues) and length(queues) > 0 -> | ||
children = | ||
Enum.map(queues, &{__MODULE__.SQS, &1}) | ||
|> maybe_add_local_broker(opts) | ||
|
||
Supervisor.init(children, strategy: :one_for_one) | ||
|
||
_ -> | ||
:ignore | ||
end | ||
end | ||
|
||
defp maybe_add_local_broker(children, opts) do | ||
if broker_spec = opts[:local_broker] do | ||
[broker_spec | children] | ||
else | ||
children | ||
end | ||
end | ||
|
||
if Application.compile_env(:nerves_hub, [__MODULE__, :local_broker], false) do | ||
def publish(serial, event, payload) do | ||
data = Jason.encode!(%{event: event, payload: payload}) | ||
PintBroker.publish(__MODULE__.PintBroker, "nh/#{serial}", data) | ||
end | ||
else | ||
def publish(serial, event, payload) do | ||
# TODO: Topic and data may change soon | ||
# Stubbing out initial idea here for now | ||
data = %{event: event, payload: payload} | ||
topic = "/topics/nh/#{serial}" | ||
|
||
ExAws.Operation.JSON.new(:iot_data, %{path: topic, data: data}) | ||
|> ExAws.request() | ||
end | ||
end | ||
|
||
defmodule SQS do | ||
@moduledoc """ | ||
Consumer for AWS SQS messages | ||
|
||
This is the ingestion point of devices coming from the MQTT | ||
broker. A message from a device must include the `"identifier"` | ||
key either in the payload or pulled from the topic via the | ||
AWS IoT rule that forwards to the queue. | ||
|
||
The system must also be setup with a rule to forward [AWS Lifecycle | ||
events](https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html) | ||
to a queue for tracking device online/offline presence | ||
|
||
Right now, all configured queues are handled by this module. | ||
In the future, we may want to separate handling for each | ||
queue in it's own module. | ||
""" | ||
use Broadway | ||
|
||
alias Broadway.Message | ||
alias NervesHub.Devices | ||
|
||
require Logger | ||
|
||
def start_link(opts), do: Broadway.start_link(__MODULE__, opts) | ||
|
||
@impl Broadway | ||
def handle_message(_processor, %{data: raw} = msg, _context) do | ||
case Jason.decode(raw) do | ||
{:ok, data} -> | ||
Message.put_data(msg, data) | ||
|> process_message() | ||
|
||
_ -> | ||
Message.failed(msg, :malformed) | ||
end | ||
end | ||
|
||
@impl Broadway | ||
def handle_batch(_batcher, messages, batch_info, _context) do | ||
Logger.debug("[SQS] Handled #{inspect(batch_info.size)}") | ||
messages | ||
end | ||
|
||
defp process_message(%{data: %{"eventType" => "connected"} = data} = msg) do | ||
# TODO: Maybe use more info from the connection? | ||
# Example payload of AWS lifecycle connected event | ||
# principalIdentifier is a SHA256 fingerprint of the certificate that | ||
# is Base16 encoded | ||
# { | ||
# "clientId": "186b5", | ||
# "timestamp": 1573002230757, | ||
# "eventType": "connected", | ||
# "sessionIdentifier": "a4666d2a7d844ae4ac5d7b38c9cb7967", | ||
# "principalIdentifier": "12345678901234567890123456789012", | ||
# "ipAddress": "192.0.2.0", | ||
# "versionNumber": 0 | ||
# } | ||
|
||
with {:ok, device} <- Devices.get_by_identifier(data["clientId"]) do | ||
Logger.debug("[AWS IoT] device #{device.identifier} connected") | ||
|
||
Tracker.online(device) | ||
|
||
msg | ||
else | ||
_ -> | ||
Message.failed(msg, :unknown_device) | ||
end | ||
end | ||
|
||
defp process_message(%{data: %{"eventType" => "disconnected"} = data} = msg) do | ||
# TODO: Maybe use more of the disconnect data? | ||
# Example payload of AWS lifecyle disconnect event | ||
# { | ||
# "clientId": "186b5", | ||
# "timestamp": 1573002340451, | ||
# "eventType": "disconnected", | ||
# "sessionIdentifier": "a4666d2a7d844ae4ac5d7b38c9cb7967", | ||
# "principalIdentifier": "12345678901234567890123456789012", | ||
# "clientInitiatedDisconnect": true, | ||
# "disconnectReason": "CLIENT_INITIATED_DISCONNECT", | ||
# "versionNumber": 0 | ||
# } | ||
with {:ok, device} <- Devices.get_by_identifier(data["clientId"]) do | ||
Logger.debug( | ||
"[AWS IoT] device #{device.identifier} disconnected: #{data["disconnectReason"]}" | ||
) | ||
|
||
Tracker.offline(device) | ||
end | ||
|
||
msg | ||
end | ||
|
||
defp process_message(msg) do | ||
# TODO: Track unhandled msg | ||
msg | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if the sup took these arguments in
start_link/1
rather than pulling them from application env on init. You can keep the same pattern if you want to use app env e.g.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The incoming opts to the GenServer are merged into the Application env so the end result is the same where opts provided to start_link will take precedence . The preference here is mostly that it is contained