ruby-dovado 1.0.2 → 1.0.3

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
  SHA1:
3
- metadata.gz: 5e7c4e1e5304745d859175e5a713dd34a1cf0ded
4
- data.tar.gz: 928808f3fb205fa3eb4f6850603dfc2fe04f04f1
3
+ metadata.gz: 00da334c3de14089a89e7dd33787879ca4f59440
4
+ data.tar.gz: b138d5e2a2ffa2b9075b9509535a138b466f8eb6
5
5
  SHA512:
6
- metadata.gz: 05c59b4500d3a2e9866d0b05c16b89dd043e33daf464ae7f7ce91bfcce7e9d1202380659caad73843976040a38170bb8243028639955641de9d7fb76de9b44b2
7
- data.tar.gz: 8da24c1d83c61cd4e4c8ada42617ec5416146fe7a800fb68c6e586954ecd9c545c899d92adcf57fb1d29b33190e9e1cff11aa8771442ae1f1082a5494183825c
6
+ metadata.gz: bacfb7aac3dcf237dc7d5a75286d94814a9d3618dfecc2a7e9852cc4be3a49608ed31b25376109a30df53b2eb837707be888dd94d2dc8f3f9af3b0c5ace5c71a
7
+ data.tar.gz: 5a2605658f78bf55b34a5cc55d94ad859e1d81f4a06e44118b4a47b0f957c440f40ecd28d0e302987c52f8a71f1fb81698a647b410ad27a4bd3dbc1e2faff168
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .env
data/README.md CHANGED
@@ -7,12 +7,14 @@ A Dovado Router API for Ruby.
7
7
  [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg?style=flat-square)](http://www.rubydoc.info/gems/ruby-dovado/frames)
8
8
  [![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg?style=flat-square)](#copyright)
9
9
 
10
- This library serves to enable easy access to the built in, Telnet-based, rudimentary API of the [routers from Dovado](http://www.dovado.com/en/products) running software version 6 and 7 (applies to the original Tiny and Go routers, among others). It might work with software version 8 routers (the Tiny AC) too but I have no means to test against that since I don't have one of the later routers.
10
+ This library serves to enable easy access to the built in, Telnet-based, rudimentary API of the [routers from Dovado](http://www.dovado.com/en/products) running software version 7 and 8 (applies to the original Tiny, Go and Pro routers). It might work with the Tiny AC too but I have no means to test against that.
11
11
 
12
12
  ## Purpose
13
13
 
14
14
  The original purpose of this library was to enable addition of router information about connection state, mobile data connection quality and data quota usage on a wall-mounted TV or a small touch screen connected to a Raspberry Pi, accessing a dashboard implemented using [Dashing](https://shopify.github.io/dashing/).
15
15
 
16
+ See this blog entry for some discussion on usage and background: "[A Ruby API for Dovado routers](http://www.janlindblom.se/blog/programming/2015/10/22/ruby-dovado-library.html)".
17
+
16
18
  ## Usage
17
19
 
18
20
  Add it to your Gemfile:
@@ -32,6 +34,16 @@ router.sms.load_messages
32
34
  message = router.sms.get_message 12
33
35
  ```
34
36
 
37
+ The main elements available on the `router` object are:
38
+
39
+ * `info` - [Dovado::Router::Info](http://www.rubydoc.info/gems/ruby-dovado/Dovado/Router/Info)
40
+ * `services` - [Dovado::Router::Services](http://www.rubydoc.info/gems/ruby-dovado/Dovado/Router/Services)
41
+ * `internet` - [Dovado::Router::Internet](http://www.rubydoc.info/gems/ruby-dovado/Dovado/Router/Internet)
42
+ * `sms` - [Dovado::Router::Sms](http://www.rubydoc.info/gems/ruby-dovado/Dovado/Router/Sms)
43
+ * `traffic` - [Dovado::Router::Traffic](http://www.rubydoc.info/gems/ruby-dovado/Dovado/Router/Traffic)
44
+
45
+ For the full documentation, head over to [rubydoc.info](http://www.rubydoc.info/gems/ruby-dovado/frames).
46
+
35
47
  ## Design Considerations
36
48
 
37
49
  Since the API published by these routers is Telnet-based, it stands to reason to limit simultaneous connections. This is achieved by a single client object implemented as a Celluloid Actor. The reason for this is because Celluloid Actor objects can be supervised, block threads and be accessed from multiple threads simultaneously without the need to implement any special locking or waiting mechanisms.
data/Rakefile CHANGED
@@ -16,12 +16,14 @@ namespace :spec do
16
16
  t.rspec_opts = "--tag online"
17
17
  end
18
18
 
19
- desc "Run all RSpec code examples"
20
19
  RSpec::Core::RakeTask.new(:all) do |t|
21
20
  t.rspec_opts = "--tag offline --tag online"
22
21
  end
23
22
  end
24
23
 
24
+ desc "Run all RSpec code examples"
25
+ task :spec => 'spec:all'
26
+
25
27
  YARD::Rake::YardocTask.new do |t|
26
28
  t.files = ['lib/**/*.rb']
27
29
  t.stats_options = ['--list-undoc']
data/lib/dovado/client.rb CHANGED
@@ -66,7 +66,9 @@ module Dovado
66
66
  'Telnetmode' => false,
67
67
  'Prompt' => />>\s/)
68
68
  end
69
- rescue IOError
69
+ rescue Net::OpenTimeout => ex
70
+ raise ConnectionError.new "Error connecting to router: #{ex.message}"
71
+ rescue IOError => ex
70
72
  disconnect
71
73
  raise ConnectionError.new "Error connecting to router: #{ex.message}"
72
74
  rescue Net::ReadTimeout => ex
@@ -135,7 +137,7 @@ module Dovado
135
137
  unless authenticated?
136
138
  raise ArgumentError.new "Username cannot be nil" if @user.nil?
137
139
  raise ArgumentError.new "Password cannot be nil" if @password.nil?
138
-
140
+
139
141
  @server.cmd "user #{@user}"
140
142
  @server.waitfor />>\s/
141
143
  @server.cmd "pass #{@password}"
@@ -0,0 +1,38 @@
1
+ module Dovado
2
+ class Router
3
+ class Info
4
+ # Signal Strength object
5
+ #
6
+ # @since 1.0.3
7
+ class Signal
8
+
9
+ # Strength as a percentage
10
+ #
11
+ # @return [Integer] an integer from +0+ to +100+
12
+ attr_reader :strength
13
+
14
+ # Signal noise level in dBm
15
+ #
16
+ # @return [Integer] noise level (dBm)
17
+ attr_reader :noise
18
+
19
+ # Network type
20
+ #
21
+ # One of:
22
+ # - 2G
23
+ # - 3G
24
+ # - 4G
25
+ #
26
+ # @return [String] current network type
27
+ attr_reader :network
28
+
29
+ def initialize(args)
30
+ @strength = args[:strength].to_i
31
+ @noise = args[:noise].to_i
32
+ @network = args[:network]
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,6 +1,7 @@
1
1
  require 'date'
2
2
  require 'time'
3
3
  require 'socket'
4
+ require 'ipaddr'
4
5
  require 'thread_safe'
5
6
 
6
7
  module Dovado
@@ -11,6 +12,105 @@ module Dovado
11
12
  class Info
12
13
  include Celluloid
13
14
 
15
+ # Get the product name
16
+ #
17
+ # @return [String] product name
18
+ # @since 1.0.3
19
+ attr_reader :product_name
20
+
21
+ # Get the current signal strength information
22
+ #
23
+ # @return [Info::Signal] current signal strength information
24
+ # @since 1.0.3
25
+ # @see {Info::Signal}
26
+ attr_reader :signal_strength
27
+
28
+ # Get the number of kilobytes sent to the internet since last traffic
29
+ # counter reset.
30
+ #
31
+ # @return [Integer] number of kilobytes sent to the internet
32
+ # @since 1.0.3
33
+ attr_reader :traffic_modem_tx
34
+
35
+ # Get the number of kilobytes read from the internet since last traffic
36
+ # counter reset.
37
+ #
38
+ # @return [Integer] number of kilobytes read from the internet
39
+ # @since 1.0.3
40
+ attr_reader :traffic_modem_rx
41
+
42
+ # Get the currently active connection
43
+ #
44
+ # @return [String] the port currently active as internet connection
45
+ # @since 1.0.3
46
+ attr_reader :connection
47
+
48
+ # Get the current state of the connected modem
49
+ #
50
+ # @return [String] current state of the connected modem
51
+ # @since 1.0.3
52
+ attr_reader :modem_status
53
+
54
+ # Get the external IP address of the router
55
+ #
56
+ # @return [IPAddr] external IP address
57
+ # @since 1.0.3
58
+ attr_reader :external_ip
59
+
60
+ # Get the current date on the router
61
+ #
62
+ # @return [Date] current date
63
+ # @since 1.0.3
64
+ attr_reader :date
65
+
66
+ # Get the current time on the router
67
+ #
68
+ # @return [Time] current time
69
+ # @since 1.0.3
70
+ attr_reader :time
71
+
72
+ # Get the GPS type
73
+ #
74
+ # @return [String] the type of GPS
75
+ # @since 1.0.3
76
+ attr_reader :gps_type
77
+
78
+ # Get the GPS latitude
79
+ #
80
+ # @return [Float] GPS latitude
81
+ # @since 1.0.3
82
+ attr_reader :gps_lat
83
+
84
+ # Get the GPS longitude
85
+ #
86
+ # @return [Float] GPS longitude
87
+ # @since 1.0.3
88
+ attr_reader :gps_long
89
+
90
+ # Get the sunrise at the current location
91
+ #
92
+ # @return [Time] sunrise at the current location
93
+ # @since 1.0.3
94
+ attr_reader :sunrise
95
+
96
+ # Get sunset at the current location
97
+ #
98
+ # @return [Time] sunset at the current location
99
+ # @since 1.0.3
100
+ attr_reader :sunset
101
+
102
+ # Get the sms object
103
+ #
104
+ # @return [Sms] the sms object
105
+ # @since 1.0.3
106
+ attr_reader :sms
107
+
108
+ # Get connected devices
109
+ #
110
+ # @return [Array<String>] an array with connected devices
111
+ # @since 1.0.3
112
+ attr_reader :connected_devices
113
+
14
114
  # Create a new {Info} object.
15
115
  #
16
116
  # @param [Hash] args optional hash to initialize with.
@@ -40,25 +140,31 @@ module Dovado
40
140
  val = entry_array[1]
41
141
  keysym = Utilities.name_to_sym(key)
42
142
  case key
43
- when 'traffic_modem_tx'
44
- @data[:traffic_modem_tx] = val.strip.to_i
45
- when 'traffic_modem_rx'
46
- @data[:traffic_modem_rx] = val.strip.to_i
47
- when 'time'
48
- @data[:time] = Time.parse(val)
143
+ when 'traffic_modem_tx', 'traffic_modem_rx'
144
+ @data[keysym] = val.strip.to_i
145
+ when 'time', 'sunset', 'sunrise'
146
+ @data[keysym] = Time.parse(val.strip)
49
147
  when 'date'
50
- @data[:date] = Date.parse(val)
148
+ @data[:date] = Date.parse(val.strip)
51
149
  when 'sms_unread'
52
150
  @data[:sms] = sms if @data[:sms].nil?
53
- @data[:sms].unread = val.to_i
151
+ @data[:sms].unread = val.strip.to_i
54
152
  when 'sms_total'
55
153
  @data[:sms] = sms if @data[:sms].nil?
56
- @data[:sms].total = val.to_i
154
+ @data[:sms].total = val.strip.to_i
57
155
  when 'connected_devices'
58
- val = val.split(',')
156
+ val = val.strip.split(',')
59
157
  @data[keysym] = val
158
+ when 'gps_lat', 'gps_long'
159
+ @data[keysym] = val.to_f
160
+ when 'external_ip'
161
+ @data[keysym] = IPAddr.new val.strip
162
+ when 'signal_strength'
163
+ match = val.strip.match(/(\d+)\W\%\W(\-?\d+)\WdBm\W\((\d\w)\)/)
164
+ so = Info::Signal.new(strength: match[1], noise: match[2], network: match[3])
165
+ @data[keysym] = so
60
166
  else
61
- @data[keysym] = val
167
+ @data[keysym] = val.strip
62
168
  end
63
169
  end
64
170
  end
@@ -95,8 +201,77 @@ module Dovado
95
201
  keys.member?(key)
96
202
  end
97
203
 
204
+ def product_name
205
+ omni_method
206
+ end
207
+
208
+ def signal_strength
209
+ omni_method
210
+ end
211
+
212
+ def traffic_modem_tx
213
+ omni_method
214
+ end
215
+
216
+ def traffic_modem_rx
217
+ omni_method
218
+ end
219
+
220
+ def connection
221
+ omni_method
222
+ end
223
+
224
+ def modem_status
225
+ omni_method
226
+ end
227
+
228
+ def external_ip
229
+ omni_method
230
+ end
231
+
232
+ def date
233
+ omni_method
234
+ end
235
+
236
+ def time
237
+ omni_method
238
+ end
239
+
240
+ def gps_type
241
+ omni_method
242
+ end
243
+
244
+ def gps_lat
245
+ omni_method
246
+ end
247
+
248
+ def gps_long
249
+ omni_method
250
+ end
251
+
252
+ def sunrise
253
+ omni_method
254
+ end
255
+
256
+ def sunset
257
+ omni_method
258
+ end
259
+
260
+ def sms
261
+ omni_method
262
+ end
263
+
264
+ def connected_devices
265
+ omni_method
266
+ end
267
+
98
268
  private
99
269
 
270
+ def omni_method
271
+ this_method = caller[0][/`([^']*)'/, 1]
272
+ @data[this_method.to_sym]
273
+ end
274
+
100
275
  def touch!
101
276
  @last_update = Time.now.to_i
102
277
  end
@@ -2,7 +2,7 @@ module Dovado
2
2
  class Router
3
3
  # Internet Connection.
4
4
  #
5
- # @since 1.0.2
5
+ # @since 1.0.3
6
6
  class Internet
7
7
  include Celluloid
8
8
 
@@ -9,6 +9,18 @@ module Dovado
9
9
  class Services
10
10
  include Celluloid
11
11
 
12
+ # Get status of sms service
13
+ #
14
+ # @return [String] a string with "enabled" or "disabled"
15
+ # @since 1.0.3
16
+ attr_reader :sms
17
+
18
+ # Get status of home automation service
19
+ #
20
+ # @return [String] a string with "enabled" or "disabled"
21
+ # @since 1.0.3
22
+ attr_reader :home_automation
23
+
12
24
  # Create a new {Services} object.
13
25
  #
14
26
  # @param [Hash] args optional argiments
@@ -73,6 +85,30 @@ module Dovado
73
85
  (@last_update + SecureRandom.random_number(9) + 1 <= Time.now.to_i)
74
86
  end
75
87
 
88
+ def sms
89
+ @list[:sms] if has_key? :sms
90
+ end
91
+
92
+ # Boolean check if sms service is enabled
93
+ #
94
+ # @return [Boolean] +true+ or +false+
95
+ # @since 1.0.3
96
+ def sms?
97
+ sms ? (sms == "enabled") : false
98
+ end
99
+
100
+ def home_automation
101
+ @list[:homeautomation] if has_key? :homeautomation
102
+ end
103
+
104
+ # Boolean check if home automation is enabled
105
+ #
106
+ # @return [Boolean] +true+ or +false+
107
+ # @since 1.0.3
108
+ def home_automation?
109
+ home_automation ? (home_automation == "enabled") : false
110
+ end
111
+
76
112
  private
77
113
 
78
114
  def touch!
@@ -4,7 +4,7 @@ module Dovado
4
4
  class Router
5
5
  # Traffic Counters.
6
6
  #
7
- # @since 1.0.2
7
+ # @since 1.0.3
8
8
  class Traffic
9
9
  include Celluloid
10
10
 
data/lib/dovado/router.rb CHANGED
@@ -22,22 +22,17 @@ module Dovado
22
22
  def initialize(args=nil)
23
23
  @address = '192.168.0.1' # Default address
24
24
  @port = 6435
25
- user = "admin" # Default username
26
- password = "password" # Default password
25
+ @user = "admin" # Default username
26
+ @password = "password" # Default password
27
27
  @connected = false
28
28
  unless args.nil?
29
- @address = args[:address] if args.has_key? :address and !args[:address].nil?
30
- @port = args[:port] if args.has_key? :port and !args[:port].nil?
31
- user = args[:user] if args.has_key? :user and !args[:user].nil?
32
- password = args[:password] if args.has_key? :password and !args[:password].nil?
29
+ @address = args[:address] if args.has_key? :address
30
+ @port = args[:port] if args.has_key? :port
31
+ @user = args[:user] if args.has_key? :user
32
+ @password = args[:password] if args.has_key? :password
33
33
  end
34
34
 
35
- Client.supervise as: :client, size: 1, args: [{
36
- server: @address,
37
- port: @port,
38
- user: user,
39
- password: password
40
- }]
35
+ supervise_client
41
36
  end
42
37
 
43
38
  # Fetch services information from the router.
@@ -45,7 +40,7 @@ module Dovado
45
40
  # @return [Services] The {Services} object
46
41
  # @see {Services}
47
42
  def services
48
- Services.supervise as: :router_services, size: 1 unless Actor[:router_services]
43
+ supervise_services
49
44
  client = Actor[:client]
50
45
  router_services = Actor[:router_services]
51
46
 
@@ -62,6 +57,11 @@ module Dovado
62
57
  sms.enabled = true
63
58
  end
64
59
  router_services
60
+ rescue ConnectionError => ex
61
+ Actor[:client].terminate
62
+ supervise_client
63
+ supervise_services
64
+ raise ex
65
65
  end
66
66
 
67
67
  # Get the Internet Connection object.
@@ -87,7 +87,7 @@ module Dovado
87
87
  # @return [Info] The {Info} object.
88
88
  # @see {Info}
89
89
  def info
90
- Info.supervise as: :router_info, size: 1 unless Actor[:router_info]
90
+ supervise_info
91
91
  router_info = Actor[:router_info]
92
92
  client = Actor[:client]
93
93
  router_info = Actor[:router_info]
@@ -99,6 +99,11 @@ module Dovado
99
99
  end
100
100
  services
101
101
  router_info
102
+ rescue ConnectionError => ex
103
+ Actor[:client].terminate
104
+ supervise_client
105
+ supervise_info
106
+ raise ex
102
107
  end
103
108
 
104
109
  # Fetch text messages from the router.
@@ -110,5 +115,29 @@ module Dovado
110
115
  Actor[:sms]
111
116
  end
112
117
 
118
+ private
119
+
120
+ def supervise_services
121
+ return Services.supervise as: :router_services, size: 1 unless Actor[:router_services]
122
+ return Services.supervise as: :router_services, size: 1 if Actor[:router_services] and Actor[:router_services].dead?
123
+ end
124
+
125
+ def supervise_client
126
+ args = [{
127
+ server: @address,
128
+ port: @port,
129
+ user: @user,
130
+ password: @password
131
+ }]
132
+
133
+ return Client.supervise as: :client, size: 1, args: args unless Actor[:client]
134
+ return Client.supervise as: :client, size: 1, args: args if Actor[:router_services] and Actor[:router_services].dead?
135
+ end
136
+
137
+ def supervise_info
138
+ return Info.supervise as: :router_info, size: 1 unless Actor[:router_info]
139
+ return Info.supervise as: :router_info, size: 1 if Actor[:router_info] and Actor[:router_info].dead?
140
+ end
141
+
113
142
  end
114
143
  end
@@ -1,4 +1,4 @@
1
1
  module Dovado
2
2
  # Current version of the library.
3
- VERSION = '1.0.2'
3
+ VERSION = '1.0.3'
4
4
  end
data/lib/dovado.rb CHANGED
@@ -3,7 +3,7 @@ require 'celluloid/current'
3
3
  # The Ruby-Dovado library.
4
4
  #
5
5
  # @author Jan Lindblom <janlindblom@fastmail.fm>
6
- # @version 1.0.2
6
+ # @version 1.0.3
7
7
  module Dovado
8
8
  end
9
9
 
@@ -16,6 +16,7 @@ require 'dovado/router'
16
16
  require 'dovado/router/services'
17
17
 
18
18
  require 'dovado/router/info'
19
+ require 'dovado/router/info/signal'
19
20
  require 'dovado/router/info/operator'
20
21
  require 'dovado/router/info/operator/telia'
21
22
 
data/ruby-dovado.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "pry", "~> 0.10"
26
26
  spec.add_development_dependency "yard", "~> 0.8"
27
27
  spec.add_development_dependency "rspec", "~> 3.3"
28
+ spec.add_development_dependency "dotenv", "~> 2.0.2"
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dovado
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Lindblom
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-22 00:00:00.000000000 Z
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: celluloid
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: dotenv
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.0.2
111
125
  description:
112
126
  email:
113
127
  - janlindblom@fastmail.fm
@@ -133,6 +147,7 @@ files:
133
147
  - lib/dovado/router/info.rb
134
148
  - lib/dovado/router/info/operator.rb
135
149
  - lib/dovado/router/info/operator/telia.rb
150
+ - lib/dovado/router/info/signal.rb
136
151
  - lib/dovado/router/internet.rb
137
152
  - lib/dovado/router/services.rb
138
153
  - lib/dovado/router/sms.rb