Commit b96e529a authored by Tonee Young's avatar Tonee Young
Browse files

call block as public method and move retry logic to private method

parent 1ce8d163
......@@ -18,8 +18,8 @@ class WatchChatWaits
def subscribe_to_chat_waits(websocket_client)
websocket_client.on :open do
p [:start]
puts "Starting..." #[:start]
# p [:start]
puts "Starting..."
msg = {}
msg[:topic] = 'chats.waiting_time_avg'
msg[:action] = 'subscribe'
......@@ -27,38 +27,13 @@ class WatchChatWaits
end
end
def check_for_success_3x_on_message(websocket_client)
websocket_client.on :message do |event|
h = JSON.parse(event.data).to_hash
wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i
tries ||= 3
if wait_time_avg.zero? && tries_remain?(tries)
tries -= 1
sleep 15
redo
elsif wait_time_avg > 0 && wait_time_avg < 45
log_success_light_rasp_pi
else
log_attempt_unsuccessful
end
wss = true
EM.stop
end
def check_for_wait_time_on_message(websocket_client)
websocket_client.on :message do |event|
hashed_json_message = JSON.parse(event.data).to_hash
wait_time_avg = dig_for_wait_time_avg(hashed_json_message)
try_then_log_result(wait_time_avg, NUMBER_OF_TRIES)
end
# def try_then_log_result(websocket_client, number_tries)
# wait_time_avg = check_for_wait_time_on_message(websocket_client)
# if !wait_time_avg.nil?
# manage_rasp_pi(wait_time_avg)
# elsif wait_time_avg.nil? && number_tries > 0
# number_tries -= 1
# sleep 5
# try_then_log_result(websocket_client, number_tries)
# else log_attempt_unsuccessful
# end
# websocket_client = true
# EM.stop
# end
end
def log_status_on_close(websocket_client)
websocket_client.on :close do |event|
......@@ -69,7 +44,20 @@ class WatchChatWaits
end
private
def manage_rasp_pi(wait_time_avg)
def try_then_log_result(wait_time_avg, number_tries)
if !wait_time_avg.nil?
manage_rasp_pi(wait_time_avg)
elsif wait_time_avg.nil? && number_tries > 0
number_tries -= 1
sleep 5
try_then_log_result(websocket_client, number_tries)
else log_attempt_unsuccessful
end
websocket_client = true
EM.stop
end
def manage_rasp_pi(wait_time_avg)
if wait_time_avg_goal_reached?(wait_time_avg)
log_success_light_rasp_pi(wait_time_avg)
else
......@@ -84,13 +72,6 @@ class WatchChatWaits
light_it_up
end
# def check_for_wait_time_on_message(websocket_client)
# websocket_client.on :message do |event|
# hashed_json_message = JSON.parse(event.data).to_hash
# wait_time_avg = dig_for_wait_time_avg(hashed_json_message)
# return wait_time_average.to_i
# end
# end
def dig_for_wait_time_avg(hashed_json_message)
[hashed_json_message.dig('content', 'data', 'waiting_time_avg')][0].to_i
......@@ -129,7 +110,7 @@ end
EM.run do
new_client = wait_watcher.create_new_request
wait_watcher.subscribe_to_chat_waits(new_client)
wait_watcher.check_for_success_3x_on_message(new_client)
# wait_watcher.try_then_log_result(new_client, WatchChatWaits::NUMBER_OF_TRIES)
# wait_watcher.check_for_success_3x_on_message(new_client)
wait_watcher.check_for_wait_time_on_message(new_client)
wait_watcher.log_status_on_close(new_client)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment