Class: WSLight::Config
- Inherits:
-
Object
- Object
- WSLight::Config
- 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
- #command_line_options ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #parse ⇒ Object
- #store_options ⇒ Object
- #yaml_options ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
24 25 26 27 |
# File 'lib/ws_light/config.rb', line 24 def initialize @config = DEFAULT_OPTIONS.merge().merge() end |
Instance Method Details
#command_line_options ⇒ Object
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 = {} OptionParser.new do |opts| opts. = 'Usage: ws_light [options]' opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v| ['verbose'] = v end opts.on('-l NUMBER', '--left-pin NUMBER', 'Pin to which the left motion detector is connected') do |number| ['pin_left'] = number end opts.on('-r NUMBER', '--right-pin NUMBER', 'Pin to which the right motion detector is connected') do |number| ['pin_right'] = number end opts.on('-o PATH', '--log PATH', 'path to the log file') do |log_file| ['log_file'] = log_file end opts.on('--quiet-log', 'do not log detected motions') do ['track_motion_in_log'] = false end opts.on('--debug', 'output all log messages to stdout, too') do ['debug'] = true end end.parse! end |
#parse ⇒ Object
35 36 37 |
# File 'lib/ws_light/config.rb', line 35 def parse @config end |
#store_options ⇒ Object
29 30 31 32 33 |
# File 'lib/ws_light/config.rb', line 29 def File.open(CONFIG_FILE, 'w') do |file| file.puts @config.to_yaml end end |
#yaml_options ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/ws_light/config.rb', line 39 def if File.exist? CONFIG_FILE ::YAML.safe_load(File.read(CONFIG_FILE)) else {} end end |