Last active
December 20, 2015 03:39
-
-
Save axeda/6064847 to your computer and use it in GitHub Desktop.
Update properties on a model
This file contains hidden or 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
| import com.axeda.drm.sdk.device.DeviceProperty | |
| import com.axeda.drm.sdk.device.ModelFinder | |
| import com.axeda.drm.sdk.device.Model | |
| import com.axeda.drm.sdk.Context | |
| import com.axeda.drm.sdk.device.DevicePropertyFinder | |
| import com.axeda.drm.sdk.device.Property | |
| import com.axeda.drm.sdk.device.PropertyType | |
| import com.axeda.common.sdk.id.Identifier | |
| Set<String> REQUIRED_PROPERTIES = [ | |
| "TestProperty0","TestProperty1","TestProperty2" | |
| ] | |
| try { | |
| final def Context CONTEXT = Context.getSDKContext() | |
| ModelFinder modelFinder = new ModelFinder(CONTEXT); | |
| modelFinder.setName(parameters.modelName) | |
| Model model = modelFinder.find(); | |
| if (model == null){ throw new Exception("No model found") } | |
| modelProperties = findModelProperties(CONTEXT, model.id) | |
| updateProperties(CONTEXT, model.id, modelProperties, REQUIRED_PROPERTIES) | |
| modelProperties.properties.each{ logger.info("$it.name :$it.value") } | |
| } | |
| catch (Exception e){ | |
| logger.info e.localizedMessage | |
| } | |
| return true | |
| private DeviceProperty findModelProperties(Context context, Identifier modelID) { | |
| def finder = new DevicePropertyFinder(context) | |
| finder.id = modelID | |
| finder.type = PropertyType.MODEL_TYPE | |
| return finder.findOne() as DeviceProperty | |
| } | |
| private void updateProperties(Context context, Identifier modelID, DeviceProperty modelProperties, Set<String> requiredProperties) { | |
| if (!modelProperties) { | |
| modelProperties = new DeviceProperty(context) | |
| modelProperties.id = modelID | |
| modelProperties.type = PropertyType.MODEL_TYPE | |
| modelProperties.properties = [] | |
| } | |
| (requiredProperties - new HashSet<String>(modelProperties.properties.collect { it.name })).inject(modelProperties.properties) { list, propertyName -> list << new Property(0, propertyName, "") } | |
| modelProperties.store() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment