Class: SonyCameraRemoteAPI::Client::ShelfCmd

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

Overview

‘shelf’ subcommand class for managing camera connection

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

Instance Method Details

#add(ssid, pass, interface) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 53

def add(ssid, pass, interface)
  read_or_create_shelf
  if @shelf.get_all.find { |c| c['ssid'] == ssid }
    # If SSID is duplicacted, ask user to overwrite or not.
    answer = $terminal.ask('SSID duplicated! Do you want to overwrite? ') { |q| q.validate = /[yn]/i; q.default = 'n' }
    if answer == 'y'
      @shelf.add ssid, pass, interface, overwrite: true
    else
      puts 'Entry not changed.'
      invoke :list, [], options
      return
    end
  else
    @shelf.add ssid, pass, interface
  end

  # Ask user to set default or not.
  if @shelf.get
    answer = $terminal.ask('Do you want set this camera as default? ') { |q| q.validate = /[yn]/i; q.default = 'y' }
    if answer == 'y'
      @shelf.select ssid
    end
  else
    @shelf.select ssid
  end

  invoke :list, [], options
end

#connectObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 166

def connect
  read_or_create_shelf

  if options[:ssid]
    camera = @shelf.get(options[:ssid])
    unless camera
      puts 'ERROR: Specified SSID is not found or too ambigous!'
      return
    end
  else
    camera = @shelf.get
    unless camera
      puts 'ERROR: Default camera is not selected yet!'
      return
    end
  end

  puts 'Camera to connect:'
  puts "  - SSID     : #{camera['ssid']}"
  puts "  - pass     : #{camera['pass']}"
  puts "  - inteface : #{camera['interface']}"

  # Connect to camera by external script
  if options[:restart]
    result = @shelf.reconnect camera['ssid']
  else
    result = @shelf.connect camera['ssid']
  end
  unless result
    puts 'Failed to connect!'
    exit 1
  end
end

#interface(if_name, id_or_ssid = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 146

def interface(if_name, id_or_ssid = nil)
  read_or_create_shelf

  if id_or_ssid
    specified = get_id_or_ssid id_or_ssid
  else
    specified = @shelf.get
    unless specified
      puts 'ERROR: Default camera is not selected yet!'
      return
    end
  end
  @shelf.set_if if_name, specified['ssid'] if specified
  invoke :list, [], options
end

#listObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 84

def list
  read_or_create_shelf
  cameras = @shelf.get_all
  default = @shelf.get

  # No camera has been added, exit with a message.
  if cameras.empty?
    puts 'No camera yet!'
    puts "Use 'sonycam shelf add' to add a new camera config."
    return
  end

  # If default camera is not set, show message.
  if default
    default_ssid = default['ssid']
  else
    puts 'Default camera is not selected yet!'
    puts "Use 'sonycam shelf use' command to select camera to use as default."
    default_ssid = nil
  end

  # Selected camera is signed by allow
  cameras.each_with_index do |v, i|
    if v['ssid'] == default_ssid
      puts "=> #{i}: SSID      : #{v['ssid']} "
    else
      puts "   #{i}: SSID      : #{v['ssid']} "
    end
    puts "      Password  : #{v['pass']} "
    puts "      Interface : #{v['interface']} "
  end
end

#remove(id_or_ssid = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 120

def remove(id_or_ssid = nil)
  read_or_create_shelf
  if options[:all]
    @shelf.remove_all
    return
  end

  specified = get_id_or_ssid id_or_ssid
  @shelf.remove specified['ssid'] if specified

  invoke :list, [], options
end

#select(id_or_ssid) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/sony_camera_remote_api/client/shelf.rb', line 135

def select(id_or_ssid)
  read_or_create_shelf

  specified = get_id_or_ssid id_or_ssid
  @shelf.select specified['ssid'] if specified

  invoke :list, [], options
end