class MFi::MPower

Attributes

host[RW]
port[RW]
user[RW]

Public Class Methods

new(opts) click to toggle source

Create MPower instance

opts connection options host, pass and user are required for SSH

# File lib/mpower.rb, line 38
def initialize opts
  @host = opts[:host]
  @pass = opts[:pass]
  @user = opts[:user]
end

Public Instance Methods

connect!() click to toggle source

Connect to the remote MPower device

# File lib/mpower.rb, line 45
def connect!
  @ssh = Net::SSH.start(@host, @user, :password => @pass)
end
enable_read(port=-1) click to toggle source

Write disable port, disabling relay toggling port the port option, default is all

# File lib/mpower.rb, line 78
def enable_read port=-1
  run(:func => "enableRead", :port => port, :value => 1)
end
enable_write(port=-1) click to toggle source

Write enable port, allowing relay toggling port the port option, default is all

# File lib/mpower.rb, line 72
def enable_write port=-1
  run(:func => "enableWrite", :port => port, :value => 1)
end
exec() { |self| ... } click to toggle source

execute commands in a given execution context

# File lib/mpower.rb, line 50
def exec &block
  connect!
  yield self
ensure
  disconnect!
end
sample(port=-1) click to toggle source

Sample all metrics from the mPower device port the port option, default is all

# File lib/mpower.rb, line 59
def sample port=-1
  data = run(:func => "powerList", :port => port)
  unless data.key?("value")
    raise "No data available"
  end

  data["value"].shift
  number = 0
  data["value"].map { |value| MPowerReading.new(number += 1, value) }
end
switch_off(port=-1) click to toggle source

Switch off, or disable power on a given port port the port option, default is all

# File lib/mpower.rb, line 84
def switch_off port=-1
  run(:func => "relayWrite", :port => port, :value => 0)
end
switch_on(port=-1) click to toggle source

Switch on, or enable power on a given port port the port option, default is all

# File lib/mpower.rb, line 90
def switch_on port=-1
  run(:func => "relayWrite", :port => port, :value => 1)
end

Private Instance Methods

disconnect!() click to toggle source
# File lib/mpower.rb, line 96
def disconnect!
  @ssh.close
end
run(opts) click to toggle source
# File lib/mpower.rb, line 100
def run opts
  env = opts.map { |k,v| "#{k}=#{v}" }.join(" ")
  cmd = "#{env} cgi -q /usr/www/mfi/io.cgi"
  JSON.parse(@ssh.exec!(cmd).strip)
end