Send Alarms To Platform In XML File

    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

    }