2013-08-07 17:50:30 +02:00
|
|
|
local lfs = require('lfs')
|
2013-07-26 10:18:55 +02:00
|
|
|
local log = require('util.logger')
|
|
|
|
local utils = require('util.utils')
|
|
|
|
local settings = require('util.settings')
|
2013-09-17 00:14:42 +02:00
|
|
|
local printDriver = require('print3d')
|
2013-07-26 10:18:55 +02:00
|
|
|
|
|
|
|
local M = {
|
|
|
|
isApi = true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
local ULTIFI_BASE_PATH = '/tmp/UltiFi'
|
|
|
|
local TEMPERATURE_FILE = 'temp.out'
|
2013-08-09 22:15:58 +02:00
|
|
|
local PROGRESS_FILE = 'progress2.out'
|
2013-07-26 10:18:55 +02:00
|
|
|
local COMMAND_FILE = 'command.in'
|
2013-08-12 17:36:07 +02:00
|
|
|
local GCODE_TMP_FILE = 'combined.gc'
|
2013-07-26 10:18:55 +02:00
|
|
|
|
2013-09-17 00:14:42 +02:00
|
|
|
|
|
|
|
--returns a printer instance or nil (and sets error state on response in the latter case)
|
|
|
|
local function createPrinterOrFail(deviceId, response)
|
|
|
|
local msg,printer = nil, nil
|
|
|
|
|
|
|
|
if deviceId and deviceId ~= "" then
|
|
|
|
printer,msg = printDriver.getPrinter(deviceId)
|
|
|
|
else
|
|
|
|
msg = "missing device ID"
|
|
|
|
end
|
|
|
|
|
|
|
|
if not printer then
|
|
|
|
response:setError("could not open printer driver (" .. msg .. ")")
|
|
|
|
response:addData('id', deviceId)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return printer
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
local function isBusy(printerPath)
|
|
|
|
local cmdPath = printerPath .. '/' .. COMMAND_FILE
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if not utils.exists(cmdPath) then return false end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
local f,msg = io.open(cmdPath, 'r')
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if not f then return nil,msg end
|
|
|
|
local size = utils.fileSize(f)
|
|
|
|
f:close()
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-08-16 19:26:23 +02:00
|
|
|
return size > 0
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function M._global(request, response)
|
|
|
|
-- TODO: list all printers (based on /dev/ttyACM* and /dev/ttyUSB*)
|
|
|
|
response:setSuccess()
|
|
|
|
end
|
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
-- TODO: reimplement the following fields
|
|
|
|
-- if withRaw then response:addData('raw', tempText) end
|
|
|
|
-- response:addData('last_mod', printer:getLastTemperatureUpdate())
|
2013-07-26 10:18:55 +02:00
|
|
|
--requires id(int)
|
2013-09-17 00:14:42 +02:00
|
|
|
--accepts with_raw(bool) to include raw printer response (currently not implemented)
|
2013-07-26 10:18:55 +02:00
|
|
|
function M.temperature(request, response)
|
2013-09-17 00:14:42 +02:00
|
|
|
local argId = request:get("id")
|
|
|
|
local printer,msg = createPrinterOrFail(argId, response)
|
|
|
|
if not printer then return end
|
2013-08-07 17:50:30 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local temperatures,msg = printer:getTemperatures()
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
response:addData('id', argId)
|
|
|
|
if temperatures then
|
|
|
|
response:setSuccess()
|
|
|
|
response:addData('hotend', temperatures.hotend)
|
|
|
|
response:addData('hotend_target', temperatures.hotend_target)
|
|
|
|
response:addData('bed', temperatures.bed)
|
|
|
|
response:addData('bed_target', temperatures.bed_target)
|
|
|
|
else
|
|
|
|
response:setError(msg)
|
|
|
|
end
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
-- TODO: reimplement last_mod field
|
2013-08-09 22:15:58 +02:00
|
|
|
--requires id(int)
|
|
|
|
function M.progress(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
|
|
|
local printer,msg = createPrinterOrFail(argId, response)
|
|
|
|
if not printer then return end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
-- NOTE: despite their names, `currentLine` is still the error indicator and `numLines` the message in such case.
|
|
|
|
local currentLine,numLines = printer:getProgress()
|
2013-08-09 22:15:58 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
response:addData('id', argId)
|
|
|
|
if currentLine then
|
|
|
|
response:setSuccess()
|
2013-08-09 22:15:58 +02:00
|
|
|
response:addData('current_line', currentLine)
|
|
|
|
response:addData('num_lines', numLines)
|
2013-09-20 11:09:25 +02:00
|
|
|
else
|
|
|
|
response:setError(numLines)
|
2013-08-09 22:15:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
--requires id(int)
|
|
|
|
function M.busy(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
response:setError("not implemented") -- TODO: reimplement
|
|
|
|
return
|
2013-07-26 10:18:55 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
-- local b,msg = isBusy(ultipath)
|
|
|
|
--
|
|
|
|
-- if b == nil then
|
|
|
|
-- response:setError("could not determine printer state")
|
|
|
|
-- response:addData('msg', msg)
|
|
|
|
-- else
|
|
|
|
-- response:setSuccess()
|
|
|
|
-- response:addData('busy', b)
|
|
|
|
-- end
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--requires id(int)
|
|
|
|
function M.heatup_POST(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
|
|
|
local printer,msg = createPrinterOrFail(argId, response)
|
|
|
|
if not printer then return end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local temperature = settings.get('printer.heatupTemperature')
|
|
|
|
local rv,msg = printer:heatup(temperature)
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
response:addData('id', argId)
|
|
|
|
if rv then response:setSuccess()
|
|
|
|
else response:setFail(msg)
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-04 11:26:47 +02:00
|
|
|
--requires id(int)
|
|
|
|
function M.stop_POST(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
|
|
|
local printer,msg = createPrinterOrFail(argId, response)
|
|
|
|
if not printer then return end
|
2013-08-04 11:26:47 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local endGcode = settings.get('printer.endgcode')
|
|
|
|
local rv,msg = printer:stopPrint(endGcode)
|
2013-08-04 11:26:47 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
response:addData('id', argId)
|
|
|
|
if rv then response:setSuccess()
|
|
|
|
else response:setError(msg)
|
2013-08-04 11:26:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
--requires id(int), gcode(string)
|
|
|
|
--accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true)
|
|
|
|
--accepts: last(bool) (chunks will be concatenated and only when this argument is true will printing be started)
|
|
|
|
function M.print_POST(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
2013-07-26 10:18:55 +02:00
|
|
|
local argGcode = request:get("gcode")
|
|
|
|
local argIsFirst = utils.toboolean(request:get("first"))
|
|
|
|
local argIsLast = utils.toboolean(request:get("last"))
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local printer,msg = createPrinterOrFail(argId, response)
|
|
|
|
if not printer then return end
|
|
|
|
|
|
|
|
response:addData('id', argId)
|
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if argGcode == nil or argGcode == '' then
|
|
|
|
response:setError("missing gcode argument")
|
|
|
|
return
|
|
|
|
end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if argIsFirst == true then
|
2013-09-20 11:09:25 +02:00
|
|
|
log:debug("clearing all gcode for " .. printer)
|
2013-07-26 10:18:55 +02:00
|
|
|
response:addData('gcode_clear',true)
|
2013-09-20 11:09:25 +02:00
|
|
|
local rv,msg = printer:clearGcode()
|
|
|
|
|
|
|
|
if not rv then
|
|
|
|
response:setError(msg)
|
|
|
|
return
|
|
|
|
end
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
local rv,msg
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
-- TODO: return errors with a separate argument like here in the rest of the code (this is how we designed the API right?)
|
|
|
|
rv,msg = printer:appendGcode(argGcode)
|
|
|
|
if rv then
|
|
|
|
--NOTE: this does not report the number of lines, but only the block which has just been added
|
|
|
|
response:addData('gcode_append',argGcode:len())
|
|
|
|
else
|
2013-07-26 10:18:55 +02:00
|
|
|
response:setError("could not add gcode")
|
|
|
|
response:addData('msg', msg)
|
|
|
|
return
|
|
|
|
end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if argIsLast == true then
|
2013-09-20 11:09:25 +02:00
|
|
|
rv,msg = printer:startPrint()
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-07-26 10:18:55 +02:00
|
|
|
if rv then
|
|
|
|
response:setSuccess()
|
|
|
|
response:addData('gcode_print',true)
|
|
|
|
else
|
|
|
|
response:setError("could not send gcode")
|
|
|
|
response:addData('msg', msg)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
response:setSuccess()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|