procon_bypass_man 0.1.14 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -0
  3. data/.github/workflows/release.yml +33 -0
  4. data/CHANGELOG.md +15 -0
  5. data/Gemfile +1 -0
  6. data/Gemfile.lock +28 -7
  7. data/README.md +26 -3
  8. data/Steepfile +31 -17
  9. data/docs/setting/left-analogstick-cap.md +60 -0
  10. data/docs/setup_raspi.md +2 -0
  11. data/docs/setup_raspi.mitamae.rb +7 -8
  12. data/docs/setup_raspi_by_mitamae.md +37 -1
  13. data/lib/ext/em_pure_ruby.rb +25 -0
  14. data/lib/procon_bypass_man/background.rb +0 -1
  15. data/lib/procon_bypass_man/buttons_setting_configuration/layer.rb +35 -13
  16. data/lib/procon_bypass_man/buttons_setting_configuration/loader.rb +1 -0
  17. data/lib/procon_bypass_man/buttons_setting_configuration/validator.rb +36 -0
  18. data/lib/procon_bypass_man/buttons_setting_configuration.rb +28 -13
  19. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +6 -4
  20. data/lib/procon_bypass_man/bypass.rb +13 -6
  21. data/lib/procon_bypass_man/commands/print_boot_message_command.rb +50 -1
  22. data/lib/procon_bypass_man/commands/print_message_command.rb +8 -0
  23. data/lib/procon_bypass_man/commands/run_remote_pbm_action_dispatch_command.rb +3 -1
  24. data/lib/procon_bypass_man/commands/send_reload_config_event_command.rb +1 -2
  25. data/lib/procon_bypass_man/commands.rb +1 -0
  26. data/lib/procon_bypass_man/configuration.rb +32 -1
  27. data/lib/procon_bypass_man/domains/binary/base.rb +25 -0
  28. data/lib/procon_bypass_man/domains/binary/has_immutable_binary.rb +5 -0
  29. data/lib/procon_bypass_man/domains/binary/has_mutable_binary.rb +5 -0
  30. data/lib/procon_bypass_man/domains/binary/inbound_procon_binary.rb +9 -0
  31. data/lib/procon_bypass_man/domains/binary/processing_procon_binary.rb +66 -0
  32. data/lib/procon_bypass_man/domains.rb +11 -0
  33. data/lib/procon_bypass_man/plugin/splatoon2/macro/fast_return.rb +17 -0
  34. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_left_key.rb +17 -0
  35. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_right_key.rb +17 -0
  36. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_up_key.rb +17 -0
  37. data/lib/procon_bypass_man/plugin/splatoon2/mode/guruguru.rb +59 -0
  38. data/lib/procon_bypass_man/plugin/splatoon2/version.rb +9 -0
  39. data/lib/procon_bypass_man/plugins.rb +11 -0
  40. data/lib/procon_bypass_man/processor.rb +4 -5
  41. data/lib/procon_bypass_man/procon/button.rb +11 -0
  42. data/lib/procon_bypass_man/procon/button_collection.rb +9 -11
  43. data/lib/procon_bypass_man/procon/layer_changer.rb +3 -2
  44. data/lib/procon_bypass_man/procon/macro_registry.rb +8 -5
  45. data/lib/procon_bypass_man/procon/mode_registry.rb +4 -4
  46. data/lib/procon_bypass_man/procon/press_button_aware.rb +9 -7
  47. data/lib/procon_bypass_man/procon/user_operation.rb +46 -63
  48. data/lib/procon_bypass_man/procon/{analog_stick.rb → value_objects/analog_stick.rb} +3 -4
  49. data/lib/procon_bypass_man/procon/value_objects/procon_reader.rb +34 -0
  50. data/lib/procon_bypass_man/procon.rb +18 -16
  51. data/lib/procon_bypass_man/remote_pbm_action/change_pbm_version_action.rb +4 -0
  52. data/lib/procon_bypass_man/remote_pbm_action/{lib → commands}/update_remote_pbm_action_status_command.rb +0 -0
  53. data/lib/procon_bypass_man/remote_pbm_action/reboot_os_action.rb +4 -0
  54. data/lib/procon_bypass_man/remote_pbm_action/restore_pbm_setting.rb +28 -0
  55. data/lib/procon_bypass_man/remote_pbm_action/stop_pbm_action.rb +4 -0
  56. data/lib/procon_bypass_man/{value_objects → remote_pbm_action/value_objects}/remote_pbm_action_object.rb +0 -0
  57. data/lib/procon_bypass_man/remote_pbm_action.rb +3 -2
  58. data/lib/procon_bypass_man/runner.rb +4 -4
  59. data/lib/procon_bypass_man/scheduler.rb +0 -7
  60. data/lib/procon_bypass_man/support/compress_array.rb +5 -0
  61. data/lib/procon_bypass_man/support/http_client.rb +4 -0
  62. data/lib/procon_bypass_man/support/on_memory_cache.rb +3 -1
  63. data/lib/procon_bypass_man/support/report_http_client.rb +3 -3
  64. data/lib/procon_bypass_man/support/server_pool.rb +4 -0
  65. data/lib/procon_bypass_man/version.rb +1 -1
  66. data/lib/procon_bypass_man/websocket/pbm_job_client.rb +90 -0
  67. data/lib/procon_bypass_man.rb +50 -33
  68. data/procon_bypass_man.gemspec +2 -0
  69. data/project_template/README.md +10 -5
  70. data/project_template/app.rb +1 -1
  71. data/project_template/setting.yml +8 -8
  72. data/sig/main.rbs +226 -41
  73. data/sig/on_memory_cache.rbs +16 -0
  74. metadata +59 -20
  75. data/lib/procon_bypass_man/background/jobs/fetch_and_run_remote_pbm_action_job.rb +0 -29
  76. data/lib/procon_bypass_man/boot_message.rb +0 -48
  77. data/lib/procon_bypass_man/commands/run_local_shell_command.rb +0 -6
  78. data/lib/procon_bypass_man/procon_reader.rb +0 -31
  79. data/lib/procon_bypass_man/splatoon2/macro/fast_return.rb +0 -15
  80. data/lib/procon_bypass_man/splatoon2/macro/jump_to_left_key.rb +0 -15
  81. data/lib/procon_bypass_man/splatoon2/macro/jump_to_right_key.rb +0 -15
  82. data/lib/procon_bypass_man/splatoon2/macro/jump_to_up_key.rb +0 -15
  83. data/lib/procon_bypass_man/splatoon2/mode/guruguru.rb +0 -57
  84. data/lib/procon_bypass_man/splatoon2/version.rb +0 -7
  85. data/lib/procon_bypass_man/splatoon2.rb +0 -11
@@ -1,6 +1,7 @@
1
1
  class ProconBypassMan::Procon::LayerChanger
2
+ # @param [ProconBypassMan::Domains::ProcessingProconBinary] binary
2
3
  def initialize(binary: )
3
- @procon_reader = ProconBypassMan::ProconReader.new(binary: binary).freeze
4
+ @procon_reader = binary.to_procon_reader
4
5
  end
5
6
 
6
7
  # @return [Symbol]
@@ -35,6 +36,6 @@ class ProconBypassMan::Procon::LayerChanger
35
36
 
36
37
  # @return [Boolean]
37
38
  def pressed?(button: )
38
- @procon_reader.pressed.include?(button)
39
+ @procon_reader.pressing.include?(button)
39
40
  end
40
41
  end
@@ -24,15 +24,18 @@ class ProconBypassMan::Procon::MacroRegistry
24
24
  null: [],
25
25
  }
26
26
 
27
- def self.install_plugin(klass)
28
- if plugins[klass.name]
29
- raise "すでに登録済みです"
27
+ def self.install_plugin(klass, steps: nil)
28
+ if plugins[klass.to_s.to_sym]
29
+ raise "#{klass} macro is already registered"
30
30
  end
31
- plugins[klass.name] = klass.steps
31
+
32
+ plugins[klass.to_s.to_sym] = ->{
33
+ ProconBypassMan::Procon::ButtonCollection.normalize(steps || klass.steps)
34
+ }
32
35
  end
33
36
 
34
37
  def self.load(name)
35
- steps = PRESETS[name] || plugins[name] || raise("unknown macro")
38
+ steps = PRESETS[name] || plugins[name].call || raise("unknown macro")
36
39
  Macro.new(name: name, steps: steps.dup)
37
40
  end
38
41
 
@@ -23,14 +23,14 @@ class ProconBypassMan::Procon::ModeRegistry
23
23
  }
24
24
 
25
25
  def self.install_plugin(klass)
26
- if plugins[klass.name.to_sym]
27
- raise "すでに登録済みです"
26
+ if plugins[klass.to_s.to_sym]
27
+ raise "#{klass} mode is already registered"
28
28
  end
29
- plugins[klass.name.to_sym] = klass.binaries
29
+ plugins[klass.to_s.to_sym] = ->{ klass.binaries }
30
30
  end
31
31
 
32
32
  def self.load(name)
33
- b = PRESETS[name] || plugins[name] || raise("unknown mode")
33
+ b = PRESETS[name] || plugins[name]&.call || raise("#{name} is unknown mode")
34
34
  Mode.new(name: name, binaries: b.dup)
35
35
  end
36
36
 
@@ -1,13 +1,15 @@
1
- class ProconBypassMan::PpressButtonAware
1
+ class ProconBypassMan::PressButtonAware
2
+ BIT_ON = '1'.freeze
3
+
2
4
  def initialize(binary)
3
5
  @binary = binary
4
6
  end
5
7
 
6
- def pressed_button?(button)
7
- @binary[
8
- ::ProconBypassMan::Procon::ButtonCollection.load(button).byte_position
9
- ].unpack("H*").first.to_i(16).to_s(2).reverse[
10
- ::ProconBypassMan::Procon::ButtonCollection.load(button).bit_position
11
- ] == '1'
8
+ # @param [Symbol]
9
+ # @return [Boolean]
10
+ def pressing_button?(button)
11
+ button_obj = ProconBypassMan::Procon::Button.new(button)
12
+ byte = @binary[button_obj.byte_position].unpack("C").first.to_s(2).reverse
13
+ byte[button_obj.bit_position] == BIT_ON
12
14
  end
13
15
  end
@@ -1,77 +1,60 @@
1
- class ProconBypassMan::Procon
2
- class UserOperation
3
- attr_reader :binary
1
+ # ボタンを押しているか判断するクラス。バイナリの書き換えはしない
2
+ class ProconBypassMan::Procon::UserOperation
3
+ attr_reader :binary
4
4
 
5
- ::ProconBypassMan::Procon::ButtonCollection::BUTTONS_MAP.each do |button, _value|
6
- define_method "pressed_#{button}?" do
7
- pressed_button?(button)
8
- end
9
- end
5
+ ASCII_ENCODING = "ASCII-8BIT"
10
6
 
11
- def initialize(binary)
12
- unless binary.encoding.name == ASCII_ENCODING
13
- raise "おかしいです"
14
- end
15
- @binary = binary
7
+ # @param [String] binary
8
+ def initialize(binary)
9
+ unless binary.encoding.name == ASCII_ENCODING
10
+ raise "おかしいです"
16
11
  end
17
12
 
18
- ZERO_BIT = ["0"].pack("H*").freeze
19
- ASCII_ENCODING = "ASCII-8BIT"
20
-
21
- def set_no_action!
22
- binary[3] = ZERO_BIT
23
- binary[4] = ZERO_BIT
24
- binary[5] = ZERO_BIT
25
- end
13
+ @binary = ProconBypassMan::Domains::ProcessingProconBinary.new(binary: binary)
14
+ end
26
15
 
27
- def apply_left_analog_stick_cap(cap: )
28
- binary[6..8] = ProconBypassMan::Procon::AnalogStickCap.new(binary).capped_position(cap_hypotenuse: cap).to_binary
29
- end
16
+ def set_no_action!
17
+ binary.set_no_action!
18
+ end
30
19
 
31
- def unpress_button(button)
32
- return if not pressed_button?(button)
20
+ def apply_left_analog_stick_cap(cap: )
21
+ binary.write_as_apply_left_analog_stick_cap(cap: cap)
22
+ end
33
23
 
34
- byte_position = ButtonCollection.load(button).byte_position
35
- value = binary[byte_position].unpack("H*").first.to_i(16) - (2**ButtonCollection.load(button).bit_position)
36
- binary[byte_position] = ["%02X" % value.to_s].pack("H*")
37
- end
24
+ # @param [Symbol] button
25
+ def unpress_button(button)
26
+ return if not pressing_button?(button)
27
+ binary.write_as_unpress_button(button)
28
+ end
38
29
 
39
- def press_button(button)
40
- return if pressed_button?(button)
30
+ # @param [Symbol] button
31
+ def press_button(button)
32
+ return if pressing_button?(button)
33
+ binary.write_as_press_button(button)
34
+ end
41
35
 
42
- byte_position = ButtonCollection.load(button).byte_position
43
- value = binary[byte_position].unpack("H*").first.to_i(16) + (2**ButtonCollection.load(button).bit_position)
44
- binary[byte_position] = ["%02X" % value.to_s].pack("H*")
45
- end
36
+ # @param [Symbol] button
37
+ def press_button_only(button)
38
+ binary.write_as_press_button_only(button)
39
+ end
46
40
 
47
- def press_button_only(button)
48
- [ProconBypassMan::Procon::Consts::NO_ACTION.dup].pack("H*").tap do |no_action_binary|
49
- ButtonCollection.load(button).byte_position
50
- byte_position = ButtonCollection.load(button).byte_position
51
- value = 2**ButtonCollection.load(button).bit_position
52
- no_action_binary[byte_position] = ["%02X" % value.to_s].pack("H*")
53
- binary[3] = no_action_binary[3]
54
- binary[4] = no_action_binary[4]
55
- binary[5] = no_action_binary[5]
56
- end
57
- end
41
+ # @return [void]
42
+ def merge(target_binary)
43
+ binary.write_as_merge!(
44
+ ProconBypassMan::Domains::ProcessingProconBinary.new(binary: target_binary)
45
+ )
46
+ end
58
47
 
59
- def merge(target_binary: )
60
- tb = [target_binary].pack("H*")
61
- binary[3] = tb[3]
62
- binary[4] = tb[4]
63
- binary[5] = tb[5]
64
- binary[6] = tb[6]
65
- binary[7] = tb[7]
66
- binary[8] = tb[8]
67
- binary[9] = tb[9]
68
- binary[10] = tb[10]
69
- binary[11] = tb[11]
70
- self.binary
71
- end
48
+ # @param [Symbol] button
49
+ # @return [Boolean]
50
+ def pressing_button?(button)
51
+ ProconBypassMan::PressButtonAware.new(@binary.raw).pressing_button?(button)
52
+ end
72
53
 
73
- def pressed_button?(button)
74
- ProconBypassMan::PpressButtonAware.new(binary).pressed_button?(button)
75
- end
54
+ # @param [Array<Symbol>] buttons
55
+ # @return [Boolean]
56
+ def pressing_all_buttons?(buttons)
57
+ aware = ProconBypassMan::PressButtonAware.new(@binary.raw)
58
+ buttons.all? { |b| aware.pressing_button?(b) }
76
59
  end
77
60
  end
@@ -4,13 +4,12 @@ class ProconBypassMan::Procon::AnalogStick
4
4
 
5
5
  def initialize(binary: )
6
6
  @neutral_position = ProconBypassMan::ButtonsSettingConfiguration.instance.neutral_position
7
-
8
- byte6 = binary[6].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
9
- byte7 = binary[7].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
10
- byte8 = binary[8].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
7
+ bytes = binary[ProconBypassMan::Procon::ButtonCollection::LEFT_ANALOG_STICK.fetch(:byte_position)]
8
+ byte6, byte7, byte8 = bytes.each_char.map { |x| x.unpack("C").first.to_s(2).rjust(8, "0") }
11
9
 
12
10
  self.bin_x = "#{byte7[4..7]}#{byte6}"
13
11
  self.bin_y = "#{byte8}#{byte7[0..3]}"
12
+ freeze
14
13
  end
15
14
 
16
15
  def abs_x
@@ -0,0 +1,34 @@
1
+ module ProconBypassMan
2
+ class ProconReader
3
+ def initialize(binary: )
4
+ @binary = binary
5
+ @analog_stick = ProconBypassMan::Procon::AnalogStick.new(binary: binary)
6
+ freeze
7
+ end
8
+
9
+ # @return [Array<Symbol>]
10
+ def pressing
11
+ aware = ProconBypassMan::PressButtonAware.new(@binary)
12
+ pressed_table = ::ProconBypassMan::Procon::ButtonCollection::BUTTONS.reduce({}) do |acc, button|
13
+ acc[button] = aware.pressing_button?(button)
14
+ acc
15
+ end
16
+ pressed_table.select { |_key, value| value }.keys
17
+ end
18
+
19
+ def left_analog_stick
20
+ { x: @analog_stick.relative_x, y: @analog_stick.relative_y }
21
+ end
22
+
23
+ def left_analog_stick_by_abs
24
+ { x: @analog_stick.abs_x, y: @analog_stick.abs_y }
25
+ end
26
+
27
+ def to_hash
28
+ { left_analog_stick: left_analog_stick,
29
+ left_analog_stick_by_abs: left_analog_stick_by_abs,
30
+ buttons: pressing,
31
+ }
32
+ end
33
+ end
34
+ end
@@ -20,8 +20,11 @@ class ProconBypassMan::Procon
20
20
  end
21
21
  reset!
22
22
 
23
+ # @param [string] binary
23
24
  def initialize(binary)
24
- self.user_operation = ProconBypassMan::Procon::UserOperation.new(binary.dup)
25
+ self.user_operation = ProconBypassMan::Procon::UserOperation.new(
26
+ binary.dup
27
+ )
25
28
  end
26
29
 
27
30
  def status; @@status[:buttons]; end
@@ -43,7 +46,7 @@ class ProconBypassMan::Procon
43
46
 
44
47
  if ongoing_macro.finished?
45
48
  current_layer.macros.each do |macro_name, options|
46
- if options[:if_pressed].all? { |b| user_operation.pressed_button?(b) }
49
+ if user_operation.pressing_all_buttons?(options[:if_pressed])
47
50
  @@status[:ongoing_macro] = MacroRegistry.load(macro_name)
48
51
  end
49
52
  end
@@ -60,7 +63,7 @@ class ProconBypassMan::Procon
60
63
  next
61
64
  end
62
65
 
63
- if options[:if_pressed] && options[:if_pressed].all? { |b| user_operation.pressed_button?(b) }
66
+ if options[:if_pressed] && user_operation.pressing_all_buttons?(options[:if_pressed])
64
67
  FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
65
68
  status[button] = !status[button]
66
69
  end
@@ -75,7 +78,7 @@ class ProconBypassMan::Procon
75
78
  @@status[:ongoing_mode] = ProconBypassMan::Procon::ModeRegistry.load(current_layer.mode)
76
79
  end
77
80
  if(binary = ongoing_mode.next_binary)
78
- self.user_operation.merge(target_binary: binary)
81
+ self.user_operation.merge([binary].pack("H*"))
79
82
  end
80
83
  return
81
84
  end
@@ -83,28 +86,28 @@ class ProconBypassMan::Procon
83
86
  status
84
87
  end
85
88
 
86
- # @return [String<binary>]
89
+ # @return [ProconBypassMan::Domains::ProcessingProconBinary]
87
90
  def to_binary
88
91
  if ongoing_mode.name != :manual
89
- return user_operation.binary
92
+ return user_operation.binary.raw
90
93
  end
91
94
 
92
95
  if ongoing_macro.ongoing?
93
- step = ongoing_macro.next_step or return(user_operation.binary)
96
+ step = ongoing_macro.next_step or return(user_operation.binary.raw)
94
97
  user_operation.press_button_only(step)
95
- return user_operation.binary
98
+ return user_operation.binary.raw
96
99
  end
97
100
 
98
101
  current_layer.disables.each do |button|
99
102
  user_operation.unpress_button(button)
100
103
  end
101
104
 
102
- current_layer.left_analog_stick_caps.each do |button, options|
103
- if button.nil? || button.all? { |b| user_operation.pressed_button?(b) }
104
- options[:force_neutral]&.each do |force_neutral_button|
105
+ current_layer.left_analog_stick_caps.each do |config|
106
+ if config[:if_pressed].nil? || user_operation.pressing_all_buttons?(config[:if_pressed])
107
+ config[:force_neutral]&.each do |force_neutral_button|
105
108
  user_operation.unpress_button(force_neutral_button)
106
109
  end
107
- user_operation.apply_left_analog_stick_cap(cap: options[:cap])
110
+ user_operation.apply_left_analog_stick_cap(cap: config[:cap])
108
111
  end
109
112
  end
110
113
 
@@ -116,7 +119,7 @@ class ProconBypassMan::Procon
116
119
  end
117
120
 
118
121
  # 押している時だけ連打
119
- if options[:if_pressed] && options[:if_pressed].all? { |b| user_operation.pressed_button?(b) }
122
+ if options[:if_pressed] && user_operation.pressing_all_buttons?(options[:if_pressed])
120
123
  if !status[button]
121
124
  user_operation.unpress_button(button)
122
125
  end
@@ -128,16 +131,15 @@ class ProconBypassMan::Procon
128
131
  end
129
132
 
130
133
  current_layer.remaps.each do |from_button, to_buttons|
131
- if user_operation.pressed_button?(from_button)
134
+ if user_operation.pressing_button?(from_button)
132
135
  user_operation.unpress_button(from_button)
133
- # TODO 2重でpressしないようにしたい
134
136
  to_buttons[:to].each do |to_button|
135
137
  user_operation.press_button(to_button)
136
138
  end
137
139
  end
138
140
  end
139
141
 
140
- user_operation.binary
142
+ user_operation.binary.raw
141
143
  end
142
144
 
143
145
  private
@@ -16,6 +16,10 @@ module ProconBypassMan
16
16
  def before_action_callback
17
17
  be_processed
18
18
  end
19
+
20
+ def after_action_callback
21
+ # no-op
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -12,6 +12,10 @@ module ProconBypassMan
12
12
  def before_action_callback
13
13
  be_processed
14
14
  end
15
+
16
+ def after_action_callback
17
+ # no-op
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -0,0 +1,28 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class RestorePbmSettingAction < BaseAction
4
+
5
+ def action_content(args: )
6
+ require "pbmenv"
7
+ ProconBypassMan.logger.info "execute RestorePbmSettingAction!"
8
+ setting = args.dig("setting") or raise(ProconBypassMan::RemotePbmAction::NeedPbmVersionError, "settingが必要です, #{args.inspect}")
9
+ File.write(
10
+ ProconBypassMan::ButtonsSettingConfiguration.instance.setting_path,
11
+ setting.to_yaml,
12
+ )
13
+ ProconBypassMan.hot_reload!
14
+ end
15
+
16
+ private
17
+
18
+ def before_action_callback
19
+ be_in_progress
20
+ end
21
+
22
+ def after_action_callback
23
+ be_processed
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -12,6 +12,10 @@ module ProconBypassMan
12
12
  def before_action_callback
13
13
  be_processed
14
14
  end
15
+
16
+ def after_action_callback
17
+ # no-op
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -4,12 +4,13 @@ module ProconBypassMan
4
4
  require "procon_bypass_man/remote_pbm_action/change_pbm_version_action"
5
5
  require "procon_bypass_man/remote_pbm_action/reboot_os_action"
6
6
  require "procon_bypass_man/remote_pbm_action/stop_pbm_action"
7
- require "procon_bypass_man/remote_pbm_action/lib/update_remote_pbm_action_status_command"
7
+ require "procon_bypass_man/remote_pbm_action/restore_pbm_setting.rb"
8
+ require "procon_bypass_man/remote_pbm_action/commands/update_remote_pbm_action_status_command"
8
9
 
9
10
  ACTION_CHANGE_PBM_VERSION = "change_pbm_version"
10
11
  ACTION_REBOOT_OS = "reboot_os"
11
12
  ACTION_STOP_PBM = "stop_pbm"
12
- ACTION_RESTORE_SETTING = "restore_setting" # TODO
13
+ ACTION_RESTORE_SETTING = "restore_pbm_setting"
13
14
 
14
15
  ACTIONS = [
15
16
  ACTION_CHANGE_PBM_VERSION,
@@ -1,5 +1,4 @@
1
1
  require_relative "io_monitor"
2
- require_relative "boot_message"
3
2
 
4
3
  class ProconBypassMan::Runner
5
4
  class InterruptForRestart < StandardError; end
@@ -27,10 +26,11 @@ class ProconBypassMan::Runner
27
26
 
28
27
  loop do
29
28
  $will_terminate_token = false
30
- # TODO forkしないでThreadでいいのでは?
29
+ # NOTE メインプロセスではThreadをいくつか起動しているので念のためパフォーマンスを優先するためにforkしていく
31
30
  main_loop_pid = Kernel.fork { ProconBypassMan::BypassCommand.new(gadget: @gadget, procon: @procon).execute }
32
31
 
33
32
  begin
33
+ # TODO 小プロセスが消滅した時に、メインプロセスは生き続けてしまい、何もできなくなる問題がある
34
34
  while(readable_io = IO.select([self_read]))
35
35
  signal = readable_io.first[0].gets.strip
36
36
  handle_signal(signal)
@@ -39,14 +39,14 @@ class ProconBypassMan::Runner
39
39
  $will_terminate_token = true
40
40
  Process.kill("TERM", main_loop_pid)
41
41
  Process.wait
42
- ProconBypassMan.logger.info("Reloading config file")
42
+ ProconBypassMan::PrintMessageCommand.execute(text: "Reloading config file")
43
43
  begin
44
44
  ProconBypassMan::ButtonsSettingConfiguration::Loader.reload_setting
45
45
  ProconBypassMan::SendReloadConfigEventCommand.execute
46
46
  rescue ProconBypassMan::CouldNotLoadConfigError
47
47
  ProconBypassMan::SendErrorCommand.execute(error: "設定ファイルが不正です。再読み込みができませんでした")
48
48
  end
49
- ProconBypassMan.logger.info("バイパス処理を再開します")
49
+ ProconBypassMan::PrintMessageCommand.execute(text: "バイパス処理を再開します")
50
50
  rescue Interrupt
51
51
  $will_terminate_token = true
52
52
  Process.kill("TERM", main_loop_pid)
@@ -68,13 +68,6 @@ module ProconBypassMan
68
68
  end
69
69
 
70
70
  def self.register_schedules
71
- register(
72
- schedule: Schedule.new(
73
- klass: ProconBypassMan::FetchAndRunRemotePbmActionJob,
74
- args: [],
75
- interval: 3,
76
- )
77
- )
78
71
  register(
79
72
  schedule: Schedule.new(
80
73
  klass: ProconBypassMan::SyncDeviceStatsJob,
@@ -54,3 +54,8 @@ module ProconBypassMan
54
54
  end
55
55
  end
56
56
  end
57
+
58
+
59
+ if $0 == __FILE__
60
+ ProconBypassMan::CompressArray.new([''])
61
+ end
@@ -100,3 +100,7 @@ module ProconBypassMan
100
100
  end
101
101
  end
102
102
  end
103
+
104
+ if $0 == __FILE__
105
+ ProconBypassMan::HttpClient.new(path: '/', server_pool: nil, retry_on_connection_error: false).get(response_body: '')
106
+ end
@@ -1,6 +1,8 @@
1
1
  class ProconBypassMan::OnMemoryCache
2
2
  class CacheValue
3
- attr_accessor :expired_at, :value
3
+ # @param [Time]
4
+ attr_accessor :expired_at
5
+ attr_accessor :value
4
6
 
5
7
  def initialize(expired_at: , value: )
6
8
  self.expired_at = expired_at
@@ -1,10 +1,10 @@
1
1
  module ProconBypassMan
2
2
  class ReportHttpClient < HttpClient
3
3
  def post(body: , event_type: )
4
- if body.is_a?(String)
5
- b = { text: body }
6
- else
4
+ if body.is_a?(Hash)
7
5
  b = body
6
+ else
7
+ b = { text: body }
8
8
  end
9
9
 
10
10
  super(request_body: {
@@ -40,3 +40,7 @@ module ProconBypassMan
40
40
  end
41
41
  end
42
42
  end
43
+
44
+ if $0 == __FILE__
45
+ ProconBypassMan::ServerPool.new(servers: ['http://example.com'])
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ProconBypassMan
4
- VERSION = "0.1.14"
4
+ VERSION = "0.1.17"
5
5
  end
@@ -0,0 +1,90 @@
1
+ module ProconBypassMan
2
+ module Websocket
3
+ module PbmJobClient
4
+ CHANNEL = 'PbmJobChannel'
5
+
6
+ def self.start!
7
+ return unless ProconBypassMan.config.enable_ws?
8
+
9
+ Thread.start do
10
+ loop do
11
+ run
12
+ rescue
13
+ retry
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.run
19
+ EventMachine.run do
20
+ client = ActionCableClient.new(
21
+ ProconBypassMan.config.current_ws_server_url, {
22
+ channel: CHANNEL, device_id: ProconBypassMan.device_id,
23
+ }
24
+ )
25
+
26
+ client.connected {
27
+ ProconBypassMan.logger.info('successfully connected in ProconBypassMan::Websocket::PbmJobClient')
28
+ }
29
+ client.subscribed { |msg|
30
+ puts({ event: :subscribed, msg: msg })
31
+ ProconBypassMan::SyncDeviceStatsJob.perform(ProconBypassMan::DeviceStatus.current)
32
+ }
33
+
34
+ client.received do |data|
35
+ ProconBypassMan.logger.info(data)
36
+
37
+ dispatch(data: data, client: client)
38
+ rescue => e
39
+ ProconBypassMan::SendErrorCommand.execute(error: e)
40
+ end
41
+
42
+ client.disconnected {
43
+ puts :disconnected
44
+ client.reconnect!
45
+ sleep 2
46
+ }
47
+ client.errored { |msg| puts :errored; puts msg }
48
+ client.pinged { |msg|
49
+ ProconBypassMan.cache.fetch key: 'ws_pinged', expires_in: 10 do
50
+ ProconBypassMan.logger.info(msg)
51
+ end
52
+ }
53
+ end
54
+ end
55
+
56
+ # @param [Hash] data
57
+ def self.dispatch(data: , client: )
58
+ pbm_job_hash = data.dig("message")
59
+ if pbm_job_hash['action'] == "ping"
60
+ client.perform('pong', { device_id: ProconBypassMan.device_id, message: 'hello from pbm' })
61
+ else
62
+ validate_and_run(data: data)
63
+ end
64
+ end
65
+
66
+ # @raise [ProconBypassMan::RemotePbmActionObject::ValidationError]
67
+ # @param [Hash] data
68
+ # @return [Void]
69
+ def self.validate_and_run(data: )
70
+ pbm_job_hash = data.dig("message")
71
+ begin
72
+ pbm_job_object = ProconBypassMan::RemotePbmActionObject.new(action: pbm_job_hash["action"],
73
+ status: pbm_job_hash["status"],
74
+ uuid: pbm_job_hash["uuid"],
75
+ created_at: pbm_job_hash["created_at"],
76
+ job_args: pbm_job_hash["args"])
77
+ pbm_job_object.validate!
78
+ rescue ProconBypassMan::RemotePbmActionObject::ValidationError => e
79
+ raise
80
+ end
81
+
82
+ ProconBypassMan::RunRemotePbmActionDispatchCommand.execute(
83
+ action: pbm_job_object.action,
84
+ uuid: pbm_job_object.uuid,
85
+ job_args: pbm_job_object.job_args
86
+ )
87
+ end
88
+ end
89
+ end
90
+ end