Class: OlympusCamera

Inherits:
Object
  • Object
show all
Defined in:
lib/olympus-camera.rb,
lib/olympus-camera/any.rb,
lib/olympus-camera/version.rb,
lib/olympus-camera/commands_parser.rb

Defined Under Namespace

Modules: CommandsParser Classes: APIError

Constant Summary collapse

DEFAULT_HEADERS =
{
  "Connection" => "close",
  "User-Agent" => "OlympusCameraKit",
}
DEFAULT_TIMEOUT =
{
  open: 1,
  read: 10,
}
ANY =
:__any__
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commandlist_xml: nil, api_host: "http://192.168.0.10") ⇒ OlympusCamera

Returns a new instance of OlympusCamera.



25
26
27
28
29
30
31
32
# File 'lib/olympus-camera.rb', line 25

def initialize(commandlist_xml: nil, api_host: "http://192.168.0.10")
  self.open_timeout = DEFAULT_TIMEOUT[:open]
  self.read_timeout = DEFAULT_TIMEOUT[:read]

  self.api_host = api_host
  parsed_commands = CommandsParser.parse(commandlist_xml || self.raw_get_commandlist.body)
  self.generate_api! parsed_commands
end

Instance Attribute Details

#api_hostObject

Returns the value of attribute api_host.



22
23
24
# File 'lib/olympus-camera.rb', line 22

def api_host
  @api_host
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



23
24
25
# File 'lib/olympus-camera.rb', line 23

def api_version
  @api_version
end

#commandsObject (readonly)

Returns the value of attribute commands.



23
24
25
# File 'lib/olympus-camera.rb', line 23

def commands
  @commands
end

#open_timeoutObject

Returns the value of attribute open_timeout.



22
23
24
# File 'lib/olympus-camera.rb', line 22

def open_timeout
  @open_timeout
end

#read_timeoutObject

Returns the value of attribute read_timeout.



22
23
24
# File 'lib/olympus-camera.rb', line 22

def read_timeout
  @read_timeout
end

#support_funcsObject (readonly)

Returns the value of attribute support_funcs.



23
24
25
# File 'lib/olympus-camera.rb', line 23

def support_funcs
  @support_funcs
end

Instance Method Details

#all_imagesObject



58
59
60
61
62
63
64
65
# File 'lib/olympus-camera.rb', line 58

def all_images
  pathes = parse_filelist self.get_imglist({ "DIR" => "/DCIM" })
  pathes.map do |path|
    parse_filelist(
      self.get_imglist({ "DIR" => path.join("/") })
    ).map { |d| d.join("/") }
  end.flatten
end

#api_listObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/olympus-camera.rb', line 46

def api_list
  @commands.map do |name, command_args|
    queries = command_args[:queries]
    if (queries.length > 0)
      params = queries.map { |query| query.to_h }
      [name.to_s, params]
    else
      [name.to_s]
    end
  end.sort
end

#cgi_request(command:, method:, query: nil, headers: {}, raw_result: false) ⇒ Object



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/olympus-camera.rb', line 88

def cgi_request(command:, method:, query: nil, headers: {}, raw_result: false)
  uri = URI.parse(api_host)
  uri.path = "/#{command}.cgi"
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = self.open_timeout
  http.read_timeout = self.read_timeout
  http.use_ssl = uri.scheme === "https"

  headers = DEFAULT_HEADERS.merge(headers)
  path = uri.path

  if method.to_sym == :post
    req = Net::HTTP::Post.new(path)
    if query
      req.set_form_data query
    end
  elsif method.to_sym == :get
    if query
      path = path + "?" + URI.encode_www_form(query)
    end
    req = Net::HTTP::Get.new(path)
  else
    raise ArgumentError.new("method: #{method} is unknown.")
  end

  req.initialize_http_header(headers)
  res = http.request(req)

  if raw_result
    return res
  end

  if res.code.to_i >= 400
    raise APIError.new("API Error: " + res.inspect)
  else
    if res.content_type&.include?("/xml") && res.body.length > 10
      XmlSimple.xml_in res.body
    else
      res.body
    end
  end
end

#generate_api!(parsed_commands) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/olympus-camera.rb', line 34

def generate_api!(parsed_commands)
  @api_version = parsed_commands[:api_version]
  @support_funcs = parsed_commands[:support_funcs]
  @commands = parsed_commands[:commands]

  @commands.each do |name, command_args|
    define_singleton_method(name) do |query = nil, headers: {}, raw_result: false|
      cgi_request(command: name, method: command_args[:method], query: query, headers: headers, raw_result: raw_result)
    end
  end
end

#get_image(path_or_params) ⇒ Object



67
68
69
70
71
72
# File 'lib/olympus-camera.rb', line 67

def get_image(path_or_params)
  path = path_or_params.kind_of?(Hash) ? path_or_params["DIR"] : path_or_params
  uri = URI.parse(api_host)
  uri.path = path
  uri.read
end

#parse_filelist(source) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/olympus-camera.rb', line 74

def parse_filelist(source)
  if source.match /^VER_100/
    source.split(/\r?\n/)[1..-1].map do |line|
      line.split(",")[0, 2]
    end
  else
    []
  end
end

#raw_get_commandlistObject



84
85
86
# File 'lib/olympus-camera.rb', line 84

def raw_get_commandlist
  cgi_request(command: :get_commandlist, method: :get, raw_result: true)
end