Class: PrintNodeServer
Constant Summary
collapse
- CHROME_PATH =
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
- POLL_EVERY =
10
Instance Method Summary
collapse
Methods inherited from ProxyServer
#initialize, #set_mime_types
Constructor Details
This class inherits a constructor from ProxyServer
Instance Method Details
#check_for_requirements ⇒ Object
18
19
20
21
|
# File 'lib/print_node_server.rb', line 18
def check_for_requirements
raise "Could not find Google Chrome. Please make sure Google Chrome is installed." if !File.exists?(CHROME_PATH)
raise "Could not find chromehtml2pdf. Run:\nnpm install -g chromehtml2pdf" if !File.which("chromehtml2pdf")
end
|
#default_printer ⇒ Object
83
84
85
|
# File 'lib/print_node_server.rb', line 83
def default_printer
""
end
|
#loop_action ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/print_node_server.rb', line 61
def loop_action
res = send_post
if match = res.body.match(/<next-job>([^<]*)<\/next-job>/)
url = match[1]
path = "/tmp/VaePrintNode-#{rand.to_s.gsub(".", "")}.pdf"
puts "Printing #{url} to #{path}"
`chromehtml2pdf --marginLeft 0.5 --marginRight 0.5 --out "#{path}" #{Shellwords.shellescape(url)}`
if File.exists?(path)
`lp #{path}`
Thread.new { sleep 15; FileUtils.rm(path) }
else
puts "Error: could not generate PDF!"
end
end
rescue StandardError => e
puts e.message
end
|
#main_loop ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/print_node_server.rb', line 53
def main_loop
loop do
break if @stop or (@last_successful_post_at + 3600) < Time.now
loop_action if (@last_post_at + POLL_EVERY) < Time.now
sleep 1
end
end
|
#node_name ⇒ Object
79
80
81
|
# File 'lib/print_node_server.rb', line 79
def node_name
Socket.gethostname
end
|
#print_queue_status ⇒ Object
91
92
93
|
# File 'lib/print_node_server.rb', line 91
def print_queue_status
`lpq`
end
|
#printers_list ⇒ Object
87
88
89
|
# File 'lib/print_node_server.rb', line 87
def printers_list
`lpstat -p`
end
|
#register_node ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/print_node_server.rb', line 23
def register_node
res = send_post
if match = res.body.match(/<secret-key>([a-f0-9]*)<\/secret-key>/)
@node_id = "/#{match[1]}"
else
raise "Could not connect to Vae to register print node."
end
end
|
#run ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/print_node_server.rb', line 5
def run
check_for_requirements
register_node
puts "Vae Print Node active, waiting for print jobs ..."
puts " (hit Control+C to exit)"
trap("INT") { @stop = true }
main_loop
puts "Quit signal received, cleaning up ..."
end
|
#send_post ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/print_node_server.rb', line 32
def send_post
@last_post_at = Time.now
options = {
method: :post,
headers: { "Content-type": "application/json" },
url: "#{@site.vaeplatform_url}/api/local/v1/print_nodes#{@node_id}",
payload: {
print_node: {
name: node_name,
default_printer: default_printer,
printers_list: printers_list,
print_queue_status: print_queue_status
}
}.to_json
}
options[:verify_ssl] = false if ENV['VAEPLATFORM_LOCAL']
res = RestClient::Request.execute(options)
@last_successful_post_at = Time.now
res
end
|