-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
174 lines (161 loc) · 6.44 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require 'sinatra'
require 'sinatra-websocket'
require 'json'
require 'erubis'
require 'securerandom'
require 'logger'
logger = Logger.new(STDOUT)
logger.level = Logger::WARN
set :server, 'thin'
set :sockets, []
set :bind, '0.0.0.0'
set :port, 4567
get '/' do
if !request.websocket?
erb :index
else
request.websocket do |ws|
ws.onopen do
settings.sockets << ws
ws.send( JSON.dump( get_state ) )
end
ws.onmessage do |msg|
begin
parsed = JSON.parse( msg )
if parsed["command"] == "open"
EM.next_tick {
settings.sockets.each{|s| s.send( JSON.dump( execute_osa_start_command(parsed) ) ) }
}
elsif parsed["command"] == "next"
EM.next_tick {
settings.sockets.each{|s| s.send( JSON.dump( execute_osa_next_command(parsed) ) ) }
}
elsif parsed["command"] == "prev"
EM.next_tick {
settings.sockets.each{|s| s.send( JSON.dump( execute_osa_prev_command(parsed) ) ) }
}
elsif parsed["command"] == "stop"
EM.next_tick {
settings.sockets.each{|s| s.send( JSON.dump( execute_osa_stop_command(parsed) ) ) }
}
else
puts "WARN: unknown command #{parsed['command']}"
end
rescue Exception => e
puts "Error while handling input: #{e}"
end
end
ws.onclose do
settings.sockets.delete(ws)
end
end
end
end
private
def get_state
keynote_state = get_keynote_state
keynote_state_file = { :name => nil, :slide => nil, :total => nil }
if keynote_state[:cmd_state] == :success
if keynote_state[:state] =~ /ok:.*/
_, file, slide, total = keynote_state[:state].split(":")
keynote_state_file = { :name => file, :slide => slide, :total => total }
logger.info "Keynote state valid: #{keynote_state_file}"
else
logger.info "Keynote state: #{keynote_state[:state]}."
end
else
logger.info "Keynote state failed: #{keynote_state[:error]}."
end
files = Dir["#{File.expand_path(File.dirname(__FILE__))}/presos/*.key"].map {|s|
base_name = File.basename(s)
if base_name == keynote_state_file[:name]
{ :path => s, :name => base_name, :r => true, :slide => keynote_state_file[:slide], :total => keynote_state_file[:total] }
else
{ :path => s, :name => base_name, :r => false }
end
}
logger.info "Returning initial state information."
{ :response => "state", :presos => files }
end
def get_keynote_state
logger.info "Attempting to execute Keynote state command..."
template = Erubis::Eruby.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/commands/keynote_state.applescript.erb"))
template_result = template.result
file_name = "/tmp/#{SecureRandom.uuid().to_s}.applescript"
File.write(file_name, template_result.to_s)
cmdresult = `cat #{file_name} | $(which osascript)`.chomp
File.delete(file_name)
code = $?.exitstatus
if code == 0
logger.info "Keynote state accessed successfully."
{ :cmd_state => :success, :state => cmdresult }
else
logger.error "Could not load Keynote state. Reason: #{cmdresult}, error code: #{code}"
{ :cmd_state => :failed, :error => cmdresult, :code => code }
end
end
def execute_osa_start_command input
logger.info "Attempting to execute presentation start command..."
template = Erubis::Eruby.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/commands/start.applescript.erb"))
template_result = template.result(:file_path => input["path"])
file_name = "/tmp/#{SecureRandom.uuid().to_s}.applescript"
File.write(file_name, template_result.to_s)
cmdresult = `cat #{file_name} | $(which osascript)`.chomp
File.delete(file_name)
code = $?.exitstatus
if code == 0
logger.info "Presentation opened successfully."
{ :response => "opened", :path => input["path"], :name => input["name"], :slides => cmdresult }
else
logger.error "Could not open the presentation. Reason: #{cmdresult}, exit code #{code}"
{ :response => :failed, :command => input["command"], :error => cmdresult, :code => code }
end
end
def execute_osa_next_command input
logger.info "Attempting progressing to the next slide. (current slide: #{input["current"]})"
template = Erubis::Eruby.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/commands/next.applescript.erb"))
template_result = template.result( :current => input["current"] )
file_name = "/tmp/#{SecureRandom.uuid().to_s}.applescript"
File.write(file_name, template_result.to_s)
cmdresult = `cat #{file_name} | $(which osascript)`.chomp
File.delete(file_name)
code = $?.exitstatus
if code == 0
{ :response => "changed", :document => input["document"], :slide => cmdresult }
else
logger.error "Could not progress to the next slide. Reason: #{cmdresult}, exit code #{code}"
{ :response => :failed, :command => input["command"], :error => cmdresult, :code => code }
end
end
def execute_osa_prev_command input
logger.info "Attempting going back to the previous slide (current slide: #{input["current"]})."
template = Erubis::Eruby.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/commands/prev.applescript.erb"))
template_result = template.result( :current => input["current"] )
file_name = "/tmp/#{SecureRandom.uuid().to_s}.applescript"
File.write(file_name, template_result.to_s)
cmdresult = `cat #{file_name} | $(which osascript)`.chomp
File.delete(file_name)
code = $?.exitstatus
if code == 0
{ :response => "changed", :document => input["document"], :slide => cmdresult }
else
logger.error "Could not go back to the previous slide. Reason: #{cmdresult}, exit code #{code}"
{ :response => :failed, :command => input["command"], :error => cmdresult, :code => code }
end
end
def execute_osa_stop_command input
logger.info "Attempting to stop Keynote...."
template = Erubis::Eruby.new(File.read("#{File.expand_path(File.dirname(__FILE__))}/commands/stop.applescript.erb"))
template_result = template.result
file_name = "/tmp/#{SecureRandom.uuid().to_s}.applescript"
File.write(file_name, template_result.to_s)
cmdresult = `cat #{file_name} | $(which osascript)`.chomp
File.delete(file_name)
code = $?.exitstatus
if code == 0
{ :response => "quit" }
else
logger.error "Could not stop Keynote. Reason: #{cmdresult}, exit code #{code}"
{ :response => :failed, :command => input["command"], :error => cmdresult, :code => code }
end
end