tp_link_smartplug 0.1.0.beta1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6632a7cf74131ed5ee449e11777980bfed90faf60451d2d8a2046cda4ec4dcb0
4
- data.tar.gz: 2800e192dfbec13272642bd3b7eb1c7112cad55dafe2a112fbb3dcf189b7b1cb
3
+ metadata.gz: 2f6b7cc98865382ad2bfa186843abca5622e0e10e3bbd47b6cbbf3c053fb53fb
4
+ data.tar.gz: 9581643fd63749b826acc8ada7fc9eaca0392e54d4a42f647d4912023cf89a66
5
5
  SHA512:
6
- metadata.gz: 12e564bf4285c62dc25eac91809137b883c0e8c314febc11cfdac4578d6242a3133c8384232f7dbc405af83a62deb1e39ab2470970603d79a793980d3d03f1b8
7
- data.tar.gz: 52d5ef5eaeec2d5bc10ae2a0669156901419f7b2e205cec6bccba550a4ed96163234fe7583f76184bca94de0d15a2ec239dcefd0818faa3cdc4814ff6e535b33
6
+ metadata.gz: 96069bf5ff04e8774b15cf9ef12ea8fe7cf0476d44d54c3577f16acf559aa41bbca39825e2a8c1e75c52c2b0a3b54f34ddff6707144ee3653cb7bf146273fcd4
7
+ data.tar.gz: ffd67c8bf48cae8cc2761390cf04b01829d6f1e03808e38d80c124f61b08204805bf629399f3fa9353e1e5edccf4f3983a5093a7a7721d96594f0a1ca4605a97
@@ -0,0 +1,17 @@
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
+
10
+ def initialize; end
11
+ end
12
+
13
+ # Base plug error class.
14
+ #
15
+ # @author Ben Hughes
16
+ class BaseError < StandardError; end
17
+ 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
- if IO.select(nil, [@socket], nil, timeout)
44
+ if @socket.wait_writable(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, Errno::EHOSTUNREACH
50
+ @socket.close
51
+ raise TpLinkSmartplug::DeviceError, "Connection refused or host unreachable 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.split('\\')}") 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.0.1'
4
+ VERSION = '0.3.0'.freeze
7
5
  end
@@ -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
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.beta1
4
+ version: 0.3.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-14 00:00:00.000000000 Z
11
+ date: 2021-10-03 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"
@@ -25,7 +26,8 @@ files:
25
26
  homepage: https://github.com/bmhughes/tp_link_smartplug
26
27
  licenses:
27
28
  - Apache-2.0
28
- metadata: {}
29
+ metadata:
30
+ github_repo: ssh://github.com/bmhughes/tp_link_smartplug
29
31
  post_install_message:
30
32
  rdoc_options: []
31
33
  require_paths:
@@ -37,12 +39,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
39
  version: '2.5'
38
40
  required_rubygems_version: !ruby/object:Gem::Requirement
39
41
  requirements:
40
- - - ">"
42
+ - - ">="
41
43
  - !ruby/object:Gem::Version
42
- version: 1.3.1
44
+ version: '0'
43
45
  requirements: []
44
- rubyforge_project:
45
- rubygems_version: 2.7.7
46
+ rubygems_version: 3.2.22
46
47
  signing_key:
47
48
  specification_version: 4
48
49
  summary: TP-Link HS100/110 Smart Plug interaction library