Class: M2X::MQTT::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/m2x/mqtt/client.rb', line 16

def initialize(api_key, options={})
  @api_key = api_key
  @options = DEFAULTS.merge(options)
end

Instance Method Details

#get_responseObject

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

See m2x.att.com/developer/documentation/v2/mqtt



38
39
40
# File 'lib/m2x/mqtt/client.rb', line 38

def publish(payload)
  mqtt_client.publish(request_topic, payload.to_json)
end

#subscribeObject

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