class SonyCameraRemoteAPI::Client::Main

Main class of client

Public Class Methods

common_options() click to toggle source

Common options for all shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 236
def self.common_options
  option :zoom, type: :numeric, desc: 'Zoom position (0-99)', banner: 'POSITION'
  option :zoom_mode, type: :string, desc: 'Zoom setting', banner: 'MODE'
  option :focus_mode, type: :string, desc: 'Focus mode', banner: 'MODE'
  option :exposure, type: :string, desc: 'Exposure mode', banner: 'MODE'
  option :ev, type: :string, desc: 'Exposure compensation', banner: 'EV'
  option :fnum, type: :string, desc: 'F number', banner: 'NUM'
  option :shutter, type: :string, desc: 'Shutter speed', banner: 'NSEC'
  option :iso, type: :string, desc: 'ISO speed rate', banner: 'NUM'
  option :wb, type: :string, desc: 'White balance mode', banner: 'MODE'
  option :temp, type: :numeric, desc: 'Color temperature', banner: 'K'
  option :scene, type: :string, desc: 'Scene selection', banner: 'MODE'
  option :flip, type: :string, desc: 'Flip', banner: 'MODE'
  option :tv, type: :string, desc: 'TV color system', banner: 'MODE'
  option :ir, type: :string, desc: 'IR remote control', banner: 'MODE'
  option :apo, type: :string, desc: 'Auto power off', banner: 'MODE'
  option :beep, type: :string, desc: 'Beep mode', banner: 'MODE'
end
movie_common_options() click to toggle source

Common options for movie/looprec shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 268
def self.movie_common_options
  option :time, type: :numeric, desc: 'Recording time (sec)', banner: 'NSEC'
  option :format, type: :string, desc: 'Movie Format', banner: 'MODE'
  option :quality, type: :string, desc: 'Movie Quality', banner: 'MODE'
  option :steady, type: :string, desc: 'Steady Mode', banner: 'MODE'
  option :color, type: :string, desc: 'Color setting', banner: 'MODE'
  option :noise, type: :string, desc: 'Wind noise reduction', banner: 'MODE'
  option :audio, type: :string, desc: 'Audio recording', banner: 'MODE'
end
still_common_options() click to toggle source

Common options for still/intervalstill shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 256
def self.still_common_options
  option :track, type: :string, desc: 'Tracking focus', banner: 'MODE'
  option :self, type: :numeric, desc: 'Self timer', banner: 'NSEC'
  option :flash, type: :string, desc: 'Flash mode', banner: 'MODE'
  option :size, type: :string, desc: 'Still size', banner: 'PIXEL'
  option :aspect, type: :string, desc: 'Still aspect', banner: 'MODE'
  option :quality, type: :string, desc: 'Still quality', banner: 'MODE'
  option :postview, type: :string, desc: 'Postview image size', banner: 'PIXEL'
  option :angle, type: :numeric, desc: 'View angle', banner: 'DEGREE'
end

Public Instance Methods

__print_version() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 620
def __print_version
  puts SonyCameraRemoteAPI::VERSION
end
config_file() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 36
def config_file
  options[:config] || GLOBAL_CONFIG_FILE
end
contents() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 571
def contents
  init_camera
  @cam.change_function_to_transfer

  puts 'Retrieving...'
  if options[:datelist]
    dates  = @cam.get_date_list date_count: options[:count]
    num_contents = dates.map { |d| d['contentCount'] }.inject(:+)
    puts "#{dates.size} date folders / #{num_contents} contents found."
    puts "Dates\t\tN-Contents"
    dates.each do |date|
      puts "#{date['title']}\t#{date['contentCount']}"
    end
    return
  end

  if options[:date]
    contents = @cam.get_content_list type: options[:type], date: options[:date], sort: options[:sort], count: options[:count]
  else
    contents = @cam.get_content_list type: options[:type], sort: options[:sort], count: options[:count]
  end

  if contents.blank?
    puts 'No contents!'
  else
    puts "#{contents.size} contents found."
    puts "File name\t\tKind\t\tCreated time\t\tURL"
    contents.each do |c|
      filename = c['content']['original'][0]['fileName']
      kind = c['contentKind']
      ctime = c['createdTime']
      url = c['content']['original'][0]['url']
      puts "#{filename}\t\t#{kind}\t\t#{ctime}\t\t#{url}"
    end
    if options[:transfer]
      @cam.transfer_contents contents
    end
    if options[:delete]
      answer = $terminal.ask('All contents listed above are deleted. Continue? [y/N]') { |q| q.validate = /[yn]/i; q.default = 'n' }
      @cam.delete_contents contents if answer == 'y'
    end
  end
  finalize
end
finalize() click to toggle source

Finalizer

# File lib/sony_camera_remote_api/client/main.rb, line 66
def finalize
  Dir.chdir @cwd
end
get_common_options() click to toggle source

Set common options for all shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 171
def get_common_options
  get_parameter_and_show :ZoomSetting
  get_parameter_and_show :FocusMode
  get_parameter_and_show :ExposureMode
  get_parameter_and_show :ExposureCompensation
  get_parameter_and_show :FNumber
  get_parameter_and_show :ShutterSpeed
  get_parameter_and_show :IsoSpeedRate
  get_parameter_and_show :WhiteBalance
  get_parameter_and_show :SceneSelection
  get_parameter_and_show :FlipSetting
  get_parameter_and_show :TvColorSystem
  get_parameter_and_show :InfraredRemoteControl
  get_parameter_and_show :AutoPowerOff
  get_parameter_and_show :BeepMode
end
get_movie_common_options() click to toggle source

Get common options for movie/looprec shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 223
def get_movie_common_options
  get_parameter_and_show :MovieFileFormat
  get_parameter_and_show :MovieQuality
  get_parameter_and_show :SteadyMode
  get_parameter_and_show :ColorSetting
  get_parameter_and_show :WindNoiseReduction
  get_parameter_and_show :AudioRecording
end
get_parameter_and_show(param_name, **opts) click to toggle source

Get supported/available/current camera parameters and show them

# File lib/sony_camera_remote_api/client/main.rb, line 71
def get_parameter_and_show(param_name, **opts)
  param_title = param_name.to_s.split(/(?=[A-Z])/).join(' ')
  result = @cam.get_parameter! param_name, timeout: 0
  # nil current value means it is unsupported parameter.
  return if result[:current].nil?

  puts "#{param_title}:"
  case param_name
    when :WhiteBalance
      show_WhiteBalance result
    else
      show_normal result
  end
  result
end
get_still_common_options() click to toggle source

Get common options for still/intervalstill shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 202
def get_still_common_options
  get_parameter_and_show :TrackingFocus
  get_parameter_and_show :SelfTimer
  get_parameter_and_show :FlashMode
  get_parameter_and_show :StillSize
  get_parameter_and_show :StillQuality
  get_parameter_and_show :PostviewImageSize
  get_parameter_and_show :ViewAngle
end
init_camera() click to toggle source

Initialize camera instance

# File lib/sony_camera_remote_api/client/main.rb, line 42
def init_camera
  @shelf = Shelf.new config_file
  unless @shelf.select options[:ssid]
    puts "No camera found having SSID #{options[:ssid]}"
    exit 1
  end
  unless @shelf.connect
    puts 'Failed to connect!'
    exit 1
  end

  puts 'Initializing camera...'
  @cam = SonyCameraRemoteAPI::Camera.new @shelf
  puts 'Camera initialization finished.'

  # Change directory if --dir options specified
  @cwd = Dir.getwd
  if options[:dir]
    FileUtils.mkdir_p options[:dir]
    Dir.chdir options[:dir]
  end
end
intstill() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 451
def intstill
  init_camera
  @cam.change_function_to_shoot('intervalstill')

  # Set/Get options
  set_parameter :IntervalTime, options[:interval]
  set_still_common_options
  set_common_options
  if options[:setting]
    get_parameter_and_show :IntervalTime
    get_still_common_options
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  @cam.start_interval_recording
  if options[:time]
    sleep options[:time]
    puts 'Time expired!'
  else
    # continue forever until trapped by SIGINT
    trapped = false
    trap(:INT) { trapped = true }
    puts 'Type C-c (SIGINT) to quit recording.'
    loop do
      break if trapped
      sleep 0.1
    end
    puts 'Trapped!'
    trap(:INT, 'DEFAULT')
  end
  @cam.stop_interval_recording transfer: options[:transfer]
  finalize
end
liveview() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 537
def liveview
  init_camera
  # Set/Get options
  set_common_options
  if options[:setting]
    get_parameter_and_show :LiveviewSize
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  th = @cam.start_liveview_thread(time: options[:time], size: options[:size]) do |img, info|
    filename = "#{img.sequence_number}.jpg"
    File.write filename, img.jpeg_data
    puts "Wrote: #{filename}."
  end
  trap(:INT) { th.kill }
  puts 'Liveview download started. Type C-c (SIGINT) to quit.'
  th.join
  trap(:INT, 'DEFAULT')
  puts 'Finished.'
  finalize
end
looprec() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 495
def looprec
  init_camera
  @cam.change_function_to_shoot('looprec')

  # Set/Get options
  set_parameter :LoopRecTime, options[:loop_time]
  set_movie_common_options
  set_common_options
  if options[:setting]
    get_parameter_and_show :LoopRecTime
    get_movie_common_options
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  @cam.start_loop_recording
  if options[:time]
    sleep(options[:time] * 60)
    puts 'Time expired!'
  else
    # record forever until trapped by SIGINT
    trapped = false
    trap(:INT) { trapped = true }
    puts 'Type C-c (SIGINT) to quit recording.'
    loop do
      break if trapped
      sleep 0.1
    end
    puts 'Trapped!'
    trap(:INT, 'DEFAULT')
  end
  @cam.stop_loop_recording filename: options[:output], transfer: options[:transfer]
  finalize
end
movie() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 409
def movie
  init_camera
  @cam.change_function_to_shoot('movie')

  # Set/Get options
  set_movie_common_options
  set_common_options
  if options[:setting]
    get_movie_common_options
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  @cam.start_movie_recording
  if options[:time]
    sleep options[:time]
    puts 'Time expired!'
  else
    # record forever until trapped by SIGINT
    trapped = false
    trap(:INT) { trapped = true }
    puts 'Type C-c (SIGINT) to quit recording.'
    loop do
      break if trapped
      sleep 0.1
    end
    puts 'Trapped!'
    trap(:INT, 'DEFAULT')
  end
  @cam.stop_movie_recording filename: options[:output], transfer: options[:transfer]
  finalize
end
rapid() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 354
def rapid
  init_camera
  unless @cam.support? :ContShootingMode
    puts 'This camera does not support continuous shooting mode. Exiting...'
    exit 1
  end
  @cam.change_function_to_shoot('still', options[:mode])

  # Set/Get options
  set_parameter :ContShootingMode, options[:mode]
  set_parameter :ContShootingSpeed, options[:speed]
  set_still_common_options
  set_common_options
  if options[:setting]
    get_parameter_and_show :ContShootingMode
    # ContShootingSpeed API will fail when ContShootingMode = 'Single'
    get_parameter_and_show :ContShootingSpeed
    get_still_common_options
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  case options[:mode]
    when 'Single', 'MotionShot', 'Burst'
      @cam.capture_still filename: options[:output], transfer: options[:transfer]
    when 'Continuous', 'Spd Priority Cont.'
      @cam.start_continuous_shooting
      if options[:time]
        sleep options[:time]
        puts 'Time expired!'
      else
        # Continue forever until trapped by SIGINT
        trapped = false
        trap(:INT) { trapped = true }
        puts 'Type C-c (SIGINT) to quit recording.'
        loop do
          break if trapped
          sleep 0.1
        end
        puts 'Trapped!'
        trap(:INT, 'DEFAULT')
      end
      @cam.stop_continuous_shooting prefix: options[:output], transfer: options[:transfer]
  end
  finalize
end
set_common_options() click to toggle source

Set common options for all shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 148
def set_common_options
  set_parameter :ZoomSetting, options[:zoom_mode]
  set_parameter :FocusMode, options[:focus_mode]
  set_parameter :ExposureMode, options[:exposure]
  set_parameter :ExposureCompensation, options[:ev].to_f if options[:ev]
  set_parameter :FNumber, options[:fnum]
  set_parameter :ShutterSpeed, options[:shutter]
  set_parameter :IsoSpeedRate, options[:iso]
  if options[:temp]
    set_parameter :WhiteBalance, whiteBalanceMode: options[:wb], colorTemperature: options[:temp]
  else
    set_parameter :WhiteBalance, whiteBalanceMode: options[:wb]
  end

  set_parameter :SceneSelection, options[:scene]
  set_parameter :FlipSetting, options[:flip]
  set_parameter :TvColorSystem, options[:tv]
  set_parameter :InfraredRemoteControl, options[:ir]
  set_parameter :AutoPowerOff, options[:apo]
  set_parameter :BeepMode, options[:beep]
end
set_movie_common_options() click to toggle source

Set common options for movie/looprec shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 213
def set_movie_common_options
  set_parameter :MovieFileFormat, options[:format]
  set_parameter :MovieQuality, options[:quality]
  set_parameter :SteadyMode, options[:steady]
  set_parameter :ColorSetting, options[:color]
  set_parameter :WindNoiseReduction, options[:noise]
  set_parameter :AudioRecording, options[:audio]
end
set_parameter(param_name, value, **opts) click to toggle source

Set parameter if value is set

# File lib/sony_camera_remote_api/client/main.rb, line 137
def set_parameter(param_name, value, **opts)
  return if value.is_a?(Hash) && value.values.none?
  return unless value
  @cam.set_parameter! param_name, value, timeout: 0
end
set_still_common_options() click to toggle source

Set common options for still/intervalstill shooting modes

# File lib/sony_camera_remote_api/client/main.rb, line 189
def set_still_common_options
  set_parameter :TrackingFocus, options[:track]
  set_parameter :SelfTimer, options[:self]
  set_parameter :FlashMode, options[:flash]
  if options[:aspect] && options[:size]
    set_parameter :StillSize, aspect: options[:aspect], size: options[:size]
  end
  set_parameter :StillQuality, options[:quality]
  set_parameter :PostviewImageSize, options[:postview]
  set_parameter :ViewAngle, options[:angle]
end
show_WhiteBalance(result) click to toggle source

Show for WhiteBalance parameter

# File lib/sony_camera_remote_api/client/main.rb, line 107
def show_WhiteBalance(result)
  array = []
  unless result[:supported]
    # If current value is present but NO supported value, it is immutable.
    result[:supported] = [result[:current]]
  end
  result[:supported].each_with_index do |v, i|
    if v.key?('colorTemperature')
      range = "#{v['colorTemperature'][0]}-#{v['colorTemperature'][-1]}K"
      step = v['colorTemperature'][1] - v['colorTemperature'][0]
      if v['whiteBalanceMode'] == result[:current]['whiteBalanceMode']
        str = "#{v['whiteBalanceMode']}, #{result[:current]['colorTemperature']}K  (#{range}, step=#{step})"
      else
        str = "#{v['whiteBalanceMode']}  (#{range}, step=#{step})"
      end
    else
      str = "#{v['whiteBalanceMode']}"
    end
    if v['whiteBalanceMode'] == result[:current]['whiteBalanceMode']
      array << "  => #{str}"
    elsif result[:available].include? v
      array << "   * #{str}"
    else
      array << "   x #{str}"
    end
  end
  print_array_in_columns(array, 120, 10, 5)
end
show_normal(result) click to toggle source

Show for other parameter

# File lib/sony_camera_remote_api/client/main.rb, line 88
def show_normal(result)
  array = []
  unless result[:supported]
    # If current value is present but NO supported value, it is immutable.
    result[:supported] = [result[:current]]
  end
  result[:supported].each_with_index do |v, i|
    if v == result[:current]
      array << "  => #{v} "
    elsif result[:available].include? v
      array << "   * #{v} "
    else
      array << "   x #{v}"
    end
  end
  print_array_in_columns(array, 120, 10, 5)
end
still() click to toggle source
# File lib/sony_camera_remote_api/client/main.rb, line 287
def still
  init_camera
  @cam.change_function_to_shoot 'still', 'Single'

  # Set/Get options
  set_still_common_options
  set_common_options
  if options[:setting]
    get_still_common_options
    get_common_options
    return
  end

  @cam.act_zoom absolute: options[:zoom]

  # Interval option resembles Intervalrec shooting mode.
  # The advantage is that we can transfer captured stills each time, meanwhile Intervalrec shooting mode cannot.
  # However, the interval time tends to be longer than that of Intervalrec mode.
  if options[:interval]
    if options[:output]
      # Generate sequencial filenames based on --output option
      generator = generate_sequencial_filenames options[:output], 'JPG'
    end

    # Capture forever until trapped by SIGINT
    trapped = false
    trap(:INT) { trapped = true }
    puts "Capturing stills by #{options[:interval]} sec. Type C-c (SIGINT) to quit capturing."
    start_time = Time.now
    loop do
      loop_start = Time.now
      if options[:output]
        @cam.capture_still filename: generator.next, transfer: options[:transfer]
      else
        @cam.capture_still transfer: options[:transfer]
      end
      if trapped
        puts 'Trapped!'
        break
      end
      loop_end = Time.now
      # If total time exceeds, quit capturing.
      if options[:time] && options[:time] < loop_end - start_time
        puts 'Time expired!'
        break
      end
      # Wait until specified interval elapses
      elapsed = loop_end - loop_start
      if options[:interval] - elapsed > 0
        sleep(options[:interval] - elapsed)
      end
    end
    trap(:INT, 'DEFAULT')
  else
    @cam.capture_still filename: options[:output], transfer: options[:transfer]
  end
  finalize
end