forked from 0110/Wordclock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.lua
186 lines (178 loc) · 5.9 KB
/
commands.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function storeConfig(_ssid, _password, _timezoneoffset, _sntpserver, _inv46, _dim, _fcolor, _colorMin1, _colorMin2, _colorMin3, _colorMin4, _bcolor, _threequater)
if ( (_ssid == nil) and
(_password == nil) and
(_timezoneoffset == nil) and
(_sntpserver == nil) and
(_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:")
print("storeConfig(ssid, ")
print(" password,")
print(" timezoneoffset,")
print(" sntpserver,")
print(" inv46,")
print(" dim,")
print(" fcolor,")
print(" colorMin1,")
print(" colorMin2,")
print(" colorMin3,")
print(" colorMin4,")
print(" bcolor,")
print(" threequater)")
print(" ")
print("e.g.:")
print('storeConfig(nil, nil, 1, nil, "on", true, "00FF00", "00FF88", "008888", "00FF44", "004488", "000000", true)')
return
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
if (_sntpserver == nil) then
sntpserver = sntpserverhostname
print("Restore SNTP: " .. tostring(sntpserver))
else
sntpserver = _sntpserver
end
if (_timezoneoffset ~= nil) then
timezoneoffset = _timezoneoffset
end
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
end
if ( _dim ~= nil) then
dim = _dim
end
if (_fcolor ~= nil) then
fcolor = _fcolor
end
if (_bcolor ~= nil) then
bcolor = _bcolor
end
if (_colorMin1 ~= nil) then
colorMin1 = _colorMin1
end
if (_colorMin2 ~= nil) then
colorMin2 = _colorMin2
end
if (_colorMin3 ~= nil) then
colorMin3 = _colorMin3
end
if (_colorMin4 ~= nil) then
colorMin4 = _colorMin4
end
if (_threequater ~= nil) then
threequater = _threequater
end
print("SSID = " .. tostring(ssid))
print("TZNE = " .. tostring(timezoneoffset))
print("NTP = " .. tostring(sntpserver))
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))
local configFile="config.lua"
-- 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")
else
print("Cannot rename " .. configFile .. ".new")
end
end