Skip to main content
10-Marble
June 12, 2014
Question

Send Alarms To Platform In XML File

  • June 12, 2014
  • 3 replies
  • 1634 views

I needed this recently but couldn't lay hands on an end-to-end XML format and scripts.  This may not be the cleanest or most robust for error checking, but it works and may be handy to others.  BTW, this is based on the 6.6 Artisan archetype.  I used the helloworld example and plugged this in being in a hurry.

XML Format:

<alarms>

        <!-- This has all the supported attributes -->

        <alarm

                name="alarm1"

                description="your description here"

                severity="123"

                notes="a note"

                extended_data="data"

        />

        <!-- These are the minimum required attributes -->

        <alarm

                name="alarm2"

                description="DESCRIPTION"

                severity="500"

        />

</alarms>

Use this XML format.  Set a file watcher on your agent or otherwise upload with the hint "alarmxmlfile" and the rule/script will process this uploaded file into alarms.  One for each alarm element.

Rule:

  <rule>

          <name>AlarmFileParseRule</name>

          <description>Parses XML file to alarms.</description>

          <enabled>true</enabled>

          <applyToAll>false</applyToAll>

          <type>File</type>

          <ifExpression><![CDATA[has("alarmxmlfile",File.hint)]]></ifExpression>

          <thenExpression><![CDATA[ExecuteCustomObject("AlarmFileParseScript")]]></thenExpression>

          <consecutive>true</consecutive>

          <models>

              <model>YOUR MODEL HERE</model>

          </models>

      </rule>

Script:

package com.yourcompany

import com.axeda.drm.sdk.Context

import com.axeda.drm.sdk.data.AlarmEntry

import com.axeda.drm.sdk.data.UploadedFile

import groovy.util.XmlSlurper

import java.util.Date

try

{

    UploadedFile[] files = compressedFile.getFiles()

    File file = files[0]?.extractFile()

    assert file != null

    Date myDate = new Date()

    def fileContent = file.getText();

    def userContext = Context.create()

    def xmlMessage = new XmlSlurper().parseText(fileContent)

    xmlMessage.alarm.each {

        AlarmEntry alarmEntry = new AlarmEntry(userContext,context.device, it.@name.toString(), it.@description.toString(), it.@severity.toInteger(), myDate, true, "", "", null, null, null, it.@notes.toString(), it.@extended_data.toString())

        alarmEntry.store()

    }

}

catch (Exception e)

{

    log("Exception: " + e.message);

}

// ===============================================================================

//  HELPER FUNCTIONS

// ===============================================================================

def log(message)

{

    addAuditEntry(message)

}

def addAuditEntry(message) {

    scriptName = "AlarmFileParseScript"

    message = scriptName + ": " + message

    logger.info message

}

    3 replies

    10-Marble
    July 3, 2014

    Hi Mike!

    Thanks so much for sharing, you sparked a lot of interesting questions and possible solutions to other problems,

    Can I ask what your use case for this was, and why you chose not to raise the Alarms directly? This could be a great integrator tool, but as there is no device element to set this context I guess this wasn't your objective,

    Thanks again,

    Alan

    10-Marble
    July 7, 2014

    In this case there was already a mechanism in place for detecting the alarms locally, but it did not send those alarms home.  The desire was to cause the agent to send up the alarms on behalf of the monitoring process.  Another way this could have been done was via web services integration, but in this case that was not a workable option for other reasons.  So we fell back to an XML format. 

    There are probably half a dozen other ways you could do this and many of those options are possibly (likely?) better than this example, but this gets the job done for this particular use case.  You could use essentially the same format to batch up events, data, etc. as well.  I'm not suggesting this as a solution, just providing it as a reference example

    10-Marble
    July 8, 2014

    Perfect!

    Thanks Mike, it's a good example and great to see a working example of the many ways to use the platform,