class Mosca::Client
Constants
- KEEP_ALIVE_MARGIN
Attributes
default_broker[RW]
default_timeout[RW]
broker[RW]
client[RW]
keep_alive[RW]
pass[RW]
port[RW]
time_out[RW]
topic_base[RW]
topic_in[RW]
topic_out[RW]
user[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/mosca/client.rb, line 22 def initialize args = {} default_attributes.merge(args).each do |k,v| self.send("#{k}=", v) end end
Public Instance Methods
connected?()
click to toggle source
# File lib/mosca/client.rb, line 79 def connected? @connection and @connection.connected? and is_alive? end
default_attributes()
click to toggle source
# 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)
click to toggle source
# File lib/mosca/client.rb, line 71 def full_topic topic_name topic_base + topic_name end
get(params = {})
click to toggle source
# File lib/mosca/client.rb, line 65 def get params = {} get! params rescue Timeout::Error nil end
get!(params = {})
click to toggle source
# 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, message| return parse_response message end end end
publish(message, params = {})
click to toggle source
# File lib/mosca/client.rb, line 50 def publish message, params = {} publish! message, params rescue Timeout::Error nil end
publish!(message, params = {})
click to toggle source
# File lib/mosca/client.rb, line 40 def publish! message, 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), message params[:response] ? get(params) : message end end
refresh_connection()
click to toggle source
# File lib/mosca/client.rb, line 75 def refresh_connection connection end
Private Instance Methods
client_options()
click to toggle source
# File lib/mosca/client.rb, line 85 def client_options {remote_host: @broker, remote_port: @port, username: @user, password: @pass} end
connection()
click to toggle source
# File lib/mosca/client.rb, line 89 def connection if connected? @connection else @connection = @client.connect(client_options) @connection.keep_alive = @keep_alive @connection end end
is_alive?()
click to toggle source
# File lib/mosca/client.rb, line 112 def is_alive? ( Time.now - @connection.last_ping_response ) < @keep_alive + KEEP_ALIVE_MARGIN end
parse_response(response)
click to toggle source
# File lib/mosca/client.rb, line 99 def parse_response response JSON.parse response rescue JSON::ParserError response end
timeout(params) { || ... }
click to toggle source
# File lib/mosca/client.rb, line 105 def timeout params, &b seconds = params[:timeout] || time_out Timeout.timeout(seconds) do yield end end