Class: MyStrom::WLANSwitch
- Inherits:
-
Object
- Object
- MyStrom::WLANSwitch
- Defined in:
- lib/mystrom/wlan_switch.rb
Overview
Basic binding to the HTTP API exposed by MyStrom WLAN switches.
Instance Attribute Summary collapse
-
#auto_refresh ⇒ Bool
If data should be refreshed after every operation.
-
#power ⇒ Fixnum
readonly
Current power throughput in W.
-
#url ⇒ String
HTTP URL of the switch.
Instance Method Summary collapse
-
#disable ⇒ Bool
Disable the relay.
-
#disabled? ⇒ Boolean
Whether relay is disabled.
-
#enable ⇒ Bool
Enable the relay.
-
#enabled? ⇒ Boolean
Whether relay is enabled.
-
#initialize(url, opts = {}) ⇒ WLANSwitch
constructor
Initialize a new instance of the class.
-
#toggle ⇒ Bool
Toggle the relay.
-
#update ⇒ WLANSwitch
Refresh data.
Constructor Details
#initialize(url, opts = {}) ⇒ WLANSwitch
Initialize a new instance of the class.
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_refresh ⇒ Bool
If data should be refreshed after every operation
38 39 40 |
# File 'lib/mystrom/wlan_switch.rb', line 38 def auto_refresh @auto_refresh end |
#power ⇒ Fixnum (readonly)
Current power throughput in W
34 35 36 |
# File 'lib/mystrom/wlan_switch.rb', line 34 def power @power end |
#url ⇒ String
HTTP URL of the switch
42 43 44 |
# File 'lib/mystrom/wlan_switch.rb', line 42 def url @url end |
Instance Method Details
#disable ⇒ Bool
Disable the relay
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.
121 122 123 |
# File 'lib/mystrom/wlan_switch.rb', line 121 def disabled? !@relay end |
#enable ⇒ Bool
Enable the relay.
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.
116 117 118 |
# File 'lib/mystrom/wlan_switch.rb', line 116 def enabled? @relay end |
#toggle ⇒ Bool
Toggle the relay.
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. })" rescue KeyError => e raise APIError, "Returned JSON was missing required key (#{ e. })" end end @relay end |
#update ⇒ WLANSwitch
Refresh data.
130 131 132 133 134 |
# File 'lib/mystrom/wlan_switch.rb', line 130 def update update_data self end |