Wordclock/telnet.lua

208 lines
6.5 KiB
Lua
Raw Normal View History

2020-02-21 20:21:54 +01:00
-- Telnet Server
function startTelnetServer()
s=net.createServer(net.TCP, 180)
s:listen(23,function(c)
global_c=c
printlines = {}
function s_output(str)
if(global_c~=nil) then
if #printlines > 0 then
printlines[ #printlines + 1] = str
else
printlines[ #printlines + 1] = str
global_c:send("\r") -- Send something, so the queue is read after sending
end
end
end
node.output(s_output, 0)
c:on("receive",function(c,l)
node.input(l)
end)
c:on("disconnection",function(c)
node.output(nil)
global_c=nil
end)
c:on("sent", function()
if #printlines > 0 then
global_c:send(table.remove(printlines, 1))
end
end)
print("Welcome to the Wordclock.")
2020-02-21 23:31:05 +01:00
print("- storeConfig()")
2020-02-21 20:21:54 +01:00
print("Visite https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for further commands")
end)
print("Telnetserver is up")
end
2020-03-04 22:00:15 +01:00
function storeConfig(_ssid, _password, _timezoneoffset, _sntpserver, _inv46, _dim, _fcolor, _colorMin1, _colorMin2, _colorMin3, _colorMin4, _bcolor, _threequater)
2020-02-21 23:31:05 +01:00
if ( (_ssid == nil) and
(_password == nil) and
(_timezoneoffset == nil) and
2020-03-04 22:00:15 +01:00
(_sntpserver == nil) and
2020-02-21 23:31:05 +01:00
(_inv46 == nil) and
(_dim == nil) and
(_fcolor == nil) and
(_colorMin1 == nil) and
(_colorMin2 == nil) and
(_colorMin3 == nil) and
(_colorMin4 == nil) and
(_bcolor == nil) and
(_threequater == nil) ) then
print("one parameter is mandatory:")
2020-03-04 22:00:15 +01:00
print("storeConfig(ssid, password, timezoneoffset, sntpserver, inv46, dim, fcolor, colorMin1, colorMin2, colorMin3, colorMin4, bcolor, threequater)")
return
2020-02-21 23:31:05 +01:00
end
if (_password==nil) then
_, password, _, _ = wifi.sta.getconfig()
print("Restore password")
else
password = _password
end
if (_ssid==nil) then
ssid, _, _, _ = wifi.sta.getconfig()
else
ssid = _ssid
end
2020-03-04 22:00:15 +01:00
if (_sntpserver == nil) then
sntpserver = sntpserverhostname
print("Restore SNTP: " .. tostring(sntpserver))
else
sntpserver = _sntpserver
end
if (_timezoneoffset ~= nil) then
2020-02-21 23:31:05 +01:00
timezoneoffset = _timezoneoffset
end
2020-03-04 22:00:15 +01:00
if (_inv46 ~= nil) then
if ((_inv46 == true) or (_inv == "on")) then
inv46 = "on"
elseif ((_inv46 == false) or (_inv == "off")) then
inv46 = "off"
else
inv46 = "off"
end
2020-02-21 23:31:05 +01:00
end
2020-03-04 22:00:15 +01:00
if (_dim ~= nil) then
2020-02-21 23:31:05 +01:00
dim = _dim
end
2020-03-04 22:00:15 +01:00
if (_fcolor ~= nil) then
2020-02-21 23:31:05 +01:00
fcolor = _fcolor
end
2020-03-04 22:00:15 +01:00
if (_bcolor ~= nil) then
2020-02-21 23:31:05 +01:00
bcolor = _bcolor
end
2020-03-04 22:00:15 +01:00
if (_colorMin1 ~= nil) then
2020-02-21 23:31:05 +01:00
colorMin1 = _colorMin1
end
2020-03-04 22:00:15 +01:00
if (_colorMin2 ~= nil) then
2020-02-21 23:31:05 +01:00
colorMin2 = _colorMin2
end
2020-03-04 22:00:15 +01:00
if (_colorMin3 ~= nil) then
2020-02-21 23:31:05 +01:00
colorMin3 = _colorMin3
end
2020-03-04 22:00:15 +01:00
if (_colorMin4 ~= nil) then
2020-02-21 23:31:05 +01:00
colorMin4 = _colorMin4
end
2020-03-04 22:00:15 +01:00
if (_threequater ~= nil) then
2020-02-21 23:31:05 +01:00
threequater = _threequater
end
print("SSID = " .. tostring(ssid))
print("TZNE = " .. tostring(timezoneoffset))
2020-03-04 22:00:15 +01:00
print("NTP = " .. tostring(sntpserver))
2020-02-21 23:31:05 +01:00
print("INVT = " .. tostring(inv46))
print("DIM = " .. tostring(dim))
print("FCOL = " .. tostring(fcolor))
print("BCOL = " .. tostring(bcolor))
print("MIN1 = " .. tostring(colorMin1))
print("MIN2 = " .. tostring(colorMin2))
print("MIN3 = " .. tostring(colorMin3))
print("MIN4 = " .. tostring(colorMin4))
print("3QRT = " .. tostring(threequater))
2020-03-04 21:01:13 +01:00
local configFile="config.lua"
2020-02-21 23:31:05 +01:00
-- Safe configuration:
file.remove(configFile .. ".new")
sec, _ = rtctime.get()
file.open(configFile.. ".new", "w+")
file.write("-- Config\n" .. "station_cfg={}\nstation_cfg.ssid=\"" .. ssid .. "\"\nstation_cfg.pwd=\"" .. password .. "\"\nstation_cfg.save=false\nwifi.sta.config(station_cfg)\n")
file.write("sntpserverhostname=\"" .. sntpserver .. "\"\n" .. "timezoneoffset=\"" .. timezoneoffset .. "\"\n".. "inv46=\"" .. tostring(inv46) .. "\"\n" .. "dim=\"" .. tostring(dim) .. "\"\n")
if (fcolor ~= nil) then
local hexColor=string.sub(fcolor, 1)
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("color=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
-- fill the current values
color=string.char(green, red, blue)
end
if (colorMin1 ~= nil) then
local hexColor=string.sub(colorMin1, 1)
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")
color1=string.char(green, red, blue)
end
if ( colorMin2 ~= nil) then
local hexColor=string.sub(colorMin2, 1)
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")
color2=string.char(green, red, blue)
end
if ( colorMin3 ~= nil) then
local hexColor=string.sub(colorMin3, 1)
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")
color3=string.char(green, red, blue)
end
if ( colorMin4 ~= nil) then
local hexColor=string.sub(colorMin4, 1)
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")
color4=string.char(green, red, blue)
end
if ( bcolor ~= nil) then
local hexColor=string.sub(bcolor, 1)
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("colorBg=string.char(" .. green .. "," .. red .. "," .. blue .. ")\n")
-- fill the current values
colorBg=string.char(green, red, blue)
end
if (getTime ~= nil) then
time = getTime(sec, timezoneoffset)
file.write("print(\"Config from " .. time.year .. "-" .. time.month .. "-" .. time.day .. " " .. time.hour .. ":" .. time.minute .. ":" .. time.second .. "\")\n")
end
if ( threequater ~= nil) then
file.write("threequater=true\n")
-- fill the current values
threequater=true
else
file.write("threequater=nil\n") -- unset threequater
-- fill the current values
threequater=nil
end
file.close()
collectgarbage()
sec=nil
file.remove(configFile)
if (file.rename(configFile .. ".new", configFile)) then
print("Rename Successfully")
2020-03-04 22:00:15 +01:00
else
print("Cannot rename " .. configFile .. ".new")
2020-02-21 23:31:05 +01:00
end
end