Class: Libmagellan::MQTT
- Inherits:
-
Object
- Object
- Libmagellan::MQTT
- 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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#client_version ⇒ Object
Returns the value of attribute client_version.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#disconnect ⇒ Object
MQTT Disconnect.
-
#get(topic = nil) ⇒ Array
Get message to me.
-
#initialize(args = {}) ⇒ MQTT
constructor
MQTT Client.
- #logger ⇒ Logger
- #logger=(logger_) ⇒ Object
-
#publish(topic, payload) ⇒ Object
(also: #pub)
Publish message to MQTT server.
-
#set_will(topic, payload, retain = false, qos = 0) ⇒ Object
MQTT Will.
-
#subscribe(topics, &block) ⇒ Object
(also: #sub)
Subscribe topic(s).
Constructor Details
#initialize(args = {}) ⇒ MQTT
MQTT Client
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
#client ⇒ Object (readonly)
Returns the value of attribute client.
47 48 49 |
# File 'lib/libmagellan/mqtt.rb', line 47 def client @client end |
#client_version ⇒ Object
Returns the value of attribute client_version.
46 47 48 |
# File 'lib/libmagellan/mqtt.rb', line 46 def client_version @client_version end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
46 47 48 |
# File 'lib/libmagellan/mqtt.rb', line 46 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
46 47 48 |
# File 'lib/libmagellan/mqtt.rb', line 46 def consumer_secret @consumer_secret end |
#host ⇒ Object
Returns the value of attribute host.
46 47 48 |
# File 'lib/libmagellan/mqtt.rb', line 46 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
46 47 48 |
# File 'lib/libmagellan/mqtt.rb', line 46 def port @port end |
#token ⇒ Object
Returns the value of attribute token.
48 49 50 |
# File 'lib/libmagellan/mqtt.rb', line 48 def token @token end |
Instance Method Details
#disconnect ⇒ Object
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
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_, | yield(topic_, ) end else # return topic, payload c.get(topic) end end end |
#logger ⇒ 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.
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)
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 |