Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save axeda/1182247 to your computer and use it in GitHub Desktop.
Save axeda/1182247 to your computer and use it in GitHub Desktop.
Axeda Scripto Service Example - Add a Device to a DeviceGroup
import net.sf.json.JSONObject
import com.axeda.drm.sdk.device.DeviceGroupFinder
import com.axeda.drm.sdk.device.DeviceGroup
import com.axeda.drm.sdk.Context
import com.axeda.common.sdk.id.Identifier
import com.axeda.drm.sdk.device.DeviceFinder
def response = [:], status
try {
if (parameters.assetId == null) { throw new IllegalArgumentException("parameter 'assetId' was not provided.")}
if (parameters.groupName == null) { throw new IllegalArgumentException("parameter 'groupName was not provided.")}
final def CONTEXT = Context.create(parameters.username)
def dgf = new DeviceGroupFinder(CONTEXT)
dgf.setName(parameters.groupName)
def group = dgf.find()
if (group == null) {
logger.error "could not retrieve group with name of '${parameters.groupName}'"
throw new Exception("could not retrieve group with id of '${parameters.groupName}'")
}
def df = new DeviceFinder(CONTEXT)
df.setId(new Identifier(parameters.assetId))
def device = df.find()
if (device == null) {
logger.error "could not retrieve asset with id of '${parameters.assetId}'"
throw new Exception("could not retrieve asset with id of '${parameters.assetId}'")
}
group.addDevice(device)
group.store()
// do a check to make sure the device is associated with the group.
group = dgf.find()
def devices = group.getDevices()
status = devices.contains(device) ? "success" : "failure"
// prepare the response.
response = [parameters: parameters, status: status]
} catch (def e) {
logger.error e.getMessage()
response = [faultcode: e.getCause(), faultstring: e.getMessage()]
}
return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(response).toString(2)];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment