mirror of
https://github.com/Doodle3D/doodle3d-firmware.git
synced 2024-12-22 11:03:48 +01:00
Added a validation function option to configuration. Added a number check
This commit is contained in:
parent
e2b28900bf
commit
b2904d532e
@ -12,6 +12,7 @@
|
||||
|
||||
NOTE that the all-caps definitions will be changed into configuration keys, or moved to a different location
|
||||
]]--
|
||||
local printer = require('util.printer')
|
||||
|
||||
local M = {}
|
||||
|
||||
@ -53,14 +54,20 @@ M.printer_type = {
|
||||
default = 'ultimaker',
|
||||
type = 'string',
|
||||
description = '',
|
||||
regex = 'rigidbot|ultimaker|makerbot_replicator2|makerbot_thingomatic|printrbot|bukobot|cartesio|cyrus|delta_rostockmax|deltamaker|eventorbot|felix|gigabot|kossel|leapfrog_creatr|lulzbot_aO-101|makergear_m2|makergear_prusa|makibox|orca_0_3|ord_bot_hadron|printxel_3d|prusa_i3|prusa_iteration_2|rapman|reprappro_huxley|reprappro_mendel|robo_3d_printer|shapercube|tantillus|vision_3d_printer|'
|
||||
isValid = function(value)
|
||||
local printers = printer.supportedPrinters()
|
||||
return printers[value] ~= nil
|
||||
end
|
||||
}
|
||||
|
||||
M.printer_baudrate = {
|
||||
default = '115200',
|
||||
type = 'int',
|
||||
description = '',
|
||||
regex = '115200|2500000'
|
||||
isValid = function(value)
|
||||
local baudrates = printer.supportedBaudRates()
|
||||
return baudrates[tostring(value)] ~= nil
|
||||
end
|
||||
}
|
||||
|
||||
M.printer_temperature = {
|
||||
|
@ -95,8 +95,13 @@ end
|
||||
-- @tparam table baseTable The base table to use constraint data from (min,max,regex).
|
||||
-- @treturn bool Returns true if the value is valid, false if it is not.
|
||||
local function isValid(value, baseTable)
|
||||
local varType, min, max, regex = baseTable.type, baseTable.min, baseTable.max, baseTable.regex
|
||||
|
||||
local varType, min, max, regex, isValid = baseTable.type, baseTable.min, baseTable.max, baseTable.regex, baseTable.isValid
|
||||
|
||||
if isValid then
|
||||
local ok = isValid(value)
|
||||
return ok or nil,"invalid value"
|
||||
end
|
||||
|
||||
if varType == 'bool' then
|
||||
return type(value) == 'boolean' or nil,"invalid bool value"
|
||||
|
||||
@ -115,7 +120,7 @@ local function isValid(value, baseTable)
|
||||
if regex then ok = ok and value:match(regex) ~= nil end
|
||||
return ok or nil,"invalid string value"
|
||||
end
|
||||
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
@ -187,12 +192,13 @@ end
|
||||
-- @treturn ?string Error message in case first return value is nil (invalid key).
|
||||
function M.set(key, value)
|
||||
key = replaceDots(key)
|
||||
|
||||
local r = utils.create(UCI_CONFIG_FILE)
|
||||
uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, UCI_CONFIG_TYPE)
|
||||
|
||||
local base = getBaseKeyTable(key)
|
||||
if not base then return nil,ERR_NO_SUCH_KEY end
|
||||
|
||||
|
||||
if M.isDefault(key) and value == nil then return true end -- key is default already
|
||||
|
||||
local current = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key)
|
||||
@ -205,6 +211,9 @@ function M.set(key, value)
|
||||
end
|
||||
elseif base.type == 'int' or base.type == 'float' then
|
||||
value = tonumber(value)
|
||||
if(value == nil) then
|
||||
return nil,"Value isn't a valid int or float"
|
||||
end
|
||||
end
|
||||
|
||||
if fromUciValue(current, base.type) == value then return true end
|
||||
|
Loading…
Reference in New Issue
Block a user