Email Last Day's Events
-
It was asked of me how one would send the previous day's Data Point events in an email. One can achieve this pretty easily with a Scripting data source and a custom FTL model, like,
Scripting data source script (will send an email at validation!):
var now = new Date().getTime(); var from = now - 86400000; var recipients = ["you@doman.extension"]; var eventList = com.serotonin.m2m2.db.dao.EventDao.instance.search(0, "DATA_POINT", null, -1, null, from, now, 1, com.serotonin.m2m2.Common.getTranslations(), java.lang.Integer.MIN_VALUE, java.lang.Integer.MAX_VALUE, null); var model = {"eventList": eventList, "CODES": com.serotonin.m2m2.rt.event.AlarmLevels.CODES}; var template = com.serotonin.m2m2.Common.freemarkerConfiguration.getTemplate("mytemplate.ftl"); var writer = new java.io.StringWriter(); template.process(model, writer); var emailContent = new com.serotonin.web.mail.EmailContent(null, writer.toString(), com.serotonin.m2m2.Common.UTF8); com.serotonin.m2m2.rt.maint.work.EmailWorkItem.queueEmail(recipients, "Event Summary", emailContent, null);
and then an email template like the following (saved in Mango/overrides/ftl, but you may need to restart if you didn't already have an overrides/ftl folder, or saved in Mango/ftl/)
<html> <head> <style> .rowHeader td { font-weight: bold; color: #FFFFFF; background-color: #F07800; text-align: center; white-space: nowrap; padding: 3px 10px 3px 10px; } .row td, .rowAlt td { color: #000000; padding: 3px; } .row td { background-color: #F0F0F0; } .rowAlt td { background-color: #DCDCDC; } </style> </head> <body> <#assign row = 1/> <table> <tr class="rowHeader"> <th>Alarm Level</th> <th>Time</th> <th>Message</th> <th>Status</th> <th>Acknowledged</th> <tr> <#list eventList as event> <#assign row = row + 1/> <#if row == 2><#assign row = 0/></#if> <tr class="row<#if row == 1>Alt</#if>"> <td>${CODES.getCode(event.getAlarmLevel())}</td> <td>${event.getPrettyActiveTimestamp()}</td> <td>${event.getMessageString()}</td> <td><#if event.isRtnApplicable() && !event.isActive()> ${event.getPrettyRtnTimestamp()} <#else>No RTN</#if></td> <td><#if event.isAcknowledged()> ${event.getFullPrettyAcknowledgedTimestamp()} <#else>No Ack</#if></td> </tr> </#list> </table> </body> </html>
One can see in the script that the name of my template file is
mytemplate.ftl
-
If anyone was using this script and noticed it break with 3.6, the issue is that the method signature for the search method changed slightly. Now to do the same thing we would call,
var eventList = com.serotonin.m2m2.db.dao.EventDao.instance.search(0, "DATA_POINT", null, null, null, from, now, 1, com.serotonin.m2m2.Common.getTranslations(), java.lang.Integer.MIN_VALUE, java.lang.Integer.MAX_VALUE, null); //the old -1 for alarm level to match became null due to refactoring the AlarmLevels class
The FTL changed too. Here's the script for 3.6
var now = new Date().getTime(); var from = now - 86400000; var recipients = ["you@doman.extension"]; var eventList = com.serotonin.m2m2.db.dao.EventDao.instance.search(0, "DATA_POINT", null, null, null, from, now, 1, com.serotonin.m2m2.Common.getTranslations(), java.lang.Integer.MIN_VALUE, java.lang.Integer.MAX_VALUE, null); var model = {"eventList": eventList}; var template = com.serotonin.m2m2.Common.freemarkerConfiguration.getTemplate("mytemplate.ftl"); var writer = new java.io.StringWriter(); template.process(model, writer); var emailContent = new com.serotonin.web.mail.EmailContent(null, writer.toString(), com.serotonin.m2m2.Common.UTF8); com.serotonin.m2m2.rt.maint.work.EmailWorkItem.queueEmail(recipients, "Event Summary", emailContent, null);
and here's the template:
<html> <head> <style> .rowHeader td { font-weight: bold; color: #FFFFFF; background-color: #F07800; text-align: center; white-space: nowrap; padding: 3px 10px 3px 10px; } .row td, .rowAlt td { color: #000000; padding: 3px; } .row td { background-color: #F0F0F0; } .rowAlt td { background-color: #DCDCDC; } </style> </head> <body> <#assign row = 1/> <table> <tr class="rowHeader"> <th>Alarm Level</th> <th>Time</th> <th>Message</th> <th>Status</th> <th>Acknowledged</th> <tr> <#list eventList as event> <#assign row = row + 1/> <#if row == 2><#assign row = 0/></#if> <tr class="row<#if row == 1>Alt</#if>"> <td>${event.getAlarmLevel().name()}</td> <td>${event.getPrettyActiveTimestamp()}</td> <td>${event.getMessageString()}</td> <td><#if event.isRtnApplicable() && !event.isActive()> ${event.getPrettyRtnTimestamp()} <#else>No RTN</#if></td> <td><#if event.isAcknowledged()> ${event.getFullPrettyAcknowledgedTimestamp()} <#else>No Ack</#if></td> </tr> </#list> </table> </body> </html>
-
@phildunlap Phildunlap
In therecipients = ["you@doman.extension"];
how to use mailling list like XID_ourGroupMailList this mail group in here? -
I think you are confusing a scripted datasource ftl script with the email mailing list.
Unless you are talking about using the email recipients, then yes they are stored as strings in an array.Fox