2013-12-20 16:29:46 +01:00
|
|
|
--
|
|
|
|
-- This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
--
|
|
|
|
-- @copyright 2013, Doodle3D
|
|
|
|
-- @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.
|
|
|
|
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
local log = require('util.logger')
|
2013-10-17 14:04:13 +02:00
|
|
|
local utils = require('util.utils')
|
2013-07-17 22:55:27 +02:00
|
|
|
local settings = require('util.settings')
|
2013-08-28 18:33:48 +02:00
|
|
|
local printer = require('util.printer')
|
2013-10-28 13:52:27 +01:00
|
|
|
--local signin = require('network.signin')
|
2013-10-18 12:57:28 +02:00
|
|
|
local wifi = require('network.wlanconfig')
|
2013-10-23 16:12:19 +02:00
|
|
|
local accessManager = require('util.access')
|
|
|
|
local printerAPI = require('rest.api.api_printer')
|
|
|
|
|
2014-04-25 14:51:29 +02:00
|
|
|
local MOD_ABBR = "ACFG"
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
local M = {
|
|
|
|
isApi = true
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:12:19 +02:00
|
|
|
-- 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)
|
2013-12-20 17:11:58 +01:00
|
|
|
-- NOTE: rv being false means a printer device exists but no server is running for it, so it cannot be 'busy'
|
|
|
|
if rv == false then return true end
|
2013-10-23 16:12:19 +02:00
|
|
|
|
|
|
|
if printerState == 'buffering' or printerState == 'printing' or printerState == 'stopping' then
|
|
|
|
response:setFail("Printer is busy, please wait")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
function M._global_GET(request, response)
|
|
|
|
response:setSuccess()
|
|
|
|
for k,v in pairs(request:getAll()) do
|
|
|
|
local r,m = settings.get(k)
|
2013-10-23 16:12:19 +02:00
|
|
|
|
2013-12-23 15:33:55 +01:00
|
|
|
if r ~= nil then
|
2013-12-23 14:35:03 +01:00
|
|
|
response:addData(k, r)
|
2013-12-23 15:33:55 +01:00
|
|
|
else
|
2013-12-23 14:35:03 +01:00
|
|
|
response:addData(k, "could not read key ('" .. m .. "')")
|
|
|
|
response:setError(m)
|
2013-12-23 15:33:55 +01:00
|
|
|
return
|
2013-07-17 22:55:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 16:59:47 +02:00
|
|
|
-- returns substituted_wifiboxid (since version 0.10.2)
|
|
|
|
-- returns substituted_ssid (since version 0.9.1)
|
2013-07-17 22:55:27 +02:00
|
|
|
function M._global_POST(request, response)
|
2014-04-25 14:51:29 +02:00
|
|
|
--log:info(MOD_ABBR, "API:config:set")
|
2013-10-23 16:12:19 +02:00
|
|
|
|
|
|
|
if not operationsAccessOrFail(request, response) then return end
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
response:setSuccess()
|
2013-10-23 16:12:19 +02:00
|
|
|
|
2013-10-18 12:57:28 +02:00
|
|
|
local validation = {}
|
2013-07-17 22:55:27 +02:00
|
|
|
for k,v in pairs(request:getAll()) do
|
2014-04-25 14:51:29 +02:00
|
|
|
--log:info(MOD_ABBR, " "..k..": "..v);
|
2013-12-23 18:05:48 +01:00
|
|
|
local r,m = settings.set(k, v, true)
|
2013-10-23 16:12:19 +02:00
|
|
|
|
|
|
|
if r then
|
2013-10-18 12:57:28 +02:00
|
|
|
validation[k] = "ok"
|
2013-12-23 14:35:03 +01:00
|
|
|
elseif r == false then
|
2013-10-18 12:57:28 +02:00
|
|
|
validation[k] = "could not save setting ('" .. m .. "')"
|
2014-04-25 14:51:29 +02:00
|
|
|
log:info(MOD_ABBR, " m: "..utils.dump(m))
|
2013-12-23 14:35:03 +01:00
|
|
|
elseif r == nil then
|
2013-12-23 18:05:48 +01:00
|
|
|
settings.commit()
|
2013-12-23 14:35:03 +01:00
|
|
|
response:setError(m)
|
2013-12-23 15:33:55 +01:00
|
|
|
return
|
2013-07-17 22:55:27 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-23 18:05:48 +01:00
|
|
|
settings.commit()
|
2013-10-18 12:57:28 +02:00
|
|
|
response:addData("validation",validation)
|
2013-10-23 16:12:19 +02:00
|
|
|
|
2013-10-18 12:57:28 +02:00
|
|
|
local substitutedSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
|
|
|
response:addData("substituted_ssid",substitutedSsid)
|
2014-04-25 14:51:29 +02:00
|
|
|
|
2014-02-28 11:11:16 +01:00
|
|
|
local substitutedWiFiBoxID = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid'))
|
|
|
|
response:addData("substituted_wifiboxid",substitutedWiFiBoxID)
|
2013-07-17 22:55:27 +02:00
|
|
|
end
|
|
|
|
|
2013-07-27 00:25:43 +02:00
|
|
|
function M.all_GET(request, response)
|
2013-12-23 14:35:03 +01:00
|
|
|
local allSettings, msg = settings.getAll();
|
|
|
|
if allSettings then
|
|
|
|
response:setSuccess()
|
|
|
|
for k,v in pairs(settings.getAll()) do
|
|
|
|
response:addData(k,v)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
response:setError(msg)
|
2013-12-23 15:33:55 +01:00
|
|
|
return
|
2013-07-27 00:25:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-29 13:12:31 +01:00
|
|
|
--- Reset specific setting to default value
|
2013-12-20 17:11:58 +01:00
|
|
|
-- When an setting has a subSection only the setting in it's current subSection is reset.
|
|
|
|
-- For example you want to reset setting _printer.startcode_
|
|
|
|
-- and it has it's _subSection_ set to 'printer_type'
|
|
|
|
-- and printer.type is set to 'ultimaker' then
|
2013-11-29 13:12:31 +01:00
|
|
|
-- only the printer.startcode under the ultimaker subsection is removed.
|
2013-11-29 13:00:09 +01:00
|
|
|
function M.reset_POST(request, response)
|
2014-04-25 14:51:29 +02:00
|
|
|
--log:info(MOD_ABBR, "API:reset");
|
2013-11-29 13:00:09 +01:00
|
|
|
if not operationsAccessOrFail(request, response) then return end
|
|
|
|
response:setSuccess()
|
2013-12-20 17:11:58 +01:00
|
|
|
|
2013-11-29 13:00:09 +01:00
|
|
|
for k,v in pairs(request:getAll()) do
|
2014-04-25 14:51:29 +02:00
|
|
|
--log:info(MOD_ABBR, " "..k..": "..v);
|
2013-11-29 13:00:09 +01:00
|
|
|
local r,m = settings.reset(k);
|
2013-12-23 14:35:03 +01:00
|
|
|
if r ~= nil then
|
|
|
|
response:addData(k, "ok")
|
2013-12-23 15:33:55 +01:00
|
|
|
else
|
2013-12-23 14:35:03 +01:00
|
|
|
response:addData(k, "could not reset key ('" .. m .. "')")
|
|
|
|
response:setError(m)
|
2013-12-23 15:33:55 +01:00
|
|
|
return
|
2013-12-23 14:35:03 +01:00
|
|
|
end
|
2013-11-29 13:00:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 17:11:58 +01:00
|
|
|
--- Reset all settings to default value
|
2013-11-22 18:05:51 +01:00
|
|
|
function M.resetall_POST(request, response)
|
2013-11-29 13:00:09 +01:00
|
|
|
if not operationsAccessOrFail(request, response) then return end
|
2013-11-22 18:05:51 +01:00
|
|
|
response:setSuccess()
|
2013-12-23 15:33:55 +01:00
|
|
|
|
2013-12-23 14:35:03 +01:00
|
|
|
local rv, msg = settings.resetAll()
|
2013-12-23 15:33:55 +01:00
|
|
|
|
2013-12-23 14:35:03 +01:00
|
|
|
if(rv == nil) then
|
|
|
|
response:setError(msg)
|
2013-12-23 15:33:55 +01:00
|
|
|
return
|
2013-12-23 14:35:03 +01:00
|
|
|
end
|
2013-12-23 15:33:55 +01:00
|
|
|
|
2013-11-22 18:05:51 +01:00
|
|
|
for k,v in pairs(settings.getAll()) do
|
|
|
|
response:addData(k,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-28 18:33:48 +02:00
|
|
|
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
|
|
|
|
|
2013-07-17 22:55:27 +02:00
|
|
|
return M
|