class SonyCameraRemoteAPI::CameraAPIGroupManager

Camera API group sublayer class, which is included in Camera API layer class. This class handles API groups that get/set specific parameters of the camera.

Public Class Methods

get_exposure_compensation_step(step) click to toggle source

Convert exposure compensation step of ExposureCompensation API group into the real step.

# File lib/sony_camera_remote_api/camera_api_group_def.rb, line 5
def self.get_exposure_compensation_step(step)
  case step
    when 1 then 0.33
    when 2 then 0.5
    else 0
  end
end
new(camera_api_manager) click to toggle source

Create CameraAPIManager object. @param [CameraAPIManager] camera_api_manager

# File lib/sony_camera_remote_api/camera_api_group.rb, line 127
def initialize(camera_api_manager)
  @api_manager = camera_api_manager
  @api_groups = make_api_group_list camera_api_manager.apis
end

Public Instance Methods

get_current(group_name, **opts) click to toggle source

Get current value of the camera parameter. @param [Symbol] group_name Parameter name @return [Object] Current value @raise APIForbidden, APINotSupported, APINotAvailable, IllegalArgument @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'still', 'Continuous'

value = cam.get_current :ContShootingMode
puts value      #=> Continuous
# File lib/sony_camera_remote_api/camera_api_group.rb, line 144
def get_current(group_name, **opts)
  get_parameter(group_name, available: false, supported: false, **opts)[:current]
end
get_current!(group_name, **opts) click to toggle source

Almost same as get_current, but this method does not raise Exception. @return [Object, nil] Current value or nil if any error occurred. @see get_current @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'still', 'Continuous'

value = cam.get_current! :ContShootingMode
if value
  puts "ContShootingMode is supported, and current value is #{value}"
else
  puts 'ContShootingMode is not supported.'
end
# File lib/sony_camera_remote_api/camera_api_group.rb, line 163
def get_current!(group_name, **opts)
  get_parameter!(group_name, available: false, supported: false, **opts)[:current]
end
get_parameter(group_name, available: true, supported: true, **opts) click to toggle source

Get supported/available/current value of the camera parameter. @param [Symbol] group_name Parameter name @param [Boolean] available If true, available values are included to the returned hash. @param [Boolean] supported If true, supported values are returned to the returned hash. @return [Hash] current/available/supported values @raise APIForbidden, APINotSupported, APINotAvailable, IllegalArgument @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'movie'

result = cam.get_parameter :ExposureMode
puts "current value   : #{result[:current]}"
puts "available values: #{result[:available]}"
puts "supported values: #{result[:supported]}"
# File lib/sony_camera_remote_api/camera_api_group.rb, line 183
def get_parameter(group_name, available: true, supported: true, **opts)
  result = { current: nil, available: nil, supported: nil }
  begin
    grp = search_group group_name
  rescue APIForbidden, APINotSupported => e
    raise e.class.new(result), e.message
  end
  condition = grp.start_condition(@api_manager)
  begin
    result.merge! grp.current_value(@api_manager, condition, **opts)
  rescue APINotAvailable, IllegalArgument => e
    raise e.class.new(result), e.message
  end
  begin
    # Timeout is set shorter than usual for getting hardware-affected parameter.
    result.merge! grp.available_values(@api_manager, condition, timeout: 1, **opts) if available
    result.merge! grp.supported_values(@api_manager, condition, timeout: 1, **opts) if supported
  rescue APINotAvailable, IllegalArgument => e
    # Comes here if the parameter is hardware-affected.
  end
  result
end
get_parameter!(group_name, **opts) click to toggle source

Almost same as get_parameter, but this method does not raise Exception. @return [Hash] current/available/supported values. If any error occurs, the value that cannot get become nil. @see get_parameter @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'still'

result = cam.get_parameter! :ExposureMode
if result[:current]
  puts 'ExposureMode is supported.'
  if result[:available] && result[:supported]
    puts 'And you can change the value by #set_parameter.'
  else
    puts 'And you can change the value by the hardware dial or switch (NOT by #set_parameter).'
  end
else
  puts 'ExposureMode is not supported!'
end
# File lib/sony_camera_remote_api/camera_api_group.rb, line 226
def get_parameter!(group_name, **opts)
  get_parameter(group_name, **opts)
rescue APIForbidden, APINotSupported, APINotAvailable, IllegalArgument => e
  log.error e.message
  e.object
rescue HTTPClient::BadResponseError => e
  log.error e.message
end
parameters() click to toggle source

Get an array of supported camera parameters. @return [Array<String>] supported camera parameters

# File lib/sony_camera_remote_api/camera_api_group.rb, line 322
def parameters
  @api_groups.keys
end
set_parameter(group_name, value, *args, **opts) click to toggle source

Set the camera parameter to the given value. @param [Symbol] group_name Parameter name @param [Object] value New value to be set @return [Hash] current/available/old values after setting parameter.

If given value is equal to current value, available/old values become nil.

@raise APIForbidden, APINotSupported, APINotAvailable, IllegalArgument @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'still', 'Single'

result = cam.set_parameter :FlashMode, 'slowSync'
if result[:old]
  puts "The value is changed from '#{result[:old]}' to '#{result[:current]}'."
else
  puts 'The value is not changed.'
end
puts "current value   : #{result[:current]}"
puts "available values: #{result[:available]}"
puts "old value       : #{result[:old]}"
# File lib/sony_camera_remote_api/camera_api_group.rb, line 256
def set_parameter(group_name, value, *args, **opts)
  result = { current: nil, available: nil, old: nil }
  begin
    grp = search_group group_name
  rescue APIForbidden, APINotSupported => e
    raise e.class.new(result), e.message
  end

  condition = grp.start_condition(@api_manager)
  begin
    value = grp.preprocess_value(value, args, condition)
    # If value is equal to current value, do nothing.
    result.merge! grp.current_value(@api_manager, condition, **opts)
    if grp.eq_current? value, result[:current], condition
      return result
    end
    # If not, check if the value is available.
    result.merge! grp.available_values(@api_manager, condition, **opts)
    if grp.is_available? value, result[:available], condition
      # Save current value and call set API.
      result[:old] = result[:current]
      result.merge! grp.set_value(@api_manager, value, result[:available], condition)
    else
      # If the value is not available, raise error.
      raise IllegalArgument.new, "The value '#{value}' is not available for parameter '#{group_name}'. current: #{result[:current]}, available: #{result[:available]}"
    end
  rescue APINotAvailable, IllegalArgument => e
    raise e.class.new(result), e.message
  end
  result
end
set_parameter!(group_name, value, **opts) click to toggle source

Almost same as set_parameter, but this method does not raise Exception. @return [Hash] current/available/old values after setting parameter.

If given value is equal to current value, available/old values become nil.
If any error occurs, the values that cannot get become nil.

@see set_parameter @example

# Initialize
cam = SonyCameraRemoteAPI::Camera.new
cam.change_function_to_shoot 'still', 'Single'

result = cam.set_parameter! :FlashMode, 'on'
if result[:current]
  puts 'FlashMode is supported.'
  if result[:current] == 'on'
    puts "And successfully set the value to '#{result[:current]}'."
  else
    puts 'But cannot set the value.'
  end
else
  puts 'FlashMode is not supported!'
end
# File lib/sony_camera_remote_api/camera_api_group.rb, line 310
def set_parameter!(group_name, value, **opts)
  set_parameter(group_name, value, **opts)
rescue APIForbidden, APINotSupported, APINotAvailable, IllegalArgument => e
  log.error e.message
  e.object
rescue HTTPClient::BadResponseError => e
  log.error e.message
end
support_group?(group_name) click to toggle source

Returns whether the parameter is supported or not. @return [Boolean] true if the parameter is supported, false otherwise.

# File lib/sony_camera_remote_api/camera_api_group.rb, line 329
def support_group?(group_name)
  @api_groups.key? group_name
end

Private Instance Methods

make_api_group_list(apis) click to toggle source

Make API Group hash list from APIInfo list. @param [Array<APIInfo>] apis @return [Hash<Symbol, APIGroup>]

# File lib/sony_camera_remote_api/camera_api_group.rb, line 339
def make_api_group_list(apis)
  api_names = apis.values.map { |a| a.name }
  setters = api_names.select { |k| k =~ /^getAvailable/ }
  api_groups = {}
  setters.map do |s|
    group_name = s.gsub(/^getAvailable/, '')
    members = [ "get#{group_name}", "getAvailable#{group_name}", "getSupported#{group_name}" ]
    result = members.map { |m| api_names.include?(m) }
    api_groups[group_name.to_sym] = @@api_groups_all[group_name.to_sym] if result.all?
  end
  api_groups
end
search_group(group_name) click to toggle source

@param [Symbol] group_name

# File lib/sony_camera_remote_api/camera_api_group.rb, line 353
def search_group(group_name)
  if support_group? group_name
    return @api_groups[group_name]
  else
    raise APINotSupported.new, "Parameter '#{group_name}' is not supported!"
  end
end