use of org.netxms.base.NXCPDataInputStream in project netxms by netxms.
the class NXCSession method parseDataRows.
/**
* Parse data from raw message CMD_DCI_DATA.
* This method is intended for calling by client internal methods only. It made public to
* allow access from client extensions.
*
* @param input Raw data
* @param data Data object to add rows to
* @return number of received data rows
*/
public int parseDataRows(final byte[] input, DciData data) {
final NXCPDataInputStream inputStream = new NXCPDataInputStream(input);
int rows = 0;
DciDataRow row = null;
try {
// DCI ID
inputStream.skipBytes(4);
rows = inputStream.readInt();
final DataType dataType = DataType.getByValue(inputStream.readInt());
data.setDataType(dataType);
// padding
inputStream.skipBytes(4);
for (int i = 0; i < rows; i++) {
// convert to milliseconds
long timestamp = inputStream.readUnsignedInt() * 1000;
Object value;
switch(dataType) {
case INT32:
value = new Long(inputStream.readInt());
break;
case UINT32:
case COUNTER32:
value = new Long(inputStream.readUnsignedInt());
break;
case INT64:
case UINT64:
case COUNTER64:
// padding
inputStream.skipBytes(4);
value = new Long(inputStream.readLong());
break;
case FLOAT:
// padding
inputStream.skipBytes(4);
value = new Double(inputStream.readDouble());
break;
case STRING:
StringBuilder sb = new StringBuilder(256);
int count;
for (count = MAX_DCI_STRING_VALUE_LENGTH; count > 0; count--) {
char ch = inputStream.readChar();
if (ch == 0) {
count--;
break;
}
sb.append(ch);
}
inputStream.skipBytes(count * 2);
value = sb.toString();
break;
default:
value = null;
break;
}
if (timestamp > 0) {
row = new DciDataRow(new Date(timestamp), value);
data.addDataRow(row);
} else {
// raw value for previous entry
if (row != null)
row.setRawValue(value);
}
}
} catch (IOException e) {
}
inputStream.close();
return rows;
}
Aggregations