wemo 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Wemo
2
2
 
3
- TODO: Write a gem description
3
+ I sort of combined some stuff. Hopefully noone is upset about the combination, I tried to make it clear where I found stuff.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,9 +16,20 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install wemo
18
18
 
19
+ Use from the command line:
20
+
21
+ $ wemo start friendlyName
22
+
23
+ $ wemo start light
24
+ $ wemo start pump
25
+ $ wemo stop pump
26
+ $ wemo stop light
27
+ $ wemo start 'exhaust fan'
28
+
19
29
  ## Usage
20
30
 
21
- TODO: Write usage instructions here
31
+ Wemo.off('friendlyName')
32
+ Wemo.on('friendlyName')
22
33
 
23
34
  ## Contributing
24
35
 
@@ -11,4 +11,8 @@ module Wemo
11
11
  def self.off(name)
12
12
  Wemo::Switch.send_command(name, 'off')
13
13
  end
14
+
15
+ def self.status(name)
16
+ Wemo::Switch.status(name)
17
+ end
14
18
  end
@@ -2,70 +2,121 @@
2
2
 
3
3
  require 'curb'
4
4
  require 'simple_upnp'
5
+ require 'nokogiri'
5
6
  module Wemo
6
7
  class Switch
8
+
9
+ attr_accessor :name, :state, :location, :details
10
+
11
+ def initialize(name)
12
+ self.name = name
13
+ self.find_device
14
+ self
15
+ end
16
+
7
17
  def self.on(name)
8
- send_command(name, 'on')
18
+ new(name).on
9
19
  end
10
20
 
11
21
  def self.off(name)
12
- send_command(name, 'off')
22
+ new(name).off
23
+ end
24
+
25
+ def self.status(name)
26
+ new(name).status
27
+ end
28
+
29
+ def on
30
+ set_binary_state(1)
31
+ end
32
+
33
+ def off
34
+ set_binary_state(0)
13
35
  end
14
36
 
15
- def self.state(name)
16
- send_command(name, 'off')
37
+ def status
38
+ get_binary_state
17
39
  end
18
40
 
19
- def self.set_binary_state(device_location, signal)
20
- c = Curl::Easy.new(device_location.to_s + 'upnp/control/basicevent1')
41
+ def set_binary_state(signal)
42
+ find_device unless self.location != nil
43
+ c = Curl::Easy.new(self.location.to_s + 'upnp/control/basicevent1')
21
44
  c.headers["Content-type"] = 'text/xml; charset="utf-8"'
22
45
  c.headers["SOAPACTION"] = "\"urn:Belkin:service:basicevent:1#SetBinaryState\""
23
46
  c.verbose = false
24
47
  begin
25
48
  c.http_post("<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><s:Body><u:SetBinaryState xmlns:u='urn:Belkin:service:basicevent:1'><BinaryState>#{signal}</BinaryState></u:SetBinaryState></s:Body></s:Envelope>")
26
49
  c.perform
27
- rescue Curb::Err
50
+ rescue
28
51
  end
52
+ self.state = signal
29
53
  # c.body_str
30
54
  end
31
55
 
32
- def self.send_command(wemo, action)
33
- case action
34
- when "on"
35
- signal = 1
36
- when "off"
37
- signal = 0
56
+
57
+ def get_binary_state
58
+ find_device unless self.location != nil
59
+ c = Curl::Easy.new(self.location.to_s + 'upnp/control/basicevent1')
60
+ c.headers["Content-type"] = 'text/xml; charset="utf-8"'
61
+ c.headers["SOAPACTION"] = "\"urn:Belkin:service:basicevent:1#GetBinaryState\""
62
+ c.verbose = false
63
+ begin
64
+ c.http_post("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"></u:GetBinaryState></s:Body></s:Envelope>")
65
+ c.perform
66
+ rescue
38
67
  end
39
- include_location_details = true
40
- wemo_device = nil
41
- device_name = nil
42
- device_location = nil
68
+
69
+ xml = Nokogiri::XML(c.body_str).text.strip.to_i
70
+ self.state = xml
71
+ if xml == 0
72
+ return false
73
+ else
74
+ return true
75
+ end
76
+ end
43
77
 
78
+ def find_device
44
79
  SimpleUpnp::Discovery.find do |device|
45
80
  begin
46
- device_json = device.to_json(include_location_details)
81
+ device_json = device.to_json(true)
47
82
  rescue
48
83
  next
49
84
  end
50
-
51
85
  # puts device_json.inspect
52
-
86
+
53
87
  if device_json['root']
54
88
  if device_json['root']['device']
55
89
  if device_json['root']['device']['friendlyName']
56
90
  friendlyName = device_json['root']['device']['friendlyName']
57
- if friendlyName.downcase == wemo.strip.downcase
58
- wemo_device = device_json['root']['device']
59
- device_name = friendlyName
60
- device_location = /https?:\/\/[\S]+\//.match(device.location)
91
+ if friendlyName.downcase == self.name.strip.downcase
92
+ self.details = device_json['root']['device']
93
+ self.name = friendlyName
94
+ self.location = /https?:\/\/[\S]+\//.match(device_json[:location]).to_s
61
95
  break
62
96
  end
63
97
  end
64
98
  end
65
99
  end
66
100
  end
67
- if wemo_device
68
- set_binary_state(device_location, signal)
101
+ status
102
+ end
103
+
104
+ def self.send_command(name, action)
105
+ case action
106
+ when "on"
107
+ signal = 1
108
+ when "off"
109
+ signal = 0
110
+ when 'status'
111
+ signal = false
112
+ end
113
+ d = Wemo::Switch.new(name)
114
+ if d.details
115
+ if signal
116
+ response = d.set_binary_state(signal)
117
+ else
118
+ response = d.get_binary_state
119
+ end
69
120
  end
70
121
  end
71
122
  end
@@ -1,3 +1,3 @@
1
1
  module Wemo
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_dependency 'simple_upnp'
24
24
  spec.add_dependency 'curb'
25
+ spec.add_dependency 'nokogiri'
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wemo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-16 00:00:00.000000000 Z
12
+ date: 2013-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: nokogiri
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: this is a combo. I took several things from around and fixed them, mostly
79
95
  taken from https://github.com/bobbrodie/siriproxy-wemo
80
96
  email:
@@ -109,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
125
  version: '0'
110
126
  segments:
111
127
  - 0
112
- hash: -1162693111217817383
128
+ hash: 2588951935745406503
113
129
  required_rubygems_version: !ruby/object:Gem::Requirement
114
130
  none: false
115
131
  requirements:
@@ -118,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
134
  version: '0'
119
135
  segments:
120
136
  - 0
121
- hash: -1162693111217817383
137
+ hash: 2588951935745406503
122
138
  requirements: []
123
139
  rubyforge_project:
124
140
  rubygems_version: 1.8.23