Class: MyStrom::WLANSwitch

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

Overview

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

Examples:

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

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ WLANSwitch

Initialize a new instance of the class.

Parameters:

  • url (String)

    URL of the MySTrom WLAN Switch web interface.

  • opts (Hash) (defaults to: {})

    Additional options

Options Hash (opts):

  • :auto_refresh (Bool) — default: false

    If data should be refreshed after every operation.

  • :ssh_gateway (Net::SSH::Gateway) — default: nil

    SSH gateway through which to proxy the requests.



52
53
54
55
56
57
# 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

Instance Attribute Details

#auto_refreshBool

If data should be refreshed after every operation

Returns:

  • (Bool)


38
39
40
# File 'lib/mystrom/wlan_switch.rb', line 38

def auto_refresh
  @auto_refresh
end

#powerFixnum (readonly)

Current power throughput in W

Returns:

  • (Fixnum)


34
35
36
# File 'lib/mystrom/wlan_switch.rb', line 34

def power
  @power
end

#urlString

HTTP URL of the switch

Returns:

  • (String)


42
43
44
# File 'lib/mystrom/wlan_switch.rb', line 42

def url
  @url
end

Instance Method Details

#disableBool

Disable the relay

Returns:

  • (Bool)

    New state of the relay.

Raises:

  • (APIError)

    If the information returned by the API was missing or incomplete.



80
81
82
83
84
85
86
87
88
89
# 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?Boolean

Whether relay is disabled.

Returns:

  • (Boolean)


121
122
123
# File 'lib/mystrom/wlan_switch.rb', line 121

def disabled?
  !@relay
end

#enableBool

Enable the relay.

Returns:

  • (Bool)

    New state of the relay.

Raises:

  • (APIError)

    If the information returned by the API was missing or incomplete.



64
65
66
67
68
69
70
71
72
73
# 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?Boolean

Whether relay is enabled.

Returns:

  • (Boolean)


116
117
118
# File 'lib/mystrom/wlan_switch.rb', line 116

def enabled?
  @relay
end

#toggleBool

Toggle the relay.

Returns:

  • (Bool)

    New state of the relay.

Raises:

  • (APIError)

    If the information returned by the API was missing or incomplete.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

#updateWLANSwitch

Refresh data.

Returns:

Raises:

  • (APIError)

    If the information returned by the API was missing or incomplete.



130
131
132
133
134
# File 'lib/mystrom/wlan_switch.rb', line 130

def update
  update_data

  self
end