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



144
145
146
# File 'lib/libmagellan/mqtt.rb', line 144

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



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/libmagellan/mqtt.rb', line 113

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

#get_packet(topic = nil) ⇒ Mqtt::Packet

Get message to me

Parameters:

  • topic (String) (defaults to: nil)

    Topic name

Returns:

  • (Mqtt::Packet)

    MQTT packet object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/libmagellan/mqtt.rb', line 129

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

#loggerLogger

Returns:

  • (Logger)


158
159
160
# File 'lib/libmagellan/mqtt.rb', line 158

def logger
  @logger_
end

#logger=(logger_) ⇒ Object



162
163
164
# File 'lib/libmagellan/mqtt.rb', line 162

def logger=(logger_)
  @logger_ = logger_
end

#publish(topic, payload, retain = false, qos = 0) ⇒ 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, retain=false, qos=0)
  payload = payload.to_json unless payload.is_a?(::String)
  with_connection do |c|
    c.publish(topic, payload, retain, qos)
    log("[PUBLISH] #{topic}: #{payload}", Logger::DEBUG)
  end
end

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

MQTT Will



149
150
151
152
153
154
155
# File 'lib/libmagellan/mqtt.rb', line 149

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|Hash)

    Subscribe topics



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

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

#unsubscribe(*topics) ⇒ Object Also known as: unsub

Unsubscribe topic(s)

Parameters:

  • topics (String|Array|Hash)

    Subscribe topics



102
103
104
105
106
107
# File 'lib/libmagellan/mqtt.rb', line 102

def unsubscribe(*topics)
  with_connection do |c|
    c.unsubscribe(*topics)
    topics.each {|t| log("[UNSUBSCRIBE] #{t}", Logger::DEBUG) }
  end
end