wemo_device 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2eae8671c6fab15a08feecb55a6164f611231f3cdeede64cfb5868709511342a
4
+ data.tar.gz: 41801313e816335bf167ef830bd9c58d965edba0202af2e4f736dedd90740706
5
+ SHA512:
6
+ metadata.gz: 6a0e2da82c6570f8a7773f6b4c011509f17edc4ef5c687d7e896e2e99d6640162c1caf1879ce056b0a94535fb024a78bc6aeb69b72a26a6c317d900c2c020975
7
+ data.tar.gz: f4672d0150edea516f3a924f7a608fbadd5cfbab4739974658467fc5e335a23d523c980d4619b8343fb02969e62b86dbdae54c3f99cc8a9b127c8ea7148b8b55
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2018 Yoshimasa Niwa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ wemo_device
2
+ ===========
3
+
4
+ NAME
5
+ ----
6
+
7
+ `wemo_device` -- A simple Ruby gem library that lookups [Blkin](http://www.belkin.com/)’s [Wemo](http://www.wemo.com/) devices.
8
+
9
+ DESCRIPTION
10
+ -----------
11
+
12
+ `wemo_device` is a Ruby gem library that provides a set of interfaces that looks up Wemo devices by using SSDP protocol, which is a part of UPnP protocols.
13
+
14
+ Since Wemo devices are using SSDP, but its implementation is for their propriety usage, it does not fully conform the specifications.
15
+
16
+ This library wraps these behaviors and provides a simple interface to lookup the devices on the network.
17
+
18
+ This library does not have any extra dependencies, works perfect with Ruby standard libraries.
19
+
20
+ USAGE
21
+ -----
22
+
23
+ Recommend to use [Bundler](https://bundler.io/) to add `wemo_device` as a dependency to your project. Add next line in `Gemfile`.
24
+
25
+ gem "wemo_device"
26
+
27
+ `wemo_device` provides a simple API to lookup. To list up all devices on the network, use next code.
28
+
29
+ require "rubygems"
30
+ require "wemo_device"
31
+ require "pp"
32
+
33
+ WemoDevice::Device.lookup.each do |device|
34
+ pp device
35
+ end
36
+
37
+ EXAMPLES
38
+ --------
39
+
40
+ In the `examples` directory, there are a few examples how to use this library.
41
+
42
+ * `lookup.rb`
43
+
44
+ Lookup all Wemo devices on the network.
45
+
46
+ * `switch.rb`
47
+
48
+ Turn on and off the Wemo switch.
49
+ Set unique service name found by `lookup.rb` in form of `--usn uuid:....` argument,
50
+ and use `--on` or `--off` to toggle the Wemo switch.
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "wemo_device"
5
+ require "pp"
6
+
7
+ WemoDevice::Device.lookup.each do |device|
8
+ pp device
9
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "wemo_device"
5
+ require "optparse"
6
+
7
+ def set_wemo_switch_state(unique_service_name, state)
8
+ device = WemoDevice::Device.lookup.find do |device|
9
+ device.unique_service_name == unique_service_name
10
+ end
11
+ return false unless device
12
+
13
+ uri = URI.parse(device.location)
14
+ header = {
15
+ "SOAPACTION" => '"urn:Belkin:service:basicevent:1#SetBinaryState"',
16
+ "Content-Type" => "text/xml"
17
+ }
18
+ payload = <<-END_OF_XML
19
+ <?xml version="1.0" encoding="utf-8"?>
20
+ <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
21
+ <s:Body>
22
+ <u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">
23
+ <BinaryState>#{state ? 1 : 0}</BinaryState>
24
+ </u:SetBinaryState>
25
+ </s:Body>
26
+ </s:Envelope>
27
+ END_OF_XML
28
+
29
+ Net::HTTP.start(uri.host, uri.port) do |http|
30
+ case http.post("/upnp/control/basicevent1", payload, header)
31
+ when Net::HTTPOK
32
+ return true
33
+ else
34
+ return false
35
+ end
36
+ end
37
+ end
38
+
39
+ options = {}
40
+ option_parser = OptionParser.new do |opts|
41
+ opts.on_tail("-u", "--usn=USN", "Wemo Switch unique service name") do |usn|
42
+ options[:usn] = usn
43
+ end
44
+ opts.on_tail("--on", "Turn on the switch") do
45
+ options[:action] = :on
46
+ end
47
+ opts.on_tail("--off", "Turn off the switch") do
48
+ options[:action] = :off
49
+ end
50
+ end
51
+ option_parser.parse!(ARGV)
52
+
53
+ raise "Missing unique service name" unless options[:usn]
54
+
55
+ case options[:action]
56
+ when :on
57
+ set_wemo_switch_state(options[:usn], true)
58
+ when :off
59
+ set_wemo_switch_state(options[:usn], false)
60
+ else
61
+ raise "Missing action"
62
+ end
@@ -0,0 +1,5 @@
1
+ module WemoDevice
2
+ autoload :SSDP, "wemo_device/ssdp.rb"
3
+ autoload :Device, "wemo_device/device.rb"
4
+ autoload :Description, "wemo_device/description.rb"
5
+ end
@@ -0,0 +1,36 @@
1
+ require "rexml/document"
2
+
3
+ module WemoDevice
4
+ URN = "urn:Belkin:service:basicevent:1".freeze
5
+
6
+ class Device
7
+ class << self
8
+ def lookup
9
+ # Wemo devices are responding only when search target is the URN.
10
+ # `ssdp:all` and other search target doesn't work.
11
+ SSDP.new.lookup(URN, 3).select do |response|
12
+ # There are many devices that ignores `ST`, like Philips Hue.
13
+ response.search_target == URN
14
+ end.map do |response|
15
+ Device.new(response)
16
+ end
17
+ end
18
+ end
19
+
20
+ def initialize(response)
21
+ @response = response
22
+ end
23
+
24
+ def unique_service_name
25
+ @response.unique_service_name
26
+ end
27
+
28
+ def location
29
+ @response.location
30
+ end
31
+
32
+ def description
33
+ @description ||= Description.new(@response.location)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,82 @@
1
+ require "socket"
2
+ require "timeout"
3
+ require "net/http"
4
+
5
+ module WemoDevice
6
+ class SSDP
7
+ MULTICAST_HOST = "239.255.255.250".freeze
8
+ MULTICAST_PORT = 1900.freeze
9
+
10
+ class Response
11
+ def initialize(host, port, payload)
12
+ @host = host
13
+ @port = port
14
+ @payload = payload
15
+ end
16
+
17
+ def search_target
18
+ http_response["st"]
19
+ end
20
+
21
+ def unique_service_name
22
+ http_response["usn"]
23
+ end
24
+
25
+ def location
26
+ http_response["location"]
27
+ end
28
+
29
+ private
30
+
31
+ def http_response
32
+ @http_response ||= parse_http_response
33
+ end
34
+
35
+ def parse_http_response
36
+ # Using Net::HTTPResponse private API to parse HTTP body from String.
37
+ # See `net/http.rb`, `net/protocol.rb`, and `net/http/response.rb`.
38
+ string_io = StringIO.new(@payload)
39
+ buffered_io = Net::BufferedIO.new(string_io)
40
+ Net::HTTPResponse.read_new(buffered_io)
41
+ end
42
+ end
43
+
44
+ def lookup(search_target, timeout)
45
+ UDPSocket.open do |socket|
46
+ # `sendto(2)` should automatically `bind(2)`.
47
+ socket.send(m_search_message(search_target), 0, MULTICAST_HOST, MULTICAST_PORT)
48
+
49
+ responses = []
50
+ begin
51
+ Timeout.timeout(timeout) do
52
+ loop do
53
+ # `recv(2)` may discard excess bytes for message based sockets such as `SOCK_DGRAM`
54
+ # when a message is too long and not fir in the given length and `MSG_PEEK` is not set.
55
+ # Thus, the entire message shall be read in a single operation.
56
+ payload, (_, port, host, _) = socket.recvfrom(4096)
57
+ responses << Response.new(host, port, payload)
58
+ end
59
+ end
60
+ rescue Timeout::Error
61
+ end
62
+
63
+ responses
64
+ end
65
+ end
66
+
67
+ def m_search_message(search_target)
68
+ [
69
+ "M-SEARCH * HTTP/1.1",
70
+ "Content-Length: 0",
71
+ "ST: #{search_target}",
72
+ # Device responses should be delayed a random duration between 0 and this many seconds
73
+ # to balance load for the control point when it processes responses.
74
+ "MX: 2",
75
+ "MAN: \"ssdp:discover\"",
76
+ "HOST: #{MULTICAST_HOST}:#{MULTICAST_PORT}",
77
+ "",
78
+ ""
79
+ ].join("\r\n")
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wemo_device
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshimasa Niwa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Generate Sparkle appcast.xml
42
+ email:
43
+ - niw@niw.at
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - LICENSE
48
+ - README.md
49
+ files:
50
+ - LICENSE
51
+ - README.md
52
+ - examples/lookup.rb
53
+ - examples/switch.rb
54
+ - lib/wemo_device.rb
55
+ - lib/wemo_device/device.rb
56
+ - lib/wemo_device/ssdp.rb
57
+ homepage: https://github.com/niw/wemo_device
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ source_code_uri: https://github.com/niw/wemo_device
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.7.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Generate Sparkle appcast.xml
82
+ test_files: []