mystrom 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +41 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +13 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/lib/mystrom/api_error.rb +9 -0
- data/lib/mystrom/version.rb +3 -0
- data/lib/mystrom/wlan_switch.rb +179 -0
- data/lib/mystrom.rb +7 -0
- data/mystrom.gemspec +28 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46b2bfe555b9d54a0835384ceed2a450a1941249
|
4
|
+
data.tar.gz: 60ab602a68a9a460b9a75ef9138580dd9eb305e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f1216fb563b2b40ba063f5ee62d1e17e4a2a6af89c444664a189796837f08770977f83c693544e7b399c79fd8683afc9eb7db73dfbcbcfff679b865ba5e1f57
|
7
|
+
data.tar.gz: ce6c86ed12e773d355a9624ec259deab6ca06e78f4016149c85251e71d29305f8e93f665354cd31d20aff9ab09050b7ac5b6efda14473723756d4c7e1a6a9637
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Change log
|
2
|
+
|
3
|
+
This document represents a high-level overview of changes made to this project.
|
4
|
+
It will not list every miniscule change, but will allow you to view - at a
|
5
|
+
glance - what to expect from upgrading to a new version.
|
6
|
+
|
7
|
+
## [unpublished]
|
8
|
+
|
9
|
+
### Added
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
|
13
|
+
### Fixed
|
14
|
+
|
15
|
+
### Security
|
16
|
+
|
17
|
+
### Deprecated
|
18
|
+
|
19
|
+
### Removed
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## [0.1.0] - 2016-09-13
|
24
|
+
|
25
|
+
### Added
|
26
|
+
|
27
|
+
- Basic interface to the HTTP API of MyStrom WLAN switches, allowing to read
|
28
|
+
the current throughput, as well as to turn them on and off.
|
29
|
+
- Support for tunneling requests via an SSH gateway.
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
|
33
|
+
### Fixed
|
34
|
+
|
35
|
+
### Security
|
36
|
+
|
37
|
+
### Deprecated
|
38
|
+
|
39
|
+
### Removed
|
40
|
+
|
41
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2016 Michael Senn
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Mystrom
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mystrom`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mystrom'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mystrom
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mystrom.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
require 'httparty'
|
7
|
+
|
8
|
+
module MyStrom
|
9
|
+
# Basic binding to the HTTP API exposed by MyStrom WLAN switches.
|
10
|
+
#
|
11
|
+
# @example Basic usage
|
12
|
+
# require 'mystrom'
|
13
|
+
#
|
14
|
+
# w = MyStrom::WLANSwitch.new('http://10.60.1.10')
|
15
|
+
# puts "Current power throughput: #{ w.power }W"
|
16
|
+
# if w.power > 150
|
17
|
+
# puts "Using too much power, going dark."
|
18
|
+
# w.disable
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# if rand() > 0.9
|
22
|
+
# puts "Messing with family - toggling floor lights."
|
23
|
+
# w.toggle
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @example Proxying via SSH host
|
27
|
+
# require 'mystrom'
|
28
|
+
# require 'net/ssh/gateway'
|
29
|
+
# gateway = Net::SSH::Gateway.new('jump.example.com', 'johndoe', password: 'hidden')
|
30
|
+
# w = Mystrom::WLANSwitch.new('http://10.60.1.80', ssh_gateway: gateway)
|
31
|
+
class WLANSwitch
|
32
|
+
# Current power throughput in W
|
33
|
+
# @return [Fixnum]
|
34
|
+
attr_reader :power
|
35
|
+
|
36
|
+
# If data should be refreshed after every operation
|
37
|
+
# @return [Bool]
|
38
|
+
attr_accessor :auto_refresh
|
39
|
+
|
40
|
+
# HTTP URL of the switch
|
41
|
+
# @return [String]
|
42
|
+
attr_accessor :url
|
43
|
+
|
44
|
+
# Initialize a new instance of the class.
|
45
|
+
#
|
46
|
+
# @param url [String] URL of the MySTrom WLAN Switch web interface.
|
47
|
+
# @param opts [Hash] Additional options
|
48
|
+
# @option opts [Bool] :auto_refresh (false) If data should be
|
49
|
+
# refreshed after every operation.
|
50
|
+
# @option opts [Net::SSH::Gateway] :ssh_gateway (nil) SSH gateway through
|
51
|
+
# which to proxy the requests.
|
52
|
+
def initialize(url, opts = {})
|
53
|
+
@url = url
|
54
|
+
@auto_refresh = opts.fetch(:auto_refresh, false)
|
55
|
+
@ssh_gateway = opts.fetch(:ssh_gateway, nil)
|
56
|
+
update_data
|
57
|
+
end
|
58
|
+
|
59
|
+
# Enable the relay.
|
60
|
+
#
|
61
|
+
# @raise [APIError] If the information returned by the API was missing or
|
62
|
+
# incomplete.
|
63
|
+
# @return [Bool] New state of the relay.
|
64
|
+
def enable
|
65
|
+
do_request('relay?state=1')
|
66
|
+
if auto_refresh
|
67
|
+
update
|
68
|
+
else
|
69
|
+
@relay = true
|
70
|
+
end
|
71
|
+
|
72
|
+
@relay
|
73
|
+
end
|
74
|
+
|
75
|
+
# Disable the relay
|
76
|
+
#
|
77
|
+
# @raise [APIError] If the information returned by the API was missing or
|
78
|
+
# incomplete.
|
79
|
+
# @return [Bool] New state of the relay.
|
80
|
+
def disable
|
81
|
+
do_request('relay?state=0')
|
82
|
+
if auto_refresh
|
83
|
+
update
|
84
|
+
else
|
85
|
+
@relay = false
|
86
|
+
end
|
87
|
+
|
88
|
+
@relay
|
89
|
+
end
|
90
|
+
|
91
|
+
# Toggle the relay.
|
92
|
+
#
|
93
|
+
# @raise [APIError] If the information returned by the API was missing or
|
94
|
+
# incomplete.
|
95
|
+
# @return [Bool] New state of the relay.
|
96
|
+
def toggle
|
97
|
+
response = do_request('toggle')
|
98
|
+
|
99
|
+
if auto_refresh
|
100
|
+
update
|
101
|
+
else
|
102
|
+
begin
|
103
|
+
data = JSON.parse(response)
|
104
|
+
@relay = data.fetch('relay')
|
105
|
+
rescue JSON::ParserError => e
|
106
|
+
raise APIError, "Returned JSON was not valid JSON (#{ e.message })"
|
107
|
+
rescue KeyError => e
|
108
|
+
raise APIError, "Returned JSON was missing required key (#{ e.message })"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
@relay
|
113
|
+
end
|
114
|
+
|
115
|
+
# Whether relay is enabled.
|
116
|
+
def enabled?
|
117
|
+
@relay
|
118
|
+
end
|
119
|
+
|
120
|
+
# Whether relay is disabled.
|
121
|
+
def disabled?
|
122
|
+
!@relay
|
123
|
+
end
|
124
|
+
|
125
|
+
# Refresh data.
|
126
|
+
#
|
127
|
+
# @raise [APIError] If the information returned by the API was missing or
|
128
|
+
# incomplete.
|
129
|
+
# @return [WLANSwitch] self
|
130
|
+
def update
|
131
|
+
update_data
|
132
|
+
|
133
|
+
self
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
def update_data
|
138
|
+
# TODO: Error handling
|
139
|
+
begin
|
140
|
+
data = JSON.parse(do_request('report'))
|
141
|
+
@power = data.fetch('power')
|
142
|
+
@relay = data.fetch('relay')
|
143
|
+
rescue JSON::ParserError => e
|
144
|
+
raise APIError, "Returned JSON was not valid JSON (#{ e.message })"
|
145
|
+
rescue KeyError => e
|
146
|
+
raise APIError, "Returned JSON was missing required key (#{ e.message })"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def do_request(action)
|
151
|
+
if @ssh_gateway
|
152
|
+
response = do_request_ssh(action)
|
153
|
+
else
|
154
|
+
response = do_request_direct(action)
|
155
|
+
end
|
156
|
+
|
157
|
+
case response.code
|
158
|
+
when 200
|
159
|
+
response.body
|
160
|
+
else
|
161
|
+
raise APIError, "HTTP response invalid: Status code: #{ response.code }, Body: #{ response.body }"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def do_request_direct(action)
|
166
|
+
url = "#{ @url }/#{ action }"
|
167
|
+
HTTParty.get(url)
|
168
|
+
end
|
169
|
+
|
170
|
+
def do_request_ssh(action)
|
171
|
+
uri = URI("#{ @url }/#{ action }")
|
172
|
+
@ssh_gateway.open(uri.hostname, uri.port) do |port|
|
173
|
+
uri.hostname = '127.0.0.1'
|
174
|
+
uri.port = port
|
175
|
+
HTTParty.get(uri.to_s)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
data/lib/mystrom.rb
ADDED
data/mystrom.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mystrom/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'mystrom'
|
8
|
+
spec.version = Mystrom::VERSION
|
9
|
+
spec.authors = ['Michael Senn']
|
10
|
+
spec.email = ['michael@morrolan.ch']
|
11
|
+
|
12
|
+
spec.summary = %q{Tiny interface to HTTP API of MyStrom WLAN switches}
|
13
|
+
spec.homepage = "https://bitbucket.org/dragaera/mystrom"
|
14
|
+
spec.license = 'Apache-2.0'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'yard'
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'httparty'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mystrom
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Senn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: httparty
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- michael@morrolan.ch
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/mystrom.rb
|
85
|
+
- lib/mystrom/api_error.rb
|
86
|
+
- lib/mystrom/version.rb
|
87
|
+
- lib/mystrom/wlan_switch.rb
|
88
|
+
- mystrom.gemspec
|
89
|
+
homepage: https://bitbucket.org/dragaera/mystrom
|
90
|
+
licenses:
|
91
|
+
- Apache-2.0
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Tiny interface to HTTP API of MyStrom WLAN switches
|
113
|
+
test_files: []
|