Class: Libmagellan::Core
- Inherits:
-
Object
- Object
- Libmagellan::Core
- Defined in:
- lib/libmagellan/core.rb
Constant Summary collapse
- DEFAULT_LOG_LEVEL =
Logger::INFO
Instance Method Summary collapse
- #disconnect ⇒ Object
- #get_message(*args, &block) ⇒ Object
-
#initialize(*args) ⇒ Core
constructor
A new instance of Core.
- #json_body(r) ⇒ Object
-
#log_level=(level) ⇒ Object
Set logger log level.
- #logger ⇒ Object
- #logger=(logger_) ⇒ Object
-
#mqtt_token(path, method = :get, body = "", headers = {}) ⇒ Object
MQTT.
- #ping ⇒ Object
- #publish(*args) ⇒ Object (also: #pub)
-
#request(*args, &block) ⇒ Object
(also: #req)
HTTP.
- #set_will(topic, payload, retain = false, qos = 0) ⇒ Object
- #subscribe(*args, &block) ⇒ Object (also: #sub)
- #test(*args, &block) ⇒ Object
- #test_ping(*args) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Core
Returns a new instance of Core.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/libmagellan/core.rb', line 12 def initialize(*args) # HTTP @http = HTTP.new(*args.dup) # MQTT = args.last.is_a?(Hash) ? args.pop : {} = .dup if [:mqtt_host].present? and [:mqtt_port].present? [:host] = .delete(:mqtt_host) [:port] = .delete(:mqtt_port) [:secure] = .delete(:mqtt_secure) || false @mqtt = MQTT.new() @mqtt.logger = logger end end |
Instance Method Details
#disconnect ⇒ Object
81 82 83 |
# File 'lib/libmagellan/core.rb', line 81 def disconnect @mqtt.disconnect end |
#get_message(*args, &block) ⇒ Object
89 90 91 |
# File 'lib/libmagellan/core.rb', line 89 def (*args, &block) @mqtt.get(*args, &block) end |
#json_body(r) ⇒ Object
50 51 52 |
# File 'lib/libmagellan/core.rb', line 50 def json_body(r) @http.json_body(r) end |
#log_level=(level) ⇒ Object
Set logger log level
108 109 110 |
# File 'lib/libmagellan/core.rb', line 108 def log_level=(level) logger.level = level end |
#logger ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/libmagellan/core.rb', line 94 def logger @logger ||= Proc.new { logger = ::Logger.new(STDOUT) logger.level = DEFAULT_LOG_LEVEL logger }.call end |
#logger=(logger_) ⇒ Object
102 103 104 |
# File 'lib/libmagellan/core.rb', line 102 def logger=(logger_) @logger = logger_ end |
#mqtt_token(path, method = :get, body = "", headers = {}) ⇒ Object
MQTT
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/libmagellan/core.rb', line 60 def mqtt_token(path, method=:get, body="", headers={}) r = request(path, method, body, headers) if r.code.to_i >= 200 and r.code.to_i < 300 token = r['authorization'] @mqtt.token = token if @mqtt token else r end end |
#ping ⇒ Object
38 39 40 |
# File 'lib/libmagellan/core.rb', line 38 def ping @http.ping end |
#publish(*args) ⇒ Object Also known as: pub
71 72 73 |
# File 'lib/libmagellan/core.rb', line 71 def publish(*args) @mqtt.publish(*args) end |
#request(*args, &block) ⇒ Object Also known as: req
HTTP
33 34 35 |
# File 'lib/libmagellan/core.rb', line 33 def request(*args, &block) @http.request(*args, &block) end |
#set_will(topic, payload, retain = false, qos = 0) ⇒ Object
85 86 87 |
# File 'lib/libmagellan/core.rb', line 85 def set_will(topic, payload, retain=false, qos=0) @mqtt.set_will(topic, payload, retain, qos) end |
#subscribe(*args, &block) ⇒ Object Also known as: sub
76 77 78 |
# File 'lib/libmagellan/core.rb', line 76 def subscribe(*args, &block) @mqtt.subscribe(*args, &block) end |
#test(*args, &block) ⇒ Object
46 47 48 |
# File 'lib/libmagellan/core.rb', line 46 def test(*args, &block) @http.test(*args, &block) end |
#test_ping(*args) ⇒ Object
42 43 44 |
# File 'lib/libmagellan/core.rb', line 42 def test_ping(*args) @http.test_ping(*args) end |