da_funk 0.4.19 → 0.4.20

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
  SHA1:
3
- metadata.gz: 581959a05ca363df037b218fb049a1337a05458e
4
- data.tar.gz: bc09d913c2a3d7d25607ff173951d267c319f6f5
3
+ metadata.gz: 62c2f815b4a2aac3e8a15cf969b0f9d7ce842140
4
+ data.tar.gz: 0d585d8bf8eec09129472b764119775a91ad290a
5
5
  SHA512:
6
- metadata.gz: 3b7ca6e71008e7dd5962e83d59179246381ada0c227c56334cab6ea449226c8f4adb8784d9d7035a7dd4a470781d7785529207dbe0230d47e446b375b3d72b84
7
- data.tar.gz: daea05521a99d66a8a3143941f1be5e7707447999003c4fe5d576bbd7cb9a2ee2db3e9883973481499c4976800ccc5ba3343d060e0afd6bcd13e676ccf2e0ccf
6
+ metadata.gz: 1ed08db806c6b47c2845c9dbca87c021b3a3256b00b0dd81261e2ad06437d594840a44299d8860b600a63ebfb51d2f428bb4a21ac33b6ba51d9e7723e562b978
7
+ data.tar.gz: 1ec0cd499e3221a80d0537949c77939294ac2bdbb68dde23364e1adfa42d55337ca2539a8364316f0349cb9758b74c26478155d516ce104849cbdf3ce9bec5cc
@@ -7,7 +7,7 @@ GIT
7
7
  PATH
8
8
  remote: .
9
9
  specs:
10
- da_funk (0.4.19)
10
+ da_funk (0.4.20)
11
11
  bundler (~> 1.7)
12
12
  cloudwalk_handshake (~> 0.4)
13
13
  rake (~> 10.4)
@@ -16,7 +16,7 @@ PATH
16
16
  GEM
17
17
  remote: https://rubygems.org/
18
18
  specs:
19
- cloudwalk_handshake (0.4.7)
19
+ cloudwalk_handshake (0.4.11)
20
20
  rake (10.4.2)
21
21
  yard (0.8.7.6)
22
22
 
@@ -1,5 +1,20 @@
1
1
  # DaFunk
2
2
 
3
+ ### 0.4.20 - 2015-12-10 - Refactoring Notification
4
+ - Implement try method on helper to be use on by communication tries.
5
+ - Serfx#auth now receives the authentication key as a parameter too, so that could be called externally in a determined moment, example TOTP authentication on the right moment (after socket open, avoiding authentication problems).
6
+ - Treat authentication error clearing cw_pos_timezone to perform entire handshake on next time.
7
+ - Make Device::Display.print call STDOUT.print if row and column are not sent.
8
+ - On Device::Display#clear call STDOUT.fresh to change the Screen state to 0(x and y).
9
+ - Refactor download of file on ParamsDat to implement method try, adopted initially 3 times.
10
+ - On Screen.setup define STDOUT on Object, instead of Kernel.
11
+ - Update cloudwalk_handshake to version 0.4.11.
12
+ - Fix Screen jump line to not jump before print if size is less than max_x.
13
+ - Support to keep alive Notifications restarting in intervals.
14
+ - On Notification Callback to display message use `getc(0)` (wait a key to be pressed forever) instead of getc(nil) (wait a key to be pressed in default time.
15
+ - Implement String#chars extension.
16
+ - Support to execute a unique file test, example: `rake test test/unit/file_test.rb`.
17
+
3
18
  ### 0.4.19 - 2015-12-04 - Refactoring Device::Setting.to_production!/.to_staging!
4
19
  - Implement FileDb#update_attributes to update more than one key in a unique save.
5
20
  - Refactoring Device::Setting.to_production!/.to_staging! to clean company_name if the last environment is different.
data/Rakefile CHANGED
@@ -56,7 +56,6 @@ FILES = FileList[
56
56
 
57
57
  DaFunk::RakeTask.new do |t|
58
58
  t.main_out = "./out/da_funk.mrb"
59
- t.mruby = "mruby -b"
60
59
  t.libs = FILES
61
60
  end
62
61
 
@@ -62,6 +62,15 @@ module DaFunk
62
62
  value
63
63
  end
64
64
 
65
+ def try(tries, &block)
66
+ tried = 0
67
+ ret = false
68
+ while (tried < tries && ! ret)
69
+ ret = block.call(tried)
70
+ tried += 1
71
+ end
72
+ ret
73
+ end
65
74
 
66
75
  # Create a form menu.
67
76
  #
@@ -129,7 +129,11 @@ module DaFunk
129
129
 
130
130
  desc "Run all test on mruby"
131
131
  task :all => "test:setup" do
132
- execute_tests(tests)
132
+ if ARGV[1]
133
+ execute_tests(FileList[ARGV[1]])
134
+ else
135
+ execute_tests(tests)
136
+ end
133
137
  end
134
138
  end
135
139
 
@@ -5,10 +5,10 @@ class Screen
5
5
 
6
6
  attr_accessor :x, :y, :max_x, :max_y
7
7
 
8
- def self.setup(max_x, max_y)
8
+ def self.setup(max_x = SCREEN_X_SIZE, max_y = SCREEN_Y_SIZE)
9
9
  $stdout.close
10
- Kernel.const_set(:STDOUT, self.new(max_x, max_y))
11
- $stdout = Kernel::STDOUT
10
+ Object.const_set(:STDOUT, self.new(max_x, max_y))
11
+ $stdout = Object::STDOUT
12
12
  end
13
13
 
14
14
  def initialize(max_x = SCREEN_X_SIZE, max_y = SCREEN_Y_SIZE)
@@ -17,7 +17,7 @@ class Screen
17
17
  self.fresh
18
18
  end
19
19
 
20
- def fresh(value_y = nil, value_x = nil)
20
+ def fresh(value_y = 0, value_x = 0)
21
21
  @x = value_x || 0
22
22
  @y = value_y || 0
23
23
  end
@@ -53,16 +53,18 @@ class Screen
53
53
  private
54
54
  def loop_split_strings(*args)
55
55
  str, value_y, value_x = *args
56
+ @y = value_y if value_y
57
+ @x = value_x if value_x
56
58
 
57
- str.lines.each_with_index do |string, index|
58
- jump_line if string[-1] == "\n"
59
- string = string.chomp
60
- if (@x + string.size) < @max_x
61
- Device::IO.print_line(string, @y, @x)
62
- @x += string.size
59
+ str.to_s.lines.each_with_index do |string, index|
60
+ buf = string.chomp
61
+ if (@x + buf.size) < @max_x
62
+ Device::Display.print(buf, @y, @x)
63
+ @x += buf.size
64
+ jump_line if string[-1] == "\n"
63
65
  else
64
66
  space = @max_x - @x
65
- Device::IO.print_line("#{string[0..(space - 1)]}", @y, @x)
67
+ Device::Display.print("#{string[0..(space - 1)]}", @y, @x)
66
68
  jump_line
67
69
  loop_split_strings("#{string[(space)..-1]}")
68
70
  end
@@ -12,7 +12,7 @@ class Device
12
12
  # @return [NilClass] nil.
13
13
  def self.print(buf, row = nil, column = nil)
14
14
  if row.nil? && column.nil?
15
- adapter.print(buf)
15
+ STDOUT.print(buf)
16
16
  else
17
17
  adapter.print_in_line(buf, row, column)
18
18
  end
@@ -38,6 +38,7 @@ class Device
38
38
  # @param line [Fixnum] Line to clear
39
39
  def self.clear(line = nil)
40
40
  if line.nil?
41
+ STDOUT.fresh
41
42
  adapter.clear
42
43
  else
43
44
  adapter.clear_line line
@@ -1,14 +1,17 @@
1
+
1
2
  class Device
2
3
  class Notification
3
- DEFAULT_TIMEOUT = 15
4
- DEFAULT_INTERVAL = 10
5
- DEFAULT_STREAM_TIMEOUT = 0
4
+ DEFAULT_TIMEOUT = 20
5
+ DEFAULT_INTERVAL = 10
6
+ DEFAULT_STREAM_TIMEOUT = 0
7
+ DEFAULT_CREATION_INTERVAL = 3600
6
8
 
7
9
  class << self
8
- attr_accessor :callbacks, :current
10
+ attr_accessor :callbacks, :current, :last_creation, :creation_interval
9
11
  end
10
12
 
11
13
  self.callbacks = Hash.new
14
+ self.creation_interval = DEFAULT_CREATION_INTERVAL
12
15
 
13
16
  attr_reader :fiber, :timeout, :interval, :last_check, :stream_timeout
14
17
 
@@ -30,6 +33,7 @@ class Device
30
33
  end
31
34
 
32
35
  def self.config
36
+ self.creation_interval = Device::Setting.notification_socket_timeout.empty? ? DEFAULT_CREATION_INTERVAL : Device::Setting.notification_socket_timeout.to_i
33
37
  notification_timeout = Device::Setting.notification_timeout.empty? ? DEFAULT_TIMEOUT : Device::Setting.notification_timeout.to_i
34
38
  notification_interval = Device::Setting.notification_interval.empty? ? DEFAULT_INTERVAL : Device::Setting.notification_interval.to_i
35
39
  notification_stream_timeout = Device::Setting.notification_stream_timeout.empty? ? DEFAULT_STREAM_TIMEOUT : Device::Setting.notification_stream_timeout.to_i
@@ -37,7 +41,7 @@ class Device
37
41
  end
38
42
 
39
43
  def self.start
40
- unless Device::Setting.logical_number.empty? || Device::Setting.company_name.empty? || (! Device::Network.connected?)
44
+ if create_fiber? && Device::Network.connected?
41
45
  unless Device::Notification.current && Device::Notification.current.closed?
42
46
  self.new(*self.config)
43
47
  end
@@ -57,10 +61,22 @@ class Device
57
61
  date = datetime.sub(" ", "-").split("-")
58
62
  Device::Display.print_line("#{date[1]}/#{date[0]}/#{date[2]} #{date[3]}", 0)
59
63
  Device::Display.print_line("#{message}", 2)
60
- getc nil
64
+ getc(0)
61
65
  }
62
66
  end
63
67
 
68
+ def self.create_fiber?
69
+ (! Device::Setting.company_name.empty?) && (! Device::Setting.logical_number.empty?) && self.valid_creation_interval?
70
+ end
71
+
72
+ def self.valid_creation_interval?
73
+ if @last_creation
74
+ (@last_creation + self.creation_interval) < Time.now
75
+ else
76
+ true
77
+ end
78
+ end
79
+
64
80
  def initialize(timeout = DEFAULT_TIMEOUT, interval = DEFAULT_INTERVAL, stream_timeout = DEFAULT_STREAM_TIMEOUT)
65
81
  @timeout = timeout
66
82
  @stream_timeout = stream_timeout
@@ -71,24 +87,34 @@ class Device
71
87
 
72
88
  # Check if there is any notification
73
89
  def check
74
- if @fiber.alive? && valid_interval? && Device::Network.connected?
75
- if (notification = @fiber.resume)
76
- Notification.execute(NotificationEvent.new(notification))
90
+ if valid_check_interval? && Device::Network.connected?
91
+ if @fiber.alive?
92
+ if (notification = @fiber.resume)
93
+ Notification.execute(NotificationEvent.new(notification))
94
+ end
95
+ @last_check = Time.now
96
+ if Device::Notification.create_fiber?
97
+ self.close
98
+ @fiber = create_fiber
99
+ end
77
100
  end
78
- @last_check = Time.now
79
101
  end
80
102
  end
81
103
 
82
104
  # Close socket and finish Fiber execution
83
105
  def close
84
- @fiber.resume "close"
106
+ if closed?
107
+ true
108
+ else
109
+ ! @fiber.resume "close"
110
+ end
85
111
  end
86
112
 
87
113
  def closed?
88
114
  ! @fiber.alive?
89
115
  end
90
116
 
91
- def valid_interval?
117
+ def valid_check_interval?
92
118
  if @last_check
93
119
  (@last_check + self.interval) < Time.now
94
120
  else
@@ -107,16 +133,27 @@ class Device
107
133
  end
108
134
  end
109
135
 
136
+ def check_errors(exception)
137
+ case exception.message
138
+ when "Invalid authentication token"
139
+ Device::Setting.cw_pos_timezone = "" # Clear timezone if authentication error
140
+ when "Socket closed"
141
+ else
142
+ end
143
+ false
144
+ end
145
+
110
146
  def create_fiber
111
147
  Fiber.new do
112
148
  begin
113
- auth = CloudwalkTOTP.at
114
- Serfx.connect(authkey: auth, socket_block: Device::Network.socket, timeout: timeout, stream_timeout: stream_timeout) do |conn|
149
+ Serfx.connect(socket_block: Device::Network.socket, timeout: timeout, stream_timeout: stream_timeout) do |conn|
150
+ conn.auth(CloudwalkTOTP.at)
151
+ Device::Notification.last_creation = Time.now
115
152
  conn.stream(subscription) { |ev| reply(conn, ev) }
116
153
  end
117
154
  true
118
- rescue => RPCError
119
- false
155
+ rescue Serfx::RPCError => exception
156
+ check_errors(exception)
120
157
  end
121
158
  end
122
159
  end
@@ -46,13 +46,14 @@ class Device
46
46
 
47
47
  def self.download
48
48
  if attach
49
- Device::Display.clear
50
- puts "Downloading Params"
51
- ret = Device::Transaction::Download.request_param_file(FILE_NAME)
52
- if value = check_download_error(ret)
53
- puts "Downloaded Successfully"
54
- parse_apps
49
+ value = try(3) do |tried|
50
+ Device::Display.clear
51
+ puts "Downloading (#{tried})"
52
+ puts "Parameters"
53
+ ret = Device::Transaction::Download.request_param_file(FILE_NAME)
54
+ check_download_error(ret)
55
55
  end
56
+ parse_apps if value
56
57
  value
57
58
  end
58
59
  end
@@ -79,12 +80,15 @@ class Device
79
80
 
80
81
  def self.update_app(application)
81
82
  if attach && application
82
- Device::Display.clear
83
- puts "Downloading #{application.file}..."
84
- ret = Device::Transaction::Download.request_file(application.file, application.zip)
85
-
86
- unless check_download_error ret
87
- sleep 2
83
+ try(3) do |tried|
84
+ Device::Display.clear
85
+ puts "Downloading (#{tried})"
86
+ puts "#{application.file}..."
87
+ ret = Device::Transaction::Download.request_file(application.file, application.zip)
88
+
89
+ ret = check_download_error(ret)
90
+ sleep(1)
91
+ ret
88
92
  end
89
93
  end
90
94
  end
@@ -29,9 +29,10 @@ class Device
29
29
  "logical_number" => "",
30
30
  "network_configured" => "",
31
31
  "environment" => "",
32
- "notification_timeout" => "",
33
- "notification_interval" => "",
34
- "notification_stream_timeout" => "",
32
+ "notification_socket_timeout" => "", # Period to create fiber
33
+ "notification_timeout" => "", # Time to wait message to read
34
+ "notification_interval" => "", # Check interval
35
+ "notification_stream_timeout" => "", # Time to wait stream message to read
35
36
  "cw_switch_version" => "",
36
37
  "cw_pos_timezone" => "",
37
38
  "uclreceivetimeout" => "0",
@@ -1,6 +1,6 @@
1
1
 
2
2
  class Device
3
- API_VERSION="0.4.19"
3
+ API_VERSION="0.4.20"
4
4
 
5
5
  def self.api_version
6
6
  Device::API_VERSION
@@ -5,5 +5,9 @@ class String
5
5
  str = mask_string.chars.map{|s| s.match(/[0-9A-Za-z]/) ? "%s" : s }.join
6
6
  str % self.ljust(mask_clean.size, " ").chars
7
7
  end
8
+
9
+ def chars
10
+ self.split("")
11
+ end
8
12
  end
9
13
 
@@ -15,8 +15,8 @@ module Serfx
15
15
  # `auth` has to be second command, immediately after `handshake`.
16
16
  #
17
17
  # @return [Response]
18
- def auth
19
- tcp_send(:auth, 'AuthKey' => @authkey)
18
+ def auth(key = @authkey)
19
+ tcp_send(:auth, 'AuthKey' => key)
20
20
  read_response(:auth)
21
21
  end
22
22
  # fires an user event
@@ -65,6 +65,7 @@ module Serfx
65
65
  def read_data(read_timeout = self.timeout)
66
66
  buf = read_buffer(read_timeout)
67
67
  return if buf.nil?
68
+ return {'Error' => "Socket closed"} if buf.empty?
68
69
  parse_package(buf)
69
70
  end
70
71
 
@@ -83,6 +84,7 @@ module Serfx
83
84
  loop do
84
85
  bytes = socket.bytes_available
85
86
  return socket.read(bytes) if bytes > 0
87
+ return socket.recv_nonblock(bytes) if bytes > 0
86
88
  break unless time_timeout > Time.now
87
89
  sleep 1
88
90
  end
@@ -103,10 +105,12 @@ module Serfx
103
105
  'Command' => command.to_s.gsub('_', '-'),
104
106
  'Seq' => seq
105
107
  }
108
+
106
109
  buff = MessagePack::Packer.new
107
110
  buff.write(header)
108
111
  buff.write(body) unless body.nil?
109
- res = socket.write(buff.to_str)
112
+
113
+ res = socket.write(buff.to_str) unless buff.to_str.empty?
110
114
  @requests[seq] = { header: header, ack?: false }
111
115
  seq
112
116
  end
@@ -7,8 +7,8 @@ require 'da_funk'
7
7
 
8
8
  DaFunk::Test.configure do |t|
9
9
  t.root_path = ROOT_PATH
10
- t.serial = "0000011700025246"
11
- t.logical_number = "002"
10
+ t.serial = "0000000001"
11
+ t.logical_number = "001"
12
12
  t.name = APP_NAME
13
13
  end
14
14
 
@@ -51,13 +51,27 @@ class DisplayTest < DaFunk::Test.case
51
51
  assert_equal 3, $stdout.y
52
52
  end
53
53
 
54
+ def test_kernel_print_small
55
+ $stdout.fresh
56
+ assert_equal nil, print("1234")
57
+ assert_equal 4, $stdout.x
58
+ assert_equal 0, $stdout.y
59
+ end
60
+
61
+ def test_kernel_puts_small
62
+ $stdout.fresh
63
+ assert_equal nil, puts("1234")
64
+ assert_equal 0, $stdout.x
65
+ assert_equal 1, $stdout.y
66
+ end
67
+
54
68
  def test_kernel_puts_string_20_plus_1
55
69
  $stdout.fresh
56
- assert_equal nil, puts("12345678901234567890")
70
+ assert_equal nil, print("12345678901234567890")
57
71
  assert_equal 20, $stdout.x
58
- assert_equal 1, $stdout.y
72
+ assert_equal 0, $stdout.y
59
73
  assert_equal nil, puts("1")
60
- assert_equal 1, $stdout.x
74
+ assert_equal 0, $stdout.x
61
75
  assert_equal 2, $stdout.y
62
76
  end
63
77
 
@@ -71,15 +85,29 @@ class DisplayTest < DaFunk::Test.case
71
85
  def test_kernel_puts_string_43
72
86
  $stdout.fresh
73
87
  assert_equal nil, puts("1234567890123456789012345678901234567890123")
74
- assert_equal 1, $stdout.x
88
+ assert_equal 0, $stdout.x
75
89
  assert_equal 3, $stdout.y
76
90
  end
77
91
 
92
+ def test_kernel_print_string_43
93
+ $stdout.fresh
94
+ assert_equal nil, print("1234567890123456789012345678901234567890123")
95
+ assert_equal 1, $stdout.x
96
+ assert_equal 2, $stdout.y
97
+ end
98
+
78
99
  def test_kernel_puts_string_22
79
100
  $stdout.fresh
80
101
  assert_equal nil, puts("1234567890123456789012")
81
- assert_equal 1, $stdout.x
102
+ assert_equal 0, $stdout.x
82
103
  assert_equal 2, $stdout.y
83
104
  end
105
+
106
+ def test_kernel_print_string_22
107
+ $stdout.fresh
108
+ assert_equal nil, print("1234567890123456789012")
109
+ assert_equal 1, $stdout.x
110
+ assert_equal 1, $stdout.y
111
+ end
84
112
  end
85
113
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: da_funk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.19
4
+ version: 0.4.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiago Scalone
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-04 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake