Search in sources :

Example 1 with NXCPDataInputStream

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;
}
Also used : DciDataRow(org.netxms.client.datacollection.DciDataRow) AtomicLong(java.util.concurrent.atomic.AtomicLong) DataType(org.netxms.client.constants.DataType) DataCollectionObject(org.netxms.client.datacollection.DataCollectionObject) NetworkMapObject(org.netxms.client.maps.elements.NetworkMapObject) WinPerfObject(org.netxms.client.datacollection.WinPerfObject) AbstractUserObject(org.netxms.client.users.AbstractUserObject) AbstractObject(org.netxms.client.objects.AbstractObject) EventObject(org.netxms.client.events.EventObject) UnknownObject(org.netxms.client.objects.UnknownObject) GenericObject(org.netxms.client.objects.GenericObject) IOException(java.io.IOException) ConnectionPoint(org.netxms.client.topology.ConnectionPoint) AccessPoint(org.netxms.client.objects.AccessPoint) Date(java.util.Date) NXCPDataInputStream(org.netxms.base.NXCPDataInputStream)

Aggregations

IOException (java.io.IOException)1 Date (java.util.Date)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 NXCPDataInputStream (org.netxms.base.NXCPDataInputStream)1 DataType (org.netxms.client.constants.DataType)1 DataCollectionObject (org.netxms.client.datacollection.DataCollectionObject)1 DciDataRow (org.netxms.client.datacollection.DciDataRow)1 WinPerfObject (org.netxms.client.datacollection.WinPerfObject)1 EventObject (org.netxms.client.events.EventObject)1 NetworkMapObject (org.netxms.client.maps.elements.NetworkMapObject)1 AbstractObject (org.netxms.client.objects.AbstractObject)1 AccessPoint (org.netxms.client.objects.AccessPoint)1 GenericObject (org.netxms.client.objects.GenericObject)1 UnknownObject (org.netxms.client.objects.UnknownObject)1 ConnectionPoint (org.netxms.client.topology.ConnectionPoint)1 AbstractUserObject (org.netxms.client.users.AbstractUserObject)1