Class: M2X::MQTT::Client
- Inherits:
-
Object
- Object
- M2X::MQTT::Client
- Defined in:
- lib/m2x/mqtt/client.rb
Constant Summary collapse
- DEFAULT_API_URL =
"api-m2x.att.com".freeze
- API_VERSION =
"v2"
- USER_AGENT =
"M2X-Ruby/#{M2X::MQTT::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PLATFORM})".freeze
- DEFAULTS =
{ api_url: DEFAULT_API_URL, use_ssl: false }
Instance Method Summary collapse
-
#get_response ⇒ Object
Public: Retrieve a response from the M2X Server.
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#publish(payload) ⇒ Object
Public: Send a payload to the M2X API server.
-
#subscribe ⇒ Object
Public: Subscribe the client to the responses topic.
Constructor Details
Instance Method Details
#get_response ⇒ Object
Public: Retrieve a response from the M2X Server.
Returns a Hash with the response from the MQTT Server in M2X. Optionally receives a block which will iterate through responses and yield each one.
47 48 49 50 51 52 53 |
# File 'lib/m2x/mqtt/client.rb', line 47 def get_response return JSON.parse(mqtt_client.get_packet(response_topic).payload) unless block_given? mqtt_client.get_packet(response_topic) do |packet| yield JSON.parse(packet.payload) end end |
#publish(payload) ⇒ Object
Public: Send a payload to the M2X API server.
payload - a Hash with the following keys:
:id
:method
:resource
:body
38 39 40 |
# File 'lib/m2x/mqtt/client.rb', line 38 def publish(payload) mqtt_client.publish(request_topic, payload.to_json) end |
#subscribe ⇒ Object
Public: Subscribe the client to the responses topic.
This is required in order to receive responses from the M2X API server. Note that #get_response already subscribes the client.
26 27 28 |
# File 'lib/m2x/mqtt/client.rb', line 26 def subscribe mqtt_client.subscribe(response_topic) end |