tp_link_smartplug 0.3.0 → 0.4.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: 2f6b7cc98865382ad2bfa186843abca5622e0e10e3bbd47b6cbbf3c053fb53fb
4
- data.tar.gz: 9581643fd63749b826acc8ada7fc9eaca0392e54d4a42f647d4912023cf89a66
3
+ metadata.gz: 1739412106dde9bd88df2a292bac503e3c7a00a41d99ea0fa5313af60cbbe873
4
+ data.tar.gz: a03b730e2e02d3829ae2f37c27dce448d1ac3ed0d2c2122096382733a53ac836
5
5
  SHA512:
6
- metadata.gz: 96069bf5ff04e8774b15cf9ef12ea8fe7cf0476d44d54c3577f16acf559aa41bbca39825e2a8c1e75c52c2b0a3b54f34ddff6707144ee3653cb7bf146273fcd4
7
- data.tar.gz: ffd67c8bf48cae8cc2761390cf04b01829d6f1e03808e38d80c124f61b08204805bf629399f3fa9353e1e5edccf4f3983a5093a7a7721d96594f0a1ca4605a97
6
+ metadata.gz: 35bd08c5cacc7e8d9a68bd8a48c3d232f8ae77c5875809d5125fa0c218c7dc260b6633c463f325fcf0ea15eda959028699ab06b5674825ef4af96788cf243e5e
7
+ data.tar.gz: 5e4ea23acfebad26a9410d0b8c1a0cd7ea755bb3847946e412100375b223ffeaec057964acb3a3a9e153d763b32a1941486babcad6d71a760cb0ad615f1986b9
@@ -6,8 +6,6 @@ module TpLinkSmartplug
6
6
  # @author Ben Hughes
7
7
  class Base
8
8
  include TpLinkSmartplug::Helpers
9
-
10
- def initialize; end
11
9
  end
12
10
 
13
11
  # Base plug error class.
@@ -13,6 +13,8 @@ module TpLinkSmartplug
13
13
  WLANSCAN = '{"netif":{"get_scaninfo":{"refresh":0}}}'.freeze
14
14
  # Plug time command
15
15
  TIME = '{"time":{"get_time":{}}}'.freeze
16
+ # Plug timezone command
17
+ TIMEZONE = '{"time":{"get_timezone":{}}}'.freeze
16
18
  # Plug schedule command
17
19
  SCHEDULE = '{"schedule":{"get_rules":{}}}'.freeze
18
20
  # Plug countdown command
@@ -25,5 +27,11 @@ module TpLinkSmartplug
25
27
  RESET = '{"system":{"reset":{"delay":1}}}'.freeze
26
28
  # Plug energy command
27
29
  ENERGY = '{"emeter":{"get_realtime":{}}}'.freeze
30
+ # Plug energy VGain/IGain settings
31
+ ENERGYGAINS = '{"emeter":{"get_vgain_igain":{}}}'.freeze
32
+ # Plug LED On
33
+ LEDON = '{"system":{"set_led_off":{"off":0}}}'.freeze
34
+ # Plug LED Off
35
+ LEDOFF = '{"system":{"set_led_off":{"off":1}}}'.freeze
28
36
  end
29
37
  end
@@ -1,8 +1,9 @@
1
1
  require 'socket'
2
2
  require 'ipaddr'
3
3
  require 'json'
4
- require_relative './message'
4
+ require_relative 'message'
5
5
 
6
+ # Top level module
6
7
  module TpLinkSmartplug
7
8
  # Provides an object to represent to a plug
8
9
  #
@@ -15,9 +16,9 @@ module TpLinkSmartplug
15
16
  include TpLinkSmartplug::Helpers
16
17
  include TpLinkSmartplug::Message
17
18
 
18
- attr_accessor :address, :port, :timeout, :poll_auto_close, :debug
19
+ attr_accessor :address, :port, :timeout, :poll_auto_close, :auto_connect, :debug
19
20
 
20
- def initialize(address:, port: 9999)
21
+ def initialize(address:, port: 9999, auto_connect: true, auto_disconnect: true)
21
22
  super()
22
23
 
23
24
  @address = IPAddr.new(address, Socket::AF_INET)
@@ -26,7 +27,10 @@ module TpLinkSmartplug
26
27
  @debug = false
27
28
  @sockaddr = Addrinfo.getaddrinfo(@address.to_s, @port, Socket::PF_INET, :STREAM, 6).first.to_sockaddr
28
29
  @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
29
- @poll_auto_close = true
30
+ @poll_auto_close = auto_disconnect
31
+ @auto_connect = auto_connect
32
+
33
+ connect if auto_connect
30
34
  end
31
35
 
32
36
  # Open connection to plug
@@ -41,7 +45,7 @@ module TpLinkSmartplug
41
45
  @socket.connect_nonblock(@sockaddr)
42
46
  debug_message('Connected') if @debug
43
47
  rescue IO::WaitWritable
44
- if @socket.wait_writable(timeout)
48
+ if @socket.wait_writable(@timeout)
45
49
  begin
46
50
  @socket.connect_nonblock(@sockaddr)
47
51
  rescue Errno::EISCONN
@@ -55,7 +59,7 @@ module TpLinkSmartplug
55
59
  raise
56
60
  end
57
61
  else
58
- @socket.close
62
+ disconnect
59
63
  raise TpLinkSmartplug::DeviceError, "Connection timeout connecting to address #{@address}, port #{@port}."
60
64
  end
61
65
  rescue Errno::EISCONN
@@ -71,7 +75,7 @@ module TpLinkSmartplug
71
75
  # Close connection to plug
72
76
  #
73
77
  def disconnect
74
- @socket.close unless @socket.closed?
78
+ @socket.close unless closed?
75
79
  end
76
80
 
77
81
  alias_method :close, :disconnect
@@ -87,7 +91,11 @@ module TpLinkSmartplug
87
91
  #
88
92
  # @return [True, False]
89
93
  def closed?
90
- @socket.closed?
94
+ @socket.recvfrom_nonblock(0)
95
+ rescue IO::WaitReadable
96
+ false
97
+ rescue Errno::ENOTCONN, IOError
98
+ true
91
99
  end
92
100
 
93
101
  # Polls plug with a command
@@ -95,29 +103,29 @@ module TpLinkSmartplug
95
103
  # @param command [String] the command to send to the plug
96
104
  # @return [Hash] the output from the plug command
97
105
  def poll(command:)
98
- connect
106
+ connect if closed?
99
107
 
100
108
  begin
101
109
  debug_message("Sending: #{decrypt(encrypt(command)[4..(command.length + 4)])}") if @debug
102
110
  @socket.write_nonblock(encrypt(command))
103
111
  rescue IO::WaitWritable
104
- IO.select(nil, [@socket])
112
+ @socket.wait_writable(@timeout)
105
113
  retry
106
114
  end
107
115
 
108
116
  begin
109
117
  data = @socket.recv_nonblock(2048)
110
118
  rescue IO::WaitReadable
111
- IO.select([@socket])
119
+ @socket.wait_readable(@timeout)
112
120
  retry
113
121
  end
114
122
 
115
123
  if @poll_auto_close && !closed?
116
124
  disconnect
117
- raise 'Error occured during disconnect' unless closed?
125
+ raise DeviceError, 'Error occured during disconnect' unless closed?
118
126
  end
119
127
 
120
- raise 'No data received' if nil_or_empty?(data)
128
+ raise DeviceError, 'No data received' if nil_or_empty?(data)
121
129
 
122
130
  debug_message("Received Raw: #{data.split('\\')}") if @debug
123
131
  data = decrypt(data[4..data.length])
@@ -144,6 +152,9 @@ module TpLinkSmartplug
144
152
  # @!method time
145
153
  # Return system time from the plug
146
154
  # @return [Hash]
155
+ # @!method timezone
156
+ # Return system timezone from the plug
157
+ # @return [Hash]
147
158
  # @!method schedule
148
159
  # Return schedule configured on the plug
149
160
  # @return [Hash]
@@ -156,12 +167,18 @@ module TpLinkSmartplug
156
167
  # @!method reboot
157
168
  # Reboot plug
158
169
  # @return [Hash]
159
- # @!method resry
170
+ # @!method reset
160
171
  # Reset plug
161
172
  # @return [Hash]
162
173
  # @!method energy
163
174
  # Return plug energy data
164
175
  # @return [Hash]
176
+ # @!method ledon
177
+ # Disable plug night mode (LED on)
178
+ # @return [Hash]
179
+ # @!method ledoff
180
+ # Enable plug night mode (LED off)
181
+ # @return [Hash]
165
182
  [
166
183
  :info,
167
184
  :on,
@@ -169,12 +186,16 @@ module TpLinkSmartplug
169
186
  :cloudinfo,
170
187
  :wlanscan,
171
188
  :time,
189
+ :timezone,
172
190
  :schedule,
173
191
  :countdown,
174
192
  :antitheft,
175
193
  :reboot,
176
194
  :reset,
177
195
  :energy,
196
+ :energygains,
197
+ :ledon,
198
+ :ledoff,
178
199
  ].each do |method|
179
200
  define_method method do
180
201
  JSON.parse(poll(command: TpLinkSmartplug::Command.const_get(method.upcase)))
@@ -182,5 +203,22 @@ module TpLinkSmartplug
182
203
  end
183
204
  end
184
205
 
206
+ # Set plug alias
207
+ #
208
+ # @param name [String] the name to assign to the plug alias
209
+ # @return [Hash] the output from the plug command
210
+ def alias=(name)
211
+ JSON.parse(poll(command: "{\"system\":{\"set_dev_alias\":{\"alias\":\"#{name}\"}}}"))
212
+ end
213
+
214
+ # Set plug location
215
+ #
216
+ # @param longitude [String] the longitude to assign to the plug location
217
+ # @param lattitude [String] the lattitude to assign to the plug location
218
+ # @return [Hash] the output from the plug command
219
+ def location!(longitude, lattitude)
220
+ JSON.parse(poll(command: "{\"system\":{\"set_dev_location\":{\"longitude\":\"#{lattitude}\", \"latitude\":\"#{longitude}\"}}}"))
221
+ end
222
+
185
223
  class DeviceError < TpLinkSmartplug::BaseError; end
186
224
  end
@@ -1,5 +1,5 @@
1
1
  # @author Ben Hughes
2
2
  module TpLinkSmartplug
3
3
  # Gem version
4
- VERSION = '0.3.0'.freeze
4
+ VERSION = '0.4.0'.freeze
5
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.3.0
4
+ version: 0.4.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: 2021-10-03 00:00:00.000000000 Z
11
+ date: 2024-01-19 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
@@ -28,6 +28,7 @@ licenses:
28
28
  - Apache-2.0
29
29
  metadata:
30
30
  github_repo: ssh://github.com/bmhughes/tp_link_smartplug
31
+ rubygems_mfa_required: 'true'
31
32
  post_install_message:
32
33
  rdoc_options: []
33
34
  require_paths:
@@ -43,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  - !ruby/object:Gem::Version
44
45
  version: '0'
45
46
  requirements: []
46
- rubygems_version: 3.2.22
47
+ rubygems_version: 3.3.26
47
48
  signing_key:
48
49
  specification_version: 4
49
50
  summary: TP-Link HS100/110 Smart Plug interaction library