23.06.2017, 15:39
(20.06.2017, 20:11)Erwin van der Zwart Wrote: Hi,
Use this as event based script attached to object 1/1/8:
It will generate a alert inside the alert tab, and that will be used in the alert app, can be downloaded from the appstore in FW 2.0.0.Code:if event.getvalue() == 1 then -- active
alert('There is a alarm with code ' .. grp.getvalue('1/1/9') .. ' at ' .. os.date("%c", grp.getvalue('1/1/6'))
end
There you will get a audio alert and a list with all available alerts.
To mail the alerts use this script as scheduled once a week: (make sure to enable 'less secure apps' in your gmail account to be able to mail and set DNS and default gateway on NIC)
Code:--Gmail (smtp) username !IMPORTANT!
user = 'YOUR EMAIL ADRESS'
--Gmail (smtp) password !IMPORTANT!
password = 'YOUR PASSWORD'
--Sender for e-mail
from = '<' .. user .. '>'
alias_from = 'YOUR ALIAS'
--Recipient for e-mail
to = '<receiver@domain.com>'
alias_to = 'receiver'
--Subject for e-mail
subjectpart1 = 'Alerts'
subjectpart2 = 'automaticly send by homeLYnk'
--Message on bottom of email (will only be showed when client don't understand attachment)
epilogue = 'End of message'
-- Get all alerts from DB
alerts_table = db:getall('SELECT * FROM alerts')
-- csv buffer
buffer = {}
-- format csv row
csv = string.format('%q,%q,%q', "ID", "ALERT", "ADDED TO LIST")
-- add to buffer
table.insert(buffer, csv)
-- add empty line to buffer
table.insert(buffer, "")
-- Loop through alerts_table
for _, alerts in ipairs(alerts_table) do
-- format csv row
csv = string.format('%q,%q,%q', alerts.id, alerts.alert, os.date("%x %X", alerts.alerttime))
-- add to buffer
table.insert(buffer, csv)
end
--Create table to include mail settings
local settings = {
from = from,
rcpt = to,
user = user,
password = password,
server = 'smtp.gmail.com',
port = 465,
secure = 'sslv23',
}
--Create attachment inside FTP server
src = 'AlertExport '.. os.date('%Y-%m-%d %H#%M#%S') .. '.csv'
dst = '/home/ftp/' .. src
io.writefile(dst, buffer)
--Create subject
subject = subjectpart1 .. ": " .. src .. " " .. subjectpart2
--Load required modules to send email with attachment
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")
--Create e-mail header
settings.source = smtp.message{
headers = {
from = '' .. alias_from .. ' ' .. from .. '',
to = '' .. alias_to .. ' ' .. to .. '',
subject = subject
},
--Load attachment inside body
body = {
preamble = "",
[1] = {
headers = {
["content-type"] = 'text/plain',
["content-disposition"] = 'attachment; filename="'..src..'"',
["content-description"] = '.. src ..',
["content-transfer-encoding"] = "BASE64",
},
body = ltn12.source.chain(
ltn12.source.file(io.open(dst, "rb")),
ltn12.filter.chain(
mime.encode("base64"),
mime.wrap()
)
)
},
epilogue = epilogue
}
}
--Send the email
r, e = smtp.send(settings)
--Create alert when sending gives an error with error message
if (e) then
log (e)
log (r)
alert("Could not send email: ", e, "\n")
end
--Delete created file from ftp folder inside HL
os.remove(dst)
Many thanks!
I'm going to implement it.
Best regards