Class: SonyCameraRemoteAPI::Shelf

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

Overview

Class for handling multiple camera’s connection configurations.

Constant Summary collapse

GLOBAL_CONFIG_FILE =

Default config file saved in home directory.

File.expand_path '~/.sonycam.shelf'

Instance Method Summary collapse

Methods included from Utils

generate_sequencial_filenames, get_next_file_number, partial_and_unique_match, print_array_in_columns

Constructor Details

#initialize(config_file = GLOBAL_CONFIG_FILE) ⇒ Shelf

Create CameraShelf object.

Parameters:

  • config_file (String) (defaults to: GLOBAL_CONFIG_FILE)

    The path of config file.



17
18
19
20
# 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

Instance Method Details

#add(ssid, pass, interface, overwrite: false) ⇒ Boolean

Add a camera config.

Parameters:

  • overwrite (Boolean) (defaults to: false)

    Overwrite if the same SSID’s config is already added.

Returns:

  • (Boolean)

    true if successfully added, false otherwise.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Boolean

Add a camera config, and select it

Parameters:

  • overwrite (Boolean) (defaults to: false)

    Overwrite if the same SSID’s config is already added.

Returns:

  • (Boolean)

    true if successfully added, false otherwise.



121
122
123
124
# 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) ⇒ Boolean

Connect to the camera. If SSID is not given, default camera is used.

Parameters:

  • ssid (String) (defaults to: nil)

    SSID

Returns:

  • (Boolean)

    true if successfully connected, false otherwise.



169
170
171
172
173
174
175
176
# 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) ⇒ Boolean

Get endpoint information to a camera config.

Returns:

  • (Boolean)

    true if successfully set endpoints, false otherwise.



142
143
144
145
146
147
148
149
# 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) ⇒ Hash?

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.

Parameters:

  • ssid (String) (defaults to: nil)

    SSID

Returns:

  • (Hash, nil)

    A camera config hash



28
29
30
31
32
33
34
# 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_allArray<Hash>

Get all camera configs.

Returns:

  • (Array<Hash>)

    An array of camera config hashes



49
50
51
# File 'lib/sony_camera_remote_api/shelf.rb', line 49

def get_all
  @config['camera']
end

#get_by_index(index) ⇒ Hash?

Get a camera config by index.

Parameters:

  • index (String)

    Index

Returns:

  • (Hash, nil)

    A camera config hash



40
41
42
43
44
# 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) ⇒ Boolean

Restart interface, and then connect to the camera. If SSID is not given, default camera is used.

Parameters:

  • ssid (String) (defaults to: nil)

    SSID

Returns:

  • (Boolean)

    true if successfully connected, false otherwise.



183
184
185
186
187
188
189
190
# 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) ⇒ Boolean

Remove a camera config.

Returns:

  • (Boolean)

    true if successfully removed, false otherwise.



75
76
77
78
79
80
81
82
# 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_allBoolean

Remove all camera configs.

Returns:

  • (Boolean)

    true if successfully removed, false otherwise.



100
101
102
# File 'lib/sony_camera_remote_api/shelf.rb', line 100

def remove_all
  create
end

#remove_by_index(index) ⇒ Hash?

Remove a camera config by index.

Parameters:

  • index (String)

    Index

Returns:

  • (Hash, nil)

    A camera config hash



88
89
90
91
92
93
94
95
# 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) ⇒ Boolean

Select a camera config as default.

Returns:

  • (Boolean)

    true if successfully set default camera, false otherwise.



107
108
109
110
111
112
113
114
115
# 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) ⇒ Boolean

Set endpoint information to a camera config.

Returns:

  • (Boolean)

    true if successfully set endpoints, false otherwise.



129
130
131
132
133
134
135
136
137
# 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) ⇒ Boolean

Set interface by which the camera is connected.

Returns:

  • (Boolean)

    true if successfully set default camera, false otherwise.



154
155
156
157
158
159
160
161
162
# 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