Module: SonyCameraRemoteAPI::SSDP

Included in:
Camera
Defined in:
lib/sony_camera_remote_api/ssdp.rb

Overview

Module providing SSDP function to discover camera (Device discovery)

Defined Under Namespace

Classes: DeviceDescriptionInvalid, DeviceNotFound

Constant Summary collapse

SSDP_SEARCH_TARGET =

The search target for Sony camera (fixed)

'urn:schemas-sony-com:service:ScalarWebAPI:1'.freeze
SSDP_SEARCH_RETRY =

Retrying limit for SSDP search

4
SSDP_RETRY_INTERVAL =

Retrying interval for SSDP search

5

Instance Method Summary collapse

Instance Method Details

#parse_device_description(dd) ⇒ Object

Parse device description and get endpoint URLs


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sony_camera_remote_api/ssdp.rb', line 51

def parse_device_description(dd)
  dd_xml = Nokogiri::XML(dd)
  raise DeviceDescriptionInvalid if dd_xml.nil?
  dd_xml.remove_namespaces!
  camera_name = dd_xml.css('device friendlyName').inner_text
  services = dd_xml.css('device X_ScalarWebAPI_Service')
  endpoints = {}
  services.each do |sv|
    service_type = sv.css('X_ScalarWebAPI_ServiceType').inner_text
    endpoints[service_type] = File.join(sv.css('X_ScalarWebAPI_ActionList_URL').inner_text, service_type)
  end
  # endpoints['liveview'] = dd_xml.css('device X_ScalarWebAPI_LiveView_URL').inner_text
  # endpoints.delete_if { |k, v| v.blank? }
  log.info "model-name: #{camera_name}"
  log.debug 'endpoints:'
  endpoints.each do |e|
    log.debug "  #{e}"
  end
  endpoints
end

#ssdp_searchHash

Perform SSDP discover to find camera on network.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sony_camera_remote_api/ssdp.rb', line 26

def ssdp_search
  log.info 'Trying SSDP discover...'
  try = 1
  while true do
    response = Frisky::SSDP.search SSDP_SEARCH_TARGET
    if response.present?
      break
    elsif try < SSDP_SEARCH_RETRY
      try += 1
      log.warn "SSDP discover failed, retrying... (#{try}/#{SSDP_SEARCH_RETRY})"
      sleep(SSDP_RETRY_INTERVAL)
    else
      raise DeviceNotFound, 'Cannot find camera API server. Please confirm network connection is correct.'
    end
  end
  log.info 'SSDP discover succeeded.'

  # get device description
  dd = HTTPClient.new.get_content(response[0][:location])
  # puts dd
  parse_device_description(dd)
end