Question
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
- import com.axeda.drm.sdk.Context
- import com.axeda.drm.sdk.device.DeviceFinder
- import com.axeda.drm.sdk.device.ModelFinder
- import com.axeda.drm.sdk.device.DataItemFinder
- import com.axeda.drm.sdk.device.DataItem
- import com.axeda.drm.sdk.data.DataValueEntry
- import java.util.regex.Pattern
- import groovy.json.*
- import com.axeda.drm.services.device.DataItemType
- import net.sf.json.JSONObject
- /**
- * CSVToData.groovy
- * -----------------------
- *
- * Executed from an expression rule with file hint "datainsert", takes a CSV file and adds data based on values.
- *
- * @note There must be a column with "model" and one with "serial". The rest of the columns should be data item names with values
- * 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.
- * Solution would be to add a library for CSV parsing such as open csv.
- *
- * @params - only needed if NOT executed from expression rule - primarily for debugging
- * modelName - (OPTIONAL) Str - name of the model
- * serialNumber - (OPTIONAL) Str - name of the serial number
- *
- *
- */
- /**
- * initialize our global variables
- * json = the contents of our response
- * infoString = a stringBuilder used to collect debug information during the script
- * contentType = the content type we will return
- * scriptname = The name of this Script, used in multiple places
- */
- def json = new groovy.json.JsonBuilder()
- def infoString = new StringBuilder()
- def contentType = "application/json"
- def scriptName = "CSVToData.groovy"
- def root = ["result":["items":[]]]
- def columns = []
- try {
- Context CONTEXT = Context.getSDKContext()
- def modelIndex
- def serialIndex
- // initialize Model and Device Finders
- ModelFinder modelFinder = new ModelFinder(CONTEXT)
- DeviceFinder deviceFinder = new DeviceFinder(CONTEXT)
- // implicit object compressedFile
- File file = compressedFile.getFiles()[0].extractFile()
- /* //begin non-expression rule code, useful for debugging
- File file
- modelFinder.setName(Request.parameters.modelname)
- def model1 = modelFinder.find()
- deviceFinder.setSerialNumber(Request.parameters.serialNumber)
- deviceFinder.setModel(model1)
- def d = deviceFinder.find()
- UploadedFileFinder uff = new UploadedFileFinder(CONTEXT)
- uff.device = d
- def ufiles = uff.findAll()
- UploadedFile ufile
- if (ufiles.size() > 0) {
- ufile = ufiles[0]
- file = ufile.extractFile()
- }
- */ //end non-expression rule code
- file.eachLine {line ->
- def row = line.tokenize(',')
- // set the column headings
- if (columns.size() == 0){
- columns = row
- // find model and serial index, assumes there's a column that has "model" and "serial", otherwise take columns 0 and 1
- def modelpatt = Pattern.compile(/[A-Za-z_\-]{0,}model[A-Za-z_\-]{0,}/, Pattern.CASE_INSENSITIVE)
- def serialpatt = Pattern.compile(/[A-Za-z_\-]{0,}serial[A-Za-z_\-]{0,}/, Pattern.CASE_INSENSITIVE)
- modelIndex = columns.findIndexOf{ it ==~ modelpatt } > -1 ? columns.findIndexOf{ it ==~ modelpatt } : 0
- serialIndex = columns.findIndexOf{ it ==~ serialpatt } > -1 ? columns.findIndexOf{ it ==~ serialpatt } : 1
- }
- // otherwise populate data
- else {
- modelFinder.setName(row.get(modelIndex))
- def model = modelFinder.find()
- deviceFinder.setModel(model)
- deviceFinder.setSerialNumber(row.get(serialIndex))
- def device = deviceFinder.find()
- def assetInfo = [
- "model": model.name,
- "serial": device.serialNumber,
- "data":[]
- ]
- row.eachWithIndex{ item, index ->
- if (index != modelIndex && index != serialIndex){
- def dataItemName = columns[index].replace(" ","")
- DataItemFinder dif = new DataItemFinder(CONTEXT);
- dif.setDataItemName(dataItemName);
- dif.setModel(model);
- DataItem dataItem = dif.find();
- if (dataItem){
- if (item.isNumber()){
- item = Double.valueOf(item)
- }
- DataValueEntry dve = new DataValueEntry(CONTEXT, device, dataItem, item)
- dve.store()
- }
- else {
- DataItem newDataItem
- if (item.isNumber()){
- newDataItem = new DataItem(CONTEXT, model,DataItemType.ANALOG, dataItemName)
- item = Double.valueOf(item)
- }
- else {
- newDataItem = new DataItem(CONTEXT, model,DataItemType.STRING, dataItemName)
- }
- newDataItem.store()
- DataValueEntry dve = new DataValueEntry(CONTEXT, device, newDataItem, item)
- dve.store()
- }
- assetInfo.data << [
- "name": dataItemName,
- "value": item
- ]
- }
- root.result.items << assetInfo
- }
- }
- }
- logger.info(JSONObject.fromObject(root).toString(2))
- } catch (Exception e) {
- processException(scriptName,json,e)
- }
- //return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(root).toString(2)]
- /*
- Processes the contents of an Exception and add it to the Errors collection
- @param json The markup builder
- */
- private def processException(String scriptName, JsonBuilder json, Exception e) {
- // catch the exception output
- def logStringWriter = new StringWriter()
- e.printStackTrace(new PrintWriter(logStringWriter))
- logger.error("Exception occurred in ${scriptName}: ${logStringWriter.toString()}")
- /*
- Construct the error response
- - errorCode Will be an element from an agreed upon enum
- - errorMessage The text of the exception
- */
- json.errors {
- error {
- message "[${scriptName}]: " + e.getMessage()
- timestamp "${System.currentTimeMillis()}"
- }
- }
- return json
- }

