Class: UPnP::ControlPoint::Device

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_info) ⇒ Device

Returns a new instance of Device.

Parameters:

  • device_info (Hash)

Options Hash (device_info):

  • ssdp_notification (Hash)
  • device_description (Hash)
  • parent_base_url (Hash)


106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/upnp/control_point/device.rb', line 106

def initialize(device_info)
  super()

  @device_info = device_info
  log "Got device info: #{@device_info}"
  @device_list = []
  @service_list = []
  @icon_list = []
  @xmlns = ""
  @done_creating_devices = false
  @done_creating_services = false
end

Instance Attribute Details

#cache_controlObject (readonly)

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Passed in as part of device_info; given by the SSDP search response.



20
21
22
# File 'lib/upnp/control_point/device.rb', line 20

def cache_control
  @cache_control
end

#dateObject (readonly)

Returns the value of attribute date.



21
22
23
# File 'lib/upnp/control_point/device.rb', line 21

def date
  @date
end

#descriptionHash (readonly)

Returns The whole parsed description… just in case.

Returns:

  • (Hash)

    The whole parsed description… just in case.



32
33
34
# File 'lib/upnp/control_point/device.rb', line 32

def description
  @description
end

#device_listObject (readonly)

Devices embedded directly into this device.



93
94
95
# File 'lib/upnp/control_point/device.rb', line 93

def device_list
  @device_list
end

#device_typeString (readonly)

Returns The type of UPnP device (URN) from the description file.

Returns:

  • (String)

    The type of UPnP device (URN) from the description file.



54
55
56
# File 'lib/upnp/control_point/device.rb', line 54

def device_type
  @device_type
end

#expirationObject (readonly)

Returns the value of attribute expiration.



34
35
36
# File 'lib/upnp/control_point/device.rb', line 34

def expiration
  @expiration
end

#extObject (readonly)

Returns the value of attribute ext.



22
23
24
# File 'lib/upnp/control_point/device.rb', line 22

def ext
  @ext
end

#friendly_nameString (readonly)

Returns Short device description for the end user.

Returns:

  • (String)

    Short device description for the end user.



57
58
59
# File 'lib/upnp/control_point/device.rb', line 57

def friendly_name
  @friendly_name
end

#icon_listArray<Hash> (readonly)

Returns An Array where each element is a Hash that describes an icon.

Returns:

  • (Array<Hash>)

    An Array where each element is a Hash that describes an icon.



87
88
89
# File 'lib/upnp/control_point/device.rb', line 87

def icon_list
  @icon_list
end

#locationObject (readonly)

Returns the value of attribute location.



23
24
25
# File 'lib/upnp/control_point/device.rb', line 23

def location
  @location
end

#manufacturerString (readonly)

Returns Manufacturer’s name.

Returns:

  • (String)

    Manufacturer’s name.



60
61
62
# File 'lib/upnp/control_point/device.rb', line 60

def manufacturer
  @manufacturer
end

#manufacturer_urlString (readonly)

Returns Manufacturer’s web site.

Returns:

  • (String)

    Manufacturer’s web site.



63
64
65
# File 'lib/upnp/control_point/device.rb', line 63

def manufacturer_url
  @manufacturer_url
end

#model_descriptionString (readonly)

Returns Long model description for the end user, from the description file.

Returns:

  • (String)

    Long model description for the end user, from the description file.



66
67
68
# File 'lib/upnp/control_point/device.rb', line 66

def model_description
  @model_description
end

#model_nameString (readonly)

Returns Model name of this device from the description file.

Returns:

  • (String)

    Model name of this device from the description file.



69
70
71
# File 'lib/upnp/control_point/device.rb', line 69

def model_name
  @model_name
end

#model_numberString (readonly)

Returns Model number of this device from the description file.

Returns:

  • (String)

    Model number of this device from the description file.



72
73
74
# File 'lib/upnp/control_point/device.rb', line 72

def model_number
  @model_number
end

#model_urlString (readonly)

Returns Web site for model of this device.

Returns:

  • (String)

    Web site for model of this device.



75
76
77
# File 'lib/upnp/control_point/device.rb', line 75

def model_url
  @model_url
end

#presentation_urlString (readonly)

Returns URL for device control via a browser.

Returns:

  • (String)

    URL for device control via a browser.



96
97
98
# File 'lib/upnp/control_point/device.rb', line 96

def presentation_url
  @presentation_url
end

#serial_numberString (readonly)

Returns The serial number from the description file.

Returns:

  • (String)

    The serial number from the description file.



78
79
80
# File 'lib/upnp/control_point/device.rb', line 78

def serial_number
  @serial_number
end

#serverObject (readonly)

Returns the value of attribute server.



24
25
26
# File 'lib/upnp/control_point/device.rb', line 24

def server
  @server
end

#service_listObject (readonly)

Services provided directly by this device.



90
91
92
# File 'lib/upnp/control_point/device.rb', line 90

def service_list
  @service_list
end

#spec_versionHash (readonly)

Returns :major and :minor revisions of the UPnP spec this device adheres to.

Returns:

  • (Hash)

    :major and :minor revisions of the UPnP spec this device adheres to.



46
47
48
# File 'lib/upnp/control_point/device.rb', line 46

def spec_version
  @spec_version
end

#ssdp_notificationObject (readonly)

Returns the value of attribute ssdp_notification.



15
16
17
# File 'lib/upnp/control_point/device.rb', line 15

def ssdp_notification
  @ssdp_notification
end

#stObject (readonly)

Returns the value of attribute st.



25
26
27
# File 'lib/upnp/control_point/device.rb', line 25

def st
  @st
end

#udnString (readonly)

Returns The UDN for the device, from the description file.

Returns:

  • (String)

    The UDN for the device, from the description file.



81
82
83
# File 'lib/upnp/control_point/device.rb', line 81

def udn
  @udn
end

#upcString (readonly)

Returns The UPC of the device from the description file.

Returns:

  • (String)

    The UPC of the device from the description file.



84
85
86
# File 'lib/upnp/control_point/device.rb', line 84

def upc
  @upc
end

#url_baseString (readonly)

Returns URLBase from the device’s description file.

Returns:

  • (String)

    URLBase from the device’s description file.



49
50
51
# File 'lib/upnp/control_point/device.rb', line 49

def url_base
  @url_base
end

#usnObject (readonly)

Returns the value of attribute usn.



26
27
28
# File 'lib/upnp/control_point/device.rb', line 26

def usn
  @usn
end

#xmlnsString (readonly)

Returns The xmlns attribute of the device from the description file.

Returns:

  • (String)

    The xmlns attribute of the device from the description file.



43
44
45
# File 'lib/upnp/control_point/device.rb', line 43

def xmlns
  @xmlns
end

Instance Method Details

#expired?Boolean

True if the device hasn’t received an alive notification since it last told its max age.

Returns:

  • (Boolean)


221
222
223
# File 'lib/upnp/control_point/device.rb', line 221

def expired?
  Time.now > @expiration if @expiration
end

#extract_description(ddf) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/upnp/control_point/device.rb', line 233

def extract_description(ddf)
  log "Extracting basic attributes from description..."

  @device_type = ddf[:deviceType] || ''
  @friendly_name = ddf[:friendlyName] || ''
  @manufacturer = ddf[:manufacturer] || ''
  @manufacturer_url = ddf[:manufacturerURL] || ''
  @model_description = ddf[:modelDescription] || ''
  @model_name = ddf[:modelName] || ''
  @model_number = ddf[:modelNumber] || ''
  @model_url = ddf[:modelURL] || ''
  @serial_number = ddf[:serialNumber] || ''
  @udn = ddf[:UDN] || ''
  @upc = ddf[:UPC] || ''
  @icon_list = extract_icons(ddf[:iconList])
  @presentation_url = ddf[:presentationURL] || ''

  log "Basic attributes extracted."

  start_device_extraction
  start_service_extraction
end

#extract_device(device, device_extractor) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/upnp/control_point/device.rb', line 413

def extract_device(device, device_extractor)
  deferred_device = Device.new(device_description: device, parent_base_url: @url_base)

  deferred_device.errback do
    msg = "Couldn't build device!"
    log "#{msg}", :error
    device_extractor.set_deferred_status(:failed, msg)

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

  deferred_device.callback do |built_device|
    log "Device created: #{built_device.device_type}"
    device_extractor.set_deferred_status(:succeeded, built_device)
  end

  deferred_device.fetch
end

#extract_devices(group_device_extractor) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/upnp/control_point/device.rb', line 340

def extract_devices(group_device_extractor)
  log "Extracting child devices for #{self.object_id} using #{group_device_extractor.object_id}"

  device_list_hash = if @description.has_key? :root
    log "Description has a :root key..."

    if @description[:root][:device][:deviceList]
      @description[:root][:device][:deviceList][:device]
    else
      log "No child devices to extract."
      group_device_extractor.set_deferred_status(:succeeded)
    end
  elsif @description[:deviceList]
    log "Description does not have a :root key..."
    @description[:deviceList][:device]
  else
    log "No child devices to extract."
    group_device_extractor.set_deferred_status(:succeeded)
  end

  if device_list_hash.nil? || device_list_hash.empty?
    group_device_extractor.set_deferred_status(:succeeded)
    return
  end

  log "device list: #{device_list_hash}"

  if device_list_hash.is_a? Array
    EM::Iterator.new(device_list_hash, device_list_hash.count).map(
      proc do |device, iter|
        single_device_extractor = EventMachine::DefaultDeferrable.new

        single_device_extractor.errback do
          msg = "Failed extracting device."
          log "#{msg}", :error

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

        single_device_extractor.callback do |device|
          iter.return(device)
        end

        extract_device(device, single_device_extractor)
      end,
      proc do |found_devices|
        group_device_extractor.set_deferred_status(:succeeded, found_devices)
      end
    )
  else
    single_device_extractor = EventMachine::DefaultDeferrable.new

    single_device_extractor.errback do
      msg = "Failed extracting device."
      log "#{msg}", :error
      group_device_extractor.set_deferred_status(:failed, msg)

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

    single_device_extractor.callback do |device|
      group_device_extractor.set_deferred_status(:succeeded, [device])
    end

    log "Extracting single device..."
    extract_device(device_list_hash, group_device_extractor)
  end
end

#extract_from_ssdp_notification(callback) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/upnp/control_point/device.rb', line 179

def extract_from_ssdp_notification(callback)
  log "Creating device from SSDP Notification info."
  @ssdp_notification = @device_info[:ssdp_notification]

  @cache_control = @ssdp_notification[:cache_control]
  @location = ssdp_notification[:location]
  @server = ssdp_notification[:server]
  @st = ssdp_notification[:st] || ssdp_notification[:nt]
  @ext = ssdp_notification.has_key?(:ext) ? true : false
  @usn = ssdp_notification[:usn]
  @date = ssdp_notification[:date] || ''
  @expiration = if @date.empty?
    Time.now + @cache_control.match(/\d+/)[0].to_i
  else
    Time.at(Time.parse(@date).to_i + @cache_control.match(/\d+/)[0].to_i)
  end

  if @location
    get_description(@location, callback)
  else
    message = "M-SEARCH response is either missing the Location header or has an empty value."
    message << "Response: #{@ssdp_notification}"

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

#extract_icons(ddf_icon_list) ⇒ Array<Hash>

Returns:



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/upnp/control_point/device.rb', line 320

def extract_icons(ddf_icon_list)
  return [] unless ddf_icon_list
  log "Icon list: #{ddf_icon_list}"

  if ddf_icon_list[:icon].is_a? Array
    ddf_icon_list[:icon].map do |values|
      values[:url] = build_url(@url_base, values[:url])
      values
    end
  else
    [{
      mimetype: ddf_icon_list[:icon][:mimetype],
      width: ddf_icon_list[:icon][:width],
      height: ddf_icon_list[:icon][:height],
      depth: ddf_icon_list[:icon][:depth],
      url: build_url(@url_base, ddf_icon_list[:icon][:url])
    }]
  end
end

#extract_service(service, single_service_extractor) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/upnp/control_point/device.rb', line 488

def extract_service(service, single_service_extractor)
  service_getter = Service.new(@url_base, service)
  log "Extracting service with #{service_getter.object_id}"

  service_getter.errback do |message|
    msg = "Couldn't build service with info: #{service}"
    log "#{msg}", :error
    single_service_extractor.set_deferred_status(:failed, msg)

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

  service_getter.callback do |built_service|
    log "Service created: #{built_service.service_type}"
    single_service_extractor.set_deferred_status(:succeeded, built_service)
  end

  service_getter.fetch
end

#extract_services(service_list, group_service_extractor) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/upnp/control_point/device.rb', line 434

def extract_services(service_list, group_service_extractor)
  log "Extracting services..."

  log "service list: #{service_list}"
  return if service_list.nil?

  service_list.each_value do |service|
    if service.is_a? Array
      EM::Iterator.new(service, service.count).map(
        proc do |s, iter|
          single_service_extractor = EventMachine::DefaultDeferrable.new

          single_service_extractor.errback do
            msg = "Failed to create service."
            log "#{msg}", :error

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

          single_service_extractor.callback do |service|
            iter.return(service)
          end

          extract_service(s, single_service_extractor)
        end,
        proc do |found_services|
          group_service_extractor.set_deferred_status(:succeeded, found_services)
        end
      )
    else
      single_service_extractor = EventMachine::DefaultDeferrable.new

      single_service_extractor.errback do
        msg = "Failed to create service."
        log "#{msg}", :error
        group_service_extractor.set_deferred_status :failed, msg

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

      single_service_extractor.callback do |service|
        group_service_extractor.set_deferred_status :succeeded, [service]
      end

      log "Extracting single service..."
      extract_service(service, single_service_extractor)
    end
  end
end

#extract_spec_versionObject



256
257
258
259
260
# File 'lib/upnp/control_point/device.rb', line 256

def extract_spec_version
  if @description[:root]
    "#{@description[:root][:specVersion][:major]}.#{@description[:root][:specVersion][:minor]}"
  end
end

#extract_url_baseObject



208
209
210
211
212
213
214
215
216
217
# File 'lib/upnp/control_point/device.rb', line 208

def extract_url_base
  if @description[:root] && @description[:root][:URLBase]
    @description[:root][:URLBase]
  elsif @device_info[:parent_base_url]
    @device_info[:parent_base_url]
  else
    tmp_uri = URI(@location)
    "#{tmp_uri.scheme}://#{tmp_uri.host}:#{tmp_uri.port}/"
  end
end

#fetchObject



119
120
121
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/upnp/control_point/device.rb', line 119

def fetch
  description_getter = EventMachine::DefaultDeferrable.new

  description_getter.errback do
    msg = "Failed getting description."
    log "#{msg}", :error
    @done_creating_devices = true
    @done_creating_services = true
    set_deferred_status(:failed, msg)

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

  if @device_info.has_key? :ssdp_notification
    extract_from_ssdp_notification(description_getter)
  elsif @device_info.has_key? :device_description
    log "Creating device from device description file info."
    description_getter.set_deferred_success @device_info[:device_description]
  else
    log "Not sure what to extract from this device's info."
    description_getter.set_deferred_failure
  end

  description_getter.callback do |description|
    log "Description received from #{description_getter.object_id}"
    @description = description

    if @description.nil?
      log "Description is empty.", :error
      set_deferred_status(:failed, "Got back an empty description...")
      return
    end

    extract_spec_version

    @url_base = extract_url_base
    log "Set url_base to #{@url_base}"

    if @device_info[:ssdp_notification]
      @xmlns = @description[:root][:@xmlns]
      log "Extracting description for root device #{description_getter.object_id}"
      extract_description(@description[:root][:device])
    elsif @device_info.has_key? :device_description
      log "Extracting description for non-root device #{description_getter.object_id}"
      extract_description(@description)
    end
  end

  tickloop = EM.tick_loop do
    if @done_creating_devices && @done_creating_services
      log "All done creating stuff"
      :stop
    end
  end

  tickloop.on_stop { set_deferred_status :succeeded, self }
end

#has_devices?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/upnp/control_point/device.rb', line 225

def has_devices?
  !@device_list.empty?
end

#has_services?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/upnp/control_point/device.rb', line 229

def has_services?
  !@service_list.empty?
end

#start_device_extractionObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/upnp/control_point/device.rb', line 292

def start_device_extraction
  device_extractor = EventMachine::DefaultDeferrable.new
  extract_devices(device_extractor)

  device_extractor.errback do
    msg = "Failed extracting device."
    log "#{msg}", :error
    @done_creating_devices = true

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

  device_extractor.callback do |device|
    if device
      log "Device extracted from #{device_extractor.object_id}."
      @device_list << device
    else
      log "Device extraction done from #{device_extractor.object_id} but none were extracted."
    end

    log "Child device size is now: #{@device_list.size}"
    @done_creating_devices = true
  end
end

#start_service_extractionObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/upnp/control_point/device.rb', line 262

def start_service_extraction
  services_extractor = EventMachine::DefaultDeferrable.new

  if @description[:serviceList]
    log "Extracting services from non-root device."
    extract_services(@description[:serviceList], services_extractor)
  elsif @description[:root][:device][:serviceList]
    log "Extracting services from root device."
    extract_services(@description[:root][:device][:serviceList], services_extractor)
  end

  services_extractor.errback do
    msg = "Failed extracting services."
    log "#{msg}", :error
    @done_creating_services = true

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

  services_extractor.callback do |services|
    log "Done extracting services."
    @service_list = services

    log "New service count: #{@service_list.size}."
    @done_creating_services = true
  end
end