Class: M2X::MQTT::Client
- Inherits:
-
Object
- Object
- M2X::MQTT::Client
- Defined in:
- lib/m2x/mqtt/client.rb
Defined Under Namespace
Classes: PacketRouter
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_command ⇒ Object
Public: Retrieve a command from the M2X Server.
-
#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
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 |
# File 'lib/m2x/mqtt/client.rb', line 16 def initialize(api_key, ={}) @api_key = api_key @options = DEFAULTS.merge() @packet_router = PacketRouter.new end |
Instance Method Details
#get_command ⇒ Object
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.
65 66 67 68 69 70 71 72 73 |
# 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 ⇒ 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.
50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
Public: Send a payload to the M2X API server.
payload - a Hash with the following keys:
:id
:method
:resource
:body
41 42 43 |
# File 'lib/m2x/mqtt/client.rb', line 41 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 or commands from the M2X API server. Note that #get_response already subscribes the client.
28 29 30 31 |
# File 'lib/m2x/mqtt/client.rb', line 28 def subscribe mqtt_client.subscribe(response_topic) mqtt_client.subscribe(command_topic) end |