0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 19:13:49 +01:00

Fix return values in printer api.

Pass along chunk metadata in printer api.
Move wifi station mode check from main.lua to signin.lua so it is always run past.
Only log postresponsequeue if it is not empty.

Conflicts:
	src/rest/api/api_printer.lua
This commit is contained in:
Wouter R 2014-05-19 21:37:17 +02:00
parent 9fd96740a3
commit c3756f5348
6 changed files with 70 additions and 41 deletions

View File

@ -254,9 +254,6 @@ local function main(environment)
elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then
log:info(MOD_ABBR, "Running in signin mode") log:info(MOD_ABBR, "Running in signin mode")
local ds = wifi.getDeviceState()
log:info(MOD_ABBR, " wifi deviceState.mode: "..util.dump(ds.mode))
if ds.mode == "sta" then
log:info(MOD_ABBR, " attempting signin") log:info(MOD_ABBR, " attempting signin")
local success,msg = Signin.signin() local success,msg = Signin.signin()
if success then if success then
@ -264,7 +261,6 @@ local function main(environment)
else else
log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg)) log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg))
end end
end
elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then
-- Note: the commented log statement will print too many data if it's for instance dumping a gcode add request -- Note: the commented log statement will print too many data if it's for instance dumping a gcode add request
--log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>") --log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "<unknown>")
@ -280,7 +276,6 @@ local function main(environment)
if err ~= nil then log:error(MOD_ABBR, err) end if err ~= nil then log:error(MOD_ABBR, err) end
response:send() response:send()
response:executePostResponseQueue() response:executePostResponseQueue()
else else
log:info(MOD_ABBR, "Nothing to do...bye.\n") log:info(MOD_ABBR, "Nothing to do...bye.\n")

View File

@ -409,6 +409,7 @@ function M.associateSsid(ssid, passphrase, recreate)
return true return true
end end
--- Disassociate wlan device as client from all SSID's. --- Disassociate wlan device as client from all SSID's.
-- Note: this function might belong in the wlanconfig module but that would introduce -- Note: this function might belong in the wlanconfig module but that would introduce
-- a circular dependency, easiest solution is to place the function here. -- a circular dependency, easiest solution is to place the function here.

View File

@ -28,15 +28,21 @@ local SIGNING_IN_STATUS = 2
--- Signin to connect.doodle3d.com server --- Signin to connect.doodle3d.com server
-- --
function M.signin() function M.signin()
log:verbose(MOD_ABBR, "signin:signin");
--log:verbose(MOD_ABBR, "signin:signin");
local code, msg = M.getStatus() local code, msg = M.getStatus()
--log:verbose(MOD_ABBR, " status: "..utils.dump(code).." "..utils.dump(msg)); --log:verbose(MOD_ABBR, " status: "..utils.dump(code).." "..utils.dump(msg));
-- if we are already signin in, skip -- if we are already signin in, skip
if(code == SIGNING_IN_STATUS) then if(code == SIGNING_IN_STATUS) then
log:verbose(MOD_ABBR, " skipping signin") log:verbose(MOD_ABBR, " already signing in, skipping")
return
end
local ds = wifi.getDeviceState()
log:verbose(MOD_ABBR, " wifi deviceState.mode: "..utils.dump(ds.mode))
if ds.mode ~= "sta" then
log:verbose(MOD_ABBR, " wifi not in station mode, skipping signin")
return return
end end

View File

@ -160,7 +160,7 @@ function M.access(request, response)
response:setSuccess() response:setSuccess()
response:addData('has_control', hasControl) response:addData('has_control', hasControl)
return true return
end end
function M.status(request, response) function M.status(request, response)

View File

@ -5,6 +5,7 @@
-- @license This software is licensed under the terms of the GNU GPL v2 or later. -- @license This software is licensed under the terms of the GNU GPL v2 or later.
-- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. -- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
-- TODO: return errors like in print_POST (error message in a 'msg' key instead of directly in the response) if this does not break API compatibility
local lfs = require('lfs') local lfs = require('lfs')
local log = require('util.logger') local log = require('util.logger')
@ -28,7 +29,7 @@ end
function M.temperature(request, response) function M.temperature(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = printerUtils.createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer or not printer:hasSocket() then return false end if not printer or not printer:hasSocket() then return end
local temperatures,msg = printer:getTemperatures() local temperatures,msg = printer:getTemperatures()
@ -39,18 +40,18 @@ function M.temperature(request, response)
response:addData('hotend_target', temperatures.hotend_target) response:addData('hotend_target', temperatures.hotend_target)
response:addData('bed', temperatures.bed) response:addData('bed', temperatures.bed)
response:addData('bed_target', temperatures.bed_target) response:addData('bed_target', temperatures.bed_target)
elseif temperatures == false then
response:addData('status', msg)
response:setFail()
else else
response:setError(msg) response:setError(msg)
return false;
end end
return true;
end end
function M.progress(request, response) function M.progress(request, response)
local argId = request:get("id") local argId = request:get("id")
local printer,msg = printerUtils.createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer or not printer:hasSocket() then return false end if not printer or not printer:hasSocket() then return end
-- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case.
local currentLine,bufferedLines,totalLines = printer:getProgress() local currentLine,bufferedLines,totalLines = printer:getProgress()
@ -61,15 +62,16 @@ function M.progress(request, response)
response:addData('current_line', currentLine) response:addData('current_line', currentLine)
response:addData('buffered_lines', bufferedLines) response:addData('buffered_lines', bufferedLines)
response:addData('total_lines', totalLines) response:addData('total_lines', totalLines)
elseif progress == false then
response:addData('status', bufferedLines)
response:setFail()
else else
response:setError(bufferedLines) response:setError(bufferedLines)
return false
end end
return true;
end end
-- NOTE: onlyReturnState is optional and prevents response from being modified, used when calling from within other api call -- Note: onlyReturnState is optional and prevents response from being modified, used when calling from within other api call
-- Note: unlike regular API-functions, this one returns either true+state or false
function M.state(request, response, onlyReturnState) function M.state(request, response, onlyReturnState)
local argId = request:get("id") local argId = request:get("id")
if not onlyReturnState then response:addData('id', argId) end if not onlyReturnState then response:addData('id', argId) end
@ -98,19 +100,19 @@ function M.state(request, response, onlyReturnState)
response:addData('state', rv) response:addData('state', rv)
end end
return true, rv return true, rv
else else -- Note: do not differentiate between false and nil here, false should never be returned
if not onlyReturnState then response:setError(msg) end if not onlyReturnState then response:setError(msg) end
return false return false
end end
end end
return true
--this point cannot be reached, no return necessary
end end
-- retrieve a list of 3D printers currently supported -- retrieve a list of 3D printers currently supported
function M.listall(request, response) function M.listall(request, response)
response:setSuccess() response:setSuccess()
response:addData('printers', printerUtils.supportedPrinters()) response:addData('printers', printerUtils.supportedPrinters())
return true
end end
@ -130,13 +132,17 @@ function M.heatup_POST(request, response)
local rv,msg = printer:heatup(temperature) local rv,msg = printer:heatup(temperature)
response:addData('id', argId) response:addData('id', argId)
if rv then response:setSuccess() if rv then
else response:setFail(msg) response:setSuccess()
elseif rv == false then
response:addData('status', msg)
response:setFail()
else
response:setError(msg)
end end
end end
function M.stop_POST(request, response) function M.stop_POST(request, response)
log:info(MOD_ABBR, "API:printer/stop") log:info(MOD_ABBR, "API:printer/stop")
if not accessManager.hasControl(request.remoteAddress) then if not accessManager.hasControl(request.remoteAddress) then
@ -147,7 +153,7 @@ function M.stop_POST(request, response)
local argId = request:get("id") local argId = request:get("id")
local argGcode = request:get("gcode") local argGcode = request:get("gcode")
local printer,msg = printerUtils.createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer or not printer:hasSocket() then return false end if not printer or not printer:hasSocket() then return end
if(argGcode == nil) then if(argGcode == nil) then
argGcode = "" argGcode = ""
@ -155,8 +161,13 @@ function M.stop_POST(request, response)
local rv,msg = printer:stopPrint(argGcode) local rv,msg = printer:stopPrint(argGcode)
response:addData('id', argId) response:addData('id', argId)
if rv then response:setSuccess() if rv then
else response:setError(msg) response:setSuccess()
elseif rv == false then
response:addData('status', msg)
response:setFail()
else
response:setError(msg)
end end
end end
@ -164,6 +175,8 @@ end
--accepts: id(string) (the printer ID to append to) --accepts: id(string) (the printer ID to append to)
--accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true)
--accepts: start(bool) (only when this argument is true will printing be started) --accepts: start(bool) (only when this argument is true will printing be started)
--accepts: seq_number(int) (sequence number of the chunk, must be given until clear() after given once, and incremented each time)
--accepts: seq_total(int) (total number of gcode chunks to be appended, must be given until clear() after given once, and stay the same)
--returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned --returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned
function M.print_POST(request, response) function M.print_POST(request, response)
log:info(MOD_ABBR, "API:printer/print") log:info(MOD_ABBR, "API:printer/print")
@ -187,9 +200,12 @@ function M.print_POST(request, response)
local argGcode = request:get("gcode") local argGcode = request:get("gcode")
local argIsFirst = utils.toboolean(request:get("first")) local argIsFirst = utils.toboolean(request:get("first"))
local argStart = utils.toboolean(request:get("start")) local argStart = utils.toboolean(request:get("start"))
local argSeqNumber = request:get("seq_number") or -1
local argSeqTotal = request:get("seq_total") or -1
local remoteHost = request:getRemoteHost()
local printer,msg = printerUtils.createPrinterOrFail(argId, response) local printer,msg = printerUtils.createPrinterOrFail(argId, response)
if not printer or not printer:hasSocket() then return false end if not printer or not printer:hasSocket() then return end
response:addData('id', argId) response:addData('id', argId)
@ -203,7 +219,10 @@ function M.print_POST(request, response)
response:addData('gcode_clear',true) response:addData('gcode_clear',true)
local rv,msg = printer:clearGcode() local rv,msg = printer:clearGcode()
if not rv then if rv == false then
response:addData('status', msg)
response:setFail("could not clear gcode")
elseif rv == nil then
response:setError(msg) response:setError(msg)
return return
end end
@ -211,14 +230,17 @@ function M.print_POST(request, response)
local rv,msg local rv,msg
-- 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, { seq_number = argSeqNumber, seq_total = argSeqTotal, source = remoteHost })
rv,msg = printer:appendGcode(argGcode)
if rv then if rv then
--NOTE: this does not report the number of lines, but only the block which has just been added --NOTE: this does not report the number of lines, but only the block which has just been added
response:addData('gcode_append',argGcode:len()) response:addData('gcode_append',argGcode:len())
elseif rv == false then
response:addData('status', msg)
response:setFail("could not add gcode")
return
else else
response:setError("could not add gcode")
response:addData('msg', msg) response:addData('msg', msg)
response:setError("could not add gcode")
return return
end end
@ -228,9 +250,14 @@ function M.print_POST(request, response)
if rv then if rv then
response:setSuccess() response:setSuccess()
response:addData('gcode_print',true) response:addData('gcode_print',true)
elseif rv == false then
response:addData('status', msg)
response:setFail("could not send gcode")
return
else else
response:setError("could not send gcode")
response:addData('msg', msg) response:addData('msg', msg)
response:setError("could not send gcode")
return
end end
else else
response:setSuccess() response:setSuccess()

View File

@ -132,7 +132,7 @@ end
--- Call all functions on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note. --- Call all functions on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note.
function M:executePostResponseQueue() function M:executePostResponseQueue()
log:verbose(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) if #self.postResponseQueue > 0 then log:verbose(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) end
for i,fn in ipairs(self.postResponseQueue) do fn() end for i,fn in ipairs(self.postResponseQueue) do fn() end
end end