Class: TpLinkSmartplug::Device
- Inherits:
-
Object
- Object
- 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.
-
#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) ⇒ Device
constructor
A new instance of Device.
-
#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.
-
#resry ⇒ Hash
Reset plug.
-
#schedule ⇒ Hash
Return schedule configured on the plug.
-
#time ⇒ Hash
Return system time from the plug.
-
#wlanscan ⇒ Hash
Perform a scan for wireless SSIDs.
Methods included from Message
Methods included from Helpers
Constructor Details
#initialize(address:, port: 9999) ⇒ Device
Returns a new instance of Device.
27 28 29 30 31 32 33 34 35 |
# File 'lib/tp_link_smartplug/device.rb', line 27 def initialize(address:, port: 9999) @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 = true end |
Instance Attribute Details
#address ⇒ IPAddr
IP address of the plug
17 18 19 |
# File 'lib/tp_link_smartplug/device.rb', line 17 def address @address end |
#debug ⇒ true, false
Control debug logging
17 18 19 |
# File 'lib/tp_link_smartplug/device.rb', line 17 def debug @debug end |
#poll_auto_close ⇒ Object
Returns the value of attribute poll_auto_close.
24 25 26 |
# File 'lib/tp_link_smartplug/device.rb', line 24 def poll_auto_close @poll_auto_close end |
#port ⇒ Integer
Port to connect to on the plug
17 18 19 |
# File 'lib/tp_link_smartplug/device.rb', line 17 def port @port end |
#timeout ⇒ Integer
Timeout value for connecting and sending commands to the plug
17 18 19 |
# File 'lib/tp_link_smartplug/device.rb', line 17 def timeout @timeout end |
Instance Method Details
#antitheft ⇒ Hash
Unsure
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
85 86 87 |
# File 'lib/tp_link_smartplug/device.rb', line 85 def closed? @socket.closed? end |
#cloudinfo ⇒ Hash
Return plug cloud account configuration
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
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 |
# File 'lib/tp_link_smartplug/device.rb', line 39 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) rescue IO::WaitWritable if IO.select(nil, [@socket], nil, timeout) begin @socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN ('Connected') if debug rescue StandardError => e disconnect ('Unexpected exception encountered.') if debug raise e end else @socket.close raise "Connection timeout connecting to address #{@address}, port #{@port}." end rescue Errno::EISCONN ('Connected') if debug end end |
#countdown ⇒ Hash
Return countdown configured on the plug
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
69 70 71 |
# File 'lib/tp_link_smartplug/device.rb', line 69 def disconnect @socket.close unless @socket.closed? end |
#energy ⇒ Hash
Return plug energy data
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#info ⇒ Hash
Return plug information
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
78 79 80 |
# File 'lib/tp_link_smartplug/device.rb', line 78 def open? !@socket.closed? end |
#poll(command:) ⇒ Hash
Polls plug with a command
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tp_link_smartplug/device.rb', line 93 def poll(command:) connect begin ("Sending: #{decrypt(encrypt(command)[4..(command.length + 4)])}") if @debug @socket.write_nonblock(encrypt(command)) rescue IO::WaitWritable IO.select(nil, [@socket]) retry end begin data = @socket.recv_nonblock(2048) rescue IO::WaitReadable IO.select([@socket]) retry end if @poll_auto_close && !closed? disconnect raise 'Error occured during disconnect' unless closed? end raise 'No data received' if data.nil? || data.empty? ("Received Raw: #{data}") if debug data = decrypt(data[4..data.length]) ("Received Decrypted: #{data}") if debug data end |
#reboot ⇒ Hash
Reboot plug
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |
#resry ⇒ Hash
Reset plug
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tp_link_smartplug/device.rb', line 161 [ :info, :on, :off, :cloudinfo, :wlanscan, :time, :schedule, :countdown, :antitheft, :reboot, :reset, :energy ].each do |method| define_method method do JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase))) end end |