Exemplo n.º 1
0
    def test_uint32_from_bytes(self):
        val = np.uint32(32)
        buffer = val.tobytes()
        val2 = DataFormat.from_bytes(buffer, DataFormat.UINT32)
        self.assertEqual(val, val2)

        # Array
        val3 = np.zeros(40, dtype=np.uint32)
        val4 = DataFormat.from_bytes(val3.tobytes(), DataFormat.UINT32)
        self.assertTrue((val3 == val4).all())
Exemplo n.º 2
0
    def test_int16_from_bytes(self):
        val = np.int16(-16)
        buffer = val.tobytes()
        val2 = DataFormat.from_bytes(buffer, DataFormat.SINT16)
        self.assertEqual(val, val2)

        # Array
        val3 = np.zeros(40, dtype=np.int16)
        val4 = DataFormat.from_bytes(val3.tobytes(), DataFormat.SINT16)
        self.assertTrue((val3 == val4).all())
Exemplo n.º 3
0
    def test_float64_from_bytes(self):
        val = np.float64(64.0)
        buffer = val.tobytes()
        val2 = DataFormat.from_bytes(buffer, DataFormat.FLOAT64)
        self.assertEqual(val, val2)

        # Array
        val3 = np.zeros(40, dtype=np.float64)
        val4 = DataFormat.from_bytes(val3.tobytes(), DataFormat.FLOAT64)
        self.assertTrue((val3 == val4).all())
Exemplo n.º 4
0
    def get_sensor_data(self, id_sensor_data):

        query = self.session.query(SensorData).filter(SensorData.id_sensor_data == id_sensor_data)

        my_sensor_data = query.first()

        # Do something to convert bytes in the right format
        my_sensor_data.data = DataFormat.from_bytes(my_sensor_data.data, my_sensor_data.channel.id_data_format)

        return my_sensor_data
Exemplo n.º 5
0
    def get_all_sensor_data(self, **kwargs):

        # Initialize from kwargs (and default values)
        convert = kwargs.get('convert', False)
        sensor = kwargs.get('sensor', None)
        channel = kwargs.get('channel', None)
        recordset = kwargs.get('recordset', None)

        # Get all sensor data
        query = self.session.query(SensorData)

        if recordset is not None:
            query = query.filter(
                SensorData.id_recordset == recordset.id_recordset)

        if sensor is not None:
            # print('Should filter sensor id', sensor.id_sensor)
            query = query.filter(SensorData.id_sensor == sensor.id_sensor)

        if channel is not None:
            # print('Should filter channel', channel.id_channel)
            query = query.filter(SensorData.id_channel == channel.id_channel)

        # print(query)
        # Make sure data is ordered by timestamps
        # query = query.order_by(SensorData.timestamps.asc())

        # print('TODO ORDERY BY TIMESTAMPS NEEDS TO BE IMPLEMENTED')

        # And then per channel
        # query = query.order_by(SensorData.channel.asc())

        if not convert:
            return query.all()
        else:
            # Read result, data will be bytes array
            result = query.all()

            # Convert to the right format
            for sensor_data in result:
                # print('data len:', len(sensor_data.data))
                sensor_data.data = DataFormat.from_bytes(
                    sensor_data.data, sensor_data.channel.id_data_format)

            return result
Exemplo n.º 6
0
    def get_all_sensor_data(self, **kwargs):

        # Initialize from kwargs (and default values)
        convert = kwargs.get('convert', False)
        sensor = kwargs.get('sensor', None)
        channel = kwargs.get('channel', None)
        recordset = kwargs.get('recordset', None)
        start_time = kwargs.get('start_time', None)
        end_time = kwargs.get('end_time', None)

        # Get all sensor data
        query = self.session.query(SensorData)

        if recordset is not None:
            query = query.filter(
                SensorData.id_recordset == recordset.id_recordset)

        if sensor is not None:
            # print('Should filter sensor id', sensor.id_sensor)
            query = query.filter(SensorData.id_sensor == sensor.id_sensor)

        if channel is not None:
            # print('Should filter channel', channel.id_channel)
            query = query.filter(SensorData.id_channel == channel.id_channel)

        if start_time is not None:
            query = query.filter(
                or_(
                    SensorData.timestamps.has(
                        SensorTimestamps.start_timestamp >= start_time),
                    and_(
                        SensorData.timestamps.has(
                            SensorTimestamps.start_timestamp <= start_time),
                        SensorData.timestamps.has(
                            SensorTimestamps.end_timestamp >= start_time))))

        if end_time is not None:
            query = query.filter(
                or_(
                    SensorData.timestamps.has(
                        SensorTimestamps.end_timestamp <= end_time),
                    and_(
                        SensorData.timestamps.has(
                            SensorTimestamps.start_timestamp <= end_time),
                        SensorData.timestamps.has(
                            SensorTimestamps.end_timestamp >= end_time))))

        # print(query)

        if not convert:
            return query.all()
        else:
            # Read result, data will be bytes array
            result = query.all()

            # Convert to the right format
            for sensor_data in result:
                # print('data len:', len(sensor_data.data))
                sensor_data.data = DataFormat.from_bytes(
                    sensor_data.data, sensor_data.channel.id_data_format)

            return result
Exemplo n.º 7
0
 def to_ndarray(self):
     if isinstance(self.data, bytes):
         return DataFormat.from_bytes(self.data,
                                      self.channel.id_data_format)
     else:
         return self.data
Exemplo n.º 8
0
 def to_ndarray(self):
     if type(self.data) is bytes:
         return DataFormat.from_bytes(self.data, self.channel.id_data_format)
     else:
         return self.data