Axeda Groovy Scripto: Get Asset Alarms

    This script will return, in XML format, details of all alarms 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 id parameter will be supplied as an argument to the call. You should define this script as a Custom Object of type Action.

     

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

     

    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()]
    }