Sha256: 33e8edca59ad995dd21b283de8c2972901ebbee13333138350e85d1d48748ce1

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'methadone'
require 'fileutils'
require 'table_print'

module ElectricEye
  class ConfigEye

    include Methadone::CLILogging
    
    # Check the directory and if it doesn't exist create it.
    def self.check_dir
      FileUtils.mkdir_p(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
    end

    # Check that the config file exists.
    def self.load
      # Check if we have a config CONFIG_FILE
      ConfigEye.check_dir
      if File.exist?(CONFIG_FILE)
        Construct.load File.read(CONFIG_FILE)
      else
        Construct.new({duration: 600, path: '~/recordings', cameras: []})
      end
    end

    # Save the config file
    def save()
      File.open(CONFIG_FILE, 'w'){ |f| f.write config.to_yaml } # Store
    end

    # Add camera
    def add_camera(camera, url)
      @config.cameras.push({name: camera, url: url})
      save
      info "Camera added"
    end

    # Remove camera
    def remove_camera(camera)
      record = @config.cameras.bsearch{ |c| c[:name] == camera }
      if record
        @config.cameras.delete(record)
        save
      end
      info "Camera removed"
    end

    # List cameras in setup
    def list_cameras
      info "Cameras"
      tp @config.cameras, :name, :url => {width: 120}
    end

    # Set duration
    def set_duration(seconds)
      @config.duration = seconds.to_i
      save
      info "Duration set to #{seconds} seconds"
    end

    # Set path
    def set_path(dir)
      @config.path = dir
      save
      info "Path set to #{dir}"
    end

    # Initialise the method.
    attr_reader :config
    def initialize
      @config = ConfigEye.load
      save
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
electric_eye-0.0.3 lib/electric_eye/config_eye.rb
electric_eye-0.0.2 lib/electric_eye/config_eye.rb
electric_eye-0.0.1 lib/electric_eye/config_eye.rb