Class: Libmagellan::Core

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

Constant Summary collapse

DEFAULT_LOG_LEVEL =
Logger::INFO

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ 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
  options = args.last.is_a?(Hash) ? args.pop : {}
  mqtt_options = options.dup
  if mqtt_options[:mqtt_host].present? and mqtt_options[:mqtt_port].present?
    mqtt_options[:host] = mqtt_options.delete(:mqtt_host)
    mqtt_options[:port] = mqtt_options.delete(:mqtt_port)
    mqtt_options[:secure] = mqtt_options.delete(:mqtt_secure) || false
    @mqtt = MQTT.new(mqtt_options)
    @mqtt.logger = logger
  end
end

Instance Method Details

#disconnectObject



90
91
92
# File 'lib/libmagellan/core.rb', line 90

def disconnect
  @mqtt.disconnect
end

#get_message(*args, &block) ⇒ Object



98
99
100
# File 'lib/libmagellan/core.rb', line 98

def get_message(*args, &block)
  @mqtt.get(*args, &block)
end

#get_packet(topic = nil, &block) ⇒ Object



86
87
88
# File 'lib/libmagellan/core.rb', line 86

def get_packet(topic=nil, &block)
  @mqtt.get_packet(topic, &block)
end

#inspectObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/libmagellan/core.rb', line 121

def inspect
  h = @http
  m = @mqtt
  arr = ["\#<#{self.class}: @base_uri=\"#{h.base_uri.to_s}\"",
         "@consumer_key=\"#{h.consumer_key}\"",
         "@client_version=\"#{h.client_version}\"",]
  arr += ["@mqtt_host=\"#{m.host}\"",
          "@mqtt_port=#{m.port}"] if @mqtt
  arr << ">"
  arr.join(", ")
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



117
118
119
# File 'lib/libmagellan/core.rb', line 117

def log_level=(level)
  logger.level = level
end

#loggerObject



103
104
105
106
107
108
109
# File 'lib/libmagellan/core.rb', line 103

def logger
  @logger ||= Proc.new {
    logger = ::Logger.new(STDOUT)
    logger.level = DEFAULT_LOG_LEVEL
    logger
  }.call
end

#logger=(logger_) ⇒ Object



111
112
113
# File 'lib/libmagellan/core.rb', line 111

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

#pingObject



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



94
95
96
# File 'lib/libmagellan/core.rb', line 94

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

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



81
82
83
# File 'lib/libmagellan/core.rb', line 81

def unsubscribe(*args)
  @mqtt.unsubscribe(*args)
end