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.
-
#get_packet(topic = nil) ⇒ Mqtt::Packet
Get message to me.
-
#initialize(args = {}) ⇒ MQTT
constructor
MQTT Client.
- #logger ⇒ Logger
- #logger=(logger_) ⇒ Object
-
#publish(topic, payload, retain = false, qos = 0) ⇒ 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).
-
#unsubscribe(*topics) ⇒ Object
(also: #unsub)
Unsubscribe 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
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
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
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 |
#logger ⇒ 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.
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)
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)
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 |