procon_bypass_man 0.1.11 → 0.1.12

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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +3 -0
  4. data/Gemfile.lock +2 -2
  5. data/README.md +3 -2
  6. data/bin/dev_api_server.rb +1 -1
  7. data/lib/procon_bypass_man/background/has_server_pool.rb +54 -0
  8. data/lib/procon_bypass_man/background/http_client.rb +70 -0
  9. data/lib/procon_bypass_man/background/job_performer.rb +16 -0
  10. data/lib/procon_bypass_man/background/job_runnable.rb +16 -0
  11. data/lib/procon_bypass_man/background/{report_thread.rb → job_runner.rb} +10 -8
  12. data/lib/procon_bypass_man/background/jobs/base_job.rb +12 -0
  13. data/lib/procon_bypass_man/background/jobs/report_boot_job.rb +10 -0
  14. data/lib/procon_bypass_man/background/jobs/report_error_job.rb +10 -0
  15. data/lib/procon_bypass_man/background/jobs/report_heartbeat_job.rb +10 -0
  16. data/lib/procon_bypass_man/background/jobs/report_pressed_buttons_job.rb +18 -0
  17. data/lib/procon_bypass_man/background/jobs/report_reload_config_job.rb +10 -0
  18. data/lib/procon_bypass_man/background.rb +10 -0
  19. data/lib/procon_bypass_man/boot_message.rb +9 -7
  20. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/layer.rb +0 -0
  21. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/loader.rb +5 -4
  22. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/validator.rb +0 -0
  23. data/lib/procon_bypass_man/buttons_setting_configuration.rb +4 -5
  24. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +16 -7
  25. data/lib/procon_bypass_man/bypass.rb +15 -14
  26. data/lib/procon_bypass_man/commands/connect_device_command.rb +11 -0
  27. data/lib/procon_bypass_man/commands/print_boot_message_command.rb +9 -0
  28. data/lib/procon_bypass_man/commands/send_error_command.rb +18 -0
  29. data/lib/procon_bypass_man/commands/send_reload_config_event_command.rb +10 -0
  30. data/lib/procon_bypass_man/commands/write_device_id_command.rb +11 -0
  31. data/lib/procon_bypass_man/commands/write_session_id_command.rb +13 -0
  32. data/lib/procon_bypass_man/commands.rb +6 -0
  33. data/lib/procon_bypass_man/configuration.rb +27 -5
  34. data/lib/procon_bypass_man/device_connector.rb +1 -2
  35. data/lib/procon_bypass_man/io_monitor.rb +9 -4
  36. data/lib/procon_bypass_man/procon/{data.rb → consts.rb} +1 -1
  37. data/lib/procon_bypass_man/procon/layer_changer.rb +40 -0
  38. data/lib/procon_bypass_man/procon/user_operation.rb +11 -17
  39. data/lib/procon_bypass_man/procon.rb +6 -12
  40. data/lib/procon_bypass_man/{readonly_procon.rb → procon_reader.rb} +3 -4
  41. data/lib/procon_bypass_man/runner.rb +18 -30
  42. data/lib/procon_bypass_man/uptime.rb +13 -3
  43. data/lib/procon_bypass_man/version.rb +1 -1
  44. data/lib/procon_bypass_man.rb +19 -12
  45. data/project_template/app.rb +2 -2
  46. data/sig/main.rbs +9 -11
  47. metadata +27 -14
  48. data/lib/procon_bypass_man/outbound/base.rb +0 -53
  49. data/lib/procon_bypass_man/outbound/error_reporter.rb +0 -13
  50. data/lib/procon_bypass_man/outbound/pressed_buttons_reporter.rb +0 -13
  51. data/lib/procon_bypass_man/outbound/reporter.rb +0 -12
  52. data/lib/procon_bypass_man/procon/layer_changeable.rb +0 -28
  53. data/lib/procon_bypass_man/procon/pressed_button_helper.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ead44cd36352dba5f4bd97af9c99936657a1a896fb9793a6735ce730a045b6f1
4
- data.tar.gz: b8d115ef493d0b58a064fc7a0a7348a66e9e331e8173edd7543d96e9e213daba
3
+ metadata.gz: 42551d494786247ae06f81c416f522360001fb0a78ffdd67311520aafb56587a
4
+ data.tar.gz: 92da20f533512ec4e1fdebe93f2c064f5ae1513fd6f26d3c4f34f2a0d488dffd
5
5
  SHA512:
6
- metadata.gz: 3229e5e9f0fef53072b4869af2c6469b55703804d12bd8a9d077fe7c413d2198f747d482b30134daa4da55a0c5309eeda45d9928e46cd7778391ed865720ca00
7
- data.tar.gz: 2de07a44334162b54ca8ba2464286c12a7a21d6ef74cb36b39a97bd5e22a833a5a6a5af2dac5ba6382da99a8635cdd9bd196a98d7f63dac311a4e61c2387ecad
6
+ metadata.gz: b52fde78831c29dc1b56b56217a604e421b6c0cdf5859b997732b36d94b9b7dc9d02f3906835d41df5f7168a2b694c2c457d3f96fd235903fc4827f42e74422b
7
+ data.tar.gz: 50defecca29919fca30680469deddc5adc5c41d0c4f02f084c0a73f9633aa8e11101a5f07e863ea4e0b6481178d4f11e0d466a4bf2d9954f08cb5407d3cb10dd
data/.gitignore CHANGED
@@ -12,3 +12,4 @@ todo.local.md
12
12
  .setting_yaml_digest
13
13
  pbm_pid
14
14
  setting.yml
15
+ device_id
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.1.12] - 2021-11-18
2
+ - 起動ログなどを設定した外部サーバに送信できるようにしました
3
+
1
4
  ## [0.1.11] - 2021-11-05
2
5
  - PBM-WEB(>= 0.1.3)でボタンの入力を見るためのスレッドが動くようになりました
3
6
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- procon_bypass_man (0.1.11)
4
+ procon_bypass_man (0.1.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -114,4 +114,4 @@ DEPENDENCIES
114
114
  webrick
115
115
 
116
116
  BUNDLED WITH
117
- 2.2.22
117
+ 2.2.30
data/README.md CHANGED
@@ -67,12 +67,13 @@ sudo kill -USR2 `cat ./pbm_pid`
67
67
  ```
68
68
 
69
69
  ### 起動ログをサーバに送信する
70
- * `ProconBypassMan.api_server = "http://.."` を設定すると、 `POST /api/reports` に対して起動ログを送信するようになります
70
+ * `ProconBypassMan.api_server = "http://.."` を設定すると、 `POST /api/events` に対して起動ログなどを送信するようになります
71
71
 
72
72
  ### 開発環境でログの送信を確認する方法
73
73
  * `bundle exec bin/dev_api_server.rb`
74
74
  * `INTERNAL_API_SERVER=http://localhost:4567 bin/console`
75
- * `message = ProconBypassMan::BootMessage.new; ProconBypassMan::Reporter.report(body: message.to_hash)"`
75
+ * `API_SERVER=http://localhost:4567 bin/console`
76
+ * `message = ProconBypassMan::BootMessage.new; ProconBypassMan::ReportBootJob.report(body: message.to_hash)`
76
77
 
77
78
  ### リリース手順
78
79
  * project_template/web.rb, project_template/app.rb, lib/procon_bypass_man/version.rb のバージョンをあげる
@@ -4,7 +4,7 @@ require "sinatra"
4
4
  require "webrick"
5
5
  require "pry"
6
6
 
7
- post '/api/reports' do
7
+ post '/api/events' do
8
8
  puts JSON.parse(request.body.read)
9
9
  status 200
10
10
  body ''
@@ -0,0 +1,54 @@
1
+ module ProconBypassMan::Background::HasServerPool
2
+ class ServerPool
3
+ def initialize(servers: )
4
+ if servers.nil? || servers.empty?
5
+ return
6
+ end
7
+
8
+ @servers = servers
9
+ if @servers.size >= 1
10
+ @index = 0
11
+ else
12
+ @index = nil
13
+ end
14
+ end
15
+
16
+ def pick
17
+ if @index.nil?
18
+ return @servers&.first
19
+ end
20
+ @servers[@index] or raise "bug!!!"
21
+ end
22
+ def server; pick; end
23
+
24
+ def next!
25
+ inc_index
26
+ if @servers[@index].nil?
27
+ reset
28
+ return
29
+ end
30
+ end
31
+
32
+ def reset
33
+ @index = 0
34
+ end
35
+
36
+ def inc_index
37
+ @index = @index + 1
38
+ end
39
+ end
40
+
41
+ def reset_server_pool!
42
+ @pool_server = nil
43
+ end
44
+
45
+ def pool_server
46
+ @pool_server ||= ServerPool.new(
47
+ servers: servers
48
+ )
49
+ end
50
+
51
+ def servers
52
+ raise NotImplementedError, nil
53
+ end
54
+ end
@@ -0,0 +1,70 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ class HttpClient
4
+ class HttpRequest
5
+ def self.request!(uri: , hostname: , body: , device_id: , session_id: nil, event_type: )
6
+ @uri = uri
7
+ @http = Net::HTTP.new(uri.host, uri.port)
8
+ @http.use_ssl = uri.scheme === "https"
9
+ @params = {
10
+ hostname: hostname,
11
+ event_type: event_type,
12
+ session_id: session_id,
13
+ device_id: device_id,
14
+ }.merge(body)
15
+ @http.post(
16
+ @uri.path,
17
+ @params.to_json,
18
+ { "Content-Type" => "application/json" },
19
+ )
20
+ end
21
+ end
22
+
23
+ def initialize(path: , pool_server: , retry_on_connection_error: false)
24
+ @path = path
25
+ @pool_server = pool_server
26
+ @hostname = `hostname`.chomp
27
+ @retry_on_connection_error = retry_on_connection_error
28
+ end
29
+
30
+ def post(body: , event_type: )
31
+ if @pool_server.server.nil?
32
+ ProconBypassMan.logger.info('送信先が未設定なのでスキップしました')
33
+ return
34
+ end
35
+ if body.is_a?(Hash)
36
+ body = { body: body, event_type: event_type }
37
+ else
38
+ body = { body: { value: body, event_type: event_type } }
39
+ end
40
+ session_id = ProconBypassMan.session_id
41
+ device_id = ProconBypassMan.device_id
42
+
43
+ response = HttpRequest.request!(
44
+ uri: URI.parse("#{@pool_server.server}#{@path}"),
45
+ hostname: @hostname,
46
+ device_id: device_id,
47
+ session_id: session_id,
48
+ body: body,
49
+ event_type: event_type,
50
+ )
51
+ case response.code
52
+ when /^200/
53
+ return
54
+ else
55
+ @pool_server.next!
56
+ ProconBypassMan.logger.error("200以外(#{response.code})が帰ってきました. #{response.body}")
57
+ end
58
+ rescue SocketError => e
59
+ ProconBypassMan.logger.error("error in outbound module: #{e}")
60
+ if @retry_on_connection_error
61
+ sleep(10)
62
+ retry
63
+ end
64
+ rescue => e
65
+ puts e
66
+ ProconBypassMan.logger.error("error in outbound module: #{e}")
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,16 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ class JobPerformer
4
+ def initialize(klass: , args: )
5
+ @klass = klass
6
+ @args = args
7
+ end
8
+
9
+ def perform
10
+ @klass.perform(*@args)
11
+ rescue => e
12
+ ProconBypassMan::ReportErrorJob.perform(e)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module ProconBypassMan
2
+ module Background
3
+ module JobRunnable
4
+ def perform(*)
5
+ raise NotImplementedError, nil
6
+ end
7
+
8
+ def perform_async(*args)
9
+ ProconBypassMan::Background::JobRunner.push(
10
+ args: args,
11
+ reporter_class: self,
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,21 +1,24 @@
1
1
  module ProconBypassMan
2
2
  module Background
3
- class Reporter
3
+ class JobRunner
4
4
  MAX_QUEUE_SIZE = 100
5
5
 
6
6
  def self.start!
7
7
  new.start!
8
8
  end
9
9
 
10
+ # for test
11
+ def self.stop!
12
+ return unless defined?(@@thread)
13
+ @@thread.kill
14
+ end
15
+
10
16
  def start!
11
17
  return if defined?(@@thread)
12
- @@latest_request_result = { stats: true, timestamp: Time.now }
13
- @@queue = Queue.new
14
18
  @@thread = Thread.new do
15
- while(item = @@queue.pop)
19
+ while(item = self.class.queue.pop)
16
20
  begin
17
- result = item[:reporter_class].report(body: item[:data])
18
- @@latest_request_result = { stats: result.stats, timestamp: Time.now }
21
+ JobPerformer.new(klass: item[:reporter_class], args: item[:args]).perform
19
22
  sleep(1)
20
23
  rescue => e
21
24
  ProconBypassMan.logger.error(e)
@@ -25,8 +28,7 @@ module ProconBypassMan
25
28
  end
26
29
 
27
30
  def self.queue
28
- raise "Do not start this thread yet" unless defined?(@@queue)
29
- @@queue
31
+ @@queue ||= Queue.new
30
32
  end
31
33
 
32
34
  def self.push(hash)
@@ -0,0 +1,12 @@
1
+ class ProconBypassMan::BaseJob
2
+ extend ProconBypassMan::Background::HasServerPool
3
+ extend ProconBypassMan::Background::JobRunnable
4
+
5
+ def self.servers
6
+ ProconBypassMan.config.api_servers
7
+ end
8
+
9
+ def self.path
10
+ "/api/events"
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportBootJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: true,
8
+ ).post(body: body, event_type: :boot)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportErrorJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :error,)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportHeartbeatJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :heartbeat)
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ class ProconBypassMan::ReportPressedButtonsJob
2
+ extend ProconBypassMan::Background::HasServerPool
3
+ extend ProconBypassMan::Background::JobRunnable
4
+
5
+ PATH = "/api/pressed_buttons"
6
+
7
+ # @param [String] body
8
+ def self.perform(body)
9
+ ProconBypassMan::Background::HttpClient.new(
10
+ path: PATH,
11
+ pool_server: pool_server,
12
+ ).post(body: body, event_type: :internal)
13
+ end
14
+
15
+ def self.servers
16
+ ProconBypassMan.config.internal_api_servers
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::ReportReloadConfigJob < ProconBypassMan::BaseJob
2
+ # @param [String] body
3
+ def self.perform(body)
4
+ ProconBypassMan::Background::HttpClient.new(
5
+ path: path,
6
+ pool_server: pool_server,
7
+ retry_on_connection_error: false,
8
+ ).post(body: body, event_type: :reload_config)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require "procon_bypass_man/background/job_runnable"
2
+ require "procon_bypass_man/background/job_performer"
3
+ require "procon_bypass_man/background/has_server_pool"
4
+ require "procon_bypass_man/background/http_client"
5
+ require "procon_bypass_man/background/jobs/base_job"
6
+ require "procon_bypass_man/background/jobs/report_boot_job"
7
+ require "procon_bypass_man/background/jobs/report_reload_config_job"
8
+ require "procon_bypass_man/background/jobs/report_error_job"
9
+ require "procon_bypass_man/background/jobs/report_heartbeat_job"
10
+ require "procon_bypass_man/background/jobs/report_pressed_buttons_job"
@@ -9,12 +9,15 @@ class ProconBypassMan::BootMessage
9
9
  @table[:setting_path] = ProconBypassMan::ButtonsSettingConfiguration.instance.setting_path
10
10
  @table[:uptime_from_boot] = ProconBypassMan::Uptime.from_boot
11
11
 
12
- build_version = `git rev-parse --short HEAD`.chomp
13
- if build_version.empty?
14
- @table[:build_version] = 'release version'
15
- else
16
- @table[:build_version] = build_version
17
- end
12
+ # 開発中のHEADを取りたかったけど、Gem::Specification経由から取得する必要がありそう
13
+ # build_version = `git rev-parse --short HEAD`.chomp
14
+ # if build_version.empty?
15
+ # @table[:build_version] = 'release version'
16
+ # else
17
+ # @table[:build_version] = build_version
18
+ # end
19
+
20
+ # build version: #{@table[:build_version]}
18
21
  end
19
22
 
20
23
  # @return [String]
@@ -28,7 +31,6 @@ class ProconBypassMan::BootMessage
28
31
  pid_path: #{@table[:pid_path]}
29
32
  setting_path: #{@table[:setting_path]}
30
33
  uptime from boot: #{@table[:uptime_from_boot]} sec
31
- build version: #{@table[:build_version]}
32
34
  ----
33
35
  EOF
34
36
  end
@@ -4,10 +4,10 @@ module ProconBypassMan
4
4
  require 'digest/md5'
5
5
 
6
6
  def self.load(setting_path: )
7
- ProconBypassMan::ButtonsSettingConfiguration.switch_new_context(:validation) do |validation_instance|
7
+ ProconBypassMan::ButtonsSettingConfiguration.switch_new_context(:validation) do |new_instance|
8
8
  yaml = YAML.load_file(setting_path) or raise "読み込みに失敗しました"
9
- validation_instance.instance_eval(yaml["setting"])
10
- validator = Validator.new(validation_instance)
9
+ new_instance.instance_eval(yaml["setting"])
10
+ validator = Validator.new(new_instance)
11
11
  if validator.valid?
12
12
  next
13
13
  else
@@ -19,11 +19,12 @@ module ProconBypassMan
19
19
  raise ProconBypassMan::CouldNotLoadConfigError, "yamlのシンタックスエラーです"
20
20
  end
21
21
 
22
- yaml = YAML.load_file(setting_path)
23
22
  ProconBypassMan::ButtonsSettingConfiguration.instance.setting_path = setting_path
24
23
  ProconBypassMan::ButtonsSettingConfiguration.instance.reset!
25
24
  ProconBypassMan.reset!
26
25
 
26
+ yaml = YAML.load_file(setting_path)
27
+ ProconBypassMan.config.raw_setting = yaml.dup
27
28
  case yaml["version"]
28
29
  when 1.0, nil
29
30
  ProconBypassMan::ButtonsSettingConfiguration.instance.instance_eval(yaml["setting"])
@@ -1,6 +1,6 @@
1
- require "procon_bypass_man/configuration/validator"
2
- require "procon_bypass_man/configuration/loader"
3
- require "procon_bypass_man/configuration/layer"
1
+ require "procon_bypass_man/buttons_setting_configuration/validator"
2
+ require "procon_bypass_man/buttons_setting_configuration/loader"
3
+ require "procon_bypass_man/buttons_setting_configuration/layer"
4
4
 
5
5
  module ProconBypassMan
6
6
  class AnalogStickPosition < Struct.new(:x, :y); end
@@ -42,7 +42,6 @@ module ProconBypassMan
42
42
  'manual'
43
43
  end
44
44
  end
45
- MODES = [:manual]
46
45
  def layer(direction, mode: ManualMode, &block)
47
46
  mode_name = case mode
48
47
  when String
@@ -52,7 +51,7 @@ module ProconBypassMan
52
51
  else
53
52
  mode.name.to_sym
54
53
  end
55
- unless (MODES + ProconBypassMan::Procon::ModeRegistry.plugins.keys).include?(mode_name)
54
+ unless ([ManualMode.name.to_sym] + ProconBypassMan::Procon::ModeRegistry.plugins.keys).include?(mode_name)
56
55
  raise("#{mode_name} mode is unknown")
57
56
  end
58
57
 
@@ -10,17 +10,26 @@ class ProconBypassMan::Bypass
10
10
  set_callback :send_procon_to_gadget, :after, :log_procon_to_gadget
11
11
 
12
12
  def log_send_gadget_to_procon
13
- ProconBypassMan.logger.debug { ">>> #{bypass_status.to_text}" }
13
+ ProconBypassMan.logger.debug { ">>> #{bypass_value.to_text}" }
14
14
  end
15
15
 
16
16
  def log_procon_to_gadget
17
- ProconBypassMan.logger.debug { "<<< #{bypass_status.to_text}" }
17
+ if ProconBypassMan.config.verbose_bypass_log
18
+ ProconBypassMan.logger.debug { "<<< #{bypass_value.to_text}" }
19
+ else
20
+ ProconBypassMan.cache.fetch key: 'bypass_log', expires_in: 1 do
21
+ ProconBypassMan.logger.debug { "<<< #{bypass_value.to_text}" }
22
+ end
23
+ end
24
+
25
+ ProconBypassMan.cache.fetch key: 'pressed_buttons_reporter', expires_in: 5 do
26
+ ProconBypassMan::ReportPressedButtonsJob.perform_async(
27
+ ProconBypassMan::ProconReader.new(binary: bypass_value.binary).to_hash
28
+ )
29
+ end
18
30
 
19
- ProconBypassMan.cache.fetch key: 'reporter', expires_in: 5 do
20
- ProconBypassMan::Background::Reporter.push({
21
- data: ProconBypassMan::ReadonlyProcon.new(binary: bypass_status.binary).to_hash,
22
- reporter_class: ProconBypassMan::PressedButtonsReporter
23
- })
31
+ ProconBypassMan.cache.fetch key: 'heartbeat_reporter', expires_in: 60 do
32
+ ProconBypassMan::ReportHeartbeatJob.perform_async(ProconBypassMan::BootMessage.new.to_hash)
24
33
  end
25
34
  end
26
35
  end
@@ -3,13 +3,13 @@ require "procon_bypass_man/bypass/usb_hid_logger"
3
3
  class ProconBypassMan::Bypass
4
4
  include ProconBypassMan::Bypass::UsbHidLogger
5
5
 
6
- class BypassStatus < Struct.new(:binary, :sent)
6
+ class BypassValue < Struct.new(:binary, :sent)
7
7
  def to_text
8
8
  "#{binary.unpack("H*").first} #{'x' unless sent}"
9
9
  end
10
10
  end
11
11
 
12
- attr_accessor :gadget, :procon, :monitor, :bypass_status
12
+ attr_accessor :gadget, :procon, :monitor, :bypass_value
13
13
 
14
14
  def initialize(gadget: , procon: , monitor: )
15
15
  self.gadget = gadget
@@ -21,14 +21,14 @@ class ProconBypassMan::Bypass
21
21
  def send_gadget_to_procon!
22
22
  monitor.record(:start_function)
23
23
  input = nil
24
- self.bypass_status = BypassStatus.new(input, sent = false)
24
+ self.bypass_value = BypassValue.new(input, sent = false)
25
25
 
26
- run_callbacks :send_gadget_to_procon do
26
+ run_callbacks(:send_gadget_to_procon) do
27
27
  begin
28
- return if $will_terminate_token
29
- # TODO blocking readにしたいが、接続時のフェーズによって長さが違宇野で厳しい
28
+ break if $will_terminate_token
29
+ # TODO blocking readにしたいが、接続時のフェーズによって長さが違うので厳しい
30
30
  input = self.gadget.read_nonblock(64)
31
- self.bypass_status.binary = input
31
+ self.bypass_value.binary = input
32
32
  rescue IO::EAGAINWaitReadable
33
33
  monitor.record(:eagain_wait_readable_on_read)
34
34
  sleep(0.001)
@@ -37,10 +37,10 @@ class ProconBypassMan::Bypass
37
37
 
38
38
  begin
39
39
  self.procon.write_nonblock(input)
40
- self.bypass_status.sent = true
40
+ self.bypass_value.sent = true
41
41
  rescue IO::EAGAINWaitReadable
42
42
  monitor.record(:eagain_wait_readable_on_write)
43
- return
43
+ break
44
44
  end
45
45
  end
46
46
 
@@ -50,18 +50,19 @@ class ProconBypassMan::Bypass
50
50
  def send_procon_to_gadget!
51
51
  monitor.record(:start_function)
52
52
  output = nil
53
- self.bypass_status = BypassStatus.new(output, sent = false)
53
+ self.bypass_value = BypassValue.new(output, sent = false)
54
54
 
55
55
  run_callbacks(:send_procon_to_gadget) do
56
56
  begin
57
- return if $will_terminate_token
57
+ break if $will_terminate_token
58
58
  Timeout.timeout(1) do
59
59
  output = self.procon.read(64)
60
- self.bypass_status.binary = output
60
+ self.bypass_value.binary = output
61
61
  end
62
62
  rescue Timeout::Error
63
63
  ProconBypassMan.logger.debug { "read timeout! do sleep. by send_procon_to_gadget!" }
64
64
  ProconBypassMan.error_logger.error { "read timeout! do sleep. by send_procon_to_gadget!" }
65
+ ProconBypassMan::SendErrorCommand.execute(error: "read timeout! do sleep. by send_procon_to_gadget!")
65
66
  monitor.record(:eagain_wait_readable_on_read)
66
67
  retry
67
68
  rescue IO::EAGAINWaitReadable
@@ -73,10 +74,10 @@ class ProconBypassMan::Bypass
73
74
 
74
75
  begin
75
76
  self.gadget.write_nonblock(ProconBypassMan::Processor.new(output).process)
76
- self.bypass_status.sent = true
77
+ self.bypass_value.sent = true
77
78
  rescue IO::EAGAINWaitReadable
78
79
  monitor.record(:eagain_wait_readable_on_write)
79
- return
80
+ break
80
81
  end
81
82
  end
82
83
  monitor.record(:end_function)
@@ -0,0 +1,11 @@
1
+ class ProconBypassMan::ConnectDeviceCommand
2
+ # @return [void]
3
+ def self.execute!
4
+ gadget, procon = ProconBypassMan::DeviceConnector.connect
5
+ rescue ProconBypassMan::Timer::Timeout
6
+ ::ProconBypassMan.logger.error "デバイスとの通信でタイムアウトが起きて接続ができませんでした。"
7
+ gadget&.close
8
+ procon&.close
9
+ raise ::ProconBypassMan::EternalConnectionError
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class ProconBypassMan::PrintBootMessageCommand
2
+ # @return [void]
3
+ def self.execute
4
+ message = ProconBypassMan::BootMessage.new
5
+ ProconBypassMan::ReportBootJob.perform_async(message.to_hash)
6
+ ProconBypassMan::ReportBootJob.perform_async(ProconBypassMan.config.raw_setting)
7
+ puts message.to_s
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ class ProconBypassMan::SendErrorCommand
2
+ # @param [String, Hash, Exception] error
3
+ # @return [void]
4
+ def self.execute(error: )
5
+ body =
6
+ case error
7
+ when String, Hash
8
+ error
9
+ else
10
+ error.full_message
11
+ end
12
+
13
+ ProconBypassMan.logger.error body
14
+ puts body
15
+
16
+ ProconBypassMan::ReportErrorJob.perform_async(error)
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ class ProconBypassMan::SendReloadConfigEventCommand
2
+ def self.execute
3
+ puts "設定ファイルの再読み込みができました"
4
+ ProconBypassMan.logger.info "設定ファイルの再読み込みができました"
5
+ ProconBypassMan::ReportReloadConfigJob.perform_async(
6
+ ProconBypassMan.config.raw_setting
7
+ )
8
+ end
9
+ end
10
+
@@ -0,0 +1,11 @@
1
+ class ProconBypassMan::WriteDeviceIdCommand
2
+ def self.execute
3
+ path = "#{ProconBypassMan.root}/device_id"
4
+ if(sid = File.read(path))
5
+ return sid
6
+ end
7
+ rescue Errno::ENOENT
8
+ File.write(path, "m_#{SecureRandom.uuid}")
9
+ return SecureRandom.uuid
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ class ProconBypassMan::WriteSessionIdCommand
2
+ # @return [String] session_id ラズパイが起動してからshutdownするまで同じ文字列を返す
3
+ # 起動すると/tmp がなくなる前提の実装
4
+ def self.execute
5
+ path = "/tmp/pbm_session_id"
6
+ if(sid = File.read(path))
7
+ return sid
8
+ end
9
+ rescue Errno::ENOENT
10
+ File.write(path, "s_#{SecureRandom.uuid}")
11
+ return SecureRandom.uuid
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require "procon_bypass_man/commands/print_boot_message_command"
2
+ require "procon_bypass_man/commands/write_session_id_command"
3
+ require "procon_bypass_man/commands/write_device_id_command"
4
+ require "procon_bypass_man/commands/send_reload_config_event_command"
5
+ require "procon_bypass_man/commands/send_error_command"
6
+ require "procon_bypass_man/commands/connect_device_command"