diff --git a/.idea/farmitry_hvac.iml b/.idea/farmitry_hvac.iml index ef0ceff..16e3543 100644 --- a/.idea/farmitry_hvac.iml +++ b/.idea/farmitry_hvac.iml @@ -131,6 +131,7 @@ + diff --git a/Gemfile b/Gemfile index d2e30ce..c671f92 100644 --- a/Gemfile +++ b/Gemfile @@ -71,3 +71,4 @@ end gem "ccutrer-serialport" gem "rmodbus" +gem "rufus-scheduler" diff --git a/Gemfile.lock b/Gemfile.lock index 8c08f76..61ec0e2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -294,6 +294,8 @@ GEM rubocop-rails (>= 2.30) ruby-progressbar (1.13.0) rubyzip (2.4.1) + rufus-scheduler (3.9.2) + fugit (~> 1.1, >= 1.11.1) securerandom (0.4.1) selenium-webdriver (4.31.0) base64 (~> 0.2) @@ -382,6 +384,7 @@ DEPENDENCIES rails (~> 8.0.2) rmodbus rubocop-rails-omakase + rufus-scheduler selenium-webdriver solid_cable solid_cache diff --git a/app/services/modbus/polling_service.rb b/app/services/modbus/polling_service.rb index 310c513..710705f 100644 --- a/app/services/modbus/polling_service.rb +++ b/app/services/modbus/polling_service.rb @@ -1,56 +1,41 @@ +# polling_service.rb +require "rufus-scheduler" + module Modbus class PollingService class << self def start - return if $modbus_polling_threads.any?(&:alive?) - $modbus_polling_threads << start_thread - end + return if defined?(@scheduler) && @scheduler&.running? - def stop - $modbus_polling_threads.each { |t| t.kill if t.alive? } - $modbus_polling_threads.clear - puts "Modbus polling service 중지됨" - end + puts "[#{Time.current}] Modbus polling service 시작됨 (Rufus)" + @scheduler = Rufus::Scheduler.new - def running? - $modbus_polling_threads.any?(&:alive?) - end - - private - def start_thread - 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 + @scheduler.cron "0 * * * * *" do + now = Time.now + current_time = format("%02d:%02d", now.hour, now.min) + puts "# current time: #{current_time}.#{now.sec}" + schedule = Schedule.find_by(hour: now.hour, minute: now.min) + apply_schedule(schedule) if schedule end end + def stop + @scheduler&.shutdown + puts "[#{Time.current}] Modbus polling service 중지됨" + end + + def running? + @scheduler&.running? + end + + private def apply_schedule(schedule) if schedule.is_active run_script("on_off.rb", "0", "[Schedule] ON") run_script( "serial.rb", (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 run_script("on_off.rb", "1", "[Schedule] OFF")