diff --git a/app/controllers/modbus_controller.rb b/app/controllers/modbus_controller.rb
index 559e264..380dcda 100644
--- a/app/controllers/modbus_controller.rb
+++ b/app/controllers/modbus_controller.rb
@@ -1,6 +1,17 @@
class ModbusController < ApplicationController
def index
@schedule = Schedule.all
+ @modbus_running = Modbus::PollingService.running?
+ end
+
+ def start
+ Modbus::PollingService.start
+ redirect_to modbus_index_path
+ end
+
+ def stop
+ Modbus::PollingService.stop
+ redirect_to modbus_index_path
end
def schedule_edit
diff --git a/app/services/modbus/polling_service.rb b/app/services/modbus/polling_service.rb
index b0f9dae..bc38eb2 100644
--- a/app/services/modbus/polling_service.rb
+++ b/app/services/modbus/polling_service.rb
@@ -2,9 +2,9 @@ module Modbus
class PollingService
class << self
def start
- return if @thread&.alive?
+ return if $modbus_polling_threads.any?(&:alive?)
- @thread = Thread.new do
+ thread = Thread.new do
puts "[#{Time.current}] Modbus polling service 시작됨"
last_logged_hour = nil
loop do
@@ -32,14 +32,17 @@ module Modbus
end
end
end
+ $modbus_polling_threads << thread
end
def stop
- if @thread
- @thread.exit
- @thread = nil
- puts "[#{Time.now}] Modbus polling service 중지됨"
- end
+ $modbus_polling_threads.each { |t| t.kill if t.alive? }
+ $modbus_polling_threads.clear
+ puts "[#{Time.now}] Modbus polling service 중지됨"
+ end
+
+ def running?
+ $modbus_polling_threads.any?(&:alive?)
end
end
end
diff --git a/app/views/modbus/index.html.erb b/app/views/modbus/index.html.erb
index c4abde2..ae77f45 100644
--- a/app/views/modbus/index.html.erb
+++ b/app/views/modbus/index.html.erb
@@ -8,26 +8,11 @@
<% end %>
-
-
-
-
-
- | 시간 |
- 온도 |
-
-
-
- <% @schedule.each do |s| %>
-
- | <%= s.hour %>시 |
- <%= s.temperature %> °C |
-
- <% end %>
-
-
+<% @schedule.each do |s| %>
+
+
<%= s.hour %>시
+
<%= s.temperature %> °C
-
- <%= link_to "수정", "/modbus/schedule_edit", class: "btn bg-default-slate" %>
-
-
\ No newline at end of file
+<% end %>
+
+<%= link_to "수정", "/modbus/schedule_edit", class: "btn btn-primary" %>
diff --git a/app/views/modbus/schedule_edit.html.erb b/app/views/modbus/schedule_edit.html.erb
index e148df1..c986172 100644
--- a/app/views/modbus/schedule_edit.html.erb
+++ b/app/views/modbus/schedule_edit.html.erb
@@ -1,24 +1,14 @@
-<%= form_with url: "/modbus/schedule_edit_update", method: :post, class: 'flex flex-col h-full divide-y divide-border-table-border' do %>
-
-
-
-
- | 시간 |
- 온도 |
-
-
-
- <% @schedule.each do |s| %>
-
- | <%= s.hour %>시 |
- <%= number_field_tag "schedule[#{s.id}][temperature]", s.temperature, step: "0.1", inputmode: "decimal", class: "border px-2 py-1 rounded" %> °C |
-
- <% end %>
-
-
-
+<%= form_with url: "/modbus/schedule_edit_update", method: :post do %>
+ <% @schedule.each do |s| %>
+
+
<%= s.hour %>시
+
+ <%= number_field_tag "schedule[#{s.id}][temperature]", s.temperature, step: "0.1", inputmode: "decimal", class: "border px-2 py-1 rounded" %> °C
+
+
+ <% end %>
-
- <%= submit_tag "업데이트", class: "btn bg-primary" %>
+
+ <%= submit_tag "업데이트", class: "bg-blue-500 text-white px-4 py-2 rounded" %>
-<% end %>
\ No newline at end of file
+<% end %>
diff --git a/config/initializers/modbus_service.rb b/config/initializers/modbus_service.rb
index 490c40b..c1c0645 100644
--- a/config/initializers/modbus_service.rb
+++ b/config/initializers/modbus_service.rb
@@ -1,3 +1,5 @@
+$modbus_polling_threads ||= []
+
Rails.application.config.after_initialize do
Modbus::PollingService.start
end
diff --git a/config/routes.rb b/config/routes.rb
index a64c5f0..788f395 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -14,9 +14,8 @@ Rails.application.routes.draw do
resources :modbus do
collection do
- get "start"
- get "stop"
- get "status"
+ post "start"
+ post "stop"
get "schedule_edit"
post "schedule_edit_update"
end
diff --git a/db/seeds.rb b/db/seeds.rb
index 64f6801..b19f220 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -8,6 +8,6 @@
# MovieGenre.find_or_create_by!(name: genre_name)
# end
-(0..23).each_with_index do |h, index|
- Schedule.create!(hour: h, temperature: index)
+(0..23).each do | h |
+ Schedule.create!(hour: h, temperature: 15.0)
end