Class: SonyCameraRemoteAPI::Client::Main

Inherits:
Thor
  • Object
show all
Includes:
Scripts, Utils
Defined in:
lib/sony_camera_remote_api/client/main.rb

Overview

Main class of client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scripts

connect, connection_script, path, restart_and_connect, root, run_external_command

Methods included from Utils

generate_sequencial_filenames, get_next_file_number, partial_and_unique_match, print_array_in_columns

Class Method Details

.common_optionsObject

Common options for all shooting modes


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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_optionsObject

Common options for movie/looprec shooting modes


268
269
270
271
272
273
274
275
276
# 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_optionsObject

Common options for still/intervalstill shooting modes


256
257
258
259
260
261
262
263
264
265
# 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

Instance Method Details

#__print_versionObject


620
621
622
# File 'lib/sony_camera_remote_api/client/main.rb', line 620

def __print_version
  puts SonyCameraRemoteAPI::VERSION
end

#contentsObject


571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# 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

#intrecObject


451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/sony_camera_remote_api/client/main.rb', line 451

def intrec
  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

#liveviewObject


537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# 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

#looprecObject


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# 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

#movieObject


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# 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

#rapidObject


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# 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

#stillObject


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# 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