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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Error while parsing CSV file in Axeda

hraj1
1-Visitor

Error while parsing CSV file in Axeda

Hi

I am using the below code to parse a CSV file but when I run the code in Axeda Server 6.5 I get the error as groovy.lang.MissingPropertyException: No such property: compressedFile for class

  1. import com.axeda.drm.sdk.Context 
  2. import com.axeda.drm.sdk.device.DeviceFinder 
  3. import com.axeda.drm.sdk.device.ModelFinder 
  4. import com.axeda.drm.sdk.device.DataItemFinder 
  5. import com.axeda.drm.sdk.device.DataItem 
  6. import com.axeda.drm.sdk.data.DataValueEntry 
  7. import java.util.regex.Pattern 
  8. import groovy.json.* 
  9. import com.axeda.drm.services.device.DataItemType 
  10. import net.sf.json.JSONObject 
  11.  
  12. /**
  13. * CSVToData.groovy
  14. * -----------------------
  15. *
  16. * Executed from an expression rule with file hint "datainsert", takes a CSV file and adds data based on values.
  17. *
  18. * @note  There must be a column with "model" and one with "serial".  The rest of the columns should be data item names with values
  19. * in the rows. DOES NOT handle null values in CSV.  Workaround is to insert blank spaces in null values and test for those on the Groovy side.
  20. * Solution would be to add a library for CSV parsing such as open csv.
  21. *
  22. * @params - only needed if NOT executed from expression rule - primarily for debugging
  23. * modelName - (OPTIONAL) Str - name of the model
  24. * serialNumber - (OPTIONAL) Str - name of the serial number
  25. *
  26. *
  27. */ 
  28.  
  29. /**
  30. * initialize our global variables
  31. * json = the contents of our response
  32. * infoString = a stringBuilder used to collect debug information during the script
  33. * contentType = the content type we will return
  34. * scriptname = The name of this Script, used in multiple places
  35. */ 
  36. def json = new groovy.json.JsonBuilder() 
  37. def infoString = new StringBuilder() 
  38. def contentType = "application/json" 
  39. def scriptName = "CSVToData.groovy" 
  40. def root = ["result":["items":[]]] 
  41.  
  42. def columns = [] 
  43.  
  44. try
  45.  
  46.   Context CONTEXT = Context.getSDKContext() 
  47.  
  48.   def modelIndex 
  49.   def serialIndex 
  50.  
  51.   // initialize Model and Device Finders 
  52.   ModelFinder modelFinder = new ModelFinder(CONTEXT) 
  53.   DeviceFinder deviceFinder = new DeviceFinder(CONTEXT) 
  54.  
  55.   // implicit object compressedFile 
  56.   File file = compressedFile.getFiles()[0].extractFile() 
  57.  
  58. /* //begin non-expression rule code, useful for debugging
  59.     File file
  60.     modelFinder.setName(Request.parameters.modelname)
  61.               def model1 = modelFinder.find()
  62.     deviceFinder.setSerialNumber(Request.parameters.serialNumber)
  63.     deviceFinder.setModel(model1)
  64.     def d = deviceFinder.find()
  65.      UploadedFileFinder uff = new UploadedFileFinder(CONTEXT)
  66.     uff.device = d
  67.     def ufiles = uff.findAll()
  68.     UploadedFile ufile
  69.     if (ufiles.size() > 0) {
  70.         ufile = ufiles[0]
  71.         file = ufile.extractFile()
  72.     }
  73.          
  74. */ //end non-expression rule code 
  75.  
  76.   file.eachLine {line -> 
  77.       def row = line.tokenize(','
  78.      
  79.       // set the column headings 
  80.       if (columns.size() == 0){ 
  81.         columns = row 
  82.        
  83.         // find model and serial index, assumes there's a column that has "model" and "serial", otherwise take columns 0 and 1 
  84.         def modelpatt = Pattern.compile(/[A-Za-z_\-]{0,}model[A-Za-z_\-]{0,}/, Pattern.CASE_INSENSITIVE) 
  85.         def serialpatt = Pattern.compile(/[A-Za-z_\-]{0,}serial[A-Za-z_\-]{0,}/, Pattern.CASE_INSENSITIVE) 
  86.         modelIndex = columns.findIndexOf{ it ==~ modelpatt } > -1 ? columns.findIndexOf{ it ==~ modelpatt } : 0 
  87.         serialIndex = columns.findIndexOf{ it ==~ serialpatt } > -1 ? columns.findIndexOf{ it ==~ serialpatt } : 1 
  88.        
  89.       } 
  90.       // otherwise populate data 
  91.       else
  92.          
  93.           modelFinder.setName(row.get(modelIndex)) 
  94.           def model = modelFinder.find() 
  95.          
  96.           deviceFinder.setModel(model) 
  97.           deviceFinder.setSerialNumber(row.get(serialIndex)) 
  98.          
  99.           def device = deviceFinder.find() 
  100.          
  101.           def assetInfo = [ 
  102.                     "model": model.name, 
  103.                     "serial": device.serialNumber, 
  104.                     "data":[] 
  105.                     ] 
  106.          
  107.           row.eachWithIndex{ item, index -> 
  108.               if (index != modelIndex && index != serialIndex){ 
  109.                 def dataItemName = columns[index].replace(" ",""
  110.                 DataItemFinder dif = new DataItemFinder(CONTEXT); 
  111.                 dif.setDataItemName(dataItemName); 
  112.                 dif.setModel(model); 
  113.                 DataItem dataItem = dif.find(); 
  114.                
  115.                 if (dataItem){ 
  116.                     if (item.isNumber()){ 
  117.                        item = Double.valueOf(item) 
  118.                     } 
  119.                     DataValueEntry dve = new DataValueEntry(CONTEXT, device, dataItem, item) 
  120.                     dve.store() 
  121.                 } 
  122.                 else
  123.                     DataItem newDataItem 
  124.                     if (item.isNumber()){ 
  125.                         newDataItem = new DataItem(CONTEXT, model,DataItemType.ANALOG, dataItemName) 
  126.                         item = Double.valueOf(item) 
  127.                     } 
  128.                     else
  129.                        newDataItem = new DataItem(CONTEXT, model,DataItemType.STRING, dataItemName) 
  130.                     } 
  131.                    newDataItem.store() 
  132.                    DataValueEntry dve = new DataValueEntry(CONTEXT, device, newDataItem, item) 
  133.                     dve.store() 
  134.                 } 
  135.                 assetInfo.data << [ 
  136.                         "name": dataItemName, 
  137.                         "value": item 
  138.                     ] 
  139.                
  140.               } 
  141.               root.result.items << assetInfo 
  142.           } 
  143.          
  144.       } 
  145.   } 
  146.   logger.info(JSONObject.fromObject(root).toString(2)) 
  147.  
  148. } catch (Exception e) { 
  149.  
  150.     processException(scriptName,json,e) 
  151.  
  152. //return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(root).toString(2)] 
  153.  
  154.  
  155.  
  156. /*
  157.     Processes the contents of an Exception and add it to the Errors collection
  158.     @param json The markup builder
  159. */ 
  160. private def processException(String scriptName, JsonBuilder json, Exception e) { 
  161.     // catch the exception output 
  162.     def logStringWriter = new StringWriter() 
  163.     e.printStackTrace(new PrintWriter(logStringWriter)) 
  164.     logger.error("Exception occurred in ${scriptName}: ${logStringWriter.toString()}"
  165.  
  166.     /*
  167.         Construct the error response
  168.         - errorCode Will be an element from an agreed upon enum
  169.         - errorMessage The text of the exception
  170.      */ 
  171.     json.errors  { 
  172.         error { 
  173.             message     "[${scriptName}]: " + e.getMessage() 
  174.             timestamp   "${System.currentTimeMillis()}" 
  175.         } 
  176.     } 
  177.  
  178.     return json 
1 REPLY 1
adharuman
1-Visitor
(To:hraj1)

Try importing com.axeda.drm.sdk.scm.CompressedFile


import com.axeda.drm.sdk.scm.CompressedFile

Regards

Arunkumar D

http://www.arundhaj.com/

Announcements


Top Tags