mfi 0.9.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.
Files changed (9) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/Gemfile.lock +20 -0
  4. data/README.md +46 -0
  5. data/Rakefile +30 -0
  6. data/lib/mfi.rb +10 -0
  7. data/lib/mpower.rb +95 -0
  8. data/mfi.gemspec +24 -0
  9. metadata +78 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b14de77104a8ccb5ed5ed50f6794ac40404d83ee
4
+ data.tar.gz: 32a1abe857da69e501019df8477cca705e2e9901
5
+ SHA512:
6
+ metadata.gz: 39f02f69df4aa53648e38278362518f3578f1f627ca7fdd7d1cc1f93ea4a9f3a4c8aca9a00abebbe36181f9b91a1a51eb73e05b3fa8c2e8a0e950ee81e2174da
7
+ data.tar.gz: 7129e7eed14410e49a89940d54a396b50cc155dd5aaeaeea4395684fbbd29174591636b0442978484a18358bede997ccac178279bf0b1ebdcac82f94f5418bac
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mfi (0.9.0)
5
+ json (>= 1.8.1)
6
+ ssh (>= 1.1.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ json (1.8.1)
12
+ open4 (1.3.0)
13
+ ssh (1.1.0)
14
+ open4 (~> 1.0)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ mfi!
@@ -0,0 +1,46 @@
1
+ #### mFi for ruby
2
+
3
+ Gem for integrating with mFi devices over SSH.
4
+
5
+ ##### Install
6
+
7
+ ```
8
+ gem install mfi
9
+ ```
10
+
11
+ ##### mPower
12
+
13
+ mPower ports are linux based wifi-enabled power strips. Each port has sensors for amps, voltage, and power factor. Additionally each port has a relay. Here is a script that samples all powers, and disables a port if the energy exceeds 10kWh.
14
+
15
+ ```ruby
16
+ device = MFi::MPower.new(
17
+ :host => "hostname",
18
+ :user => "username",
19
+ :pass => "password"
20
+ )
21
+
22
+ device.connect!
23
+
24
+ device.sample.each { |reading|
25
+ puts reading.to_s
26
+ if reading.energy > 10
27
+ puts "Port #{reading.port} has consumed >= 10 kWh. Switching off..."
28
+ device.switch_off(reading.port)
29
+ end
30
+ }
31
+ ```
32
+
33
+ ##### Rake
34
+
35
+ You can also run the rakefile when checking out the repository
36
+
37
+ ```
38
+ $ rake sample HOST=192.168.3.201 USER=ubnt PASS=ubnt
39
+ Port 1: 20.5W ( 0.7pf 0.3A 112.3V)
40
+ Port 2: 0.0W ( 0.0pf 0.0A 113.9V)
41
+ Port 3: 0.0W ( 0.0pf 0.0A 113.2V)
42
+ ```
43
+
44
+ ##### Notes
45
+
46
+ This is a quick sunday hack, so use at your own risk. If you extend or enhance this, send a pull request.
@@ -0,0 +1,30 @@
1
+ $: << File.dirname(__FILE__) + "/lib"
2
+
3
+ require "mfi"
4
+
5
+ task :default => :sample
6
+
7
+ task :env do
8
+ @mpower = MFi::MPower.new(
9
+ :host => ENV["HOST"],
10
+ :user => ENV["USER"],
11
+ :pass => ENV["PASS"]
12
+ )
13
+ end
14
+
15
+ task :sample => :env do
16
+ @mpower.connect!
17
+ @mpower.sample.each { |reading|
18
+ puts reading
19
+ }
20
+ end
21
+
22
+ task :switch_off => :env do
23
+ @mpower.connect!
24
+ @mpower.switch_off
25
+ end
26
+
27
+ task :switch_on => :env do
28
+ @mpower.connect!
29
+ @mpower.switch_on
30
+ end
@@ -0,0 +1,10 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require "net/ssh"
4
+ require "json"
5
+
6
+ module MFi
7
+ Version = "0.9.0"
8
+ end
9
+
10
+ require "mpower"
@@ -0,0 +1,95 @@
1
+ module MFi
2
+
3
+ class MPowerReading
4
+
5
+ attr_accessor :port, :power, :relay, :enable, :energy, :current, :voltage, :powerfactor
6
+
7
+ def initialize port, opts
8
+ @port = port
9
+ @enable = opts["enable"].to_i
10
+ @relay = opts["relay"].to_i
11
+ @power = opts["power"].to_f
12
+ @energy = opts["energy"].to_f
13
+ @current = opts["current"].to_f
14
+ @voltage = opts["voltage"].to_f
15
+ @powerfactor = opts["powerfactor"].to_f
16
+ end
17
+
18
+ def to_s
19
+ "Port #{@port}: #{fmt(@power)}W (#{fmt(@powerfactor)}pf #{fmt(@current)}A #{fmt(@voltage)}V)"
20
+ end
21
+
22
+ def fmt v
23
+ "%5.1f" % v
24
+ end
25
+
26
+ end
27
+
28
+ class MPower
29
+
30
+ attr_accessor :host, :user, :port
31
+
32
+ #
33
+ # Create MPower instance
34
+ #
35
+ # +opts+ connection options
36
+ # host, pass and user are required for SSH
37
+ #
38
+ def initialize opts
39
+ @host = opts[:host]
40
+ @pass = opts[:pass]
41
+ @user = opts[:user]
42
+ end
43
+
44
+ # Connect to the remote MPower device
45
+ def connect!
46
+ @ssh = Net::SSH.start(@host, @user, :password => @pass)
47
+ end
48
+
49
+ # Sample all metrics from the mPower device
50
+ # +port+ the port option, default is all
51
+ def sample port=-1
52
+ data = run(:func => "powerList", :port => port)
53
+ unless data.key?("value")
54
+ raise "No data available"
55
+ end
56
+
57
+ data["value"].shift
58
+ number = 0
59
+ data["value"].map { |value| MPowerReading.new(number += 1, value) }
60
+ end
61
+
62
+ # Write enable port, allowing relay toggling
63
+ # +port+ the port option, default is all
64
+ def enable port=-1
65
+ run(:func => "enableWrite", :port => port)
66
+ end
67
+
68
+ # Write disable port, disabling relay toggling
69
+ # +port+ the port option, default is all
70
+ def disable port=-1
71
+ run(:func => "enableRead", :port => port)
72
+ end
73
+
74
+ # Switch off, or disable power on a given port
75
+ # +port+ the port option, default is all
76
+ def switch_off port=-1
77
+ run(:func => "relayWrite", :port => port, :value => 0)
78
+ end
79
+
80
+ # Switch on, or enable power on a given port
81
+ # +port+ the port option, default is all
82
+ def switch_on port=-1
83
+ run(:func => "relayWrite", :port => port, :value => 1)
84
+ end
85
+
86
+ private
87
+
88
+ def run opts
89
+ env = opts.map { |k,v| "#{k}=#{v}" }.join(" ")
90
+ cmd = "#{env} cgi -q /usr/www/mfi/io.cgi"
91
+ JSON.parse(@ssh.exec!(cmd).strip)
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "mfi"
4
+
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = "mfi"
7
+ s.version = MFi::Version
8
+ s.license = "MIT"
9
+ s.date = "2013-11-02"
10
+ s.summary = "gem for integrating with Ubiquiti mFi sensors and devices"
11
+ s.email = "dan.simpson@gmail.com"
12
+ s.homepage = "https://github.com/dansimpson/mfi"
13
+ s.description = "gem for integrating with Ubiquiti mFi sensors and devices"
14
+ s.has_rdoc = true
15
+
16
+ s.authors = ["Dan Simpson"]
17
+ s.add_dependency("ssh", ">= 1.1.0")
18
+ s.add_dependency("json", ">= 1.8.1")
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mfi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Simpson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.1
41
+ description: gem for integrating with Ubiquiti mFi sensors and devices
42
+ email: dan.simpson@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - Gemfile
48
+ - Gemfile.lock
49
+ - README.md
50
+ - Rakefile
51
+ - lib/mfi.rb
52
+ - lib/mpower.rb
53
+ - mfi.gemspec
54
+ homepage: https://github.com/dansimpson/mfi
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: gem for integrating with Ubiquiti mFi sensors and devices
78
+ test_files: []