Class: Adafruit::IO::MQTT
- Inherits:
-
Object
- Object
- Adafruit::IO::MQTT
- Defined in:
- lib/adafruit/io/mqtt.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
provide access to the raw MQTT library client.
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#get(&block) ⇒ Object
Retrieve the last value received from the MQTT connection for any subscribed feeds or groups.
-
#initialize(username, key, opts = {}) ⇒ MQTT
constructor
A new instance of MQTT.
-
#publish(key, value, location = {}) ⇒ Object
Publish value to feed with given key.
-
#publish_group(key, values, location = {}) ⇒ Object
Publish to multiple feeds within a group.
-
#subscribe(key, options = {}) ⇒ Object
Subscribe to the feed with the given key.
-
#subscribe_group(key) ⇒ Object
Subscribe to a group with the given key.
- #unsubscribe(key) ⇒ Object
Constructor Details
#initialize(username, key, opts = {}) ⇒ MQTT
Returns a new instance of MQTT.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/adafruit/io/mqtt.rb', line 40 def initialize(username, key, opts={}) @options = { uri: 'io.adafruit.com', protocol: 'mqtts', port: 8883, username: username, key: key }.merge(opts) @connect_uri = "%{protocol}://%{username}:%{key}@%{uri}" % @options connect end |
Instance Attribute Details
#client ⇒ Object (readonly)
provide access to the raw MQTT library client
38 39 40 |
# File 'lib/adafruit/io/mqtt.rb', line 38 def client @client end |
Instance Method Details
#connect ⇒ Object
54 55 56 57 58 |
# File 'lib/adafruit/io/mqtt.rb', line 54 def connect if @client.nil? || !@client.connected? @client = ::MQTT::Client.connect @connect_uri, @options[:port], ack_timeout: 10 end end |
#disconnect ⇒ Object
60 61 62 63 64 |
# File 'lib/adafruit/io/mqtt.rb', line 60 def disconnect if @client && @client.connected? @client.disconnect end end |
#get(&block) ⇒ Object
Retrieve the last value received from the MQTT connection for any subscribed feeds or groups. This is a blocking method, which means it won’t return until a message is retrieved.
Returns [topic, message] or yields it into the given block.
With no block:
mqtt_client.subscribe('feed-key')
loop do
topic, = mqtt_client.get
# do something
end
With a block:
mqtt_client.subscribe('feed-key')
mqtt_client.get do |topic, |
# do something
end
153 154 155 |
# File 'lib/adafruit/io/mqtt.rb', line 153 def get(&block) @client.get(&block) end |
#publish(key, value, location = {}) ⇒ Object
Publish value to feed with given key
67 68 69 70 71 72 73 74 75 |
# File 'lib/adafruit/io/mqtt.rb', line 67 def publish(key, value, location={}) raise 'client is not connected' unless @client.connected? topic = key_to_feed_topic(key) location = indifferent_keys(location) payload = payload_from_value_with_location(value, location) @client.publish(topic, payload) end |
#publish_group(key, values, location = {}) ⇒ Object
Publish to multiple feeds within a group.
-
‘key` is a group key
-
‘values` is a hash where keys are feed keys and values are the published value.
-
‘location` is the optional { :lat, :lon, :ele } hash specifying the location data for this publish event.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/adafruit/io/mqtt.rb', line 85 def publish_group(key, values, location={}) raise 'client is not connected' unless @client.connected? raise 'values must be a hash' unless values.is_a?(Hash) topic = key_to_group_topic(key, false) location = indifferent_keys(location) payload = payload_from_values_with_location(values, location) @client.publish(topic, payload) end |
#subscribe(key, options = {}) ⇒ Object
Subscribe to the feed with the given key. Use .get to retrieve messages from subscribed feeds.
Include the { last_value: true } option if you’d like the feed to receive the last value immediately. (like MQTT retain)
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/adafruit/io/mqtt.rb', line 101 def subscribe(key, ={}) raise 'client is not connected' unless @client.connected? topic = key_to_feed_topic(key) @client.subscribe(topic) if [:last_value] @client.publish(topic + '/get', '') end end |
#subscribe_group(key) ⇒ Object
Subscribe to a group with the given key.
NOTE: Unlike feed subscriptions, group subscriptions return a JSON representation of the group record with a ‘feeds’ property containing a JSON object whose keys are feed keys and whose values are the last value received for that feed.
125 126 127 128 129 130 |
# File 'lib/adafruit/io/mqtt.rb', line 125 def subscribe_group(key) raise 'client is not connected' unless @client.connected? topic = key_to_group_topic(key) @client.subscribe(topic) end |
#unsubscribe(key) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/adafruit/io/mqtt.rb', line 112 def unsubscribe(key) raise 'client is not connected' unless @client.connected? topic = key_to_feed_topic(key) @client.unsubscribe(topic) end |