class SonyCameraRemoteAPI::Shelf

Class for handling multiple camera's connection configurations.

Constants

GLOBAL_CONFIG_FILE

Default config file saved in home directory.

Public Class Methods

new(config_file = GLOBAL_CONFIG_FILE) click to toggle source

Create CameraShelf object. @param [String] config_file The path of config file.

# File lib/sony_camera_remote_api/shelf.rb, line 17
def initialize(config_file = GLOBAL_CONFIG_FILE)
  @config_file = File.expand_path config_file
  read_or_create
end

Public Instance Methods

add(ssid, pass, interface, overwrite: false) click to toggle source

Add a camera config. @param [Boolean] overwrite Overwrite if the same SSID's config is already added. @return [Boolean] true if successfully added, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 57
def add(ssid, pass, interface, overwrite: false)
  # If input SSID is already registered, ask user to overwrite
  same_one = @config['camera'].find { |n| n['ssid'] == ssid }
  if same_one && !overwrite
    false
  else
    @config['camera'].delete_if { |c| c['ssid'] == ssid }
    @config['camera'] << { 'ssid' => ssid, 'pass' => pass, 'interface' => interface }
    if @config['camera'].size == 1
      @config['default'] = ssid
    end
    write
  end
end
add_and_select(ssid, pass, interface, overwrite: false) click to toggle source

Add a camera config, and select it @param [Boolean] overwrite Overwrite if the same SSID's config is already added. @return [Boolean] true if successfully added, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 121
def add_and_select(ssid, pass, interface, overwrite: false)
  add ssid, pass, interface, overwrite: overwrite
  select ssid
end
connect(ssid = nil) click to toggle source

Connect to the camera. If SSID is not given, default camera is used. @param [String] ssid SSID @return [Boolean] true if successfully connected, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 169
def connect(ssid = nil)
  entry = get(ssid)
  if entry
    Scripts.connect entry['ssid'], entry['pass'], entry['interface']
  else
    false
  end
end
ep(ssid = nil) click to toggle source

Get endpoint information to a camera config. @return [Boolean] true if successfully set endpoints, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 142
def ep(ssid = nil)
  entry = get(ssid)
  if entry
    entry['endpoints']
  else
    nil
  end
end
get(ssid = nil) click to toggle source

Get a camera config by SSID. You can use a partial string as long as it is unique. If SSID is not given, get the default camera config. @param [String] ssid SSID @return [Hash, nil] A camera config hash

# File lib/sony_camera_remote_api/shelf.rb, line 28
def get(ssid = nil)
  if ssid.nil?
    get_default
  else
    get_unique(ssid)
  end
end
get_all() click to toggle source

Get all camera configs. @return [Array<Hash>] An array of camera config hashes

# File lib/sony_camera_remote_api/shelf.rb, line 49
def get_all
  @config['camera']
end
get_by_index(index) click to toggle source

Get a camera config by index. @param [String] index Index @return [Hash, nil] A camera config hash

# File lib/sony_camera_remote_api/shelf.rb, line 40
def get_by_index(index)
  if index.between? 0, @config['camera'].size - 1
    @config['camera'][index]
  end
end
reconnect(ssid = nil) click to toggle source

Restart interface, and then connect to the camera. If SSID is not given, default camera is used. @param [String] ssid SSID @return [Boolean] true if successfully connected, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 183
def reconnect(ssid = nil)
  entry = get(ssid)
  if entry
    Scripts.restart_and_connect entry['ssid'], entry['pass'], entry['interface']
  else
    false
  end
end
remove(ssid) click to toggle source

Remove a camera config. @return [Boolean] true if successfully removed, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 75
def remove(ssid)
  entry = get_unique(ssid)
  if @config['camera'].delete entry
    write
  else
    false
  end
end
remove_all() click to toggle source

Remove all camera configs. @return [Boolean] true if successfully removed, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 100
def remove_all
  create
end
remove_by_index(index) click to toggle source

Remove a camera config by index. @param [String] index Index @return [Hash, nil] A camera config hash

# File lib/sony_camera_remote_api/shelf.rb, line 88
def remove_by_index(index)
  if index.between? 0, @config['camera'].size - 1
    @config['camera'].delete_at index
    write
  else
    false
  end
end
select(ssid) click to toggle source

Select a camera config as default. @return [Boolean] true if successfully set default camera, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 107
def select(ssid)
  entry = get(ssid)
  if entry
    @config['default'] = entry['ssid']
    write
  else
    false
  end
end
set_ep(endpoints, ssid = nil) click to toggle source

Set endpoint information to a camera config. @return [Boolean] true if successfully set endpoints, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 129
def set_ep(endpoints, ssid = nil)
  entry = get(ssid)
  if entry
    entry['endpoints'] = endpoints
    write
  else
    false
  end
end
set_if(interface, ssid = nil) click to toggle source

Set interface by which the camera is connected. @return [Boolean] true if successfully set default camera, false otherwise.

# File lib/sony_camera_remote_api/shelf.rb, line 154
def set_if(interface, ssid = nil)
  entry = get(ssid)
  if entry
    entry['interface'] = interface
    write
  else
    false
  end
end

Private Instance Methods

create() click to toggle source

@return [Boolean]

# File lib/sony_camera_remote_api/shelf.rb, line 221
def create
  @config = { 'camera' => [] }
  write
end
get_default() click to toggle source

Get the default camera config. @return [Hash, nil] A camera config hash

# File lib/sony_camera_remote_api/shelf.rb, line 207
def get_default
  @config['camera'].find { |c| c['ssid'] == @config['default'] }
end
get_unique(ssid) click to toggle source

Get camera whose SSID uniquely matches with specified string. @param [String] ssid SSID @return [Hash, nil] A camera config hash

# File lib/sony_camera_remote_api/shelf.rb, line 198
def get_unique(ssid)
  complete_ssid, num = partial_and_unique_match(ssid, @config['camera'].map { |c| c['ssid'] })
  return unless num == 1
  @config['camera'].find { |c| c['ssid'] == complete_ssid }
end
read() click to toggle source

@return [Boolean]

# File lib/sony_camera_remote_api/shelf.rb, line 227
def read
  if File.exists? @config_file
    @config = YAML.load_file(@config_file)
  end
  @config.present? ? true : false
end
read_or_create() click to toggle source

@return [Boolean]

# File lib/sony_camera_remote_api/shelf.rb, line 212
def read_or_create
  unless read
    create
  else
    false
  end
end
write() click to toggle source

@return [Boolean]

# File lib/sony_camera_remote_api/shelf.rb, line 235
def write
  open(@config_file, 'w') do |e|
    YAML.dump(@config, e)
  end
  true
end