Class: AwsIotDevice::MqttShadowClient::MqttManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MqttManager

Returns a new instance of MqttManager.



15
16
17
18
19
20
21
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 15

def initialize(*args)
  @client = create_mqtt_adapter(*args)
  @mqtt_operation_timeout_s = 2
  @mutex_publish = Mutex.new()
  @mutex_subscribe = Mutex.new()
  @mutex_unsubscribe = Mutex.new()
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 7

def client_id
  @client_id
end

#connection_timeout_sObject

Returns the value of attribute connection_timeout_s.



9
10
11
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 9

def connection_timeout_s
  @connection_timeout_s
end

#mqtt_operation_timeout_sObject

Returns the value of attribute mqtt_operation_timeout_s.



11
12
13
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 11

def mqtt_operation_timeout_s
  @mqtt_operation_timeout_s
end

#sslObject

Returns the value of attribute ssl.



13
14
15
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 13

def ssl
  @ssl
end

Instance Method Details

#add_topic_callback(topic, callback, &block) ⇒ Object



174
175
176
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 174

def add_topic_callback(topic, callback, &block)
  @client.add_callback_filter_topic(topic, callback, &block)
end

#config_endpoint(host, port) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 47

def config_endpoint(host, port)
  raise ArgumentError, "configure endpoint either host or port is nil" if host.nil? || port.nil?
  @client.host = host
  @client.port = port
end

#config_ssl_context(ca_file, key, cert) ⇒ Object



53
54
55
56
57
58
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 53

def config_ssl_context(ca_file, key, cert)
  self.ca_file = ca_file
  self.key = key
  self.cert = cert
  @client.set_tls_ssl_context(@ca_file, @cert, @key)
end

#connect(*args, &block) ⇒ Object



60
61
62
63
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 60

def connect(*args, &block)
  ### Execute a mqtt opration loop in background for time period defined by mqtt_connection_timeout
  @client.connect(*args, &block)
end

#create_mqtt_adapter(*args) ⇒ Object



43
44
45
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 43

def create_mqtt_adapter(*args)
  @client = MqttAdapter::Client.new(*args)
end

#disconnectObject



65
66
67
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 65

def disconnect
  @client.disconnect
end

#hostObject



27
28
29
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 27

def host
  @client.host
end

#host=(host) ⇒ Object



23
24
25
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 23

def host=(host)
  @client.host = host
end

#on_connack(&block) ⇒ Object



142
143
144
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 142

def on_connack(&block)
  @client.on_connack(&block) if paho_client?
end

#on_connack=(callback) ⇒ Object



110
111
112
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 110

def on_connack=(callback)
  @client.on_connack = callback if paho_client?
end

#on_message(&block) ⇒ Object



170
171
172
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 170

def on_message(&block)
  @client.on_message(&block)
end

#on_message=(callback) ⇒ Object



138
139
140
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 138

def on_message=(callback)
  @client.on_message = callback
end

#on_puback(&block) ⇒ Object



154
155
156
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 154

def on_puback(&block)
  @client.on_puback(&block) if paho_client?
end

#on_puback=(callback) ⇒ Object



122
123
124
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 122

def on_puback=(callback)
  @client.on_puback = callback if paho_client?
end

#on_pubcomp(&block) ⇒ Object



166
167
168
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 166

def on_pubcomp(&block)
  @client.on_pubcomp(&block) if paho_client?
end

#on_pubcomp=(callback) ⇒ Object



134
135
136
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 134

def on_pubcomp=(callback)
  @client.on_pubcomp = callback if paho_client?
end

#on_pubrec(&block) ⇒ Object



158
159
160
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 158

def on_pubrec(&block)
  @client.on_pubrec(&block) if paho_client?
end

#on_pubrec=(callback) ⇒ Object



126
127
128
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 126

def on_pubrec=(callback)
  @client.on_pubrec = callback if paho_client?
end

#on_pubrel(&block) ⇒ Object



162
163
164
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 162

def on_pubrel(&block)
  @client.on_pubrel(&block) if paho_client?
end

#on_pubrel=(callback) ⇒ Object



130
131
132
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 130

def on_pubrel=(callback)
  @client.on_pubrel = callback if paho_client?
end

#on_suback(&block) ⇒ Object



146
147
148
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 146

def on_suback(&block)
  @client.on_suback(&block) if paho_client?
end

#on_suback=(callback) ⇒ Object



114
115
116
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 114

def on_suback=(callback)
  @client.on_suback = callback if paho_client?
end

#on_unsuback(&block) ⇒ Object



150
151
152
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 150

def on_unsuback(&block)
  @client.on_unsuback(&block) if paho_client?
end

#on_unsuback=(callback) ⇒ Object



118
119
120
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 118

def on_unsuback=(callback)
  @client.on_unsuback = callback if paho_client?
end

#paho_client?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 182

def paho_client?
  @client.adapter.class == MqttAdapter::PahoMqttAdapter
end

#portObject



35
36
37
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 35

def port
  @client.port
end

#port=(port) ⇒ Object



31
32
33
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 31

def port=(port)
 @client.port = port
end

#publish(topic, payload = "", retain = nil, qos = 0) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 69

def publish(topic, payload="", retain=nil, qos=0)
  raise ArgumentError, "publish topic cannot be nil" if topic.nil?
  @mutex_publish.synchronize {
    @client.publish(topic, payload, retain, qos)
  }
end

#remove_topic_callback(topic) ⇒ Object



178
179
180
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 178

def remove_topic_callback(topic)
  @client.remove_callback_filter_topic(topic)
end

#subscribe(topic, qos = 0, callback = nil) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 76

def subscribe(topic, qos=0, callback=nil)
  raise ArgumentError, "subscribe topic cannot be nil" if topic.nil?
  @mutex_subscribe.synchronize {
    @client.add_callback_filter_topic(topic, callback)
    @client.subscribe(topic, qos)
  }
end

#subscribe_bunch(*topics) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 84

def subscribe_bunch(*topics)
  @mutex_subscribe.synchronize {
    topics.each do |topic|
      @client.add_callback_filter_topic(topic.first, topic.pop) if !topic[2].nil? && topic[2].is_a?(Proc)
    end
    @client.subscribe_bunch(topics)
  }
end

#unsubscribe(topic) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 93

def unsubscribe(topic)
  raise ArgumentError, "unsubscribe topic cannot be nil" if topic.nil?
  @mutex_unsubscribe.synchronize{
    @client.remove_callback_filter_topic(topic)
    @client.unsubscribe(topic)
  }
end

#unsubscribe_bunch(*topics) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/aws_iot_device/mqtt_shadow_client/mqtt_manager.rb', line 101

def unsubscribe_bunch(*topics)
  @mutex_unsubscribe.synchronize {
    topics.each do |topic|
      @client.remove_callback_filter_topic(topic)
    end
    @client.unsubscribe_bunch(topics)
  }
end