def test_decode_is_valid_case2(self):
        decoder = UrlDecoder()
        data = decoder.decode_data('AjgbAMFc')

        self.assertEqual(data['temperature'], 27)
        self.assertEqual(data['pressure'], 995)
        self.assertEqual(data['humidity'], 28)
    def test_decode_is_valid(self):
        decoder = UrlDecoder()
        data = decoder.decode_data('AjwYAMFc')

        self.assertEqual(data['temperature'], 24)
        self.assertEqual(data['pressure'], 995)
        self.assertEqual(data['humidity'], 30)
Example #3
0
    def test_decode_is_valid_weatherstation_2017_04_12(self):
        decoder = UrlDecoder()
        data = decoder.decode_data('AjUX1MAw0')

        self.assertEqual(data['temperature'], 25.12)
        self.assertEqual(data['pressure'], 992.0)
        self.assertEqual(data['humidity'], 26.5)
        self.assertEqual(data['identifier'], '0')
Example #4
0
    def find_ruuvitags():
        """
        Find all RuuviTags. Function will print the mac and the state of the sensors when found.
        Function will execute as long as it is  stopped. Stop ecexution with Crtl+C.

        Returns:
            Dictionary containing mac and state of found sensors
        """

        print('Finding RuuviTags. Stop with Ctrl+C.')
        datas = dict()
        for ble_data in ble.get_datas():
            # If mac already in datas continue
            if ble_data[0] in datas:
                continue
            encoded = RuuviTagSensor.convert_data(ble_data[1])
            # Check that encoded data is valid ruuvitag data it is sensor data
            if encoded is not None:
                state = UrlDecoder().decode_data(encoded)
                if state is not None:
                    datas[ble_data[0]] = state
                    print(ble_data[0])
                    print(state)

        return datas
Example #5
0
    def get_data_for_sensors(macs, search_duratio_sec=5):
        """
        Get lates data for sensors in the macs list.

        Args:
            macs: List of mac addresses
            search_duratio_sec: Search duration in seconds. Default 5.
        Returns:
            Dictionary containing mac and state of found sensors
        """

        print('Get latest data for sensors. Search duration is {}s'.format(
            search_duratio_sec))
        print('MACs: {}'.format(macs))

        start_time = time.time()
        datas = dict()
        data_iter = ble.get_datas()

        for ble_data in data_iter:
            if time.time() - start_time > search_duratio_sec:
                data_iter.send(StopIteration)
                break
            # If mac in whitelist
            if not ble_data[0] in macs:
                continue
            encoded = RuuviTagSensor.convert_data(ble_data[1])
            # Check that encoded data is valid ruuvitag data it is sensor data
            if encoded is not None:
                state = UrlDecoder().decode_data(encoded)
                if state is not None:
                    datas[ble_data[0]] = state

        return datas
Example #6
0
    def update(self):
        """
        Get lates data from the sensor and update own state.

        Returns:
            dict: Latest state
        """

        data = RuuviTagSensor.get_data(self._mac)

        if data == self._data:
            return self._state

        self._data = data

        if self._data is None:
            self._state = {}
        else:
            self._state = UrlDecoder().decode_data(self._data)

        return self._state
Example #7
0
    def _get_ruuvitag_datas(macs=[],
                            search_duratio_sec=None,
                            run_flag=RunFlag()):
        """
        Get data from BluetoothCommunication and handle data encoding.

        Args:
            macs (list): MAC addresses. Default empty list.
            search_duratio_sec (int): Search duration in seconds. Default None.
            run_flag (object): RunFlag object. Function executes while run_flag.running. Default new RunFlag.

        Yields:
            tuple: MAC and State of RuuviTag sensor data
        """

        start_time = time.time()
        data_iter = ble.get_datas()

        for ble_data in data_iter:
            # Check duration
            if search_duratio_sec and time.time(
            ) - start_time > search_duratio_sec:
                data_iter.send(StopIteration)
                break
            # Check running flag
            if not run_flag.running:
                data_iter.send(StopIteration)
                break
            # Check MAC whitelist
            if macs and not ble_data[0] in macs:
                continue
            encoded = RuuviTagSensor.convert_data(ble_data[1])
            # Check that encoded data is valid RuuviTag data and it is sensor data
            if encoded is not None:
                state = UrlDecoder().decode_data(encoded)
                if state is not None:
                    yield (ble_data[0], state)

now = time.strftime('%Y-%m-%d %H:%M:%S')
print(now + "\n")

dweetData = {}
dbData = {}

for mac, name in tags.items():
    tag = Rtag(mac, name)

    print("Looking for {} ({})".format(tag._name, tag._mac))
    # if weather station
    if dataFormat == '1':  # get parsed data

        data = UrlDecoder().decode_data(
            RuuviTagSensor.convert_data(tag.getData()))
        print("Data received:", data)

        dbData[tag._mac] = {'name': tag._name}
        # add each sensor with value to the lists
        for sensor, value in data.items():
            dweetData[tag._name + ' ' + sensor] = value
            dbData[tag._mac].update({sensor: value})

    elif dataFormat == '3':  # under development
        print("Data:", tag.getData())

    else:  # if unknown format, just print raw data
        print("Data:", tag.getData())

    print("\n")