Axeda Groovy Script: Copy Data Item value to Asset Property value

    This Groovy script takes any dataitem values and writes them to properties of the same name - if they exist. The rule to call this script needs to be a data trigger such as:

     

    If: some condition

    Then: ExecuteCustomObject("CopyParameters")

     

    The script uses the default context that contains an asset (device) and the default parameter dataItems that contains the current reported dataitems (from an agent)

     

    import com.axeda.drm.sdk.user.User
    import com.axeda.drm.sdk.data.DataValue
    import groovy.lang.PropertyValue
    import com.axeda.drm.sdk.device.DevicePropertyFinder
    import com.axeda.drm.sdk.device.Property
    import com.axeda.drm.sdk.device.PropertyType
    import com.axeda.drm.sdk.device.DeviceProperty
    
    logger.info "Executing groovy script for device: " + context?.device?.serialNumber
    
    if (dataItems != null)
    {
      logger.info "** Data Items **"
      // show data item values
      dataItems?.each {di ->
        logger.info "dataitem: ${di.name} = ${di.value} = ${di.timestamp}"
    
    }
      def dataItemMap = [:]
      dataItems.each{ dataItemMap[it.name] = it }
    
      DevicePropertyFinder dpf = new DevicePropertyFinder (context.context)
      dpf.type = PropertyType.DEVICE_TYPE
      dpf.id = context.device.id
    
      DeviceProperty dp = dpf.findOne()
      List<Property> props = dp.getProperties()
      props.each {Property prop->
        if (dataItemMap.containsKey(prop.name)) {
          prop.value = dataItemMap[prop.name].value?.toString()
          //logger.info "Setting ${prop.name} to ${dataItemMap[prop.name].value?.toString()}"
        }
      }
    
      dp.store()
    
    }