-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users can opt in to have their messages forwarded to an MQTT topic that they subscribe to, identified by a secret forwarding_key
- Loading branch information
1 parent
77bb525
commit 6b5363e
Showing
21 changed files
with
440 additions
and
87 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module MQTTClientFactory | ||
def self.create_client(args={}, &block) | ||
host = args.fetch(:host, default_host) | ||
port = args.fetch(:port, default_port) | ||
clean_sesion = args.fetch(:clean_session, default_clean_session) | ||
client_id = args.fetch(:client_id, default_client_id) | ||
ssl = args.fetch(:ssl, default_ssl) | ||
MQTT::Client.connect( | ||
{ host: host, port: port, clean_session: clean_sesion, client_id: client_id, ssl: ssl}, | ||
&block | ||
) | ||
end | ||
|
||
def self.default_host | ||
ENV.fetch('MQTT_HOST', 'mqtt') | ||
end | ||
|
||
def self.default_port | ||
ENV.fetch('MQTT_PORT', "1883").to_i | ||
end | ||
|
||
def self.default_clean_session | ||
ENV.fetch('MQTT_CLEAN_SESSION', "true") == "true" | ||
end | ||
|
||
def self.default_client_id | ||
ENV.fetch('MQTT_CLIENT_ID', nil) | ||
end | ||
|
||
def self.default_ssl | ||
ENV.fetch('MQTT_SSL', "false") == "true" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class MQTTForwarder | ||
def initialize(client, prefix="forward/devices", suffix="readings") | ||
@client = client | ||
@prefix = prefix | ||
@suffix = suffix | ||
end | ||
|
||
def forward_reading(token, device_id, reading) | ||
topic = topic_path(token, device_id) | ||
client.publish(topic, reading) | ||
end | ||
|
||
private | ||
|
||
def topic_path(token, device_id) | ||
[prefix, token, device_id, suffix].join("/") | ||
end | ||
|
||
attr_reader :client, :prefix, :suffix | ||
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
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,15 @@ | ||
module MessageForwarding | ||
|
||
extend ActiveSupport::Concern | ||
|
||
def forward_reading(device, reading) | ||
forwarder = MQTTForwarder.new(mqtt_client) | ||
forwarder.forward_reading(device.forwarding_token, device.id, reading) if device.forward_readings? | ||
end | ||
|
||
private | ||
|
||
def mqtt_client | ||
raise NotImplementedError | ||
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
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,5 @@ | ||
class AddForwardingTokenToUsers < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :users, :forwarding_token, :string | ||
end | ||
end |
Oops, something went wrong.