Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions app/services/print/octoprint_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "faraday"
require "faraday/multipart"

class Print::OctoprintService
# i18n-tasks-use t("print_hosts.protocols.octoprint")
PROTOCOL = "octoprint".freeze

INPUT_TYPES = [Mime[:gcode]].freeze

def initialize(print_host:)
@print_host = print_host
end

def ok?
response = connection.get(info_uri, {}, headers)
response.success?
rescue => ex
Rails.logger.warn(ex.message)
false
end

def upload(file:, start_print: true)
raise ArgumentError unless file.mime_type.to_sym == :gcode
connection.post(upload_uri, payload(file: file, start_print: start_print), headers)
end

private

def connection
Faraday.new do
it.request :multipart
end
end

def payload(file:, start_print: true)
{
print: start_print ? "true" : "false",
file: Faraday::Multipart::FilePart.new(
file.attachment.open,
file.mime_type.to_s,
file.filename
)
}
end

def info_uri
"#{@print_host.endpoint}/api/server"
end

def upload_uri
"#{@print_host.endpoint}/api/files/local"
end

def headers
{
"X-Api-Key" => @print_host.credentials
}.compact_blank
end
end
1 change: 1 addition & 0 deletions config/locales/cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ cs:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ de:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ en:
sent: File sent to printer
protocols:
moonraker: Moonraker / Klipper
octoprint: Octoprint
update:
success: Printer created successfully
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ es:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,7 @@ fr:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ ja:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ nl:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ pl:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ pt:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ ru:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ zh-CN:
sent:
protocols:
moonraker:
octoprint:
update:
success:
renderer:
Expand Down
Loading