Class: TpLinkSmartplug::Device
- Defined in:
- lib/tp_link_smartplug/device.rb
Overview
Provides an object to represent to a plug
Instance Attribute Summary collapse
-
#address ⇒ IPAddr
IP address of the plug.
-
#auto_connect ⇒ Object
Returns the value of attribute auto_connect.
-
#debug ⇒ true, false
Control debug logging.
-
#poll_auto_close ⇒ Object
Returns the value of attribute poll_auto_close.
-
#port ⇒ Integer
Port to connect to on the plug.
-
#timeout ⇒ Integer
Timeout value for connecting and sending commands to the plug.
Instance Method Summary collapse
-
#antitheft ⇒ Hash
Unsure.
-
#closed? ⇒ True, False
Return connection state closed.
-
#cloudinfo ⇒ Hash
Return plug cloud account configuration.
-
#connect ⇒ Object
Open connection to plug.
-
#countdown ⇒ Hash
Return countdown configured on the plug.
-
#disconnect ⇒ Object
(also: #close)
Close connection to plug.
-
#energy ⇒ Hash
Return plug energy data.
-
#info ⇒ Hash
Return plug information.
-
#initialize(address:, port: 9999, auto_connect: true, auto_disconnect: true) ⇒ Device
constructor
A new instance of Device.
-
#ledoff ⇒ Hash
Enable plug night mode (LED off).
-
#ledon ⇒ Hash
Disable plug night mode (LED on).
-
#off ⇒ Hash
Turn plug output off.
-
#on ⇒ Hash
Turn plug output on.
-
#open? ⇒ True, False
Return connection state open.
-
#poll(command:) ⇒ Hash
Polls plug with a command.
-
#reboot ⇒ Hash
Reboot plug.
-
#reset ⇒ Hash
Reset plug.
-
#schedule ⇒ Hash
Return schedule configured on the plug.
-
#time ⇒ Hash
Return system time from the plug.
-
#timezone ⇒ Hash
Return system timezone from the plug.
-
#wlanscan ⇒ Hash
Perform a scan for wireless SSIDs.
Methods included from Message
Methods included from Helpers
#debug_message, #nil_or_empty?
Constructor Details
#initialize(address:, port: 9999, auto_connect: true, auto_disconnect: true) ⇒ Device
Returns a new instance of Device.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tp_link_smartplug/device.rb', line 21 def initialize(address:, port: 9999, auto_connect: true, auto_disconnect: true) super() @address = IPAddr.new(address, Socket::AF_INET) @port = port @timeout = 3 @debug = false @sockaddr = Addrinfo.getaddrinfo(@address.to_s, @port, Socket::PF_INET, :STREAM, 6).first.to_sockaddr @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) @poll_auto_close = auto_disconnect @auto_connect = auto_connect connect if auto_connect end |
Instance Attribute Details
#address ⇒ IPAddr
IP address of the plug
15 16 17 |
# File 'lib/tp_link_smartplug/device.rb', line 15 def address @address end |
#auto_connect ⇒ Object
Returns the value of attribute auto_connect.
19 20 21 |
# File 'lib/tp_link_smartplug/device.rb', line 19 def auto_connect @auto_connect end |
#debug ⇒ true, false
Control debug logging
15 16 17 |
# File 'lib/tp_link_smartplug/device.rb', line 15 def debug @debug end |
#poll_auto_close ⇒ Object
Returns the value of attribute poll_auto_close.
19 20 21 |
# File 'lib/tp_link_smartplug/device.rb', line 19 def poll_auto_close @poll_auto_close end |
#port ⇒ Integer
Port to connect to on the plug
15 16 17 |
# File 'lib/tp_link_smartplug/device.rb', line 15 def port @port end |
#timeout ⇒ Integer
Timeout value for connecting and sending commands to the plug
15 16 17 |
# File 'lib/tp_link_smartplug/device.rb', line 15 def timeout @timeout end |
Instance Method Details
#antitheft ⇒ Hash
Unsure
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#closed? ⇒ True, False
Return connection state closed
93 94 95 96 97 98 99 |
# File 'lib/tp_link_smartplug/device.rb', line 93 def closed? @socket.recvfrom_nonblock(0) rescue IO::WaitReadable false rescue Errno::ENOTCONN, IOError true end |
#cloudinfo ⇒ Hash
Return plug cloud account configuration
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#connect ⇒ Object
Open connection to plug
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tp_link_smartplug/device.rb', line 38 def connect ("Connecting to #{@address} port #{@port}") if @debug ("Connecting, socket state: #{@socket.closed? ? 'closed' : 'open'}") if @debug @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) if closed? begin @socket.connect_nonblock(@sockaddr) ('Connected') if @debug rescue IO::WaitWritable if @socket.wait_writable(@timeout) begin @socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN ('Connected') if @debug rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH @socket.close raise TpLinkSmartplug::DeviceError, "Connection refused or host unreachable connecting to address #{@address}, port #{@port}!" rescue StandardError => e disconnect ("Unexpected exception encountered. Error: #{e}") if @debug raise end else disconnect raise TpLinkSmartplug::DeviceError, "Connection timeout connecting to address #{@address}, port #{@port}." end rescue Errno::EISCONN ('Connected') if @debug rescue Errno::EINPROGRESS ('Connection in progress') if @debug retry rescue Errno::ECONNREFUSED raise TpLinkSmartplug::DeviceError, "Connection refused connecting to address #{@address}, port #{@port}." end end |
#countdown ⇒ Hash
Return countdown configured on the plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#disconnect ⇒ Object Also known as: close
Close connection to plug
77 78 79 |
# File 'lib/tp_link_smartplug/device.rb', line 77 def disconnect @socket.close unless closed? end |
#energy ⇒ Hash
Return plug energy data
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#info ⇒ Hash
Return plug information
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#ledoff ⇒ Hash
Enable plug night mode (LED off)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#ledon ⇒ Hash
Disable plug night mode (LED on)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#off ⇒ Hash
Turn plug output off
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#on ⇒ Hash
Turn plug output on
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#open? ⇒ True, False
Return connection state open
86 87 88 |
# File 'lib/tp_link_smartplug/device.rb', line 86 def open? !@socket.closed? end |
#poll(command:) ⇒ Hash
Polls plug with a command
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tp_link_smartplug/device.rb', line 105 def poll(command:) connect if closed? begin ("Sending: #{decrypt(encrypt(command)[4..(command.length + 4)])}") if @debug @socket.write_nonblock(encrypt(command)) rescue IO::WaitWritable @socket.wait_writable(@timeout) retry end begin data = @socket.recv_nonblock(2048) rescue IO::WaitReadable @socket.wait_readable(@timeout) retry end if @poll_auto_close && !closed? disconnect raise DeviceError, 'Error occured during disconnect' unless closed? end raise DeviceError, 'No data received' if nil_or_empty?(data) ("Received Raw: #{data.split('\\')}") if @debug data = decrypt(data[4..data.length]) ("Received Decrypted: #{data}") if @debug data end |
#reboot ⇒ Hash
Reboot plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#reset ⇒ Hash
Reset plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#schedule ⇒ Hash
Return schedule configured on the plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#time ⇒ Hash
Return system time from the plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#timezone ⇒ Hash
Return system timezone from the plug
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#wlanscan ⇒ Hash
Perform a scan for wireless SSIDs
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tp_link_smartplug/device.rb', line 182 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :timezone, :schedule, :countdown, :antitheft, :reboot, :reset, :energy, :energygains, :ledon, :ledoff, ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |