• Recent
    • Tags
    • Popular
    • Register
    • Login

    Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

    Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website Mango 5 Documentation Website

    Email Last Day's Events

    Scripting general Discussion
    3
    4
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • phildunlapP
      phildunlap
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • phildunlapP
        phildunlap
        last edited by phildunlap

        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>
        
        seanS 1 Reply Last reply Reply Quote 0
        • seanS
          sean @phildunlap
          last edited by

          @phildunlap Phildunlap
          In the recipients = ["you@doman.extension"];
          0_1599538791554_1599538752056.jpg
          how to use mailling list like XID_ourGroupMailList this mail group in here?

          1 Reply Last reply Reply Quote 0
          • MattFoxM
            MattFox
            last edited by MattFox

            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

            Do not follow where the path may lead; go instead where there is no path.
            And leave a trail - Muriel Strode

            1 Reply Last reply Reply Quote 0
            • First post
              Last post