Skip to content

Commit

Permalink
fixing the boostrap sequence yet again.
Browse files Browse the repository at this point in the history
aligning integration tests with default bootstrap setup.
  • Loading branch information
fikin committed Sep 21, 2023
1 parent af6bd0c commit 0d072b3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions integration-tests/lua/testInit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ local function assertTelnet()
local skt = nodemcu.net_tpc_connect_to_listener(23, "0.0.0.0")
assertSktReceived(skt, "Enter username:")
assertSktSendReceived(skt, "dummy", "Enter username:")
assertSktSendReceived(skt, "telnet", "Enter password:")
assertSktSendReceived(skt, "admin", "Enter password:")
assertSktSendReceived(skt, "dummy", "Enter username:")
assertSktSendReceived(skt, "telnet", "Enter password:")
assertSktSendReceived(skt, "admin", "Enter password:")
assertSktSendReceived(skt, "admin", "") -- this is now deviating from actual device, as node.output() is not properly captured in mock setup
assertSktSendReceived(skt, "return 'abc'", "abc\n")
end
Expand Down
31 changes: 24 additions & 7 deletions lua_modules/bootstrap/fs/bootstrap-sw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@

local fs = require("factory-settings")

--fs("wifi-sta"):set("config.ssid","<SSID>"):set("config.pwd","<PWD>"):set("hostname","<HOSTNAME>"):done()
-- TODO : typically one wants to update these ones as part
-- of device inital boostrap.
-- Or use web-portal to update it later on too.
-- By default hostname is based on chipID
local hostname = "nodemcu" .. require("node").chipid()
local staSsid = hostname
local staPwd = "1234567890"
local apSsid = hostname .. "_ap"
local apPwd = "1234567890"

-- minimal set of modules on
-- fs("init-seq"):set("bootsequence",{ "bootstrap", "user-settings", "log-start", "wifi-apply-config", "wifi-mgr", "http-srv", "telnet" }):done()
local mac = require("wifi").ap.getmac()
fs("wifi-sta"):set("config.ssid", staSsid):set("config.pwd", staPwd):set("hostname", hostname):set("mac", mac):done()
fs("wifi-ap"):set("config.ssid", apSsid):set("config.pwd", apPwd):set("mac", mac):done()

-- likely same credentials for admin services
-- fs("telnet"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()
-- fs("web-portal"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()
-- fs("web-ota"):set("usr", "<ADMIN>"):set("pwd", "<APWD>"):done()
-- TODO likely update these too
local adminUsr = "admin"
local adminPwd = "admin"
fs("telnet"):set("usr", adminUsr):set("pwd", adminPwd):done()
fs("web-portal"):set("usr", adminUsr):set("pwd", adminPwd):done()
fs("web-ota"):set("usr", adminUsr):set("pwd", adminPwd):done()

-- minimal HomeAssistant modules and credentials
--fs("web-ha"):set("entities", { "system-hass" }):set("credentials.usr", "<HAUSER>"):set("credentials.pwd", "<HAPWD>"):done()
-- fs("web-ha"):set("entities", { "system-hass" }):set("credentials.usr", "<HAUSER>"):set("credentials.pwd", "<HAPWD>"):done()

-- minimal set of modules on.
-- restart at the end since the boot sequence is modified.
-- fs("init-seq"):set("bootsequence",{ "bootstrap", "user-settings", "log-start", "wifi-apply-config", "wifi-mgr", "http-srv", "telnet" }):done()
-- node.restart()
35 changes: 20 additions & 15 deletions lua_modules/bootstrap/lua/bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ local modname = ...

local file, log = require("file"), require("log")

local fName = "bootstrap-sw.lc"
local fName = "bootstrap-sw"
local fNameErr = "bootstrap-sw.PANIC.txt"

local function main()
package.loaded[modname] = nil

if file.exists(fName) then
log.info("running %s", fName)

local ok, err = pcall(require, "bootstrap-sw")

file.remove(fName)

if not ok then
log.error("bootstrap failed : %s : %s", fName, err)
file.remove(fNameErr)
file.putcontents(fNameErr, err)
for _, ext in ipairs({".lc", ".lua"}) do
local f = fName .. ext
if file.exists(f) then
log.info("running %s", f)

local ok, err = pcall(require, "bootstrap-sw")

file.remove(f)

if not ok then
log.error("bootstrap failed : %s : %s", f, err)
file.remove(fNameErr)
file.putcontents(fNameErr, err)
end

collectgarbage()
collectgarbage()

return
end

collectgarbage()
collectgarbage()
end
end

Expand Down
15 changes: 3 additions & 12 deletions lua_modules/device-settings/lua/user-settings.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--[[
Add here device settings which you want to apply programmatically at boot time.
Add here device settings which you want to
apply programmatically at boot time each time.
Use builder:set("field path", value) to set a value.
Expand All @@ -14,23 +15,13 @@
]]
local modname = ...

local fs = require("factory-settings")

---place to provide with device specific hardcoded device settings.
---feel free to modify the settings here, boot sequence will ensure
---the data is properly handled in device settings.
local function main()
package.loaded[modname] = nil

-- typically set hostname is based on chipID
-- until user overwrites it via web-portal for example

-- Superseeded by boostrap-sw module !!!
-- local hostname = "nodemcu" .. require("node").chipid()
-- local mac = require("wifi").ap.getmac()
-- fs("wifi-sta"):default("hostname", hostname):default("mac", mac):done()
-- fs("wifi-ap"):default("config.ssid", hostname .. "_ap"):default("mac", mac):done()

-- local fs = require("factory-settings")

-- TODO add here your other hardcoded settings if you want to
end
Expand Down
2 changes: 1 addition & 1 deletion lua_modules/temp-sensor/lua/temp-sensor-control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local function updateTempState(temp)
addrsCnt = addrsCnt + 1
end
if addrsCnt == 0 then
log.error("no temp sensor readings provided : %s", log.tbl, temp)
log.error("no temp sensor readings provided : %s", log.json, temp)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua_modules/wifimgr/fs/fs-wifi-ap.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"config": {
"beacon": 1000,
"ssid": "<AP_SSID>",
"pwd": "1234567890",
"pwd": "<AP_PWD>",
"save": false,
"max": 1,
"auth": 3,
Expand Down

0 comments on commit 0d072b3

Please sign in to comment.