Class: Mosca::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mosca/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
# File 'lib/mosca/client.rb', line 20

def initialize args = {}
  @user = args[:user] || ENV["MOSCA_USER"]
  @pass = args[:user] || ENV["MOSCA_PASS"]
  @topic_in = args[:topic_in]
  @topic_out = args[:topic_out]
  @topic_base = args[:topic_base] || ""
  @broker = args[:broker] || ENV["MOSCA_BROKER"] || self.class.default_broker
  @client = args[:client] || MQTT::Client
end

Class Attribute Details

.default_brokerObject

Returns the value of attribute default_broker.



11
12
13
# File 'lib/mosca/client.rb', line 11

def default_broker
  @default_broker
end

.default_timeoutObject

Returns the value of attribute default_timeout.



11
12
13
# File 'lib/mosca/client.rb', line 11

def default_timeout
  @default_timeout
end

Instance Attribute Details

#brokerObject

Returns the value of attribute broker.



17
18
19
# File 'lib/mosca/client.rb', line 17

def broker
  @broker
end

#clientObject

Returns the value of attribute client.



17
18
19
# File 'lib/mosca/client.rb', line 17

def client
  @client
end

#passObject

Returns the value of attribute pass.



17
18
19
# File 'lib/mosca/client.rb', line 17

def pass
  @pass
end

#topic_baseObject

Returns the value of attribute topic_base.



17
18
19
# File 'lib/mosca/client.rb', line 17

def topic_base
  @topic_base
end

#topic_inObject

Returns the value of attribute topic_in.



17
18
19
# File 'lib/mosca/client.rb', line 17

def topic_in
  @topic_in
end

#topic_outObject

Returns the value of attribute topic_out.



17
18
19
# File 'lib/mosca/client.rb', line 17

def topic_out
  @topic_out
end

#userObject

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/mosca/client.rb', line 69

def connected?
  @connection and @connection.connected? and is_alive?
end

#full_topic(topic_name) ⇒ Object



61
62
63
# File 'lib/mosca/client.rb', line 61

def full_topic topic_name
  topic_base + topic_name
end

#get(params = {}) ⇒ Object



55
56
57
58
59
# File 'lib/mosca/client.rb', line 55

def get params = {}
  get! params
rescue Timeout::Error
  nil
end

#get!(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mosca/client.rb', line 46

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 = {}) ⇒ Object



40
41
42
43
44
# File 'lib/mosca/client.rb', line 40

def publish message, params = {}
  publish! message, params
rescue Timeout::Error
  nil
end

#publish!(message, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/mosca/client.rb', line 30

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_connectionObject



65
66
67
# File 'lib/mosca/client.rb', line 65

def refresh_connection
  connection
end