tp_link_smartplug 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eac4dd14ffb0af2de298ea34c8084b954b927450ea1548f86cffdcebfbb70686
4
- data.tar.gz: 2d459cc145b1c311e049a2af193bbb8141b19c6be933acd4a12244d83ff11d9a
3
+ metadata.gz: 41a05841cdc6ffdd697f903abc1d620debf999f2a9a6c5f248edc9ec93635e30
4
+ data.tar.gz: c3f77299aaeb6b90506f04730467c70889745d85e47148ba738ccc82de484cbd
5
5
  SHA512:
6
- metadata.gz: ab4e8ba2f988b6f3b983706eed8dfbdcff0d97f6acb625235b55ddad803f423bc65ab12b8cca93e84b7f3d398b639289453250f82ae799e161f8811e4d9e5b96
7
- data.tar.gz: 650a5edc7d2b10a9e67712b6587c1db755c3d700dc412e89820b79890445bac0c9997b9e5c95115b643e9aa0fe90b74d00d5d4a7b4381483c319641843320e59
6
+ metadata.gz: eac7e52a043aebdeeb595aba15df19be7923d2da40a6d214bb41955f4baf4da1e2853e935cd7ade3b410981bd8698024f9598a923b20a5c0311ed2398dbced01
7
+ data.tar.gz: 74f0ba6ff80393ee43560a76ec6cab25d94fb1b8b5b3b078884be64b9db274e657053f2c90007df89aa2bde7e849b63a049e4628c91ff4bb90cf1e29b59c7d23
@@ -1,7 +1,11 @@
1
- # frozen_string_literal: true
1
+ require_relative 'tp_link_smartplug/_base'
2
+ require_relative 'tp_link_smartplug/command'
3
+ require_relative 'tp_link_smartplug/device'
4
+ require_relative 'tp_link_smartplug/helpers'
5
+ require_relative 'tp_link_smartplug/message'
6
+ require_relative 'tp_link_smartplug/version'
2
7
 
3
- require 'tp_link_smartplug/command'
4
- require 'tp_link_smartplug/device'
5
- require 'tp_link_smartplug/helpers'
6
- require 'tp_link_smartplug/message'
7
- require 'tp_link_smartplug/version'
8
+ # Top level module
9
+ #
10
+ # @author Ben Hughes
11
+ module TpLinkSmartplug; end
@@ -0,0 +1,15 @@
1
+ require_relative 'helpers'
2
+
3
+ module TpLinkSmartplug
4
+ # Base plug class.
5
+ #
6
+ # @author Ben Hughes
7
+ class Base
8
+ include TpLinkSmartplug::Helpers
9
+ end
10
+
11
+ # Base plug error class.
12
+ #
13
+ # @author Ben Hughes
14
+ class BaseError < StandardError; end
15
+ end
@@ -1,31 +1,29 @@
1
- # frozen_string_literal: true
2
-
3
1
  module TpLinkSmartplug
4
2
  # Module containing the static commands for the plug
5
3
  module Command
6
4
  # Plug information command
7
- INFO = '{"system":{"get_sysinfo":{}}}'
5
+ INFO = '{"system":{"get_sysinfo":{}}}'.freeze
8
6
  # Plug output on command
9
- ON = '{"system":{"set_relay_state":{"state":1}}}'
7
+ ON = '{"system":{"set_relay_state":{"state":1}}}'.freeze
10
8
  # Plug output off command
11
- OFF = '{"system":{"set_relay_state":{"state":0}}}'
9
+ OFF = '{"system":{"set_relay_state":{"state":0}}}'.freeze
12
10
  # Plug cloud info command
13
- CLOUDINFO = '{"cnCloud":{"get_info":{}}}'
11
+ CLOUDINFO = '{"cnCloud":{"get_info":{}}}'.freeze
14
12
  # Plug WLAN SSID scan command
15
- WLANSCAN = '{"netif":{"get_scaninfo":{"refresh":0}}}'
13
+ WLANSCAN = '{"netif":{"get_scaninfo":{"refresh":0}}}'.freeze
16
14
  # Plug time command
17
- TIME = '{"time":{"get_time":{}}}'
15
+ TIME = '{"time":{"get_time":{}}}'.freeze
18
16
  # Plug schedule command
19
- SCHEDULE = '{"schedule":{"get_rules":{}}}'
17
+ SCHEDULE = '{"schedule":{"get_rules":{}}}'.freeze
20
18
  # Plug countdown command
21
- COUNTDOWN = '{"count_down":{"get_rules":{}}}'
19
+ COUNTDOWN = '{"count_down":{"get_rules":{}}}'.freeze
22
20
  # Plug antitheft command
23
- ANTITHEFT = '{"anti_theft":{"get_rules":{}}}'
21
+ ANTITHEFT = '{"anti_theft":{"get_rules":{}}}'.freeze
24
22
  # Plug reboot command
25
- REBOOT = '{"system":{"reboot":{"delay":1}}}'
23
+ REBOOT = '{"system":{"reboot":{"delay":1}}}'.freeze
26
24
  # Plug reset command
27
- RESET = '{"system":{"reset":{"delay":1}}}'
25
+ RESET = '{"system":{"reset":{"delay":1}}}'.freeze
28
26
  # Plug energy command
29
- ENERGY = '{"emeter":{"get_realtime":{}}}'
27
+ ENERGY = '{"emeter":{"get_realtime":{}}}'.freeze
30
28
  end
31
29
  end
@@ -1,10 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'socket'
4
2
  require 'ipaddr'
5
3
  require 'json'
6
- require 'tp_link_smartplug/helpers'
7
- require 'tp_link_smartplug/message'
4
+ require_relative './message'
8
5
 
9
6
  module TpLinkSmartplug
10
7
  # Provides an object to represent to a plug
@@ -14,17 +11,15 @@ module TpLinkSmartplug
14
11
  # @attr [Integer] port Port to connect to on the plug
15
12
  # @attr [Integer] timeout Timeout value for connecting and sending commands to the plug
16
13
  # @attr [true, false] debug Control debug logging
17
- class Device
14
+ class Device < TpLinkSmartplug::Base
18
15
  include TpLinkSmartplug::Helpers
19
16
  include TpLinkSmartplug::Message
20
17
 
21
- attr_accessor :address
22
- attr_accessor :port
23
- attr_accessor :timeout
24
- attr_accessor :poll_auto_close
25
- attr_accessor :debug
18
+ attr_accessor :address, :port, :timeout, :poll_auto_close, :debug
26
19
 
27
20
  def initialize(address:, port: 9999)
21
+ super
22
+
28
23
  @address = IPAddr.new(address, Socket::AF_INET)
29
24
  @port = port
30
25
  @timeout = 3
@@ -44,23 +39,32 @@ module TpLinkSmartplug
44
39
 
45
40
  begin
46
41
  @socket.connect_nonblock(@sockaddr)
42
+ debug_message('Connected') if @debug
47
43
  rescue IO::WaitWritable
48
44
  if IO.select(nil, [@socket], nil, timeout)
49
45
  begin
50
46
  @socket.connect_nonblock(@sockaddr)
51
47
  rescue Errno::EISCONN
52
- debug_message('Connected') if debug
48
+ debug_message('Connected') if @debug
49
+ rescue Errno::ECONNREFUSED
50
+ @socket.close
51
+ raise TpLinkSmartplug::DeviceError, "Connection refused connecting to address #{@address}, port #{@port}!"
53
52
  rescue StandardError => e
54
53
  disconnect
55
- debug_message('Unexpected exception encountered.') if debug
56
- raise e
54
+ debug_message("Unexpected exception encountered. Error: #{e}") if @debug
55
+ raise
57
56
  end
58
57
  else
59
58
  @socket.close
60
- raise "Connection timeout connecting to address #{@address}, port #{@port}."
59
+ raise TpLinkSmartplug::DeviceError, "Connection timeout connecting to address #{@address}, port #{@port}."
61
60
  end
62
61
  rescue Errno::EISCONN
63
- debug_message('Connected') if debug
62
+ debug_message('Connected') if @debug
63
+ rescue Errno::EINPROGRESS
64
+ debug_message('Connection in progress') if @debug
65
+ retry
66
+ rescue Errno::ECONNREFUSED
67
+ raise TpLinkSmartplug::DeviceError, "Connection refused connecting to address #{@address}, port #{@port}."
64
68
  end
65
69
  end
66
70
 
@@ -113,11 +117,11 @@ module TpLinkSmartplug
113
117
  raise 'Error occured during disconnect' unless closed?
114
118
  end
115
119
 
116
- raise 'No data received' if data.nil? || data.empty?
120
+ raise 'No data received' if nil_or_empty?(data)
117
121
 
118
- debug_message("Received Raw: #{data}") if debug
122
+ debug_message("Received Raw: #{data}") if @debug
119
123
  data = decrypt(data[4..data.length])
120
- debug_message("Received Decrypted: #{data}") if debug
124
+ debug_message("Received Decrypted: #{data}") if @debug
121
125
 
122
126
  data
123
127
  end
@@ -170,11 +174,13 @@ module TpLinkSmartplug
170
174
  :antitheft,
171
175
  :reboot,
172
176
  :reset,
173
- :energy
177
+ :energy,
174
178
  ].each do |method|
175
179
  define_method method do
176
180
  JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase)))
177
181
  end
178
182
  end
179
183
  end
184
+
185
+ class DeviceError < TpLinkSmartplug::BaseError; end
180
186
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  require 'time'
4
2
 
5
3
  module TpLinkSmartplug
@@ -10,7 +8,16 @@ module TpLinkSmartplug
10
8
  # @param string [String] the message to be formatted for debug output
11
9
  def debug_message(string)
12
10
  caller_method = caller_locations(1..1).first.label
13
- STDOUT.puts(Time.now.strftime('%Y-%m-%d %H:%M:%S: ').concat("#{caller_method}: ").concat(string))
11
+ $stdout.puts(Time.now.strftime('%Y-%m-%d %H:%M:%S: ').concat("#{caller_method}: ").concat(string))
12
+ end
13
+
14
+ # Tests a variable for nil or empty status
15
+ #
16
+ # @param v the variable to be tested
17
+ def nil_or_empty?(value)
18
+ return true if value.nil? || (value.respond_to?(:empty?) && value.empty?)
19
+
20
+ false
14
21
  end
15
22
  end
16
23
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: false
2
-
3
1
  module TpLinkSmartplug
4
2
  # Helper methods for plug communication messages
5
3
  module Message
@@ -1,7 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
1
  # @author Ben Hughes
4
2
  module TpLinkSmartplug
5
3
  # Gem version
6
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'.freeze
7
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp_link_smartplug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-26 00:00:00.000000000 Z
11
+ date: 2021-01-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Control and retrieve data from a TP-Link HS100/110 (Metered) Smartplug
14
14
  email: bmhughes@bmhughes.co.uk
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - "./lib/tp_link_smartplug.rb"
20
+ - "./lib/tp_link_smartplug/_base.rb"
20
21
  - "./lib/tp_link_smartplug/command.rb"
21
22
  - "./lib/tp_link_smartplug/device.rb"
22
23
  - "./lib/tp_link_smartplug/helpers.rb"
@@ -41,8 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
42
  - !ruby/object:Gem::Version
42
43
  version: '0'
43
44
  requirements: []
44
- rubyforge_project:
45
- rubygems_version: 2.7.7
45
+ rubygems_version: 3.1.4
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: TP-Link HS100/110 Smart Plug interaction library