Beispiel #1
0
    def additional_checks(self):
        if not cache.get(self.device_id):
            abort(404)

        # compare route device_id and telemetry device_id
        # We already checked that telemetry is present and contains a device_id with cerberus
        telemetry = self.payload.get('telemetry', {})
        if isinstance(telemetry, dict):
            device_id = telemetry.get('device_id', None)
            if device_id and device_id != self.device_id:
                self.bad_param.append('device_id')

        # event_type value affects event_type_reason and trip_id
        event_type = self.payload.get('event_type', None)
        if event_type:
            # Check event_type_reason values
            event_type_to_event_types_reasons = {
                'service_end': [
                    'low_battery',
                    'maintenance',
                    'compliance',
                    'off_hours',
                ],
                'provider_pick_up': [
                    'rebalance',
                    'maintenance',
                    'charge',
                    'compliance',
                ],
                'deregister': [
                    'missing',
                    'decommissioned',
                ],
            }
            allowed_event_types_reasons = event_type_to_event_types_reasons.get(
                event_type, None)
            if allowed_event_types_reasons:
                # event_type_reason is required
                try:
                    event_type_reason = self.payload['event_type_reason']
                except KeyError:
                    self.missing_param.append('event_type_reason')
                    # TODO confirm if event_type == deregister implies that
                    # event_type_reason is required
                else:
                    if event_type_reason not in allowed_event_types_reasons:
                        self.bad_param.append('event_type_reason')
            elif 'event_type_reason' in self.payload:
                # event_type_reason should not be there
                self.bad_param.append('event_type_reason')

            # Check trip_id
            if event_type in [
                    'trip_start', 'trip_enter', 'trip_leave', 'trip_end'
            ]:
                if 'trip_id' not in self.payload:
                    self.missing_param.append('trip_id')
            else:
                if 'trip_id' in self.payload:
                    self.bad_param.append('trip_id')
Beispiel #2
0
    def analyze_payload(self):
        # Replace base cerberus errors parsing
        self.cerberus_validator.validate(self.payload)
        # on this payload (list of dict) the errors will be a list with only one dict inside
        # containing the list index as keys :
        # errors = [{0: {<anomalies on first telemetry>},  {<anomalies on 2nd telemetry>}}]
        # We need to store failures in self.failures to return them in 201 Success responses
        errors = self.cerberus_validator.errors.get('data', [{}])[0]
        data = self.payload['data']
        for i, telemetry in enumerate(data):
            # if cerberus found an error, or if device isn't registred
            if i in errors or not cache.get(telemetry.get('device_id', None)):
                self.failures.append(data[i])

        self.result = len(data) - len(self.failures)
Beispiel #3
0
    def additional_checks(self):
        if not cache.get(self.device_id):
            abort(404)

        # compare route device_id and telemetry device_id
        # We already checked that telemetry is present and contains a device_id with cerberus
        telemetry = self.payload.get('telemetry', {})
        if isinstance(telemetry, dict):
            device_id = telemetry.get('device_id', None)
            if device_id and device_id != self.device_id:
                self.bad_param.append('device_id')

        # event_type value affects event_type_reason and trip_id
        try:
            vehicle_state = self.payload['vehicle_state']
        except KeyError:
            self.missing_param.append('vehicle_state')
            abort(400)

        try:
            event_types = set(self.payload['event_types'])
        except KeyError:
            self.missing_param.append('event_types')
            abort(400)

        if not event_types & {'comms_restored', 'located'}:
            allowed_event_types = ALLOWED_STATE_TRANSITIONS[vehicle_state]
            if not event_types & set(
                    allowed_event_types) and not 'unspecified' in event_types:
                self.bad_param.append('event_types')

        # Check trip_id
        if event_types & {
                'trip_start',
                'trip_cancel',
                'trip_enter_jurisdiction',
                'trip_leave_jurisdiction',
                'trip_end',
        }:
            if 'trip_id' not in self.payload:
                self.missing_param.append('trip_id')
        else:
            if 'trip_id' in self.payload:
                self.bad_param.append('trip_id')
Beispiel #4
0
 def additional_checks(self):
     if not cache.get(self.device_id):
         abort(404)
Beispiel #5
0
 def additional_checks(self):
     device_id = self.payload.get('device_id', None)
     if device_id and cache.get(device_id):
         abort(409, 'already_registered')