This repository has been archived by the owner on Aug 21, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
229b455
commit 2669e32
Showing
2 changed files
with
86 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,110 @@ | ||
require 'faye/websocket' | ||
require 'eventmachine' | ||
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets' | ||
require '/Users/tyoung/workspace/Pegacorn_Project/.gitignore/pegacorn_secrets.rb' | ||
require 'json' | ||
require 'pi_piper' | ||
require 'date' | ||
|
||
EM.run do | ||
wss = Faye::WebSocket::Client.new('wss://rtm.zopim.com/stream', | ||
nil, | ||
:headers => | ||
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZENDESK_OAUTH_ACCESS_TOKEN}" }) | ||
# class WatchChatWaits | ||
def create_new_request | ||
Faye::WebSocket::Client.new(ZendeskSecrets::ZOPIM_STREAM_ENDPOINT, | ||
nil, | ||
:headers => | ||
authorization_credentials) | ||
end | ||
|
||
def authorization_credentials | ||
{ 'Authorization' => "Bearer #{ZendeskSecrets::ZENDESK_OAUTH_ACCESS_TOKEN}" } | ||
end | ||
|
||
wss.on :open do | ||
def subscribe_to_chat_waits(wss) | ||
p [:start] | ||
msg = {} | ||
msg[:topic] = 'chats.waiting_time_avg' | ||
msg[:action] = 'subscribe' | ||
wss.send msg.to_json | ||
end | ||
|
||
wss.on :message do |event| | ||
h = JSON.parse(event.data).to_hash | ||
wait_time_avg = [h.dig('content', 'data', 'waiting_time_avg')][0] | ||
tries ||= 3 | ||
if wait_time_avg.to_i.zero? && !(tries -= 1).zero? | ||
tries -= 1 | ||
sleep 15 | ||
redo | ||
elsif !wait_time_avg.to_i.zero? && | ||
wait_time_avg.to_i < 45 | ||
def wait_time_avg_goal_reached? (wait_time_avg) | ||
if wait_time_avg < 45 | ||
return true | ||
end | ||
end | ||
|
||
def log_success_light_rasp_pi | ||
puts "It's currently #{DateTime.now}." | ||
puts "Chat wait time average is #{wait_time_avg} seconds" | ||
puts "\nLight the pegacorn!\n" | ||
pin = PiPiper::Pin.new( :pin => 17, :direction => :out ) | ||
pin.off | ||
1.times do | ||
pin.on | ||
sleep 15 #seconds | ||
pin.off | ||
end | ||
else | ||
pin.on | ||
sleep 15 # seconds | ||
pin.off | ||
end | ||
|
||
def log_attempt_unsuccessful | ||
puts "It's currently #{DateTime.now}." | ||
puts 'Chat wait time average is not available.' | ||
puts "\nPegacorn time has not yet come.\n" | ||
end | ||
wss = true | ||
EM.stop | ||
|
||
# ** REDO doesn't work in either of the methods below ** # | ||
|
||
# def retry_3x(tries) | ||
# tries -= 1 | ||
# sleep 15 | ||
# redo | ||
# end | ||
|
||
# def manage_wait_time | ||
# wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i | ||
# tries ||= 3 | ||
# if wait_time_avg.zero? && !(tries -= 1).zero? | ||
# tries -= 1 | ||
# sleep 15 | ||
# redo | ||
# # retry_3x | ||
# elsif wait_time_avg > 0 && wait_time_avg < 45 | ||
# log_success_light_rasp_pi | ||
# else | ||
# log_attempt_unsuccessful | ||
# end | ||
# end | ||
|
||
|
||
EM.run do | ||
wss = create_new_request | ||
wss.on :open do | ||
subscribe_to_chat_waits(wss) | ||
end | ||
|
||
wss.on :message do |event| | ||
h = JSON.parse(event.data).to_hash | ||
# manage_wait_time | ||
wait_time_avg = ([h.dig('content', 'data', 'waiting_time_avg')][0]).to_i | ||
tries ||= 3 | ||
if wait_time_avg.zero? && !(tries -= 1).zero? | ||
tries -= 1 | ||
sleep 15 | ||
redo | ||
# retry_3x | ||
elsif wait_time_avg > 0 && wait_time_avg < 45 | ||
log_success_light_rasp_pi | ||
else | ||
log_attempt_unsuccessful | ||
end | ||
|
||
wss = true | ||
EM.stop | ||
end | ||
|
||
wss.on :close do |event| | ||
puts "Something's gronked up." if event.code != 1006 | ||
p ["Closing", "Event code: #{event.code}", "Event reason: #{event.reason}"] | ||
EM.stop | ||
end | ||
end | ||
|
||
wss.on :close do |event| | ||
puts "Something's gronked up." if event.code != 1006 | ||
p [:close, event.code, event.reason] | ||
EM.stop | ||
def pin | ||
PiPiper::Pin.new( :pin => 17, :direction => :out ) | ||
end | ||
end | ||
# end |