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-10-10 12:11:16 +02:00
|
|
|
local printerUtils = require('util.printer')
|
|
|
|
local accessManager = require('util.access')
|
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
|
|
|
|
|
|
|
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
|
|
|
--requires id(string)
|
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")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-17 00:14:42 +02:00
|
|
|
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
|
|
|
--requires id(string)
|
2013-08-09 22:15:58 +02:00
|
|
|
function M.progress(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
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-09-20 11:09:25 +02:00
|
|
|
--TODO: remove busy function (client should use state function)
|
|
|
|
--requires id(string)
|
2013-07-26 10:18:55 +02:00
|
|
|
function M.busy(request, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
if not printer then return end
|
|
|
|
|
|
|
|
local rv,msg = printer:getState()
|
|
|
|
|
|
|
|
response:addData('id', argId)
|
|
|
|
if rv then
|
|
|
|
response:setSuccess()
|
|
|
|
response:addData('busy', (rv ~= 'idle'))
|
|
|
|
else
|
|
|
|
response:setError(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--requires id(string)
|
|
|
|
function M.state(request, response)
|
|
|
|
local argId = request:get("id")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
if not printer then return end
|
|
|
|
|
|
|
|
local rv,msg = printer:getState()
|
|
|
|
|
|
|
|
response:addData('id', argId)
|
|
|
|
if rv then
|
|
|
|
response:setSuccess()
|
|
|
|
response:addData('state', rv)
|
|
|
|
else
|
|
|
|
response:setError(msg)
|
|
|
|
end
|
2013-07-26 10:18:55 +02:00
|
|
|
end
|
|
|
|
|
2013-10-10 12:11:16 +02:00
|
|
|
|
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
--requires id(string)
|
2013-07-26 10:18:55 +02:00
|
|
|
function M.heatup_POST(request, response)
|
2013-10-10 12:11:16 +02:00
|
|
|
|
|
|
|
if not accessManager.hasControl(request.remoteAddress) then
|
|
|
|
response:setFail("No control access")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
if not printer then return end
|
2013-09-17 00:14:42 +02:00
|
|
|
|
2013-09-27 18:25:16 +02:00
|
|
|
local temperature = settings.get('printer.heatup.temperature')
|
2013-09-20 11:09:25 +02:00
|
|
|
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-09-20 11:09:25 +02:00
|
|
|
--requires id(string)
|
2013-08-04 11:26:47 +02:00
|
|
|
function M.stop_POST(request, response)
|
2013-10-10 12:11:16 +02:00
|
|
|
|
|
|
|
if not accessManager.hasControl(request.remoteAddress) then
|
|
|
|
response:setFail("No control access")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-09-20 11:09:25 +02:00
|
|
|
local argId = request:get("id")
|
2013-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
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-09-20 11:09:25 +02:00
|
|
|
--requires id(string), gcode(string)
|
2013-07-26 10:18:55 +02:00
|
|
|
--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-10-10 12:11:16 +02:00
|
|
|
|
|
|
|
local controllerIP = accessManager.getController()
|
|
|
|
local hasControl = false
|
|
|
|
if controllerIP == "" then
|
|
|
|
accessManager.setController(request.remoteAddress)
|
|
|
|
hasControl = true
|
|
|
|
elseif controllerIP == request.remoteAddress then
|
|
|
|
hasControl = true
|
|
|
|
end
|
|
|
|
|
|
|
|
log:info(" hasControl: "..utils.dump(hasControl))
|
|
|
|
if not hasControl then
|
|
|
|
response:setFail("No control access")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
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-10-10 12:11:16 +02:00
|
|
|
local printer,msg = printerUtils.createPrinterOrFail(argId, response)
|
2013-09-20 11:09:25 +02:00
|
|
|
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-27 18:25:16 +02:00
|
|
|
log:debug("clearing all gcode for " .. printer:getId())
|
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
|