Module: Sonos::Endpoint::Upnp

Included in:
Device::Speaker
Defined in:
lib/sonos/endpoint/upnp.rb

Defined Under Namespace

Classes: Subscription

Constant Summary collapse

UPNP_TIMEOUT =
600
ENDPOINT =
{
  content_directory: '/MediaServer/ContentDirectory/Event',
  av_transport: '/MediaRenderer/AVTransport/Event',
  zone_group_topology: '/ZoneGroupTopology/Event',
  device_properties: '/DeviceProperties/Event',
  group_management: '/GroupManagement/Event',
  group_rendering_control: '/MediaRenderer/GroupRenderingControl/Event',
  rendering_control: '/MediaRenderer/RenderingControl/Event',
  connection_manager: '/MediaRenderer/ConnectionManager/Event',
  queue: '/MediaRenderer/Queue/Event',
  alarm_clock: '/AlarmClock/Event',
  music_services: '/MusicServices/Event',
  system_properties: '/SystemProperties/Event'
}.freeze

Instance Method Summary collapse

Instance Method Details

#subscribe_to_upnp_events(callback_url, event) ⇒ Hash

Subscribes to UPNP events, with callbacks being sent to the provided URL

Parameters:

  • The (String)

    URL to receive http callbacks from the device

  • The (Symbol)
    Sonos::Endpoint::Upnp::ENDPOINT

    to subscribe to

Returns:

  • (Hash)

    Returns the timeout for the HTTP listener as well as the device SID



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sonos/endpoint/upnp.rb', line 25

def subscribe_to_upnp_events(callback_url, event)
  client = HTTPClient.new

  request_headers = {
    # The URL to be requested when a callback happens
    'CALLBACK' => "<#{callback_url}>",
    # Apparently required in order to appease the UPNP gods
    'NT' => 'upnp:event',
    # The timeout for the subscription - set to 10 minutes
    'TIMEOUT' => "Second-#{UPNP_TIMEOUT}"
  }

  response = client.request(:subscribe, event_url(event), header: request_headers)

  # Convert the resulting headers into something less shouty
  result_headers = response.header.all.inject({}) do |result, item|
    result[item[0].downcase.to_sym] = item[1]
    result
  end

  # Return all the information you'd need in order to do an unsusbscribe
  Subscription.new({
    timeout: result_headers[:timeout][/(\d+)/].to_i,
    sid: result_headers[:sid],
    event: event
  })
end

#unsubscribe_from_upnp_events(subscription) ⇒ Object

Unsubscribes an existing UPNP event

Parameters:

  • The (String)

    subscription ID that you wish to cancel

  • The (Symbol)
    Sonos::Endpoint::Upnp::ENDPOINT

    to which it belongs



56
57
58
# File 'lib/sonos/endpoint/upnp.rb', line 56

def unsubscribe_from_upnp_events(subscription)
  HTTPClient.new.request(:unsubscribe, event_url(subscription.event), header: {'SID' => subscription.sid })
end