Class: Mosca::Client
- Inherits:
-
Object
- Object
- Mosca::Client
- Extended by:
- Forwardable
- Defined in:
- lib/mosca/client.rb
Constant Summary collapse
- KEEP_ALIVE_MARGIN =
5
Class Attribute Summary collapse
-
.default_broker ⇒ Object
Returns the value of attribute default_broker.
-
.default_timeout ⇒ Object
Returns the value of attribute default_timeout.
Instance Attribute Summary collapse
-
#broker ⇒ Object
Returns the value of attribute broker.
-
#client ⇒ Object
Returns the value of attribute client.
-
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
-
#pass ⇒ Object
Returns the value of attribute pass.
-
#port ⇒ Object
Returns the value of attribute port.
-
#time_out ⇒ Object
Returns the value of attribute time_out.
-
#topic_base ⇒ Object
Returns the value of attribute topic_base.
-
#topic_in ⇒ Object
Returns the value of attribute topic_in.
-
#topic_out ⇒ Object
Returns the value of attribute topic_out.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #connected? ⇒ Boolean
- #default_attributes ⇒ Object
- #full_topic(topic_name) ⇒ Object
- #get(params = {}) ⇒ Object
- #get!(params = {}) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #publish(message, params = {}) ⇒ Object
- #publish!(message, params = {}) ⇒ Object
- #refresh_connection ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 |
# File 'lib/mosca/client.rb', line 22 def initialize args = {} default_attributes.merge(args).each do |k,v| self.send("#{k}=", v) end end |
Class Attribute Details
.default_broker ⇒ Object
Returns the value of attribute default_broker.
14 15 16 |
# File 'lib/mosca/client.rb', line 14 def default_broker @default_broker end |
.default_timeout ⇒ Object
Returns the value of attribute default_timeout.
14 15 16 |
# File 'lib/mosca/client.rb', line 14 def default_timeout @default_timeout end |
Instance Attribute Details
#broker ⇒ Object
Returns the value of attribute broker.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def broker @broker end |
#client ⇒ Object
Returns the value of attribute client.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def client @client end |
#keep_alive ⇒ Object
Returns the value of attribute keep_alive.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def keep_alive @keep_alive end |
#pass ⇒ Object
Returns the value of attribute pass.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def pass @pass end |
#port ⇒ Object
Returns the value of attribute port.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def port @port end |
#time_out ⇒ Object
Returns the value of attribute time_out.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def time_out @time_out end |
#topic_base ⇒ Object
Returns the value of attribute topic_base.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def topic_base @topic_base end |
#topic_in ⇒ Object
Returns the value of attribute topic_in.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def topic_in @topic_in end |
#topic_out ⇒ Object
Returns the value of attribute topic_out.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def topic_out @topic_out end |
#user ⇒ Object
Returns the value of attribute user.
17 18 19 |
# File 'lib/mosca/client.rb', line 17 def user @user end |
Instance Method Details
#connected? ⇒ Boolean
79 80 81 |
# File 'lib/mosca/client.rb', line 79 def connected? @connection and @connection.connected? and is_alive? end |
#default_attributes ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mosca/client.rb', line 28 def default_attributes { user: ENV["MOSCA_USER"], pass: ENV["MOSCA_PASS"], topic_base: "", broker: ENV["MOSCA_BROKER"] || self.class.default_broker || "test.mosquitto.org", client: MQTT::Client, keep_alive: 10, time_out: (ENV["MOSCA_TIMEOUT"] || self.class.default_timeout || 5).to_i, port: (ENV["MOSCA_PORT"] || 1883).to_i } end |
#full_topic(topic_name) ⇒ Object
71 72 73 |
# File 'lib/mosca/client.rb', line 71 def full_topic topic_name topic_base + topic_name end |
#get(params = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/mosca/client.rb', line 65 def get params = {} get! params rescue Timeout::Error nil end |
#get!(params = {}) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/mosca/client.rb', line 56 def get! params = {} timeout(params) do topic = params[:topic_in] || params[:topic] || @topic_in || Exceptions.raise_missing_topic connection.get(full_topic topic) do |topic, | return parse_response end end end |
#publish(message, params = {}) ⇒ Object
50 51 52 53 54 |
# File 'lib/mosca/client.rb', line 50 def publish , params = {} publish! , params rescue Timeout::Error nil end |
#publish!(message, params = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/mosca/client.rb', line 40 def publish! , params = {} timeout(params) do topic_out = params[:topic_out] || params[:topic] || @topic_out || Exceptions.raise_missing_topic topic_in = params[:topic_in] || @topic_in connection.subscribe full_topic(topic_in) if params[:response] connection.publish full_topic(topic_out), params[:response] ? get(params) : end end |
#refresh_connection ⇒ Object
75 76 77 |
# File 'lib/mosca/client.rb', line 75 def refresh_connection connection end |