0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-11-17 02:57:56 +01:00
doodle3d-firmware/src/rest/api/api_config.lua

129 lines
3.2 KiB
Lua
Raw Normal View History

local log = require('util.logger')
2013-10-17 14:04:13 +02:00
local utils = require('util.utils')
local settings = require('util.settings')
local printer = require('util.printer')
--local signin = require('network.signin')
local wifi = require('network.wlanconfig')
local accessManager = require('util.access')
local printerAPI = require('rest.api.api_printer')
local M = {
isApi = true
}
-- TODO: this function is also defined in 2 other places, combine them (and avoid require loops)
local function operationsAccessOrFail(request, response)
if not accessManager.hasControl(request.remoteAddress) then
response:setFail("No control access")
return false
end
local rv, printerState = printerAPI.state(request, response, true)
if(rv == false) then
response:setError("Could not get printer state")
return false
end
if printerState == 'buffering' or printerState == 'printing' or printerState == 'stopping' then
response:setFail("Printer is busy, please wait")
return false
end
return true
end
function M._global_GET(request, response)
response:setSuccess()
for k,v in pairs(request:getAll()) do
local r,m = settings.get(k)
if r ~= nil then response:addData(k, r)
else response:addData(k, "could not read key ('" .. m .. "')")
end
end
end
function M._global_POST(request, response)
2013-10-17 14:04:13 +02:00
--log:info("API:config:set")
if not operationsAccessOrFail(request, response) then return end
response:setSuccess()
local validation = {}
for k,v in pairs(request:getAll()) do
2013-10-17 14:04:13 +02:00
--log:info(" "..k..": "..v);
local r,m = settings.set(k, v)
if r then
--response:addData(k, "ok")
validation[k] = "ok"
else
--response:addData(k, "could not save setting ('" .. m .. "')")
validation[k] = "could not save setting ('" .. m .. "')"
2013-10-17 14:04:13 +02:00
log:info(" m: "..utils.dump(m))
end
end
response:addData("validation",validation)
local substitutedSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
response:addData("substituted_ssid",substitutedSsid)
-- we now call signin seperatly trough cgi-bin
--[[log:info("API:Network:try signing in")
if signin.signin() then
log:info("API:Network:signin successfull")
else
log:info("API:Network:signin failed")
end]]--
end
function M.all_GET(request, response)
response:setSuccess()
for k,v in pairs(settings.getAll()) do
response:addData(k,v)
end
end
function M.reset_POST(request, response)
--log:info("API:reset");
if not operationsAccessOrFail(request, response) then return end
response:setSuccess()
for k,v in pairs(request:getAll()) do
--log:info(" "..k..": "..v);
local r,m = settings.reset(k);
if r ~= nil then response:addData(k, "ok")
else response:addData(k, "could not reset key ('" .. m .. "')") end
end
end
2013-11-22 18:05:51 +01:00
function M.resetall_POST(request, response)
if not operationsAccessOrFail(request, response) then return end
2013-11-22 18:05:51 +01:00
response:setSuccess()
settings.resetAll()
for k,v in pairs(settings.getAll()) do
response:addData(k,v)
end
end
function M.supportedprinters_GET(request, response)
response:setSuccess()
for k,v in pairs(printer.supportedPrinters()) do
response:addData(k,v)
end
end
function M.supportedbaudrates_GET(request, response)
response:setSuccess()
for k,v in pairs(printer.supportedBaudRates()) do
response:addData(k,v)
end
end
return M