procon_bypass_man 0.1.10 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +13 -1
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +15 -12
  6. data/README.md +13 -9
  7. data/bin/console +4 -0
  8. data/bin/{report_receive_server.rb → dev_api_server.rb} +8 -1
  9. data/lib/procon_bypass_man/background/job_performer.rb +16 -0
  10. data/lib/procon_bypass_man/background/job_runner.rb +45 -0
  11. data/lib/procon_bypass_man/background/jobs/base_job.rb +7 -0
  12. data/lib/procon_bypass_man/background/jobs/concerns/has_external_api_setting.rb +5 -0
  13. data/lib/procon_bypass_man/background/jobs/concerns/has_internal_api_setting.rb +5 -0
  14. data/lib/procon_bypass_man/background/jobs/concerns/job_runnable.rb +16 -0
  15. data/lib/procon_bypass_man/background/jobs/fetch_and_run_remote_pbm_action_job.rb +29 -0
  16. data/lib/procon_bypass_man/background/jobs/report_boot_job.rb +12 -0
  17. data/lib/procon_bypass_man/background/jobs/report_error_job.rb +11 -0
  18. data/lib/procon_bypass_man/background/jobs/report_event_base_job.rb +5 -0
  19. data/lib/procon_bypass_man/background/jobs/report_load_config_job.rb +11 -0
  20. data/lib/procon_bypass_man/background/jobs/report_pressed_buttons_job.rb +15 -0
  21. data/lib/procon_bypass_man/background/jobs/report_reload_config_job.rb +11 -0
  22. data/lib/procon_bypass_man/background/jobs/sync_device_stats_job.rb +16 -0
  23. data/lib/procon_bypass_man/background.rb +14 -0
  24. data/lib/procon_bypass_man/boot_message.rb +16 -8
  25. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/layer.rb +34 -29
  26. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/loader.rb +5 -4
  27. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/validator.rb +0 -0
  28. data/lib/procon_bypass_man/buttons_setting_configuration.rb +8 -7
  29. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +20 -2
  30. data/lib/procon_bypass_man/bypass.rb +15 -15
  31. data/lib/procon_bypass_man/commands/bypass_command.rb +86 -0
  32. data/lib/procon_bypass_man/commands/connect_device_command.rb +16 -0
  33. data/lib/procon_bypass_man/commands/print_boot_message_command.rb +9 -0
  34. data/lib/procon_bypass_man/commands/run_local_shell_command.rb +6 -0
  35. data/lib/procon_bypass_man/commands/run_remote_pbm_action_dispatch_command.rb +19 -0
  36. data/lib/procon_bypass_man/commands/send_error_command.rb +19 -0
  37. data/lib/procon_bypass_man/commands/send_reload_config_event_command.rb +11 -0
  38. data/lib/procon_bypass_man/commands/write_device_id_command.rb +12 -0
  39. data/lib/procon_bypass_man/commands/write_session_id_command.rb +7 -0
  40. data/lib/procon_bypass_man/commands.rb +8 -0
  41. data/lib/procon_bypass_man/configuration.rb +48 -8
  42. data/lib/procon_bypass_man/device_connector.rb +33 -26
  43. data/lib/procon_bypass_man/device_status.rb +44 -0
  44. data/lib/procon_bypass_man/io_monitor.rb +9 -4
  45. data/lib/procon_bypass_man/procon/analog_stick.rb +31 -0
  46. data/lib/procon_bypass_man/procon/analog_stick_cap.rb +9 -32
  47. data/lib/procon_bypass_man/procon/button_collection.rb +1 -0
  48. data/lib/procon_bypass_man/procon/{data.rb → consts.rb} +1 -1
  49. data/lib/procon_bypass_man/procon/layer_changer.rb +40 -0
  50. data/lib/procon_bypass_man/procon/user_operation.rb +11 -17
  51. data/lib/procon_bypass_man/procon.rb +6 -10
  52. data/lib/procon_bypass_man/procon_reader.rb +31 -0
  53. data/lib/procon_bypass_man/remote_pbm_action/base_action.rb +53 -0
  54. data/lib/procon_bypass_man/remote_pbm_action/change_pbm_version_action.rb +21 -0
  55. data/lib/procon_bypass_man/remote_pbm_action/lib/update_remote_pbm_action_status_command.rb +24 -0
  56. data/lib/procon_bypass_man/remote_pbm_action/reboot_os_action.rb +17 -0
  57. data/lib/procon_bypass_man/remote_pbm_action/stop_pbm_action.rb +17 -0
  58. data/lib/procon_bypass_man/remote_pbm_action.rb +31 -0
  59. data/lib/procon_bypass_man/runner.rb +14 -112
  60. data/lib/procon_bypass_man/scheduler.rb +92 -0
  61. data/lib/procon_bypass_man/splatoon2/macro/fast_return.rb +15 -0
  62. data/lib/procon_bypass_man/splatoon2/macro/jump_to_left_key.rb +15 -0
  63. data/lib/procon_bypass_man/splatoon2/macro/jump_to_right_key.rb +15 -0
  64. data/lib/procon_bypass_man/splatoon2/macro/jump_to_up_key.rb +15 -0
  65. data/lib/procon_bypass_man/splatoon2/mode/guruguru.rb +57 -0
  66. data/lib/procon_bypass_man/splatoon2/version.rb +7 -0
  67. data/lib/procon_bypass_man/splatoon2.rb +11 -0
  68. data/lib/procon_bypass_man/{callbacks.rb → support/callbacks.rb} +0 -0
  69. data/lib/procon_bypass_man/support/compress_array.rb +56 -0
  70. data/lib/procon_bypass_man/support/http_client.rb +102 -0
  71. data/lib/procon_bypass_man/{on_memory_cache.rb → support/on_memory_cache.rb} +0 -0
  72. data/lib/procon_bypass_man/support/report_http_client.rb +19 -0
  73. data/lib/procon_bypass_man/{timer.rb → support/safe_timeout.rb} +1 -1
  74. data/lib/procon_bypass_man/support/send_device_stats_http_client.rb +9 -0
  75. data/lib/procon_bypass_man/support/server_pool.rb +42 -0
  76. data/lib/procon_bypass_man/support/signal_handler.rb +11 -0
  77. data/lib/procon_bypass_man/support/update_remote_pbm_action_status_http_client.rb +9 -0
  78. data/lib/procon_bypass_man/support/uptime.rb +25 -0
  79. data/lib/procon_bypass_man/value_objects/remote_pbm_action_object.rb +38 -0
  80. data/lib/procon_bypass_man/version.rb +1 -1
  81. data/lib/procon_bypass_man.rb +57 -20
  82. data/procon_bypass_man.gemspec +3 -3
  83. data/project_template/README.md +18 -11
  84. data/project_template/app.rb +3 -4
  85. data/project_template/web.rb +1 -1
  86. data/sig/{README.rb → README.md} +0 -0
  87. data/sig/main.rbs +12 -13
  88. metadata +82 -26
  89. data/examples/practical/app.rb +0 -21
  90. data/examples/practical/setting.yml +0 -24
  91. data/lib/procon_bypass_man/analog_stick_position.rb +0 -8
  92. data/lib/procon_bypass_man/outbound/base.rb +0 -40
  93. data/lib/procon_bypass_man/outbound/error_reporter.rb +0 -13
  94. data/lib/procon_bypass_man/outbound/reporter.rb +0 -12
  95. data/lib/procon_bypass_man/outbound/usb_hid_data_reporter.rb +0 -13
  96. data/lib/procon_bypass_man/procon/debug_dumper.rb +0 -17
  97. data/lib/procon_bypass_man/procon/layer_changeable.rb +0 -28
  98. data/lib/procon_bypass_man/procon/pressed_button_helper.rb +0 -15
  99. data/lib/procon_bypass_man/uptime.rb +0 -15
@@ -1,5 +1,6 @@
1
1
  class ProconBypassMan::DeviceConnector
2
2
  class BytesMismatchError < StandardError; end
3
+ class NotFoundProconError < StandardError; end
3
4
 
4
5
  class Value
5
6
  attr_accessor :read_from, :values
@@ -48,6 +49,7 @@ class ProconBypassMan::DeviceConnector
48
49
  end
49
50
 
50
51
  def drain_all
52
+ debug_log_buffer = []
51
53
  unless @initialized_devices
52
54
  init_devices
53
55
  end
@@ -55,11 +57,13 @@ class ProconBypassMan::DeviceConnector
55
57
  while(item = @stack.shift)
56
58
  item.values.each do |value|
57
59
  data = nil
58
- timer = ProconBypassMan::Timer.new
60
+ timer = ProconBypassMan::SafeTimeout.new
59
61
  begin
60
62
  timer.throw_if_timeout!
61
63
  data = from_device(item).read_nonblock(64)
64
+ debug_log_buffer << "read_from(#{item.read_from}): #{data}"
62
65
  rescue IO::EAGAINWaitReadable
66
+ debug_log_buffer << "read_from(#{item.read_from}): IO::EAGAINWaitReadable"
63
67
  retry
64
68
  end
65
69
 
@@ -74,15 +78,19 @@ class ProconBypassMan::DeviceConnector
74
78
  end
75
79
  if result
76
80
  ProconBypassMan.logger.info "OK(expected: #{value}, got: #{data.unpack("H*")})"
81
+ debug_log_buffer << "OK(expected: #{value}, got: #{data.unpack("H*")})"
77
82
  else
78
83
  ProconBypassMan.logger.info "NG(expected: #{value}, got: #{data.unpack("H*")})"
84
+ debug_log_buffer << "NG(expected: #{value}, got: #{data.unpack("H*")})"
79
85
  raise BytesMismatchError if @throw_error_if_mismatch
80
86
  end
81
87
  to_device(item).write_nonblock(data)
82
88
  end
83
89
  end
84
- rescue ProconBypassMan::Timer::Timeout
90
+ rescue ProconBypassMan::SafeTimeout::Timeout
85
91
  ProconBypassMan.logger.error "timeoutになりました"
92
+ copressed_buffer_text = ProconBypassMan::CompressArray.new(debug_log_buffer).compress.join("\n")
93
+ ProconBypassMan::SendErrorCommand.execute(error: copressed_buffer_text)
86
94
  raise if @throw_error_if_timeout
87
95
  end
88
96
 
@@ -95,31 +103,31 @@ class ProconBypassMan::DeviceConnector
95
103
  init_devices
96
104
  end
97
105
 
98
- timer = ProconBypassMan::Timer.new
106
+ timer = ProconBypassMan::SafeTimeout.new
99
107
  data = nil
100
108
  begin
101
109
  timer.throw_if_timeout!
102
110
  switch.write_nonblock(data)
103
111
  rescue IO::EAGAINWaitReadable
104
112
  retry
105
- rescue ProconBypassMan::Timer::Timeout
113
+ rescue ProconBypassMan::SafeTimeout::Timeout
106
114
  ProconBypassMan.logger.error "writeでtimeoutになりました"
107
115
  raise
108
116
  end
109
117
  return(data.unpack("H*")) if only_write
110
118
 
111
- timer = ProconBypassMan::Timer.new
119
+ timer = ProconBypassMan::SafeTimeout.new
112
120
  begin
113
121
  timer.throw_if_timeout!
114
122
  data = switch.read_nonblock(64)
115
123
  ProconBypassMan.logger.debug { " >>> #{data.unpack("H*")})" }
116
124
  rescue IO::EAGAINWaitReadable
117
125
  retry
118
- rescue ProconBypassMan::Timer::Timeout
126
+ rescue ProconBypassMan::SafeTimeout::Timeout
119
127
  ProconBypassMan.logger.error "readでtimeoutになりました"
120
128
  raise
121
129
  end
122
- rescue ProconBypassMan::Timer::Timeout
130
+ rescue ProconBypassMan::SafeTimeout::Timeout
123
131
  raise if @throw_error_if_timeout
124
132
  end
125
133
 
@@ -131,30 +139,30 @@ class ProconBypassMan::DeviceConnector
131
139
  init_devices
132
140
  end
133
141
 
134
- timer = ProconBypassMan::Timer.new
142
+ timer = ProconBypassMan::SafeTimeout.new
135
143
  begin
136
144
  timer.throw_if_timeout!
137
145
  procon.write_nonblock(data)
138
146
  rescue IO::EAGAINWaitReadable
139
147
  retry
140
- rescue ProconBypassMan::Timer::Timeout
148
+ rescue ProconBypassMan::SafeTimeout::Timeout
141
149
  ProconBypassMan.logger.error "writeでtimeoutになりました"
142
150
  raise
143
151
  end
144
152
  return(data.unpack("H*")) if only_write
145
153
 
146
- timer = ProconBypassMan::Timer.new
154
+ timer = ProconBypassMan::SafeTimeout.new
147
155
  begin
148
156
  timer.throw_if_timeout!
149
157
  data = procon.read_nonblock(64)
150
158
  ProconBypassMan.logger.error " <<< #{data.unpack("H*")})"
151
159
  rescue IO::EAGAINWaitReadable
152
160
  retry
153
- rescue ProconBypassMan::Timer::Timeout
161
+ rescue ProconBypassMan::SafeTimeout::Timeout
154
162
  ProconBypassMan.logger.error "readでtimeoutになりました"
155
163
  raise
156
164
  end
157
- rescue ProconBypassMan::Timer::Timeout
165
+ rescue ProconBypassMan::SafeTimeout::Timeout
158
166
  raise if @throw_error_if_timeout
159
167
  end
160
168
 
@@ -164,30 +172,30 @@ class ProconBypassMan::DeviceConnector
164
172
  end
165
173
 
166
174
  data = nil
167
- timer = ProconBypassMan::Timer.new
175
+ timer = ProconBypassMan::SafeTimeout.new
168
176
  begin
169
177
  timer.throw_if_timeout!
170
178
  data = procon.read_nonblock(64)
171
179
  ProconBypassMan.logger.debug { " <<< #{data.unpack("H*")})" }
172
180
  rescue IO::EAGAINWaitReadable
173
181
  retry
174
- rescue ProconBypassMan::Timer::Timeout
182
+ rescue ProconBypassMan::SafeTimeout::Timeout
175
183
  ProconBypassMan.logger.error "readでtimeoutになりました"
176
184
  raise
177
185
  end
178
186
  return(data.unpack("H*")) if only_read
179
187
 
180
- timer = ProconBypassMan::Timer.new
188
+ timer = ProconBypassMan::SafeTimeout.new
181
189
  begin
182
190
  timer.throw_if_timeout!
183
191
  switch.write_nonblock(data)
184
192
  rescue IO::EAGAINWaitReadable
185
193
  retry
186
- rescue ProconBypassMan::Timer::Timeout
194
+ rescue ProconBypassMan::SafeTimeout::Timeout
187
195
  ProconBypassMan.logger.error "writeでtimeoutになりました"
188
196
  raise
189
197
  end
190
- rescue ProconBypassMan::Timer::Timeout
198
+ rescue ProconBypassMan::SafeTimeout::Timeout
191
199
  raise if @throw_error_if_timeout
192
200
  end
193
201
 
@@ -197,30 +205,30 @@ class ProconBypassMan::DeviceConnector
197
205
  end
198
206
 
199
207
  data = nil
200
- timer = ProconBypassMan::Timer.new
208
+ timer = ProconBypassMan::SafeTimeout.new
201
209
  begin
202
210
  timer.throw_if_timeout!
203
211
  data = switch.read_nonblock(64)
204
212
  ProconBypassMan.logger.debug { " >>> #{data.unpack("H*")})" }
205
213
  rescue IO::EAGAINWaitReadable
206
214
  retry
207
- rescue ProconBypassMan::Timer::Timeout
215
+ rescue ProconBypassMan::SafeTimeout::Timeout
208
216
  ProconBypassMan.logger.error "readでtimeoutになりました"
209
217
  raise
210
218
  end
211
219
  return(data.unpack("H*")) if only_read
212
220
 
213
- timer = ProconBypassMan::Timer.new
221
+ timer = ProconBypassMan::SafeTimeout.new
214
222
  begin
215
223
  timer.throw_if_timeout!
216
224
  procon.write_nonblock(data)
217
225
  rescue IO::EAGAINWaitReadable
218
226
  retry
219
- rescue ProconBypassMan::Timer::Timeout
227
+ rescue ProconBypassMan::SafeTimeout::Timeout
220
228
  ProconBypassMan.logger.error "writeでtimeoutになりました"
221
229
  raise
222
230
  end
223
- rescue ProconBypassMan::Timer::Timeout
231
+ rescue ProconBypassMan::SafeTimeout::Timeout
224
232
  raise if @throw_error_if_timeout
225
233
  end
226
234
 
@@ -293,7 +301,7 @@ class ProconBypassMan::DeviceConnector
293
301
  @procon = File.open(PROCON2_PATH, "w+b")
294
302
  @gadget = File.open('/dev/hidg0', "w+b")
295
303
  else
296
- raise "/dev/hidraw0, /dev/hidraw1の両方見つかりませんでした"
304
+ raise NotFoundProconError, "/dev/hidraw0, /dev/hidraw1の両方見つかりませんでした"
297
305
  end
298
306
  system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
299
307
  system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')
@@ -309,11 +317,10 @@ class ProconBypassMan::DeviceConnector
309
317
  end
310
318
  rescue Errno::ENXIO => e
311
319
  # /dev/hidg0 をopenできないときがある
312
- ProconBypassMan.logger.error "Errno::ENXIO (No such device or address @ rb_sysopen - /dev/hidg0)が起きました。resetします"
313
- ProconBypassMan.logger.error e
320
+ ProconBypassMan::SendErrorCommand.execute(error: "Errno::ENXIO (No such device or address @ rb_sysopen - /dev/hidg0)が起きました。resetします. #{e.full_message}")
314
321
  system('echo > /sys/kernel/config/usb_gadget/procon/UDC')
315
322
  system('ls /sys/class/udc > /sys/kernel/config/usb_gadget/procon/UDC')
316
- sleep 2
323
+ sleep 0.5
317
324
  retry
318
325
  end
319
326
  end
@@ -0,0 +1,44 @@
1
+ class ProconBypassMan::DeviceStatus
2
+ INITIALIZED = :initialized
3
+ RUNNING = :running
4
+ CONNECTED_BUT_SLEEPING = :connected_but_sleeping # コードはつながっているが、switchがsleepしているとき
5
+ PROCON_NOT_FOUND_ERROR = :procon_not_found_error # 繋がっていないとか、デバイスが使えない時
6
+ CONNECTED_BUT_ERROR = :connected_but_error # 実行時エラーあたり
7
+ SETTING_SYNTAX_ERROR_AND_SHUTDOWN = :setting_syntax_error_and_shutdown
8
+
9
+ @@status = nil
10
+
11
+ def self.current
12
+ @@status || INITIALIZED
13
+ end
14
+
15
+ def self.change_to_running!
16
+ @@status = RUNNING
17
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
18
+ end
19
+
20
+ def self.change_to_connected_but_sleeping!
21
+ @@status = CONNECTED_BUT_SLEEPING
22
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
23
+ end
24
+
25
+ def self.change_to_procon_not_found_error!
26
+ @@status = PROCON_NOT_FOUND_ERROR
27
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
28
+ end
29
+
30
+ def self.change_to_device_error!
31
+ @@status = DEVICE_ERROR
32
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
33
+ end
34
+
35
+ def self.change_to_connected_but_error!
36
+ @@status = CONNECTED_BUT_ERROR
37
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
38
+ end
39
+
40
+ def self.change_to_setting_syntax_error_and_shutdown!
41
+ @@status = SETTING_SYNTAX_ERROR_AND_SHUTDOWN
42
+ ProconBypassMan::SyncDeviceStatsJob.perform_async(ProconBypassMan::DeviceStatus.current)
43
+ end
44
+ end
@@ -1,11 +1,12 @@
1
1
  module ProconBypassMan
2
2
  class Counter
3
- attr_accessor :label, :table, :previous_table
3
+ attr_accessor :label, :table, :previous_table, :active
4
4
 
5
5
  def initialize(label: )
6
6
  self.label = label
7
7
  self.table = {}
8
8
  self.previous_table = {}
9
+ self.active = true
9
10
  end
10
11
 
11
12
  # アクティブなバケットは1つだけ
@@ -24,7 +25,7 @@ module ProconBypassMan
24
25
  self
25
26
  end
26
27
 
27
- def formated_previous_table
28
+ def formatted_previous_table
28
29
  t = previous_table.dup
29
30
  start_function = t[:start_function] || 0
30
31
  end_function = t[:end_function] || 0
@@ -32,6 +33,10 @@ module ProconBypassMan
32
33
  eagain_wait_readable_on_write = t[:eagain_wait_readable_on_write] || 0
33
34
  "(#{(end_function / start_function.to_f * 100).floor(1)}%(#{end_function}/#{start_function}), loss: #{eagain_wait_readable_on_read}, #{eagain_wait_readable_on_write})"
34
35
  end
36
+
37
+ def shutdown
38
+ self.active = false
39
+ end
35
40
  end
36
41
 
37
42
  module IOMonitor
@@ -51,14 +56,14 @@ module ProconBypassMan
51
56
  Thread.start do
52
57
  max_output_length = 0
53
58
  loop do
54
- list = @@list.dup
59
+ list = @@list.select(&:active).dup
55
60
  unless list.all? { |x| x&.previous_table.is_a?(Hash) }
56
61
  sleep 0.5
57
62
  next
58
63
  end
59
64
 
60
65
  line = list.map { |counter|
61
- "#{counter.label}(#{counter.formated_previous_table})"
66
+ "#{counter.label}(#{counter.formatted_previous_table})"
62
67
  }.join(", ")
63
68
  max_output_length = line.length
64
69
  sleep 0.7
@@ -0,0 +1,31 @@
1
+ class ProconBypassMan::Procon::AnalogStick
2
+ attr_accessor :neutral_position
3
+ attr_accessor :bin_x, :bin_y
4
+
5
+ def initialize(binary: )
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")
11
+
12
+ self.bin_x = "#{byte7[4..7]}#{byte6}"
13
+ self.bin_y = "#{byte8}#{byte7[0..3]}"
14
+ end
15
+
16
+ def abs_x
17
+ bin_x.to_i(2)
18
+ end
19
+
20
+ def abs_y
21
+ bin_y.to_i(2)
22
+ end
23
+
24
+ def relative_x
25
+ bin_x.to_i(2) - neutral_position.x
26
+ end
27
+
28
+ def relative_y
29
+ bin_y.to_i(2) - neutral_position.y
30
+ end
31
+ end
@@ -18,19 +18,9 @@ class ProconBypassMan::Procon::AnalogStickCap
18
18
  end
19
19
  end
20
20
 
21
- attr_accessor :bin_x, :bin_y
22
- attr_accessor :neutral_position
23
-
24
21
  def initialize(binary)
25
- @neutral_position = ProconBypassMan::ButtonsSettingConfiguration.instance.neutral_position
26
22
  @binary = binary
27
-
28
- byte6 = binary[6].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
29
- byte7 = binary[7].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
30
- byte8 = binary[8].unpack("H*").first.to_i(16).to_s(2).rjust(8, "0")
31
-
32
- self.bin_x = "#{byte7[4..7]}#{byte6}"
33
- self.bin_y = "#{byte8}#{byte7[0..3]}"
23
+ @analog_stick = ProconBypassMan::Procon::AnalogStick.new(binary: binary)
34
24
  end
35
25
 
36
26
  # @return [ProconBypassMan::Procon::AnalogStickCap::Position]
@@ -41,8 +31,8 @@ class ProconBypassMan::Procon::AnalogStickCap
41
31
  relative_capped_x = -(relative_capped_x.abs) if relative_x.negative?
42
32
  relative_capped_y = -(relative_capped_y.abs) if relative_y.negative?
43
33
  return Position.new(
44
- x: relative_capped_x + neutral_position.x,
45
- y: relative_capped_y + neutral_position.y,
34
+ x: relative_capped_x + @analog_stick.neutral_position.x,
35
+ y: relative_capped_y + @analog_stick.neutral_position.y,
46
36
  )
47
37
  else
48
38
  return position
@@ -54,23 +44,10 @@ class ProconBypassMan::Procon::AnalogStickCap
54
44
  Position.new(x: abs_x, y: abs_y)
55
45
  end
56
46
 
57
- # 0, 0からのx
58
- def abs_x
59
- bin_x.to_i(2)
60
- end
61
-
62
- # 0, 0からのy
63
- def abs_y
64
- bin_y.to_i(2)
65
- end
66
-
67
- def relative_x
68
- bin_x.to_i(2) - neutral_position.x
69
- end
70
-
71
- def relative_y
72
- bin_y.to_i(2) - neutral_position.y
73
- end
47
+ def abs_x; @analog_stick.abs_x; end # 0, 0からのx
48
+ def abs_y; @analog_stick.abs_y; end # 0, 0からのy
49
+ def relative_x; @analog_stick.relative_x; end
50
+ def relative_y; @analog_stick.relative_y; end
74
51
 
75
52
  # @deprecated
76
53
  def x; relative_x; end
@@ -78,11 +55,11 @@ class ProconBypassMan::Procon::AnalogStickCap
78
55
 
79
56
  def rad
80
57
  (
81
- Math.atan(y / x.to_f) * 180 / Math::PI
58
+ Math.atan(relative_y / relative_x.to_f) * 180 / Math::PI
82
59
  ).floor(6)
83
60
  end
84
61
 
85
62
  def hypotenuse
86
- Math.sqrt(x**2 + y**2).floor(6)
63
+ Math.sqrt((relative_x**2) + (relative_y**2)).floor(6)
87
64
  end
88
65
  end
@@ -37,6 +37,7 @@ class ProconBypassMan::Procon::ButtonCollection
37
37
  BUTTONS_MAP = BYTES_MAP.reduce({}) { |acc, value|
38
38
  next acc if value[1].nil?
39
39
  value[1].reverse.each.with_index do |button, index|
40
+ next(acc) if button == :grip || button == :_undefined_key
40
41
  acc[button] = { byte_position: value[0], bit_position: index }
41
42
  end
42
43
  acc
@@ -1,5 +1,5 @@
1
1
  class ProconBypassMan::Procon
2
- module Data
2
+ module Consts
3
3
  NO_ACTION = "30f28100800078c77448287509550274ff131029001b0022005a0271ff191028001e00210064027cff1410280020002100000000000000000000000000000000".freeze
4
4
  end
5
5
  end
@@ -0,0 +1,40 @@
1
+ class ProconBypassMan::Procon::LayerChanger
2
+ def initialize(binary: )
3
+ @procon_reader = ProconBypassMan::ProconReader.new(binary: binary).freeze
4
+ end
5
+
6
+ # @return [Symbol]
7
+ def next_layer_key
8
+ case
9
+ when pressed?(button: :up)
10
+ :up
11
+ when pressed?(button: :right)
12
+ :right
13
+ when pressed?(button: :left)
14
+ :left
15
+ when pressed?(button: :down)
16
+ :down
17
+ else
18
+ ProconBypassMan.logger.warn("next_layer_key is unknown")
19
+ :up
20
+ end
21
+ end
22
+
23
+ # @return [Boolean]
24
+ def change_layer?
25
+ if ProconBypassMan::ButtonsSettingConfiguration.instance.prefix_keys.empty?
26
+ raise "prefix_keysが未設定です"
27
+ end
28
+ ProconBypassMan::ButtonsSettingConfiguration.instance.prefix_keys.map { |b| pressed?(button: b) }.all?
29
+ end
30
+
31
+ # @return [Boolean]
32
+ def pressed_next_layer?
33
+ change_layer? && (pressed?(button: :up) || pressed?(button: :right) || pressed?(button: :left) || pressed?(button: :down))
34
+ end
35
+
36
+ # @return [Boolean]
37
+ def pressed?(button: )
38
+ @procon_reader.pressed.include?(button)
39
+ end
40
+ end
@@ -1,12 +1,14 @@
1
1
  class ProconBypassMan::Procon
2
2
  class UserOperation
3
- include LayerChangeable
4
- extend PushedButtonHelper::Dynamic
5
-
6
3
  attr_reader :binary
7
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
10
+
8
11
  def initialize(binary)
9
- self.class.compile_if_not_compile_yet!
10
12
  unless binary.encoding.name == ASCII_ENCODING
11
13
  raise "おかしいです"
12
14
  end
@@ -16,20 +18,16 @@ class ProconBypassMan::Procon
16
18
  ZERO_BIT = ["0"].pack("H*").freeze
17
19
  ASCII_ENCODING = "ASCII-8BIT"
18
20
 
19
- # @depilicate
20
- def binary=(binary)
21
- unless binary.encoding.name == ASCII_ENCODING
22
- raise "おかしいです"
23
- end
24
- @binary = binary
25
- end
26
-
27
21
  def set_no_action!
28
22
  binary[3] = ZERO_BIT
29
23
  binary[4] = ZERO_BIT
30
24
  binary[5] = ZERO_BIT
31
25
  end
32
26
 
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
30
+
33
31
  def unpress_button(button)
34
32
  return if not pressed_button?(button)
35
33
 
@@ -38,10 +36,6 @@ class ProconBypassMan::Procon
38
36
  binary[byte_position] = ["%02X" % value.to_s].pack("H*")
39
37
  end
40
38
 
41
- def apply_left_analog_stick_cap(cap: )
42
- binary[6..8] = ProconBypassMan::Procon::AnalogStickCap.new(binary).capped_position(cap_hypotenuse: cap).to_binary
43
- end
44
-
45
39
  def press_button(button)
46
40
  return if pressed_button?(button)
47
41
 
@@ -51,7 +45,7 @@ class ProconBypassMan::Procon
51
45
  end
52
46
 
53
47
  def press_button_only(button)
54
- [ProconBypassMan::Procon::Data::NO_ACTION.dup].pack("H*").tap do |no_action_binary|
48
+ [ProconBypassMan::Procon::Consts::NO_ACTION.dup].pack("H*").tap do |no_action_binary|
55
49
  ButtonCollection.load(button).byte_position
56
50
  byte_position = ButtonCollection.load(button).byte_position
57
51
  value = 2**ButtonCollection.load(button).bit_position
@@ -1,10 +1,9 @@
1
1
  class ProconBypassMan::Procon
2
- require "procon_bypass_man/procon/data"
2
+ require "procon_bypass_man/procon/consts"
3
3
  require "procon_bypass_man/procon/mode_registry"
4
4
  require "procon_bypass_man/procon/macro_registry"
5
- require "procon_bypass_man/procon/layer_changeable"
5
+ require "procon_bypass_man/procon/layer_changer"
6
6
  require "procon_bypass_man/procon/button_collection"
7
- require "procon_bypass_man/procon/pressed_button_helper"
8
7
  require "procon_bypass_man/procon/user_operation"
9
8
  require "procon_bypass_man/procon/flip_cache"
10
9
  require "procon_bypass_man/procon/press_button_aware"
@@ -35,8 +34,9 @@ class ProconBypassMan::Procon
35
34
  end
36
35
 
37
36
  def apply!
38
- if user_operation.change_layer?
39
- @@status[:current_layer_key] = user_operation.next_layer_key if user_operation.pressed_next_layer?
37
+ layer_changer = ProconBypassMan::Procon::LayerChanger.new(binary: user_operation.binary)
38
+ if layer_changer.change_layer?
39
+ @@status[:current_layer_key] = layer_changer.next_layer_key if layer_changer.pressed_next_layer?
40
40
  user_operation.set_no_action!
41
41
  return
42
42
  end
@@ -137,11 +137,7 @@ class ProconBypassMan::Procon
137
137
  end
138
138
  end
139
139
 
140
- b = user_operation.binary
141
- ProconBypassMan.cache.fetch key: 'user_operation.binary', expires_in: 60 do
142
- ProconBypassMan::Procon::DebugDumper.new(binary: b).dump_analog_sticks
143
- end
144
- b
140
+ user_operation.binary
145
141
  end
146
142
 
147
143
  private
@@ -0,0 +1,31 @@
1
+ class ProconBypassMan::ProconReader
2
+ def initialize(binary: )
3
+ @binary = binary
4
+ @analog_stick = ProconBypassMan::Procon::AnalogStick.new(binary: binary)
5
+ end
6
+
7
+ # @return [Array<Symbol>]
8
+ def pressed
9
+ aware = ProconBypassMan::PpressButtonAware.new(@binary)
10
+ pressed_table = ::ProconBypassMan::Procon::ButtonCollection::BUTTONS.reduce({}) do |acc, button|
11
+ acc[button] = aware.pressed_button?(button)
12
+ acc
13
+ end
14
+ pressed_table.select { |_key, value| value }.keys
15
+ end
16
+
17
+ def left_analog_stick
18
+ { x: @analog_stick.relative_x, y: @analog_stick.relative_y }
19
+ end
20
+
21
+ def left_analog_stick_by_abs
22
+ { x: @analog_stick.abs_x, y: @analog_stick.abs_y }
23
+ end
24
+
25
+ def to_hash
26
+ { left_analog_stick: left_analog_stick,
27
+ left_analog_stick_by_abs: left_analog_stick_by_abs,
28
+ buttons: pressed,
29
+ }
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class ActionUnexpectedError < StandardError; end
4
+ class NeedPbmVersionError < ActionUnexpectedError; end
5
+
6
+ class BaseAction
7
+ attr_accessor :pbm_job_uuid
8
+
9
+ # @param [String] pbm_job_uuid
10
+ def initialize(pbm_job_uuid: )
11
+ self.pbm_job_uuid = pbm_job_uuid
12
+ end
13
+
14
+ # @return [void]
15
+ def action_content(_args)
16
+ raise NotImplementedError, nil
17
+ end
18
+
19
+ # @param [Hash] args
20
+ # @return [void]
21
+ def run!(job_args: )
22
+ before_action_callback
23
+ action_content(args: job_args)
24
+ after_action_callback
25
+ rescue => e
26
+ be_failed
27
+ ProconBypassMan::SendErrorCommand.execute(error: e)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [void]
33
+ def before_action_callback; end
34
+ # @return [void]
35
+ def after_action_callback; end
36
+
37
+ # @return [void]
38
+ def be_failed
39
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :failed)
40
+ end
41
+
42
+ # @return [void]
43
+ def be_in_progress
44
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :in_progress)
45
+ end
46
+
47
+ # @return [void]
48
+ def be_processed
49
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :processed)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class ChangePbmVersionAction < BaseAction
4
+
5
+ def action_content(args: )
6
+ require "pbmenv"
7
+ ProconBypassMan.logger.info "execute ChangePbmVersionAction!"
8
+ pbm_version = args["pbm_version"] or raise(ProconBypassMan::RemotePbmAction::NeedPbmVersionError, "pbm_versionが必要です, #{args.inspect}")
9
+ Pbmenv.install(pbm_version)
10
+ Pbmenv.use(pbm_version)
11
+ `reboot` # symlinkの参照先が変わるのでrebootする必要がある
12
+ end
13
+
14
+ private
15
+
16
+ def before_action_callback
17
+ be_processed
18
+ end
19
+ end
20
+ end
21
+ end