20.05.2025, 15:24
Hi, I have been having a few problems saving the "Common functions" script and an event-based script recently. My problem is that every time I try to save, the LM webpage becomes unreachable (Ping still works), but only after a few minutes does it come back online. Could the scripts crash LM? If you need the actual backup, I can send it through private messages.
Firmware version: 20240426
Common function code:
Firmware version: 20240426
Common function code:
Code:
-- user function library
-- send an e-mail
function mail(to, subject, message)
-- make sure these settings are correct
local settings = {
-- "from" field, only e-mail must be specified here
from = 'example@gmail.com',
-- smtp username
user = 'example@gmail.com',
-- smtp password
password = 'mypassword',
-- smtp server
server = 'smtp.gmail.com',
-- smtp server port
port = 465,
-- enable tls, required for gmail smtp
secure = 'tlsv1_2',
}
local smtp = require('socket.smtp')
if type(to) ~= 'table' then
to = { to }
end
for index, email in ipairs(to) do
to[ index ] = '<' .. tostring(email) .. '>'
end
-- fixup from field
local from = '<' .. tostring(settings.from) .. '>'
-- message headers and body
settings.source = smtp.message({
headers = {
to = table.concat(to, ', '),
subject = subject,
['From'] = from,
['Content-type'] = 'text/html; charset=utf-8',
},
body = message
})
settings.from = from
settings.rcpt = to
return smtp.send(settings)
end
-- sunrise / sunset calculation
function rscalc(latitude, longitude, when)
local pi = math.pi
local doublepi = pi * 2
local rads = pi / 180.0
local TZ = function(when)
local ts = os.time(when)
local utcdate, localdate = os.date('!*t', ts), os.date('*t', ts)
localdate.isdst = false
local diff = os.time(localdate) - os.time(utcdate)
return math.floor(diff / 60) / 60
end
local range = function(x)
local a = x / doublepi
local b = doublepi * (a - math.floor(a))
return b < 0 and (doublepi + b) or b
end
when = when or os.date('*t')
local y2k = { year = 2000, month = 1, day = 1 }
local y2kdays = math.floor((os.time(when) - os.time(y2k)) / 86400)
local meanlongitude = range(280.461 * rads + 0.9856474 * rads * y2kdays)
local meananomaly = range(357.528 * rads + 0.9856003 * rads * y2kdays)
local lambda = range(meanlongitude + 1.915 * rads * math.sin(meananomaly) + rads / 50 * math.sin(2 * meananomaly))
local obliq = 23.439 * rads - y2kdays * rads / 2500000
local alpha = math.atan2(math.cos(obliq) * math.sin(lambda), math.cos(lambda))
local declination = math.asin(math.sin(obliq) * math.sin(lambda))
local LL = meanlongitude - alpha
if meanlongitude < pi then
LL = LL + doublepi
end
local dfo = pi / 216.45
if latitude < 0 then
dfo = -dfo
end
local fo = math.tan(declination + dfo) * math.tan(latitude * rads)
-- polar day / night
if fo < -1 or fo > 1 then
return 1, 23 * 60 + 59
end
local ha = 12 * math.asin(fo) / pi + 6
local timezone = TZ(when)
local equation = 12 + timezone + 24 * (1 - LL / doublepi) - longitude / 15
local sunrise, sunset = equation - ha, equation + ha
if sunrise > 24 then
sunrise = sunrise - 24
end
if sunset > 24 then
sunset = sunset - 24
end
return math.floor(sunrise * 60), math.ceil(sunset * 60)
end
function get_st_add(ga)
new_ga = ""
gas = string.split(ga, "/")
gas[3] = tonumber(gas[3])+1
--log(gas)
for k,v in ipairs(gas) do
if k == 3 then
new_ga = new_ga..tostring(v)
else
new_ga = new_ga..v.."/"
end
end
return new_ga
end
--- BEFORE SUNRISE ---
function before_sr(offset) --- offset= min_offset
off= offset.hour * 60 + offset.min
sr,ss = rscalc(44.060, 11.844)
t = os.date("*t")
time = t.hour*60 + t.min
if sr - off > time then
return true
else
return false
end
end
--- AFTER SUNSET ---
function after_ss(offset)
off= offset.hour * 60 + offset.min
sr,ss = rscalc(44.060, 11.844)
t = os.date("*t")
time = t.hour*60 + t.min
if ss + off < time then
return true
else
return false
end
end
function birthday(compleanno)
d = os.date("*t")
birth = grp.getvalue(compleanno)
if d.year < birth.year then
birth.year = birth.year - 100
end
if d.day == birth.day and d.month == birth.month then
return {true, d.year-birth.year}
else
return {false, d.year-birth.year}
end
end
--- LED CHE GUIDANO VERSO LE POSTAZIONI ---
function guide_me(n_led, colore, lampeggio, range, time_step, path, debug, ip)
if range == nil then range = 15 end
if time_step == nil then time_step = 0.5 end
if path == nil then path = "i" end
if debug == nil then debug = false end
if lampeggio == nil then lampeggio = true end
if ip == nil then ip = "10.11.24.115" end
step = math.ceil(n_led/range)
----- RICHIESTA POST HTTP GENERICA -----
function basic_post_req(body_req)
res, err = http.request({
url = 'http://'..ip..'/json/state',
method = "POST",
body = body_req,
headers = {["Content-Type"] = "text/plain"}
})
return res,err
end
if path == "i" then
for i = 1, step, 1 do -- es. 13 step
--basic_post_req('{"on": true, "bri": 10}')
led_on = {start = range*(i-1), stop = range*i}
led_off = {start = 0, stop = range*(i-1)}
if debug == true then log(led_on, led_off, i) end
if i == step and lampeggio then
for k = 1, 3, 1 do
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "bri": 255, tt: 500}')
os.sleep(1)
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "bri": 50, tt: 500}')
os.sleep(1)
end
elseif i == 1 then
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "on": true, bri: 150}')
os.sleep(time_step)
else
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], bri: 150}')
os.sleep(time_step)
end
end
else
for i = 1, step, 1 do -- es. 13 step
--basic_post_req('{"on": true, "bri": 10}')
led_on = {start = range*(i-1)+140, stop = range*i+140}
led_off = {start = range*(i-2)+140, stop = range*(i-1)+140}
if debug == true then log(led_on, led_off, i) end
if i == step and lampeggio then
for k = 1, 3, 1 do
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "bri": 255, tt: 500}')
os.sleep(1)
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "bri": 50, tt: 500}')
os.sleep(1)
end
elseif i == 1 then
led_on = {start = range*(i-1)+140, stop = range*i+140}
led_off = {start = 0, stop = range*(i-1)+140}
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], "on": true, bri: 150}')
os.sleep(time_step)
else
basic_post_req('{"seg":[{"i":['..led_off.start..', '..led_off.stop..', "000000", '..led_on.start..', '..led_on.stop..', "'..colore..'"]}], bri: 150}')
os.sleep(time_step)
end
end
end
basic_post_req('{"on": false}')
end
function broadcast_c4()
pl_select = grp.tag("PLAYER_SELECT_AUDIO")
mute = grp.tag("PLAYER_MUTE_AUDIO")
vol = grp.tag("VOLUME_AUDIO")
if not grp.getvalue("5/1/104") then
for i, o in ipairs(pl_select) do
--grp.write(o.address, 6)
grp.write(mute[i].address, false)
grp.write(vol[i].address, 40)
end
else
for i, o in ipairs(pl_select) do
if not i == 8 then
--grp.write(o.address, 6)
grp.write(mute[i].address, false)
--grp.write(vol[i].address, 40)
end
end
end
end
function no_martini_no_party_bd(colore, value)
player_select = {"7/2/6", "7/2/26", "7/2/46", "7/2/66", "7/2/86", "7/2/106", "7/2/126", "7/2/146", "7/2/166"}
--colore = 0xff0000
tempo_canzone = 35
if value then
grp.write('1/2/1', false)
grp.write('0/1/1', true)
--os.sleep(1)
--broadcast_c4()
grp.write("7/0/2", 31)
grp.write('1/3/250', true)
grp.write('1/3/251', 100)
grp.write('1/3/254', 90)
grp.write('1/3/253', 0)
grp.write('1/3/252', colore)
os.sleep(tempo_canzone)
grp.write('1/3/250', false)
if grp.getvalue('4/4/22') then
--grp.write("1/2/16", true)
grp.write("1/2/31", true)
else
grp.write("0/1/1", false)
end
end
end
function hex_c(val)
hexcolor = string.format("%X",tostring(val))
while hexcolor:len() < 6 do
hexcolor = "0"..hexcolor
end
return hexcolor
end
function blink(light, time, range, bool)
if bool == nil then
step = math.floor(time/range)
for i=1,step, 1 do
grp.write(light, true)
os.sleep(range)
grp.write(light, false)
os.sleep(range)
end
else
while bool do
grp.write(light, true)
os.sleep(range)
grp.write(light, false)
os.sleep(range)
end
end
end
function last_d_month(month, year)
bis = tonumber(year)%4
if bis == 0 then
d_m = {31,29,31,30,31,30,31,31,30,31,30,31}
return d_m[tonumber(month)]
else
d_m = {31,28,31,30,31,30,31,31,30,31,30,31}
return d_m[tonumber(month)]
end
end