Class: E3DCMqtt::App

Inherits:
Object
  • Object
show all
Defined in:
lib/e3dc_mqtt/app.rb

Overview

Wires E3DC and MQTT together and runs the publish loop with aligned tickers. “Let it crash” — on unrecoverable errors, raises the mapped ExitError and main translates that to a specific exit code.

Defined Under Namespace

Classes: ExitError

Constant Summary collapse

EXIT_CONFIG =
1
EXIT_E3DC =
4
EXIT_MQTT =
5
EXIT_SYSTEM_INFO =
6
EXIT_STATUS =
10
EXIT_STATS =
11
EXIT_BATTERIES =
12

Instance Method Summary collapse

Constructor Details

#initialize(config, logger: default_logger(config.log_level)) ⇒ App

Returns a new instance of App.



29
30
31
32
33
# File 'lib/e3dc_mqtt/app.rb', line 29

def initialize(config, logger: default_logger(config.log_level))
  @config = config
  @logger = logger
  @stop = false
end

Instance Method Details

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/e3dc_mqtt/app.rb', line 35

def run
  install_signal_handlers

  e3dc = connect_e3dc
  mqtt = connect_mqtt(e3dc.topic_prefix)

  @logger.info "model=#{e3dc.model} serial=#{e3dc.serial_number} batteries=#{e3dc.batteries.size}"

  publish_system_info(e3dc, mqtt)

  # Initial publish before starting the clock.
  publish_status(e3dc, mqtt)
  publish_statistics(e3dc, mqtt)
  publish_batteries(e3dc, mqtt)

  # Align to the next tick boundary so timestamps fall on clean seconds.
  sleep_until_next_tick(@config.e3dc.interval)

  loop_with_tickers(e3dc, mqtt)
rescue ExitError
  raise
rescue Interrupt
  @logger.info "interrupted"
ensure
  mqtt&.disconnect
  e3dc&.close
end