Class: Druzy::Upnp::UpnpService

Inherits:
Object
  • Object
show all
Defined in:
lib/druzy/upnp/upnp_service.rb

Constant Summary collapse

@@event_port =
15323

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ UpnpService

Returns a new instance of UpnpService.



18
19
20
21
22
23
24
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/druzy/upnp/upnp_service.rb', line 18

def initialize(args)
            
  @service_type = args[:service_type]
  @service_id = args[:service_id]
  @location = args[:location]
  @control_url = args[:control_url]
  @event_sub_url = args[:event_sub_url]
  @event_timeout = 300
  @event_sid = nil
  
  uri = URI(@location)
  xml=Net::HTTP.get(uri)
  xml_nok = Nokogiri::XML(xml)
  xml_nok.remove_namespaces!
  xml_nok.xpath('scpd/actionList/action').to_a.each do |el|
    
    action_name = el.xpath('name').text
    define_singleton_method(action_name.to_sym) do |arguments={}|
      message = <<-MESSAGE
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
  <u:#{action_name} xmlns:u="#{@service_type}">
      MESSAGE
      
      arguments.each do |cle, valeur|
        message.concat <<-MESSAGE
    <#{cle.to_s}>#{valeur.to_s}</#{cle.to_s}>
        MESSAGE
      end
      
      message.concat <<-MESSAGE
  </u:#{action_name}>
</s:Body>
</s:Envelope>            
      MESSAGE
      
      header = {
        "HOST" => uri.host.to_s+':'+uri.port.to_s,
        "CONTENT-LENGTH" => message.size.to_s,
        "CONTENT-TYPE" => 'text/xml; charset="utf-8"',
        "SOAPACTION" => @service_type.to_s+"#"+action_name.to_s
      }            
      
      http = Net::HTTP.new(uri.host,uri.port)
      request = Net::HTTP::Post.new(uri.request_uri,header)
      request.body = message
      response = http.request(request)
      xml = Nokogiri.XML(response.body)
      xml.remove_namespaces!

      result = {}
      xml.xpath("Envelope/Body/"+action_name.to_s+"Response").children.each do |child|
        result[child.name.to_s] = child.text
      end
      
      return result
    end

  end
  
end

Instance Attribute Details

#control_urlObject (readonly)

Returns the value of attribute control_url.



16
17
18
# File 'lib/druzy/upnp/upnp_service.rb', line 16

def control_url
  @control_url
end

#event_sub_urlObject (readonly)

Returns the value of attribute event_sub_url.



16
17
18
# File 'lib/druzy/upnp/upnp_service.rb', line 16

def event_sub_url
  @event_sub_url
end

#locationObject (readonly)

Returns the value of attribute location.



16
17
18
# File 'lib/druzy/upnp/upnp_service.rb', line 16

def location
  @location
end

#service_idObject (readonly)

Returns the value of attribute service_id.



16
17
18
# File 'lib/druzy/upnp/upnp_service.rb', line 16

def service_id
  @service_id
end

#service_typeObject (readonly)

Returns the value of attribute service_type.



16
17
18
# File 'lib/druzy/upnp/upnp_service.rb', line 16

def service_type
  @service_type
end

Instance Method Details

#renew_subscriptionObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/druzy/upnp/upnp_service.rb', line 116

def renew_subscription
  uri = URI(@event_sub_url)
  http = Net::HTTP.new(uri.host,uri.port)
  request = Net::HTTPGenericRequest.new('SUBSCRIBE',false,true,uri)
  request['SID'] = @event_sid
  request['TIMEOUT'] = 'Second-'+@event_timeout.to_s
  
  response = http.request(request)
  if response.code.to_i == 200
    @event_timeout = response['TIMEOUT'][7..-1].to_i

    Thread.new do
      sleep @event_timeout
      if @event_sid !=nil
        renew_subscription
      end
    end
  end
end

#subscribeObject



81
82
83
84
85
86
87
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
# File 'lib/druzy/upnp/upnp_service.rb', line 81

def subscribe
  if block_given?
    server = Druzy::Upnp::Event::UpnpEventServer.instance(@@event_port)
    uri = URI(@event_sub_url)
    http = Net::HTTP.new(uri.host,uri.port)
    request = Net::HTTPGenericRequest.new('SUBSCRIBE',false,true,uri)
    request['CALLBACK'] = '<'+server.event_address+'>'
    request['NT'] = 'upnp:event'
    request['TIMEOUT'] = 'Second-'+@event_timeout.to_s
    
    response = http.request(request)
    if response.code.to_i == 200
      @event_timeout = response['TIMEOUT'][7..-1].to_i
      @event_sid = response['SID']
                
      Thread.new do
        puts "début thread renew"
        sleep @event_timeout
        if @event_sid !=nil
          renew_subscription
        end
      end
                
      server.add_property_change_listener(@event_sid, Druzy::MVC::PropertyChangeListener.new do |event|
        yield(event)
      end)
      return @event_sid
    else
      return nil
    end
  else 
    return nil
  end
end

#unsubscribeObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/druzy/upnp/upnp_service.rb', line 136

def unsubscribe
  if @event_sid !=nil
    server = Druzy::Upnp::Event::UpnpEventServer.instance(@@event_port)
    server.remove_property_change_listener(@event_sid)
    
    uri = URI(@event_sub_url)
    http = Net::HTTP.new(uri.host,uri.port)
    request = Net::HTTPGenericRequest.new('UNSUBSCRIBE',false,true,nil)
    request['SID'] = @event_sid
    
    response = http.request(request)
    if response.code == 200
      @event_sid = nil
      return true
    else
      return false
    end
  else
    return true
  end
end