diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 9dedf8d..4b32cd9 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -44,9 +44,20 @@ M.network_ap_address = { } M.network_ap_key = { - default = '', - type = 'string', - description = 'Access Point security key' + default = '', + type = 'string', + description = 'Access Point security key', + isValid = function(value) + if value == "" then + return true; + elseif value:len() < 8 then + return false, "too short" + elseif value:len() > 63 then + return false, "too long" + else + return true + end + end } M.network_ap_netmask = { @@ -184,13 +195,13 @@ M.printer_enableTraveling = { } M.printer_startgcode = { - default = ';Generated with Doodle3D\nG21 ;metric values\nG91 ;relative positioning\nM107 ;start with the fan off\nG28 X0 Y0 ;move X/Y to min endstops\nG28 Z0 ;move Z to min endstops\nG1 Z15 F9000 ;move the platform down 15mm\nG92 E0 ;zero the extruded length\nG1 F200 E10 ;extrude 10mm of feed stock\nG92 E0 ;zero the extruded length again\nG92 E0 ;zero the extruded length again\nG1 F9000\nG90 ;absolute positioning\nM117 Printing Doodle... ;display message (20 characters to clear whole screen)', + default = ';Generated with Doodle3D\nM109 S{printingTemp} ;set target temperature \nG21 ;metric values\nG91 ;relative positioning\nM107 ;start with the fan off\nG28 X0 Y0 ;move X/Y to min endstops\nG28 Z0 ;move Z to min endstops\nG1 Z15 F9000 ;move the platform down 15mm\nG92 E0 ;zero the extruded length\nG1 F200 E10 ;extrude 10mm of feed stock\nG92 E0 ;zero the extruded length again\nG92 E0 ;zero the extruded length again\nG1 F9000\nG90 ;absolute positioning\nM117 Printing Doodle... ;display message (20 characters to clear whole screen)', type = 'string', description = '' } M.printer_endgcode = { - default = 'M107 ;fan off\nG91 ;relative positioning\nG1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure\nG1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more\nG28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way\nM84 ;disable axes / steppers\nG90 ;absolute positioning\nM104 S180\nM117 Done ;display message (20 characters to clear whole screen)', + default = 'M107 ;fan off\nG91 ;relative positioning\nG1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure\nG1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more\nG28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way\nM84 ;disable axes / steppers\nG90 ;absolute positioning\nM104 {preheatTemp}\nM117 Done ;display message (20 characters to clear whole screen)', type = 'string', description = '' } diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index e9cb8d3..96ddcbc 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -17,9 +17,9 @@ M.WWW_RENAME_NAME = '/www-regular' M.CONNECTING_FAILED = -1 M.NOT_CONNECTED = 0 M.CONNECTING = 1 -M.CONNECTED = 2 -M.CREATING = 3 -M.CREATED = 4 +M.CONNECTED = 2 +M.CREATING = 3 +M.CREATED = 4 local function reloadBit(dlist, itemname) if dlist[itemname] == nil then dlist[itemname] = '' end @@ -76,9 +76,15 @@ function M.commitComponent(c) end function M.reloadComponent(c, silent) - log:info("reloading component '" .. c .. "'") - if silent ~= nil and silent then os.execute('/etc/init.d/' .. c .. ' reload &> /dev/null') - else os.execute('/etc/init.d/' .. c .. ' reload') end + log:info("reloading component '" .. c .. "'") + local cmd = '/etc/init.d/' .. c .. ' reload' + if silent ~= nil and silent then + cmd = cmd .. ' &> /dev/null' + os.execute(cmd) + else + rv = utils.captureCommandOutput(cmd) + log:info(" result reloading component '" .. c .. "': "..utils.dump(rv)) + end end function M.uciTableSet(config, section, options) @@ -259,17 +265,34 @@ end -- @tparam string ssid The SSID to use for the access point. -- @return True on success or nil+msg on error. function M.setupAccessPoint(ssid) - - M.setStatus(M.CREATING,"Creating access point..."); - + + M.setStatus(M.CREATING,"Creating access point '"..ssid.."'..."); + M.switchConfiguration{apnet="add_noreload"} wifi.activateConfig(ssid) -- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" } M.switchConfiguration{dhcp="reload"} - + M.setStatus(M.CREATED,"Access point created"); - + + local ds = wifi.getDeviceState() + --log:info(" network/status: ") + log:info(" ssid: ".. utils.dump(ds.ssid)) + --[[log:info(" bssid: ".. utils.dump(ds.bssid)) + log:info(" channel: ".. utils.dump(ds.channel)) + log:info(" mode: ".. utils.dump(ds.mode)) + log:info(" encryption: ".. utils.dump(ds.encryption)) + log:info(" quality: ".. utils.dump(ds.quality)) + log:info(" quality_max: ".. utils.dump(ds.quality_max)) + log:info(" txpower: ".. utils.dump(ds.txpower)) + log:info(" signal: ".. utils.dump(ds.signal)) + log:info(" noise: ".. utils.dump(ds.noise)) + log:info(" raw: ".. utils.dump(ds)) + + local localip = wifi.getLocalIP() + log:info(" localip: "..utils.dump(localip))]]-- + return true end @@ -282,9 +305,9 @@ end -- @return True on success or nil+msg on error. function M.associateSsid(ssid, passphrase, recreate) log:info("netconfig:associateSsid: "..(ssid or "")..", "..(passphrase or "")..", "..(recreate or "")) - + M.setStatus(M.CONNECTING,"Connecting..."); - + -- see if previously configured network for given ssid exists local cfg = nil for _, net in ipairs(wifi.getConfigs()) do @@ -293,7 +316,7 @@ function M.associateSsid(ssid, passphrase, recreate) break end end - + -- if not, or if newly created configuration is requested, create a new configuration if cfg == nil or recreate ~= nil then local scanResult = wifi.getScanInfo(ssid) @@ -320,14 +343,14 @@ function M.associateSsid(ssid, passphrase, recreate) M.setStatus(M.CONNECTING_FAILED,msg); return nil,msg end - + M.setStatus(M.CONNECTED,"Connected"); - + -- signin to connect.doodle3d.com local success, output = signin.signin() if success then log:info("Signed in") - else + else log:info("Signing in failed") end @@ -340,7 +363,7 @@ end function M.disassociate() M.setStatus(M.NOT_CONNECTED,"Not connected"); - + wifi.activateConfig() return wifi.restart() end diff --git a/src/rest/api/api_config.lua b/src/rest/api/api_config.lua index ba07751..0f19a1c 100644 --- a/src/rest/api/api_config.lua +++ b/src/rest/api/api_config.lua @@ -1,7 +1,9 @@ local log = require('util.logger') +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 M = { isApi = true @@ -19,14 +21,27 @@ function M._global_GET(request, response) end function M._global_POST(request, response) + --log:info("API:config:set") response:setSuccess() + + local validation = {} for k,v in pairs(request:getAll()) do + --log:info(" "..k..": "..v); local r,m = settings.set(k, v) - if r then response:addData(k, "ok") - else response:addData(k, "could not set key ('" .. m .. "')") + 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 .. "')" + 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) log:info("API:Network:try signing in") if signin.signin() then diff --git a/src/util/settings.lua b/src/util/settings.lua index 5d264d6..fbb6127 100644 --- a/src/util/settings.lua +++ b/src/util/settings.lua @@ -13,6 +13,8 @@ local uci = require('uci').cursor() local utils = require('util.utils') local baseconfig = require('conf_defaults') +local utils = require('util.utils') +local log = require('util.logger') local M = {} @@ -98,8 +100,9 @@ local function isValid(value, baseTable) 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" + local ok,msg = isValid(value) + if msg == nil then msg = "invalid value" end + return ok or nil,msg end if varType == 'bool' then @@ -107,18 +110,25 @@ local function isValid(value, baseTable) elseif varType == 'int' or varType == 'float' then local numValue = tonumber(value) - local ok = numValue and true or false - ok = ok and (varType == 'float' or math.floor(numValue) == numValue) - if min then ok = ok and numValue >= min end - if max then ok = ok and numValue <= max end - return ok or nil,"invalid int/float value or out of range" + if numValue == nil then + return nil, "invalid number" + elseif varType == 'int' and math.floor(numValue) ~= numValue then + return nil, "invalid int" + elseif min and numValue < min then + return nil, "too low" + elseif max and numValue > max then + return nil, "too high" + end elseif varType == 'string' then local ok = true - if min then ok = ok and value:len() >= min end - if max then ok = ok and value:len() <= max end - if regex then ok = ok and value:match(regex) ~= nil end - return ok or nil,"invalid string value" + if min and value:len() < min then + return nil,"too short" + elseif max and value:len() > max then + return nil,"too long" + elseif regex and value:match(regex) == nil then + return nil,"invalid value" + end end return true @@ -191,6 +201,7 @@ end -- @treturn bool|nil True if everything went well, nil in case of error. -- @treturn ?string Error message in case first return value is nil (invalid key). function M.set(key, value) + --log:info("settings:set: "..utils.dump(key)) key = replaceDots(key) local r = utils.create(UCI_CONFIG_FILE) @@ -198,11 +209,11 @@ function M.set(key, value) 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 - + --log:info(" not default") local current = uci:get(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key) - + --log:info(" base.type: "..utils.dump(base.type)) if base.type == 'bool' then if value ~= "" then value = utils.toboolean(value) @@ -216,15 +227,16 @@ function M.set(key, value) end end + + local valid,m = isValid(value, base) + if not valid then + return nil,m + end + if fromUciValue(current, base.type) == value then return true end if value ~= nil then - local valid,m = isValid(value, base) - if (valid) then - uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key, toUciValue(value, base.type)) - else - return nil,m - end + uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key, toUciValue(value, base.type)) else uci:delete(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, key) end diff --git a/src/www b/src/www index b1ded5d..10724b1 160000 --- a/src/www +++ b/src/www @@ -1 +1 @@ -Subproject commit b1ded5d41bb4b234c0715dfdf28a54486658b393 +Subproject commit 10724b17469371570f49804a7aed5ee2c0641ce6