Class: AwsIotDevice::MqttAdapter::RubyMqttAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RubyMqttAdapter

Returns a new instance of RubyMqttAdapter.



12
13
14
15
16
17
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 12

def initialize(*args)
  @client = MQTT::Client.new(*args)
  @filtered_topics = {}
  @client_id = ""
  @client_id = generate_client_id
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 8

def client_id
  @client_id
end

#filtered_topicsObject

Returns the value of attribute filtered_topics.



10
11
12
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 10

def filtered_topics
  @filtered_topics
end

Instance Method Details

#add_callback_filter_topic(topic, callback) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 168

def add_callback_filter_topic(topic, callback)
  unless callback.nil?
    @filtered_topics[topic] = callback
  else
    @filtered_topic.delete(topic)
  end
end

#connect(*args, &block) ⇒ Object



31
32
33
34
35
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 31

def connect(*args, &block)
  @client = create_client(*args) if @client.nil?
  @client.connect(&block)
  loop_start
end

#connected?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 50

def connected?
  @client.connected?
end

#create_client(*args) ⇒ Object



27
28
29
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 27

def create_client(*args)
  @client = MQTT::Client.new(*args)
end

#disconnect(send_msg = true) ⇒ Object



46
47
48
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 46

def disconnect(send_msg=true)
  @client.disconnect(send_msg)
end

#generate_client_id(prefix = 'ruby', lenght = 16) ⇒ Object



37
38
39
40
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 37

def generate_client_id(prefix='ruby', lenght=16)
  charset = Array('A'..'Z') + Array('a'..'z') + Array('0'..'9')
  @client_id << prefix << Array.new(lenght) { charset.sample }.join
end

#get(topic = nil, &block) ⇒ Object



58
59
60
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 58

def get(topic=nil, &block)
  @client.get(topic, &block)
end

#get_packet(topic = nil, &block) ⇒ Object



62
63
64
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 62

def get_packet(topic=nil, &block)
  @client.get_packet(topic, &block)
end

#hostObject



78
79
80
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 78

def host
  @client.host
end

#host=(host) ⇒ Object



82
83
84
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 82

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

#loop_foreverObject



116
117
118
119
120
121
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 116

def loop_forever
  loop do
    mqtt_loop
    sleep 0.005
  end
end

#loop_miscObject



149
150
151
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 149

def loop_misc
  puts "loop_misc is unavailable for ruby-mqtt client's"
end

#loop_read(max_message = 10) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 129

def loop_read(max_message=10)
  counter_message = 0
  while !@client.queue_empty? && counter_message <= max_message
    message = get_packet
    ### Fitlering message if matching to filtered topic
    topic = message.topic
    if @filtered_topics.key?(topic)
      callback = @filtered_topics[topic]
      callback.call(message)
    else
      on_message_callback(message)
    end
    counter_message += 1
  end
end

#loop_startObject

Custom Features #################



108
109
110
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 108

def loop_start
  Thread.new{ loop_forever }
end

#loop_stop(thread) ⇒ Object



112
113
114
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 112

def loop_stop(thread)
  thread.join
end

#loop_writeObject



145
146
147
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 145

def loop_write
  puts "loop_write is unavailable for ruby-mqtt client's"
end

#mqtt_loopObject



123
124
125
126
127
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 123

def mqtt_loop
  loop_read
  loop_write
  loop_misc
end

#on_message(&block) ⇒ Object



157
158
159
160
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 157

def on_message(&block)
  @on_message = block if block_given?
  @on_message
end

#on_message=(callback) ⇒ Object



153
154
155
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 153

def on_message=(callback)
  @on_message = callback
end

#on_message_callback(message) ⇒ Object



162
163
164
165
166
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 162

def on_message_callback(message)
  if @on_message.is_a?(Proc) || @on_message.lambda?
    @on_message.call(message)
  end
end

#portObject



86
87
88
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 86

def port
  @client.port
end

#port=(port) ⇒ Object



90
91
92
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 90

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

#publish(topic, payload = '', retain = false, qos = 0) ⇒ Object



23
24
25
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 23

def publish(topic, payload='', retain=false, qos=0)
  @client.publish(topic, payload, retain, qos)
end

#queue_empty?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 66

def queue_empty?
  @client.queue_empty?
end

#queue_lengthObject



70
71
72
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 70

def queue_length
  @client.queue_length
end

#remove_callback_filter_topic(topic) ⇒ Object



176
177
178
179
180
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 176

def remove_callback_filter_topic(topic)
  if @filtered_topics.key(topic)
    @filtered_topics.delete(topic)
  end
end

#set_tls_ssl_context(ca_cert, cert = nil, key = nil) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 98

def set_tls_ssl_context(ca_cert, cert=nil, key=nil)
  @client.ssl ||= true
  @client.ssl_context
  @client.cert_file = cert
  @client.key_file = key
  @client.ca_file = ca_cert
end

#ssl=(ssl) ⇒ Object



94
95
96
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 94

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

#ssl_contextObject



42
43
44
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 42

def ssl_context
  @client.ssl_context
end

#subscribe(topics, qos = 0) ⇒ Object



54
55
56
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 54

def subscribe(topics, qos=0)
  @client.subscribe(topics, qos)
end

#unsubscribe(*topics) ⇒ Object



74
75
76
# File 'lib/aws_iot_device/mqtt_adapter/ruby_mqtt_adapter.rb', line 74

def unsubscribe(*topics)
  @client.unsubscribe(*topics)
end