cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Axeda Groovy Script: Update properties on a model

Highlighted
Marble

Axeda Groovy Script: Update properties on a model

Update properties on a model.

Parameter:

  1. modelName (REQUIRED) - String - the name of the model to have its properties updated.

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

}

Tags (2)