class MyStrom::WLANSwitch

Basic binding to the HTTP API exposed by MyStrom WLAN switches.

@example Basic usage

require 'mystrom'

w = MyStrom::WLANSwitch.new('http://10.60.1.10')
puts "Current power throughput: #{ w.power }W"
if w.power > 150
  puts "Using too much power, going dark."
  w.disable
end

if rand() > 0.9
  puts "Messing with family - toggling floor lights."
  w.toggle
end

@example Proxying via SSH host

require 'mystrom'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new('jump.example.com', 'johndoe', password: 'hidden')
w = Mystrom::WLANSwitch.new('http://10.60.1.80', ssh_gateway: gateway)

Attributes

auto_refresh[RW]

If data should be refreshed after every operation @return [Bool]

power[R]

Current power throughput in W @return [Fixnum]

url[RW]

HTTP URL of the switch @return [String]

Public Class Methods

new(url, opts = {}) click to toggle source

Initialize a new instance of the class.

@param url [String] URL of the MySTrom WLAN Switch web interface. @param opts [Hash] Additional options @option opts [Bool] :auto_refresh (false) If data should be

refreshed after every operation.

@option opts [Net::SSH::Gateway] :ssh_gateway (nil) SSH gateway through

which to proxy the requests.
# File lib/mystrom/wlan_switch.rb, line 52
def initialize(url, opts = {})
  @url = url
  @auto_refresh = opts.fetch(:auto_refresh, false)
  @ssh_gateway = opts.fetch(:ssh_gateway, nil)
  update_data
end

Public Instance Methods

disable() click to toggle source

Disable the relay

@raise [APIError] If the information returned by the API was missing or

incomplete.

@return [Bool] New state of the relay.

# File lib/mystrom/wlan_switch.rb, line 80
def disable
  do_request('relay?state=0')
  if auto_refresh
    update
  else
    @relay = false
  end

  @relay
end
disabled?() click to toggle source

Whether relay is disabled.

# File lib/mystrom/wlan_switch.rb, line 121
def disabled?
  !@relay
end
enable() click to toggle source

Enable the relay.

@raise [APIError] If the information returned by the API was missing or

incomplete.

@return [Bool] New state of the relay.

# File lib/mystrom/wlan_switch.rb, line 64
def enable
  do_request('relay?state=1')
  if auto_refresh
    update
  else
    @relay = true
  end

  @relay
end
enabled?() click to toggle source

Whether relay is enabled.

# File lib/mystrom/wlan_switch.rb, line 116
def enabled?
  @relay
end
toggle() click to toggle source

Toggle the relay.

@raise [APIError] If the information returned by the API was missing or

incomplete.

@return [Bool] New state of the relay.

# File lib/mystrom/wlan_switch.rb, line 96
def toggle
  response = do_request('toggle')

  if auto_refresh
    update
  else
    begin
      data   = JSON.parse(response)
      @relay = data.fetch('relay')
    rescue JSON::ParserError => e
      raise APIError, "Returned JSON was not valid JSON (#{ e.message })"
    rescue KeyError => e
      raise APIError, "Returned JSON was missing required key (#{ e.message })"
    end
  end

  @relay
end
update() click to toggle source

Refresh data.

@raise [APIError] If the information returned by the API was missing or

incomplete.

@return [WLANSwitch] self

# File lib/mystrom/wlan_switch.rb, line 130
def update
  update_data

  self
end

Private Instance Methods

do_request(action) click to toggle source
# File lib/mystrom/wlan_switch.rb, line 150
def do_request(action)
  if @ssh_gateway
    response = do_request_ssh(action)
  else
    response = do_request_direct(action)
  end

  case response.code
  when 200
    response.body
  else
    raise APIError, "HTTP response invalid: Status code: #{ response.code }, Body: #{ response.body }"
  end
end
do_request_direct(action) click to toggle source
# File lib/mystrom/wlan_switch.rb, line 165
def do_request_direct(action)
  url = "#{ @url }/#{ action }"
  HTTParty.get(url)
end
do_request_ssh(action) click to toggle source
# File lib/mystrom/wlan_switch.rb, line 170
def do_request_ssh(action)
  uri = URI("#{ @url }/#{ action }")
  @ssh_gateway.open(uri.hostname, uri.port) do |port|
    uri.hostname = '127.0.0.1'
    uri.port     = port
    HTTParty.get(uri.to_s)
  end
end
update_data() click to toggle source
# File lib/mystrom/wlan_switch.rb, line 137
def update_data
  # TODO: Error handling
  begin
    data = JSON.parse(do_request('report'))
    @power = data.fetch('power')
    @relay = data.fetch('relay')
  rescue JSON::ParserError => e
    raise APIError, "Returned JSON was not valid JSON (#{ e.message })"
  rescue KeyError => e
    raise APIError, "Returned JSON was missing required key (#{ e.message })"
  end
end