class M2X::MQTT::Client

Constants

API_VERSION
DEFAULTS
DEFAULT_API_URL
USER_AGENT

Attributes

packet_router[R]

Public Class Methods

new(api_key, options={}) click to toggle source
# File lib/m2x/mqtt/client.rb, line 16
def initialize(api_key, options={})
  @api_key = api_key
  @options = DEFAULTS.merge(options)

  @packet_router = PacketRouter.new
end

Public Instance Methods

get_command() { |command(self, json_fetch)| ... } click to toggle source

Public: Retrieve a command from the M2X Server.

Returns a Hash with the command from the MQTT Server in M2X. Optionally receives a block which will iterate through commands and yield each one.

# File lib/m2x/mqtt/client.rb, line 65
def get_command
  mqtt_client.subscribe(command_topic)

  return M2X::MQTT::Command.new(self, packet_router.json_fetch(mqtt_client, command_topic)) unless block_given?

  loop do
    yield M2X::MQTT::Command.new(self, packet_router.json_fetch(mqtt_client, command_topic))
  end
end
get_response() { |json_fetch| ... } click to toggle source

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.

# File lib/m2x/mqtt/client.rb, line 50
def get_response
  mqtt_client.subscribe(response_topic)

  return packet_router.json_fetch(mqtt_client, response_topic) unless block_given?

  loop do
    yield packet_router.json_fetch(mqtt_client, response_topic)
  end
end
publish(payload) click to toggle source

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

# File lib/m2x/mqtt/client.rb, line 41
def publish(payload)
  mqtt_client.publish(request_topic, payload.to_json)
end
subscribe() click to toggle source

Public: Subscribe the client to the responses topic.

This is required in order to receive responses or commands from the M2X API server. Note that get_response already subscribes the client.

# File lib/m2x/mqtt/client.rb, line 28
def subscribe
  mqtt_client.subscribe(response_topic)
  mqtt_client.subscribe(command_topic)
end

Private Instance Methods

command_topic() click to toggle source
# File lib/m2x/mqtt/client.rb, line 110
def command_topic
  @command_topic ||= "m2x/#{@api_key}/commands".freeze
end
mqtt_client() click to toggle source
# File lib/m2x/mqtt/client.rb, line 114
def mqtt_client
  @mqtt_client ||= ::MQTT::Client.new.tap do |client|
                     client.host     = @options[:api_url]
                     client.username = @api_key

                     if @options[:use_ssl]
                       client.ssl  = true
                       client.port = 8883
                     end
                   end

  unless @mqtt_client.connected?
    @mqtt_client.connect
  end

  @mqtt_client
end
request(verb, path, params=nil) click to toggle source
# File lib/m2x/mqtt/client.rb, line 85
def request(verb, path, params=nil)
  path  = versioned(path)
  body  = params || {}

  payload = {
    id:       SecureRandom.hex,
    agent:    USER_AGENT,
    method:   verb.upcase,
    resource: path,
    body:     body
  }

  publish(payload)

  { id: payload[:id] }
end
request_topic() click to toggle source
# File lib/m2x/mqtt/client.rb, line 102
def request_topic
  @request_topic ||= "m2x/#{@api_key}/requests".freeze
end
response_topic() click to toggle source
# File lib/m2x/mqtt/client.rb, line 106
def response_topic
  @response_topic ||= "m2x/#{@api_key}/responses".freeze
end
versioned(path) click to toggle source
# File lib/m2x/mqtt/client.rb, line 132
def versioned(path)
  versioned?(path) ? path : "/#{API_VERSION}#{path}"
end
versioned?(path) click to toggle source
# File lib/m2x/mqtt/client.rb, line 136
def versioned?(path)
  path =~ /^\/v\d+\//
end