Skip to content

Instantly share code, notes, and snippets.

@axeda
Created July 26, 2011 14:32
Show Gist options
  • Select an option

  • Save axeda/1106901 to your computer and use it in GitHub Desktop.

Select an option

Save axeda/1106901 to your computer and use it in GitHub Desktop.
Get Asset Alarms script
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()]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment