rufus-scheduler 적용
This commit is contained in:
parent
f7131c827f
commit
cf3dae63f3
|
|
@ -131,6 +131,7 @@
|
||||||
<orderEntry type="library" scope="PROVIDED" name="rubocop-rails-omakase (v1.1.0, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="rubocop-rails-omakase (v1.1.0, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="rubyzip (v2.4.1, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="rubyzip (v2.4.1, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="rufus-scheduler (v3.9.2, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="securerandom (v0.4.1, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="securerandom (v0.4.1, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="selenium-webdriver (v4.31.0, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="selenium-webdriver (v4.31.0, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
<orderEntry type="library" scope="PROVIDED" name="smart_properties (v1.17.0, rbenv: 3.4.2) [gem]" level="application" />
|
<orderEntry type="library" scope="PROVIDED" name="smart_properties (v1.17.0, rbenv: 3.4.2) [gem]" level="application" />
|
||||||
|
|
|
||||||
1
Gemfile
1
Gemfile
|
|
@ -71,3 +71,4 @@ end
|
||||||
|
|
||||||
gem "ccutrer-serialport"
|
gem "ccutrer-serialport"
|
||||||
gem "rmodbus"
|
gem "rmodbus"
|
||||||
|
gem "rufus-scheduler"
|
||||||
|
|
|
||||||
|
|
@ -294,6 +294,8 @@ GEM
|
||||||
rubocop-rails (>= 2.30)
|
rubocop-rails (>= 2.30)
|
||||||
ruby-progressbar (1.13.0)
|
ruby-progressbar (1.13.0)
|
||||||
rubyzip (2.4.1)
|
rubyzip (2.4.1)
|
||||||
|
rufus-scheduler (3.9.2)
|
||||||
|
fugit (~> 1.1, >= 1.11.1)
|
||||||
securerandom (0.4.1)
|
securerandom (0.4.1)
|
||||||
selenium-webdriver (4.31.0)
|
selenium-webdriver (4.31.0)
|
||||||
base64 (~> 0.2)
|
base64 (~> 0.2)
|
||||||
|
|
@ -382,6 +384,7 @@ DEPENDENCIES
|
||||||
rails (~> 8.0.2)
|
rails (~> 8.0.2)
|
||||||
rmodbus
|
rmodbus
|
||||||
rubocop-rails-omakase
|
rubocop-rails-omakase
|
||||||
|
rufus-scheduler
|
||||||
selenium-webdriver
|
selenium-webdriver
|
||||||
solid_cable
|
solid_cable
|
||||||
solid_cache
|
solid_cache
|
||||||
|
|
|
||||||
|
|
@ -1,56 +1,41 @@
|
||||||
|
# polling_service.rb
|
||||||
|
require "rufus-scheduler"
|
||||||
|
|
||||||
module Modbus
|
module Modbus
|
||||||
class PollingService
|
class PollingService
|
||||||
class << self
|
class << self
|
||||||
def start
|
def start
|
||||||
return if $modbus_polling_threads.any?(&:alive?)
|
return if defined?(@scheduler) && @scheduler&.running?
|
||||||
$modbus_polling_threads << start_thread
|
|
||||||
end
|
|
||||||
|
|
||||||
def stop
|
puts "[#{Time.current}] Modbus polling service 시작됨 (Rufus)"
|
||||||
$modbus_polling_threads.each { |t| t.kill if t.alive? }
|
@scheduler = Rufus::Scheduler.new
|
||||||
$modbus_polling_threads.clear
|
|
||||||
puts "Modbus polling service 중지됨"
|
|
||||||
end
|
|
||||||
|
|
||||||
def running?
|
@scheduler.cron "0 * * * * *" do
|
||||||
$modbus_polling_threads.any?(&:alive?)
|
now = Time.now
|
||||||
end
|
current_time = format("%02d:%02d", now.hour, now.min)
|
||||||
|
puts "# current time: #{current_time}.#{now.sec}"
|
||||||
private
|
schedule = Schedule.find_by(hour: now.hour, minute: now.min)
|
||||||
def start_thread
|
apply_schedule(schedule) if schedule
|
||||||
Thread.new { run_polling_loop }
|
|
||||||
end
|
|
||||||
|
|
||||||
def run_polling_loop
|
|
||||||
puts "Modbus polling service 시작됨"
|
|
||||||
last_logged = nil
|
|
||||||
|
|
||||||
loop do
|
|
||||||
begin
|
|
||||||
now = Time.now
|
|
||||||
current = [ now.hour, now.min ]
|
|
||||||
current_time = format("%02d:%02d", current[0], current[1])
|
|
||||||
puts "# current time: #{current_time}.#{now.sec}"
|
|
||||||
if current != last_logged
|
|
||||||
puts "시간 변경 감지됨: #{current_time}"
|
|
||||||
schedule = Schedule.find_by(hour: current[0], minute: current[1])
|
|
||||||
apply_schedule(schedule) if schedule
|
|
||||||
last_logged = current
|
|
||||||
end
|
|
||||||
sleep 1
|
|
||||||
rescue => e
|
|
||||||
puts "[#{Time.current}] #{file} 에러: #{e.message}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
@scheduler&.shutdown
|
||||||
|
puts "[#{Time.current}] Modbus polling service 중지됨"
|
||||||
|
end
|
||||||
|
|
||||||
|
def running?
|
||||||
|
@scheduler&.running?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
def apply_schedule(schedule)
|
def apply_schedule(schedule)
|
||||||
if schedule.is_active
|
if schedule.is_active
|
||||||
run_script("on_off.rb", "0", "[Schedule] ON")
|
run_script("on_off.rb", "0", "[Schedule] ON")
|
||||||
run_script(
|
run_script(
|
||||||
"serial.rb",
|
"serial.rb",
|
||||||
(schedule.temperature * 10).to_i.to_s,
|
(schedule.temperature * 10).to_i.to_s,
|
||||||
"[Schedule] #{schedule.hour}:#{schedule.minute} → #{schedule.temperature}°C"
|
"[Schedule] #{format('%02d:%02d', schedule.hour, schedule.minute)} → #{schedule.temperature}°C"
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
run_script("on_off.rb", "1", "[Schedule] OFF")
|
run_script("on_off.rb", "1", "[Schedule] OFF")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue