Class: MFi::MPower

Inherits:
Object
  • Object
show all
Defined in:
lib/mpower.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ MPower

Create MPower instance

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



38
39
40
41
42
# File 'lib/mpower.rb', line 38

def initialize opts
  @host = opts[:host]
  @pass = opts[:pass]
  @user = opts[:user]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



30
31
32
# File 'lib/mpower.rb', line 30

def host
  @host
end

#portObject

Returns the value of attribute port.



30
31
32
# File 'lib/mpower.rb', line 30

def port
  @port
end

#userObject

Returns the value of attribute user.



30
31
32
# File 'lib/mpower.rb', line 30

def user
  @user
end

Instance Method Details

#connect!Object

Connect to the remote MPower device



45
46
47
# File 'lib/mpower.rb', line 45

def connect!
  @ssh = Net::SSH.start(@host, @user, :password => @pass)
end

#enable_read(port = -1) ⇒ Object

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



78
79
80
# File 'lib/mpower.rb', line 78

def enable_read port=-1
  run(:func => "enableRead", :port => port, :value => 1)
end

#enable_write(port = -1) ⇒ Object

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



72
73
74
# File 'lib/mpower.rb', line 72

def enable_write port=-1
  run(:func => "enableWrite", :port => port, :value => 1)
end

#exec(&block) ⇒ Object

execute commands in a given execution context



50
51
52
53
54
55
# File 'lib/mpower.rb', line 50

def exec &block
  connect!
  yield self
ensure
  disconnect!
end

#sample(port = -1) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

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



84
85
86
# File 'lib/mpower.rb', line 84

def switch_off port=-1
  run(:func => "relayWrite", :port => port, :value => 0)
end

#switch_on(port = -1) ⇒ Object

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



90
91
92
# File 'lib/mpower.rb', line 90

def switch_on port=-1
  run(:func => "relayWrite", :port => port, :value => 1)
end