Class: WSLight::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ws_light/config.rb

Overview

Reads config file and parses command parameters

Constant Summary collapse

CONFIG_FILE =
'/etc/ws_light.conf'.freeze
DEFAULT_OPTIONS =
{
  'pin_right' => 23,
  'pin_left' => 24,
  'log_file' => '/var/log/motion.log',
  'track_motion_in_log' => true,
  'debug' => false,
  'sensor_right_name' => 'motion_right',
  'sensor_left_name' => 'motion_left',
  'sensor_right_description' => 'Motion sensor right',
  'sensor_left_description' => 'Motion sensor left',
  'hass_integration' => false,
  'hass_url' => '',
  'hass_api_password' => ''
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



24
25
26
27
# File 'lib/ws_light/config.rb', line 24

def initialize
  @config = DEFAULT_OPTIONS.merge(yaml_options).merge(command_line_options)
  store_options
end

Instance Method Details

#command_line_optionsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ws_light/config.rb', line 47

def command_line_options
  options = {}
  OptionParser.new do |opts|
    opts.banner = 'Usage: ws_light [options]'

    opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
      options['verbose'] = v
    end

    opts.on('-l NUMBER', '--left-pin NUMBER', 'Pin to which the left motion detector is connected') do |number|
      options['pin_left'] = number
    end

    opts.on('-r NUMBER', '--right-pin NUMBER', 'Pin to which the right motion detector is connected') do |number|
      options['pin_right'] = number
    end

    opts.on('-o PATH', '--log PATH', 'path to the log file') do |log_file|
      options['log_file'] = log_file
    end

    opts.on('--quiet-log', 'do not log detected motions') do
      options['track_motion_in_log'] = false
    end

    opts.on('--debug', 'output all log messages to stdout, too') do
      options['debug'] = true
    end
  end.parse!
  options
end

#parseObject



35
36
37
# File 'lib/ws_light/config.rb', line 35

def parse
  @config
end

#store_optionsObject



29
30
31
32
33
# File 'lib/ws_light/config.rb', line 29

def store_options
  File.open(CONFIG_FILE, 'w') do |file|
    file.puts @config.to_yaml
  end
end

#yaml_optionsObject



39
40
41
42
43
44
45
# File 'lib/ws_light/config.rb', line 39

def yaml_options
  if File.exist? CONFIG_FILE
    ::YAML.safe_load(File.read(CONFIG_FILE))
  else
    {}
  end
end