Created
July 26, 2011 14:21
-
-
Save axeda/1106869 to your computer and use it in GitHub Desktop.
Asset Association
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script to run from a rule, associates the device running the rule to the provided model/serialnumber | |
*/ | |
import com.axeda.drm.sdk.Context | |
import com.axeda.drm.sdk.device.* | |
import com.axeda.drm.sdk.m2m.* | |
try { | |
Context ctx = Context.create() | |
// get the parameters | |
String serialNumber = parameters.serialNumber | |
String modelname = parameters.model | |
//logger.info "serial ${serialNumber} and model ${modelname}" | |
// find the model by name | |
ModelFinder modelFinder = new ModelFinder(ctx) | |
modelFinder.setName modelname | |
Model model = modelFinder.find() | |
// get this asset | |
Device asset = getAsset(ctx, serialNumber, model) | |
//logger.info "asset ${asset.name} ${asset.model.name}" | |
// associate the asset to this one | |
DeviceAssociation da = associateAsset(ctx, context.device, asset) | |
} catch (Exception e) { | |
logger.error e.message | |
} | |
return true | |
// gets an existing asset, or creates it if needed | |
def Device getAsset(Context ctx, String serial, Model model) | |
{ | |
Device asset = null | |
try | |
{ | |
if(model==null) | |
return null; | |
DeviceFinder deviceFinder = new DeviceFinder(ctx) | |
deviceFinder.setModel model | |
deviceFinder.setSerialNumber serial | |
asset = deviceFinder.find() | |
if (asset ==null) | |
{ | |
asset = new Device(ctx, serial, model) | |
asset?.store() | |
} | |
} | |
catch (Exception e) | |
{ | |
logger.error e.message | |
} | |
return asset; | |
} | |
// ================== | |
def DeviceAssociation associateAsset(Context ctx, Device source, Device destination) | |
{ | |
//logger.info "associateAsset ${source} to ${destination}" | |
DeviceAssociation da = null | |
try | |
{ | |
da = new DeviceAssociation(ctx, source.id, destination.id) | |
da.store() | |
} | |
catch (Exception e) | |
{ | |
logger.error e.message | |
} | |
return da | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment