More dynamic content is generated
This commit is contained in:
parent
d2ad06abf1
commit
687a2bbbb9
@ -57,5 +57,6 @@ Please note that all settings are mandatory<br /><br />
|
||||
<tr><td colspan="3"><div align="center"><input type="submit" name="action" value="Reboot"></div></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
$ADDITIONAL_LINE
|
||||
</body>
|
||||
</html>
|
||||
|
149
webserver.lua
149
webserver.lua
@ -61,8 +61,56 @@ function sendPage(conn, nameOfFile, replaceMap)
|
||||
conn:send(buf)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function fillDynamicMap()
|
||||
replaceMap = {}
|
||||
ssid, _ = wifi.sta.getconfig()
|
||||
|
||||
if (ssid == nil) then
|
||||
ssid="Not set"
|
||||
end
|
||||
if (sntpserverhostname == nil) then
|
||||
sntpserverhostname="ptbtime1.ptb.de"
|
||||
end
|
||||
if (timezoneoffset == nil) then
|
||||
timezoneoffset=1
|
||||
end
|
||||
-- Set the default color, if nothing is set
|
||||
if (color == nil) then
|
||||
color=string.char(0,0,250)
|
||||
end
|
||||
if (color1 == nil) then
|
||||
color1=color
|
||||
end
|
||||
if (color2 == nil) then
|
||||
color2=color
|
||||
end
|
||||
if (color3 == nil) then
|
||||
color3=color
|
||||
end
|
||||
if (color4 == nil) then
|
||||
color4=color
|
||||
end
|
||||
local hexColor = "#" .. string.format("%02x",string.byte(color,1)) .. string.format("%02x",string.byte(color,2)) .. string.format("%02x",string.byte(color,3))
|
||||
local hexColor1 = "#" .. string.format("%02x",string.byte(color1,1)) .. string.format("%02x",string.byte(color1,2)) .. string.format("%02x",string.byte(color1,3))
|
||||
local hexColor2 = "#" .. string.format("%02x",string.byte(color2,1)) .. string.format("%02x",string.byte(color2,2)) .. string.format("%02x",string.byte(color2,3))
|
||||
local hexColor3 = "#" .. string.format("%02x",string.byte(color3,1)) .. string.format("%02x",string.byte(color3,2)) .. string.format("%02x",string.byte(color3,3))
|
||||
local hexColor4 = "#" .. string.format("%02x",string.byte(color4,1)) .. string.format("%02x",string.byte(color4,2)) .. string.format("%02x",string.byte(color4,3))
|
||||
|
||||
|
||||
|
||||
replaceMap["$SSID"]=ssid
|
||||
replaceMap["$SNTPSERVER"]=sntpserverhostname
|
||||
replaceMap["$TIMEOFFSET"]=timezoneoffset
|
||||
replaceMap["$THREEQUATER"]=(threequater and "checked" or "")
|
||||
replaceMap["$ADDITIONAL_LINE"]=""
|
||||
replaceMap["$HEXCOLOR"]=hexColor
|
||||
replaceMap["$HEXCOLOR1"]=hexColor1
|
||||
replaceMap["$HEXCOLOR2"]=hexColor2
|
||||
replaceMap["$HEXCOLOR3"]=hexColor3
|
||||
replaceMap["$HEXCOLOR4"]=hexColor4
|
||||
return replaceMap
|
||||
end
|
||||
|
||||
function startWebServer()
|
||||
@ -76,19 +124,104 @@ function startWebServer()
|
||||
if (sendPage ~= nil) then
|
||||
print("Sending webpage.html ...")
|
||||
-- Load the sendPagewebcontent
|
||||
replaceMap = {}
|
||||
replaceMap["$SSID"]="33C3"
|
||||
replaceMap["$SNTPSERVER"]="time.server23.org"
|
||||
replaceMap["$TIMEOFFSET"]="1"
|
||||
replaceMap["$THREEQUATER"]="checked"
|
||||
|
||||
replaceMap=fillDynamicMap()
|
||||
sendPage(conn, "webpage.html", replaceMap)
|
||||
end
|
||||
|
||||
else if (payload:find("POST /") ~=nil) then
|
||||
-- Fixme handle it later
|
||||
print("POST request detected")
|
||||
--code for handling the POST-request (updating settings)
|
||||
_, postdatastart = payload:find("\r\n\r\n")
|
||||
--Next lines catches POST-requests without POST-data....
|
||||
if postdatastart==nil then postdatastart = 1 end
|
||||
local postRequestData=string.sub(payload,postdatastart+1)
|
||||
local _POST = {}
|
||||
for i, j in string.gmatch(postRequestData, "(%w+)=([^&]+)&*") do
|
||||
_POST[i] = j
|
||||
end
|
||||
|
||||
--- Do the magic!
|
||||
if (_POST.action ~= nil and _POST.action == "Reboot") then
|
||||
node.restart()
|
||||
end
|
||||
|
||||
if ((_POST.ssid~=nil) and (_POST.sntpserver~=nil) and (_POST.timezoneoffset~=nil)) then
|
||||
print("New config!")
|
||||
if (_POST.password==nil) then
|
||||
_, password, _, _ = wifi.sta.getconfig()
|
||||
print("Restoring password : " .. password)
|
||||
_POST.password = password
|
||||
password = nil
|
||||
end
|
||||
-- Safe configuration:
|
||||
file.remove(configFile .. ".new")
|
||||
sec, _ = rtctime.get()
|
||||
file.open(configFile.. ".new", "w+")
|
||||
file.write("-- Config\n" .. "wifi.sta.config(\"" .. _POST.ssid .. "\",\"" .. _POST.password .. "\")\n" .. "sntpserverhostname=\"" .. _POST.sntpserver .. "\"\n" .. "timezoneoffset=\"" .. _POST.timezoneoffset .. "\"\n")
|
||||
if ( _POST.fcolor ~= nil) then
|
||||
-- color=string.char(_POST.green, _POST.red, _POST.blue)
|
||||
print ("Got fcolor: " .. _POST.fcolor)
|
||||
local hexColor=string.sub(_POST.fcolor, 4)
|
||||
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
||||
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
||||
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
||||
color=string.char(green, red, blue)
|
||||
end
|
||||
if ( _POST.colorMin1 ~= nil) then
|
||||
local hexColor=string.sub(_POST.colorMin1, 4)
|
||||
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
||||
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
||||
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
||||
file.write("color1=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
||||
end
|
||||
if ( _POST.colorMin2 ~= nil) then
|
||||
local hexColor=string.sub(_POST.colorMin2, 4)
|
||||
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
||||
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
||||
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
||||
file.write("color2=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
||||
end
|
||||
if ( _POST.colorMin3 ~= nil) then
|
||||
local hexColor=string.sub(_POST.colorMin3, 4)
|
||||
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
||||
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
||||
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
||||
file.write("color3=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
||||
end
|
||||
if ( _POST.colorMin4 ~= nil) then
|
||||
local hexColor=string.sub(_POST.colorMin4, 4)
|
||||
local red = tonumber(string.sub(hexColor, 1, 2), 16)
|
||||
local green = tonumber(string.sub(hexColor, 3, 4), 16)
|
||||
local blue = tonumber(string.sub(hexColor, 5, 6), 16)
|
||||
file.write("color4=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
|
||||
end
|
||||
time = getTime(sec, timezoneoffset)
|
||||
file.write("color=string.char(" .. string.byte(color,1) .. "," .. string.byte(color, 2) .. "," .. string.byte(color, 3) .. ")\n")
|
||||
file.write("print(\"Config from " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second .. "\")\n")
|
||||
if (_POST.threequater ~= nil) then
|
||||
file.write("threequater=true\n")
|
||||
else
|
||||
file.write("threequater=nil\n") -- unset threequater
|
||||
end
|
||||
file.close()
|
||||
sec=nil
|
||||
file.remove(configFile)
|
||||
print("Rename config")
|
||||
if (file.rename(configFile .. ".new", configFile)) then
|
||||
print("Successfully")
|
||||
dofile(configFile) -- load the new values
|
||||
replaceMap=fillDynamicMap()
|
||||
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"green\">New configuration saved</font></h2>"
|
||||
sendPage(conn, "webpage.html", replaceMap)
|
||||
else
|
||||
replaceMap=fillDynamicMap()
|
||||
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"red\">ERROR</font></h2>"
|
||||
sendPage(conn, "webpage.html", replaceMap)
|
||||
end
|
||||
else
|
||||
replaceMap=fillDynamicMap()
|
||||
replaceMap["$ADDITIONAL_LINE"]="<h2><font color=\"orange\">Not all parameters set</font></h2>"
|
||||
sendPage(conn, "webpage.html", replaceMap)
|
||||
end
|
||||
else
|
||||
print("Hello via telnet")
|
||||
--here is code, if the connection is not from a webbrowser, i.e. telnet or nc
|
||||
|
Loading…
Reference in New Issue
Block a user