cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Axeda Groovy Scripto: Get DataItems and Alarms for selected Asset

No ratings

This script will return, in XML format, the alarms, location information and some data for a particular asset, identified by serial number.

It is designed to be called as a web service, in which case the username parameter will be supplied by the platform from the user authentication information passed in the web service call, and the devicename parameter will be passed as an argument to the call.

You can test this script in the Groovy development environment on the platform by explicitly supplying the username and devicename parameters (i.e., your email address and "asset1").

The code below will filter data items and return values for the data item "value1".

import com.axeda.drm.sdk.Context;

import com.axeda.drm.sdk.device.DeviceFinder;

import com.axeda.drm.sdk.device.Device;

import com.axeda.drm.sdk.data.AlarmFinder;

import com.axeda.drm.sdk.data.Alarm;

import com.axeda.drm.sdk.mobilelocation.MobileLocation;

import com.axeda.common.sdk.jdbc.StringQuery;

import com.axeda.common.sdk.id.Identifier;

import groovy.xml.MarkupBuilder;

try {

  logger.info "parameters: " + parameters

  if (!parameters.id) {

    throw new Exception("parameters.id required");

  }

  // operate in the context of the user calling the service

  Context ctx = Context.create(parameters.username);

  // setup the finders

  DeviceFinder df = new DeviceFinder(ctx);

  df.id = new Identifier(Long.parseLong(parameters.id));

  // find the device and its data

  logger.info "Finding asset"

  Device device = df.find();

  if (!device) {

    throw new Exception("Unable to find asset with id "+ parameters.id);

  }

  AlarmFinder af = new AlarmFinder(ctx);

  af.device = device;

  // generate the XML

  writer = new StringWriter();

  xml = new MarkupBuilder(writer);

  xml.Alarms() {

    af.findAll().each() { Alarm alarm ->

      xml.Alarm('id':alarm.id) {

        xml.DataItemName(alarm.dataItemName);

        xml.DataItemValue(alarm.dataItemValue);

        xml.Date(alarm.date.time);

        xml.Description(alarm.description);

        xml.MobileLocation() {

          MobileLocation ml = alarm.mobileLocation;

          if (ml) {

            xml.BeginTimeStamp(ml.beginTimeStamp);

            xml.EndTimeStamp(ml.endTimeStamp);

            xml.Lat(ml.lat);

            xml.Lng(ml.lng);

            xml.Alt(ml.alt);

            xml.IsCurrentLocation(ml.isCurrentLocation());

          }

        }

        xml.Name(alarm.name);

        xml.Note(alarm.note);

        xml.Severity(alarm.severity);

        xml.State(alarm.state);

      }

    }

  }

  // return the results

  return ['Content-Type': 'text/xml', 'Content': writer.toString()]

} catch (Exception ex) {

  // return the exception

  writer = new StringWriter();

  xml = new MarkupBuilder(writer);

  xml.error() {

    faultcode("ErrorType.Exception");

    faultstring(ex.message);

  }

  return ['Content-Type': 'text/xml', 'Content': writer.toString()]

}

Version history
Last update:
‎Jun 01, 2016 10:11 AM
Updated by:
Labels (2)