Skip to content

Commit f3262d1

Browse files
committed
Add print via OpenResinAlliance Odyssey API
1 parent a194577 commit f3262d1

15 files changed

Lines changed: 262 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require "faraday"
2+
require "faraday/multipart"
3+
4+
class Print::OdysseyService
5+
# i18n-tasks-use t("print_hosts.protocols.odyssey")
6+
PROTOCOL = "odyssey".freeze
7+
8+
INPUT_TYPES = [Mime[:sl1]].freeze
9+
10+
def initialize(print_host:)
11+
@print_host = print_host
12+
end
13+
14+
def ok?
15+
response = connection.get(info_uri, {}, {})
16+
Rails.logger.warn(response.inspect) unless response.success?
17+
response.success?
18+
rescue => ex
19+
Rails.logger.warn(ex.message)
20+
false
21+
end
22+
23+
def upload(file:, start_print: true)
24+
raise ArgumentError unless file.mime_type.to_sym == :sl1
25+
raise PrintHost::NotReady unless ok?
26+
response = connection.post(
27+
upload_uri,
28+
payload(file: file)
29+
)
30+
Rails.logger.warn(response.inspect) unless response.success?
31+
uploaded = response.success?
32+
print(file: file) if start_print
33+
uploaded
34+
end
35+
36+
def print(file:)
37+
response = Faraday.new.post("#{@print_host.endpoint}/print/start?filename=#{CGI.escapeURIComponent(file.filename)}")
38+
Rails.logger.warn(response.inspect) unless response.success?
39+
end
40+
41+
private
42+
43+
def connection
44+
Faraday.new do
45+
it.request :multipart
46+
end
47+
end
48+
49+
def payload(file:)
50+
{
51+
file: Faraday::Multipart::FilePart.new(
52+
file.attachment.open,
53+
file.mime_type.to_s,
54+
file.filename
55+
)
56+
}
57+
end
58+
59+
def upload_uri
60+
"#{@print_host.endpoint}/files"
61+
end
62+
63+
def info_uri
64+
"#{@print_host.endpoint}/status"
65+
end
66+
end

config/locales/cs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ cs:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/de.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ de:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ en:
10021002
protocols:
10031003
moonraker: Moonraker / Klipper
10041004
octoprint: Octoprint
1005+
odyssey: Odyssey
10051006
update:
10061007
success: Printer created successfully
10071008
renderer:

config/locales/es.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ es:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/fr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ fr:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/ja.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ ja:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/nl.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ nl:
1111
one: '1 fout verhinderde dat deze %{resource} werd opgeslagen:'
1212
other: "%{count} fouten verhinderden dat deze %{resource} werd opgeslagen:"
1313
weak_password: niet sterk genoeg. Overweeg een cijfer, symbolen of meer letters toe te voegen om het sterker te maken.
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/pl.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pl:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

config/locales/pt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pt:
1111
one:
1212
other:
1313
weak_password:
14+
print_hosts:
15+
protocols:
16+
odyssey:
1417
views:
1518
print_hosts:
1619
form:

0 commit comments

Comments
 (0)