diff --git a/Gemfile b/Gemfile index d42da647..23f3c908 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,10 @@ gem 'rails', '~> 5.1.0' # Use mysql as the database for Active Record gem 'mysql2', '>= 0.3.18', '< 0.5' #gem 'pg' + +# redis server for cable +gem 'redis', '~> 3.0' + # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index 6916f408..46cdc6ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,9 +48,9 @@ GEM sass (>= 3.4.19) builder (3.2.3) byebug (9.0.6) - coffee-rails (4.2.1) + coffee-rails (4.2.2) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.2.x) + railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs @@ -97,7 +97,7 @@ GEM minitest (5.10.2) multi_json (1.12.1) mysql2 (0.4.6) - nio4r (2.0.0) + nio4r (2.1.0) nokogiri (1.7.2) mini_portile2 (~> 2.1.0) pdf-core (0.7.0) @@ -238,6 +238,7 @@ DEPENDENCIES puma (~> 3.0) rack-cors rails (~> 5.1.0) + redis (~> 3.0) rspec-rails (~> 3.5) sass-rails (~> 5.0) schema_to_scaffold @@ -255,4 +256,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.14.6 + 1.15.0 diff --git a/app/assets/javascripts/cable.js b/app/assets/javascripts/cable.js index 71ee1e66..739aa5f0 100644 --- a/app/assets/javascripts/cable.js +++ b/app/assets/javascripts/cable.js @@ -1,5 +1,5 @@ // Action Cable provides the framework to deal with WebSockets in Rails. -// You can generate new channels where WebSocket features live using the rails generate channel command. +// You can generate new channels where WebSocket features live using the `rails generate channel` command. // //= require action_cable //= require_self diff --git a/app/assets/javascripts/channels/order_queue_station.js b/app/assets/javascripts/channels/order_queue_station.js new file mode 100644 index 00000000..00151e5e --- /dev/null +++ b/app/assets/javascripts/channels/order_queue_station.js @@ -0,0 +1,24 @@ +App.order_queue_station = App.cable.subscriptions.create("OrderQueueStationChannel", { + connected: function() {}, + + disconnected: function() {}, + + received: function(message) { + alert(message); + }, + + order: function(message) { + return this.perform('order', { + message: message + }); + } +}); + +// $(function(){ +// $("#submit_order").on('click', function(event) { +// var orderData=$("#new_order").serializeObject(); +// App.order_station.order(orderData); +// //orderData=''; +// return event.preventDefault(); +// }); +// }); diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb index d6726972..a66bc071 100644 --- a/app/channels/application_cable/channel.rb +++ b/app/channels/application_cable/channel.rb @@ -1,4 +1,14 @@ module ApplicationCable - class Channel < ActionCable::Channel::Base - end + class Channel < ActionCable::Channel::Base + end + # Order Queue Station Channel + class OrderChannel < ActionCable::Channel::Base + + end + + # Order Queue Station Channel + class OQSChannel < ActionCable::Channel::Base + + end + end diff --git a/app/channels/order_queue_station_channel.rb b/app/channels/order_queue_station_channel.rb new file mode 100644 index 00000000..8206c64b --- /dev/null +++ b/app/channels/order_queue_station_channel.rb @@ -0,0 +1,13 @@ +class OrderQueueStationChannel < ApplicationCable::Channel + def subscribed + stream_from "order_queue_station_channel" + end + + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end + + def order(message) + # ToDo + end +end diff --git a/app/jobs/order_broadcast_job.rb b/app/jobs/order_broadcast_job.rb new file mode 100644 index 00000000..b29f4cd8 --- /dev/null +++ b/app/jobs/order_broadcast_job.rb @@ -0,0 +1,8 @@ +class OrderBroadcastJob < ApplicationJob + queue_as :default + + def perform(message) + order = Order.find(message) # message come as order_id + ApplicationCable.server.broadcast "order_queue_station_channel", order: order + end +end diff --git a/app/jobs/order_queue_processor_job.rb b/app/jobs/order_queue_processor_job.rb index 33380674..e9a8c13f 100644 --- a/app/jobs/order_queue_processor_job.rb +++ b/app/jobs/order_queue_processor_job.rb @@ -3,8 +3,9 @@ class OrderQueueProcessorJob < ApplicationJob def perform(order_id) # Do something later - #Order ID + #Order ID order = Order.find(order_id) + #Loop through the order stations and process the items #Execute orders and send to order stations if order diff --git a/app/models/order.rb b/app/models/order.rb index 0bec3a1e..7061fdb7 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -8,7 +8,6 @@ class Order < ApplicationRecord #internal references attributes for business logic control attr_accessor :items, :guest, :table_id, :new_booking, :booking_type, :employee_name, :booking_id - #Main Controller method to create new order - validate all inputs and generate new order # order_item : { # order_item_code : "", @@ -43,6 +42,9 @@ class Order < ApplicationRecord #Send order to queue one it done! process_order_queue + #send order to broadcast job + send_order_broadcast + return true, booking end @@ -198,7 +200,13 @@ class Order < ApplicationRecord #Process order items and send to order queue def process_order_queue - #Send to background job for processing + #Send to background job for processing OrderQueueProcessorJob.perform_later(self.id) end + + #send order items and send to order queue + def send_order_broadcast + #Send to background job for processing + OrderBroadcastJob.perform_later(self.id) + end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f719704..f5ab7304 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -51,4 +51,7 @@ Rails.application.configure do # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + # Set Cable URL + config.action_cable.url = "ws://192.168.1.140:3002/cable" end diff --git a/config/routes.rb b/config/routes.rb index ed2cdab0..138137dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,9 @@ Rails.application.routes.draw do root 'home#index' mount Sidekiq::Web => '/kiq' + # Action Cable Creation + mount ActionCable.server => "/cable" + #--------- SmartSales Installation ------------# get 'install' => 'install#index' post 'install' => 'install#create' diff --git a/spec/jobs/order_broadcast_job_spec.rb b/spec/jobs/order_broadcast_job_spec.rb new file mode 100644 index 00000000..74f8f77c --- /dev/null +++ b/spec/jobs/order_broadcast_job_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe OrderBroadcastJob, type: :job do + pending "add some examples to (or delete) #{__FILE__}" +end