Class: UPnP::ControlPoint::Service

Inherits:
Base
  • Object
show all
Includes:
EventMachine::Deferrable, LogSwitch::Mixin
Defined in:
lib/upnp/control_point/service.rb

Overview

An object of this type functions as somewhat of a proxy to a UPnP device’s service. The object sort of defines itself when you call #fetch; it gets the description file from the device, parses it, populates its attributes (as accessors) and defines singleton methods from the list of actions that the service defines.

After the fetch is done, you can call Ruby methods on the service and expect a Ruby Hash back as a return value. The methods will look just the SOAP actions and will always return a Hash, where key/value pairs are converted from the SOAP response; values are converted to the according Ruby type based on <dataType> in the <serviceStateTable>.

Types map like:

* Integer
  * ui1
  * ui2
  * ui4
  * i1
  * i2
  * i4
  * int
* Float
  * r4
  * r8
  * number
  * fixed.14.4
  * float
* String
  * char
  * string
  * uuid
* TrueClass
  * 1
  * true
  * yes
* FalseClass
  * 0
  * false
  * no

Examples:

No “in” params

my_service.GetSystemUpdateID    # => { "Id" => 1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_base_url, service_list_info) ⇒ Service

Returns a new instance of Service.

Parameters:

  • device_base_url (String)

    URL given (or otherwise determined) by <URLBase> from the device that owns the service.

  • service_list_info (Hash)

    Info given in the <serviceList> section of the device description.



110
111
112
113
114
115
116
# File 'lib/upnp/control_point/service.rb', line 110

def initialize(device_base_url, service_list_info)
  @service_list_info = service_list_info
  @action_list = []
  @xmlns = ""
  extract_service_list_info(device_base_url)
  configure_savon
end

Instance Attribute Details

#action_listArray<Hash> (readonly)

Returns:



94
95
96
# File 'lib/upnp/control_point/service.rb', line 94

def action_list
  @action_list
end

#control_urlURI::HTTP (readonly)

Returns Control URL.

Returns:

  • (URI::HTTP)

    Control URL.



74
75
76
# File 'lib/upnp/control_point/service.rb', line 74

def control_url
  @control_url
end

#descriptionHash (readonly)

Returns The whole description… just in case.

Returns:

  • (Hash)

    The whole description… just in case.



104
105
106
# File 'lib/upnp/control_point/service.rb', line 104

def description
  @description
end

#event_sub_urlURI::HTTP (readonly)

Returns Eventing URL.

Returns:

  • (URI::HTTP)

    Eventing URL.



77
78
79
# File 'lib/upnp/control_point/service.rb', line 77

def event_sub_url
  @event_sub_url
end

#scpd_urlURI::HTTP (readonly)

Returns Service description URL.

Returns:

  • (URI::HTTP)

    Service description URL.



71
72
73
# File 'lib/upnp/control_point/service.rb', line 71

def scpd_url
  @scpd_url
end

#service_idString (readonly)

Returns Service identifier, unique within this service’s devices.

Returns:

  • (String)

    Service identifier, unique within this service’s devices.



68
69
70
# File 'lib/upnp/control_point/service.rb', line 68

def service_id
  @service_id
end

#service_state_tableObject (readonly)

Probably don’t need to keep this long-term; just adding for testing.



97
98
99
# File 'lib/upnp/control_point/service.rb', line 97

def service_state_table
  @service_state_table
end

#service_typeString (readonly)

Returns UPnP service type, including URN.

Returns:

  • (String)

    UPnP service type, including URN.



65
66
67
# File 'lib/upnp/control_point/service.rb', line 65

def service_type
  @service_type
end

#spec_versionString (readonly)

Returns:



91
92
93
# File 'lib/upnp/control_point/service.rb', line 91

def spec_version
  @spec_version
end

#xmlnsString (readonly)

Returns:



88
89
90
# File 'lib/upnp/control_point/service.rb', line 88

def xmlns
  @xmlns
end

Instance Method Details

#fetchObject

Fetches the service description file, parses it, extracts attributes into accessors, and defines Ruby methods from SOAP actions. Since this is a long-ish process, this is done using EventMachine Deferrable behavior.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/upnp/control_point/service.rb', line 122

def fetch
  if @scpd_url.empty?
    log "NO SCPDURL to get the service description from.  Returning."
    set_deferred_success self
    return
  end

  description_getter = EventMachine::DefaultDeferrable.new
  log "Fetching service description with #{description_getter.object_id}"
  get_description(@scpd_url, description_getter)

  description_getter.errback do
    msg = "Failed getting service description."
    log "#{msg}", :error
    # @todo Should this return self? or should it succeed?
    set_deferred_status(:failed, msg)

    if ControlPoint.raise_on_remote_error
      raise ControlPoint::Error, msg
    end
  end

  description_getter.callback do |description|
    log "Service description received for #{description_getter.object_id}."
    @description = description
    @xmlns = @description[:scpd][:@xmlns]
    extract_spec_version
    extract_service_state_table

    if @description[:scpd][:actionList]
      log "Defining methods from action_list using [#{description_getter.object_id}]"
      define_methods_from_actions(@description[:scpd][:actionList][:action])
    end

    set_deferred_status(:succeeded, self)
  end
end