From 0667dfe0703f3d9f0bf93c6254d7dc4763ec90aa Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Thu, 10 Oct 2013 17:51:14 +0200 Subject: [PATCH] Handle errors in api:info/status. Use Disconnected state when there is no printer driver --- src/rest/api/api_info.lua | 23 +++++++----- src/rest/api/api_printer.lua | 70 +++++++++++++++--------------------- 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index a484428..b681ebf 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -112,25 +112,32 @@ function M.access(request, response) end else response:setError(msg) - return + return false end end local hasControl = accessManager.hasControl(request.remoteAddress) response:setSuccess() - response:addData('hasControl', hasControl) + response:addData('has_control', hasControl) - return + return true end function M.status(request, response) - printerAPI.temperature(request, response) - printerAPI.progress(request, response) - printerAPI.state(request, response) - M.access(request, response) + local rv + rv, state = printerAPI.state(request, response) + if(rv == false) then return end - response:addData('v', 9) + if(state ~= "disconnected") then + rv = printerAPI.temperature(request, response) + if(rv == false) then return end + rv = printerAPI.progress(request, response) + if(rv == false) then return end + rv = M.access(request, response) + if(rv == false) then return end + end + response:addData('v', 10) end diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 77ebc44..300f561 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -22,11 +22,10 @@ function M._global(request, response) response:setSuccess() end ---requires id(string) function M.temperature(request, response) local argId = request:get("id") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer then return end + if not printer then return false end local temperatures,msg = printer:getTemperatures() @@ -39,66 +38,57 @@ function M.temperature(request, response) response:addData('bed_target', temperatures.bed_target) else response:setError(msg) + return false; end + + return true; end ---requires id(string) function M.progress(request, response) local argId = request:get("id") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer then return end + if not printer then return false end -- NOTE: despite their names, `currentLine` is still the error indicator and `numLines` the message in such case. - local currentLine,numLines = printer:getProgress() + local currentLine,totalLines = printer:getProgress() response:addData('id', argId) if currentLine then response:setSuccess() response:addData('current_line', currentLine) - response:addData('num_lines', numLines) + response:addData('total_lines', totalLines) else - response:setError(numLines) + response:setError(totalLines) + return false end + + return true; end ---TODO: remove busy function (client should use state function) ---requires id(string) -function M.busy(request, response) - local argId = request:get("id") - local printer,msg = printerUtils.createPrinterOrFail(argId, response) - 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") - local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer then return end - - local rv,msg = printer:getState() - response:addData('id', argId) - if rv then + + local printer,msg = printerUtils.createPrinterOrFail(argId, response) + if not printer then response:setSuccess() - response:addData('state', rv) - else - response:setError(msg) + local printerState = "disconnected" + response:addData('state', printerState) + return true, printerState + else + local rv,msg = printer:getState() + if rv then + response:setSuccess() + response:addData('state', rv) + return true, rv + else + response:setError(msg) + return false + end end + return true; end - - ---requires id(string) function M.heatup_POST(request, response) if not accessManager.hasControl(request.remoteAddress) then @@ -108,7 +98,7 @@ function M.heatup_POST(request, response) local argId = request:get("id") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer then return end + if not printer then return false end local temperature = settings.get('printer.heatup.temperature') local rv,msg = printer:heatup(temperature) @@ -119,7 +109,6 @@ function M.heatup_POST(request, response) end end ---requires id(string) function M.stop_POST(request, response) if not accessManager.hasControl(request.remoteAddress) then @@ -140,7 +129,6 @@ function M.stop_POST(request, response) end end ---requires id(string), 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)