Class: Cocoro::Mqtt

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoro/mqtt.rb,
lib/cocoro/mqtt/version.rb

Overview

A bridge between Cocoro Air API and MQTT

Constant Summary collapse

DEFAULT_INTERVAL =

seconds

30
VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocoro_client:, mqtt_client:, interval: DEFAULT_INTERVAL, logger: nil) ⇒ Mqtt

Returns a new instance of Mqtt.


15
16
17
18
19
20
21
22
23
# File 'lib/cocoro/mqtt.rb', line 15

def initialize(cocoro_client:, mqtt_client:, interval: DEFAULT_INTERVAL, logger: nil)
  @cocoro = cocoro_client
  @mqtt = mqtt_client
  @interval = interval
  @logger = logger || Logger.new($stdout).tap do |l|
    l.level = Logger::WARN
  end
  @mutex = Mutex.new
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.


13
14
15
# File 'lib/cocoro/mqtt.rb', line 13

def logger
  @logger
end

Instance Method Details

#startObject


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cocoro/mqtt.rb', line 25

def start
  @mqtt.connect do |client|
    air_cleaners.each do |device|
      subscribe_to_device_command_topics(device, client)
      make_device_discoverable(device, client)
    end
    subscriber = Thread.new { keep_handling_commands(client) }
    publisher = Thread.new { keep_publishing_state_updates(client) }
    [subscriber, publisher].each(&:join)
  end
end