Skip to content

Commit

Permalink
Merge pull request #3 from splaspla-hacker/edge
Browse files Browse the repository at this point in the history
0.1.3
  • Loading branch information
jiikko authored Jul 3, 2021
2 parents 84788e7 + f4f6381 commit 86dc83b
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 123 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.1.3] - 2021-07-03
- 接続のしやすさ向上

## [0.1.2] - 2021-06-25
- 設定ファイルのライブリロードができるようになった
- tmp/pidにUSR2シグナルを送信すると設定を再読み込みする
Expand Down
84 changes: 0 additions & 84 deletions CODE_OF_CONDUCT.md

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
procon_bypass_man (0.1.2)
procon_bypass_man (0.1.3)

GEM
remote: https://rubygems.org/
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Switch <-- (PBM): ZR連打
## 使うハードウェア
* プロコン
* Switch本体とドック
* Raspberry Pi4
* Raspberry Pi4 Model B/4GB(Raspberry Pi OS (32-bit))
* 他のシリーズは未確認です
* データ通信が可能なUSBケーブル

Expand Down Expand Up @@ -55,23 +55,20 @@ Switch <-- (PBM): ZR連打
* 設定ファイルをwebから反映できる
* ラズパイのプロビジョニングを楽にしたい
* レコーディング機能(プロコンの入力をマクロとして登録ができる)
* swtichとの接続完了はIOを見て判断する
* webページから設定ファイルを変更できるようにする(sshしたくない)
* webサーバのデーモンとPBMはプロセスを分ける(NOTE)
* プロセスを停止するときにtmp/pidを削除する

## 開発系TIPS
### ロギング
## 開発系
```ruby
ProconBypassMan.tap do |pbm|
pbm.logger = STDOUT
pbm.logger = Logger.new("#{ProconBypassMan.root}/app.log", 5, 1024 * 1024 * 10)
pbm.logger.level = :debug
end
```

### 設定ファイルのライブリロード
### プロコンとの接続を維持したまま、現在の設定ファイルをPBMに反映する
```shell
sudo kill -USR2 `cat tmp/pid`
sudo kill -USR2 `cat ./pbm_pid`
```

## License
Expand Down
27 changes: 27 additions & 0 deletions examples/pbm.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# sudo ln -s /home/pi/src/procon_bypass_man/examples/pbm.service /etc/systemd/system/pbm.service
# Usage:
# systemctl daemon-reload
# systemctl enable pbm.service
# systemctl start pbm.service
# systemctl status pbm.service
# systemctl restart pbm.service
# Debug:
# journalctl -xe -f
# TODO:
# do not make PIDFILE

[Unit]
Description=PBM
# After=network-online.target
After=network.target

[Service]
Type=simple
# PIDFile=/home/pi/src/procon_bypass_man/pbm_pid
# WatchdogSec=60
WorkingDirectory=/home/pi/src/procon_bypass_man
ExecStart=/home/pi/.rbenv/versions/3.0.1/bin/ruby examples/practical/app.rb
Restart=always

[Install]
WantedBy=multi-user.target
6 changes: 5 additions & 1 deletion examples/practical/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env ruby

# sudo ln -s /home/pi/src/procon_bypass_man/examples/practical/setting.yml /home/pi/src/procon_bypass_man/setting.yml
# cd src/procon_bypass_man
# sudo ruby examples/practical/app.rb

require 'bundler/inline'

gemfile do
Expand All @@ -9,7 +13,7 @@
end

ProconBypassMan.tap do |pbm|
pbm.logger = "./app.log"
pbm.logger = Logger.new("#{ProconBypassMan.root}/app.log", 5, 1024 * 1024 * 10) # 5世代まで残して, 10MBでローテーション
pbm.logger.level = :debug
end

Expand Down
23 changes: 17 additions & 6 deletions lib/procon_bypass_man.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "logger"
require 'yaml'
require "fileutils"

require_relative "procon_bypass_man/version"
require_relative "procon_bypass_man/device_registry"
Expand All @@ -16,7 +17,7 @@
module ProconBypassMan
class ProConRejected < StandardError; end
class CouldNotLoadConfigError < StandardError; end
class CouldNotConnectDeviceError < StandardError; end
class FirstConnectionError < StandardError; end

def self.configure(setting_path: nil, &block)
unless setting_path
Expand All @@ -32,20 +33,21 @@ def self.configure(setting_path: nil, &block)

def self.run(setting_path: nil, &block)
configure(setting_path: setting_path, &block)
at_exit { FileUtils.rm_rf(pid_path) }
File.write(pid_path, $$)
registry = ProconBypassMan::DeviceRegistry.new
Runner.new(gadget: registry.gadget, procon: registry.procon).run
rescue CouldNotLoadConfigError
ProconBypassMan.logger.error "設定ファイルが不正です。設定ファイルの読み込みに失敗しました"
puts "設定ファイルが不正です。設定ファイルの読み込みに失敗しました"
exit 1
rescue CouldNotConnectDeviceError
ProconBypassMan.logger.error "デバイスと接続中です"
puts "デバイスと接続中です"
rescue FirstConnectionError
puts "接続を確立できませんでした。やりなおします。"
retry
end

def self.logger=(dev)
@@logger = Logger.new(dev, 5, 1024 * 1024 * 10) # 5世代まで残して, 10MBでローテーション
def self.logger=(logger)
@@logger = logger
end

def self.logger
Expand All @@ -56,11 +58,20 @@ def self.logger
end
end

DEFAULT_PID_PATH = File.expand_path('../pbm_pid', __dir__).freeze
def self.pid_path
@@pid_path ||= DEFAULT_PID_PATH
end

def self.reset!
ProconBypassMan::Procon::MacroRegistry.reset!
ProconBypassMan::Procon::ModeRegistry.reset!
ProconBypassMan::Procon.reset!
ProconBypassMan::Configuration.instance.reset!
ProconBypassMan::IOMonitor.reset!
end

def self.root
File.expand_path('..', __dir__).freeze
end
end
1 change: 1 addition & 0 deletions lib/procon_bypass_man/device_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def init_devices
break
else
puts "プロコンをラズベイに挿してください"
ProconBypassMan.logger.info("プロコンをラズベイに挿してください")
sleep(1)
end
end
Expand Down
8 changes: 0 additions & 8 deletions lib/procon_bypass_man/io_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def initialize(label: )

# アクティブなバケットは1つだけ
def record(event_name)
return unless $is_stable
key = Time.now.strftime("%S").to_i
if table[key].nil?
self.previous_table = table.values.first
Expand Down Expand Up @@ -58,13 +57,6 @@ def self.start!
next
end

s_to_p = list.detect { |x| x.label == "switch -> procon" }
previous_table = s_to_p&.previous_table.dup
if previous_table && previous_table.dig(:eagain_wait_readable_on_read) && previous_table.dig(:eagain_wait_readable_on_read) > 300
# ProconBypassMan.logger.debug { "接続の確立ができません" }
# Process.kill("USR1", Process.ppid)
end

line = list.map { |counter|
"#{counter.label}(#{counter.formated_previous_table})"
}.join(", ")
Expand Down
47 changes: 33 additions & 14 deletions lib/procon_bypass_man/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class ProconBypassMan::Runner
class InterruptForRestart < StandardError; end
class InterruptForCouldNotConnect < StandardError; end

def initialize(gadget: , procon: )
@gadget = gadget
Expand All @@ -14,7 +13,7 @@ def initialize(gadget: , procon: )

def run
first_negotiation
$is_stable = false
print_booted_message

self_read, self_write = IO.pipe
%w(TERM INT USR1 USR2).each do |sig|
Expand All @@ -27,9 +26,6 @@ def run
end
end

FileUtils.mkdir_p "tmp"
File.write "tmp/pid", $$

loop do
$will_terminate_token = false
main_loop_pid = fork { main_loop }
Expand All @@ -39,11 +35,6 @@ def run
signal = readable_io.first[0].gets.strip
handle_signal(signal)
end
rescue InterruptForCouldNotConnect
$will_terminate_token = true
Process.kill("TERM", main_loop_pid)
Process.wait
raise ProconBypassMan::CouldNotConnectDeviceError
rescue InterruptForRestart
$will_terminate_token = true
Process.kill("TERM", main_loop_pid)
Expand Down Expand Up @@ -72,10 +63,9 @@ def run
def main_loop
# TODO 接続確立完了をswitchを読み取るようにして、この暫定で接続完了sleepを消す
Thread.new do
sleep(10)
sleep(5)
$will_interval_0_0_0_5 = 0.005
$will_interval_1_6 = 1.6
$is_stable = true
end

ProconBypassMan::IOMonitor.start!
Expand Down Expand Up @@ -155,17 +145,46 @@ def first_negotiation
rescue IO::EAGAINWaitReadable
end
end

# ...
# switch) 8001
# procon) 8101
# switch) 8002
# が返ってくるプロトコルがあって、これができていないならやり直す
loop do
begin
data = @procon.read_nonblock(128)
if data[0] == "\x81".b && data[1] == "\x01".b
ProconBypassMan.logger.debug { "接続を確認しました" }
@gadget.write_nonblock(data)
break
else
raise ::ProconBypassMan::FirstConnectionError
end
rescue IO::EAGAINWaitReadable
end
end
end

def handle_signal(sig)
ProconBypassMan.logger.info "#{$$}#{sig}を受け取りました"
case sig
when 'USR1'
raise InterruptForCouldNotConnect
when 'USR2'
raise InterruptForRestart
when 'INT', 'TERM'
raise Interrupt
end
end

# @return [void]
def print_booted_message
booted_message = <<~EOF
ProconBypassMan: #{ProconBypassMan::VERSION}
pid_path: #{ProconBypassMan.pid_path}
pid: #{$$}
project_path: #{ProconBypassMan.root}
EOF
ProconBypassMan.logger.info(booted_message)
puts booted_message
end
end
2 changes: 1 addition & 1 deletion lib/procon_bypass_man/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ProconBypassMan
VERSION = "0.1.2"
VERSION = "0.1.3"
end

0 comments on commit 86dc83b

Please sign in to comment.