def load(self, data):
		"""
		Parses data buffer to set member variables.

		data : list of bytes

		Raises exception when parsing fails.
		"""
		streamBuf = BufferReader(data)

		samplesTypeVal = streamBuf.getUInt8()
		self.samplesType = PowerSamplesType(samplesTypeVal) # Throws exception of value is not in enum
		self.index = streamBuf.getUInt8()
		self.count = streamBuf.getUInt16()
		self.timestamp = streamBuf.getUInt32()
		self.delayUs = streamBuf.getUInt16()
		self.sampleIntervalUs = streamBuf.getUInt16()
		streamBuf.skip(2)
		self.offset = streamBuf.getInt16()
		self.multiplier = streamBuf.getFloat()
		self.samples = []
		for i in range(0, self.count):
			self.samples.append(streamBuf.getInt16())
Пример #2
0
    def _getElementValues(
            self,
            elementType: int,  # TODO: make enum
            elementSize: int,
            elementData: list) -> list:
        bufferReader = BufferReader(elementData)
        dataSize = len(elementData)
        if dataSize % elementSize != 0:
            _LOGGER.warning(
                f"Remaining data with element size of {elementSize} and element data of size {dataSize}"
            )
            return []

        vals = []
        numElements = int(dataSize / elementSize)
        _LOGGER.debug(
            f"dataSize={dataSize} elementSize={elementSize} numElements={numElements}"
        )
        for i in range(0, numElements):
            if elementType == 0:
                # Signed integer
                if elementSize == 1:
                    vals.append(bufferReader.getInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 1:
                # Unsigned integer
                if elementSize == 1:
                    vals.append(bufferReader.getUInt8())
                elif elementSize == 2:
                    vals.append(bufferReader.getUInt16())
                elif elementSize == 4:
                    vals.append(bufferReader.getUInt32())
                elif elementSize == 8:
                    vals.append(bufferReader.getUInt64())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []

            elif elementType == 2:
                # Floating point
                if elementSize == 4:
                    vals.append(bufferReader.getFloat())
                else:
                    _LOGGER.warning(
                        f"Unknown type: element with type {elementType} and size {elementSize}"
                    )
                    return []
        return vals