Class: Beebotte::Stream
- Inherits:
-
Object
- Object
- Beebotte::Stream
- Defined in:
- lib/beebotte.rb
Defined Under Namespace
Classes: NoSubscriptions, NotConnected
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #get(&block) ⇒ Object
-
#initialize(opts = {}) ⇒ Stream
constructor
A new instance of Stream.
- #publish(channel, resource, data) ⇒ Object
- #subscribe(topic) ⇒ Object
- #write(channel, resource, data) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Stream
Returns a new instance of Stream.
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/beebotte.rb', line 308 def initialize(opts = {}) @token = opts[:token] @secretKey = opts[:secret_key] raise ArguementError, 'Must set token OR secret_key in opts' if (@token.nil? && @secretKey.nil?) @host = opts[:host] || "mqtt.beebotte.com" @port = opts[:port] || 1883 @ssl = opts[:ssl] || false @subscriptions = [] @client = MQTT::Client.new end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
302 303 304 |
# File 'lib/beebotte.rb', line 302 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
302 303 304 |
# File 'lib/beebotte.rb', line 302 def port @port end |
#ssl ⇒ Object
Returns the value of attribute ssl.
302 303 304 |
# File 'lib/beebotte.rb', line 302 def ssl @ssl end |
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
303 304 305 |
# File 'lib/beebotte.rb', line 303 def subscriptions @subscriptions end |
#token ⇒ Object
Returns the value of attribute token.
302 303 304 |
# File 'lib/beebotte.rb', line 302 def token @token end |
Instance Method Details
#connect ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/beebotte.rb', line 319 def connect @client.disconnect() if @client.connected? @client.host = @host @client.port = @port @client.ssl = @ssl @client.username = @client.password = nil @subscriptions = [] if @token @client.username = "token:#{@token}" else @client.username = @secretKey end @client.connect() @client.connected? end |
#connected? ⇒ Boolean
341 342 343 |
# File 'lib/beebotte.rb', line 341 def connected? @client.connected? end |
#disconnect ⇒ Object
335 336 337 338 339 |
# File 'lib/beebotte.rb', line 335 def disconnect @subscriptions = [] @client.disconnect() true end |
#get(&block) ⇒ Object
345 346 347 348 |
# File 'lib/beebotte.rb', line 345 def get(&block) raise NoSubscriptions if @subscriptions.length == 0 @client.get(&block) end |
#publish(channel, resource, data) ⇒ Object
350 351 352 353 354 355 356 |
# File 'lib/beebotte.rb', line 350 def publish(channel, resource, data) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) && channel.length.between?(2,30) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) && resource.length.between?(2,30) raise ArgumentError, 'Data name must be a Hash' unless data.is_a?(Hash) data = {data: data, write: false } @client.publish("#{channel}/#{resource}" , data.to_json) end |
#subscribe(topic) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/beebotte.rb', line 368 def subscribe(topic) raise ArgumentError, "Topic must be in the form of 'channel/resource'" unless topic.is_a?(String) && topic.match(/^[a-zA-Z0-9_]*\/[a-zA-Z0-9_]*$/) raise NotConnected unless connected? return true if @subscriptions.include?(topic) if @client.subscribe(topic) @subscriptions << topic return true else return false end end |
#write(channel, resource, data) ⇒ Object
359 360 361 362 363 364 365 366 |
# File 'lib/beebotte.rb', line 359 def write(channel, resource, data) raise ArgumentError, 'Channel name must be a string' unless channel.is_a?(String) && channel.length.between?(2,30) raise ArgumentError, 'Resource name must be a string' unless resource.is_a?(String) && resource.length.between?(2,30) raise ArgumentError, 'Data name must be a Hash' unless data.is_a?(Hash) data = {data: data, write: true } @client.publish("#{channel}/#{resource}" , data.to_json) end |