Beispiel #1
0
    def _fetch_data(self, settings: Dict) -> Dict:
        fetchType = settings['fetchType']
        if fetchType == 'minMax':
            joinedIDs = '&'.join([
                f'sensorIds={item}' for item in settings["sensorIDsForMinMax"]
            ])
            startDateTime = urllib.parse.quote(settings['startDateTime'])
            endDateTime = urllib.parse.quote(settings['endDateTime'])

            urlMinMax = Helpers.join_url_parts(
                settings['url'], f'measurements/minMax?{joinedIDs}'
                f'&startDateTime={startDateTime}'
                f'&endDateTime={endDateTime}')
            minMaxResponse = requests.get(urlMinMax)
            if minMaxResponse.status_code != 200:
                raise Exception(
                    f'Invalid status code: {minMaxResponse.status_code}')
            return minMaxResponse.json()

        urlSensorInfo = Helpers.join_url_parts(settings['url'], 'sensor',
                                               str(settings['sensorID']))
        if fetchType == 'all':
            if 'startDateTime' in settings and 'endDateTime' in settings:
                startDateTime = urllib.parse.quote(settings['startDateTime'])
                endDateTime = urllib.parse.quote(settings['endDateTime'])
                urlSensorValue = Helpers.join_url_parts(
                    urlSensorInfo, f'measurements'
                    f'?startDateTime={startDateTime}&endDateTime={endDateTime}'
                )
            else:
                urlSensorValue = Helpers.join_url_parts(
                    urlSensorInfo, 'measurements')
        else:
            urlSensorValue = Helpers.join_url_parts(urlSensorInfo,
                                                    'measurements/latest')

        sensorInfoResponse = requests.get(urlSensorInfo)
        if sensorInfoResponse.status_code != 200:
            raise Exception(
                f'Invalid status code: {sensorInfoResponse.status_code}')

        urlDeviceInfo = Helpers.join_url_parts(
            settings['url'], 'device',
            str(sensorInfoResponse.json()['device_id']))
        deviceInfoResponse = requests.get(urlDeviceInfo)
        if deviceInfoResponse.status_code != 200:
            raise Exception(
                f'Invalid status code: {deviceInfoResponse.status_code}')

        sensorValueResponse = requests.get(urlSensorValue)
        if sensorValueResponse.status_code != 200:
            raise Exception(
                f'Invalid status code: {sensorValueResponse.status_code}')

        return {
            'deviceInfo': deviceInfoResponse.json(),
            'sensorInfo': sensorInfoResponse.json(),
            'sensorValue': sensorValueResponse.json()
        }
Beispiel #2
0
 def test_join_url_parts(self):
     self.assertEqual(
         'https://myWebsite/eimer/0815',
         Helpers.join_url_parts('https://myWebsite', 'eimer', '0815'))