Class: Libmagellan::MQTT

Inherits:
Object
  • Object
show all
Defined in:
lib/libmagellan/mqtt.rb

Defined Under Namespace

Classes: MqttError

Constant Summary collapse

AUTH_BASE_URL =
"mqtt://mqtt.magellanic-clouds.com".freeze
MQTT_METHOD =
"mqtt".freeze
PROTOCOL =
"mqtt".freeze
LOG_LEVELS =
{
  debug: Logger::DEBUG,
  info:  Logger::INFO,
  warn:  Logger::WARN,
  error: Logger::ERROR,
  fatal: Logger::FATAL,
}
DEFAULT_LOG_LEVEL =
Logger::INFO
COLOURS =
{
  red:    "\e[31m",
  blue:   "\e[34m",
  green:  "\e[32m",
  yellow: "\e[33m",
  white:  "\e[37m",
}
OPTIONS =
{
  host: {required: true},
  port: {required: true},
  consumer_key: {required: true},
  consumer_secret: {required: true},
  client_version: {required: true},
  secure: {default: false},
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ MQTT

MQTT Client

Parameters:

  • args (Hash) (defaults to: {})

    MQTT Arguments

Options Hash (args):

  • :host (String)

    MQTT server host address

  • :port (Integer)

    MQTT server port

  • :project (String)

    Project name

  • :consumer_key (String)

    MQTT authorization consumer-key

  • :consumer_key (String)

    MQTT authorization consumer-secret



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/libmagellan/mqtt.rb', line 57

def initialize(args={})
  args, errors = validate_args(args)
  show_errors(errors, initialize_usage()) if errors.present?

  @host = args.delete(:host)
  @port = args.delete(:port)
  @project = args.delete(:project)
  @consumer_key = args.delete(:consumer_key) || @project
  @consumer_secret = args.delete(:consumer_secret)
  @client_version = args.delete(:client_version)
  @secure = args.delete(:secure) || false
  @skip_verify = args[:skip_verify] || false

  @token = nil
  @client = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



47
48
49
# File 'lib/libmagellan/mqtt.rb', line 47

def client
  @client
end

#client_versionObject

Returns the value of attribute client_version.



46
47
48
# File 'lib/libmagellan/mqtt.rb', line 46

def client_version
  @client_version
end

#consumer_keyObject

Returns the value of attribute consumer_key.



46
47
48
# File 'lib/libmagellan/mqtt.rb', line 46

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



46
47
48
# File 'lib/libmagellan/mqtt.rb', line 46

def consumer_secret
  @consumer_secret
end

#hostObject

Returns the value of attribute host.



46
47
48
# File 'lib/libmagellan/mqtt.rb', line 46

def host
  @host
end

#portObject

Returns the value of attribute port.



46
47
48
# File 'lib/libmagellan/mqtt.rb', line 46

def port
  @port
end

#tokenObject

Returns the value of attribute token.



48
49
50
# File 'lib/libmagellan/mqtt.rb', line 48

def token
  @token
end

Instance Method Details

#disconnectObject

MQTT Disconnect



120
121
122
# File 'lib/libmagellan/mqtt.rb', line 120

def disconnect
  connection.disconnect if connected?
end

#get(topic = nil) ⇒ Array

Get message to me

Parameters:

  • topic (String) (defaults to: nil)

    Topic name

Returns:

  • (Array)

    topic and payload



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/libmagellan/mqtt.rb', line 105

def get(topic=nil)
  with_connection do |c|
    if block_given?
      c.get(topic) do |topic_, message_|
        yield(topic_, message_)
      end
    else
      # return topic, payload
      c.get(topic)
    end
  end
end

#loggerLogger

Returns:

  • (Logger)


134
135
136
# File 'lib/libmagellan/mqtt.rb', line 134

def logger
  @logger_
end

#logger=(logger_) ⇒ Object



138
139
140
# File 'lib/libmagellan/mqtt.rb', line 138

def logger=(logger_)
  @logger_ = logger_
end

#publish(topic, payload) ⇒ Object Also known as: pub

Publish message to MQTT server.

Parameters:

  • topic (String)

    Topic name

  • payload (String)

    Payload



78
79
80
81
82
83
84
# File 'lib/libmagellan/mqtt.rb', line 78

def publish(topic, payload)
  payload = payload.to_json unless payload.is_a?(::String)
  with_connection do |c|
    c.publish(topic, payload)
    log("[PUBLISH] #{topic}: #{payload}", Logger::DEBUG)
  end
end

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

MQTT Will



125
126
127
128
129
130
131
# File 'lib/libmagellan/mqtt.rb', line 125

def set_will(topic, payload, retain=false, qos=0)
  @will_topic = topic
  @will_payload = payload
  @will_qos = qos
  @will_retain = retain
  @with_will = true
end

#subscribe(topics, &block) ⇒ Object Also known as: sub

Subscribe topic(s)

Parameters:

  • topics (String|Array)

    Subscribe topics



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/libmagellan/mqtt.rb', line 89

def subscribe(topics, &block)
  topics = [topics] unless topics.is_a?(::Array)
  topics = topics.select{|t| t.is_a?(::String) }
  with_connection do |c|
    c.subscribe(*topics)
    topics.each {|t| log("[SUBSCRIBE] #{t}", Logger::DEBUG) }
  end
  if block_given?
    get(nil, &block)
  end
end