def ValidateMessage(self, message):
        """Validates a telemetry message.

    Adds all validation errors for the message to a list of all errors
    discovered by this validator."""

        t = telemetry.Telemetry(message)
        entity_name = t.attributes[DEVICE_ID]

        if entity_name not in self.entities:
            self.AddError(
                telemetry_error.TelemetryError(entity_name, None,
                                               "Unknown entity"))
            message.ack()
            return

        if entity_name in self.validated_entities:
            # Already validated telemetry for this entity,
            # so the message can be skipped.
            return
        self.validated_entities[entity_name] = True

        entity = self.entities[entity_name]
        for point_name, point_config in entity[TRANSLATION].items():
            if point_name not in t.points.keys():
                self.AddError(
                    telemetry_error.TelemetryError(entity_name, point_name,
                                                   "Missing point"))
                continue

            point = t.points[point_name]
            pv = point.present_value
            if pv is None:
                self.AddError(
                    telemetry_error.TelemetryError(entity_name, point_name,
                                                   "Missing present value"))
                continue

            has_states = STATES in point_config
            has_units = UNITS in point_config

            if has_states:
                states = point_config[STATES]
                if pv not in states.values():
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, point_name,
                            "Invalid state: {}".format(pv)))
                    continue

            if has_units and not self.ValueIsNumeric(pv):
                self.AddError(
                    telemetry_error.TelemetryError(
                        entity_name, point_name,
                        "Invalid number: {}".format(pv)))

        message.ack()
        self.CallbackIfCompleted()
Пример #2
0
def message_handler(message):
    """Handles a pubsub message.
    Args:
      message: a pubsub message containing telemetry payload.
  """
    t = telemetry.Telemetry(message)
    for key, value in t.points.items():
        print()
        print('-point: ', key)
        print('-- point_name: ', value.point_name)
        print('-- present_value: ', value.present_value)
    message.ack()
    def ValidateMessage(self, message):
        """Validates a telemetry message.

    Args:
      message: the telemetry message to validate.

    Adds all validation errors for the message to a list of all errors
    discovered by this validator.
    """

        tele = telemetry.Telemetry(message)
        entity_name = tele.attributes[DEVICE_ID]

        # Telemetry message received for an entity not in building config
        if entity_name not in self.entities_with_translation.keys():
            # TODO(charbull): refactor warning class
            self.AddWarning(
                telemetry_warning.TelemetryWarning(
                    entity_name, None,
                    'Telemetry message received for an entity not '
                    'in building config'))
            message.ack()
            return

        if entity_name in self.validated_entities:
            # Already validated telemetry for this entity,
            # so the message can be skipped.
            message.ack()
            return
        self.validated_entities[entity_name] = True

        entity = self.entities_with_translation[entity_name]

        print(f'Validating telemetry message for entity: {entity_name}')
        point_full_paths = {
            f'points.{key}.present_value': key
            for key in tele.points
        }
        for field_translation in entity.translation.values():
            if isinstance(field_translation, ft_lib.UndefinedField):
                continue
            if field_translation.raw_field_name not in point_full_paths:
                if not tele.is_partial:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Field missing from telemetry message'))
                continue
            point = tele.points[point_full_paths[
                field_translation.raw_field_name]]
            pv = point.present_value
            if pv is None:
                if isinstance(field_translation, ft_lib.MultiStateValue):
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            f'Missing state in telemetry message: {pv}'))
                elif isinstance(field_translation, ft_lib.DimensionalValue):
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            f'Missing number in telemetry message: {pv}'))
                else:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Present value missing from telemetry message'))
                continue

            if isinstance(field_translation, ft_lib.MultiStateValue):
                if pv not in field_translation.raw_values:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            f'Invalid state in telemetry message: {pv}'))

                    continue

            if isinstance(
                    field_translation,
                    ft_lib.DimensionalValue) and not self.ValueIsNumeric(pv):
                self.AddError(
                    telemetry_error.TelemetryError(
                        entity_name, field_translation.raw_field_name,
                        f'Invalid number in telemetry message: {pv}'))

        message.ack()
        self.CallbackIfCompleted()
Пример #4
0
    def ValidateMessage(self, message):
        """Validates a telemetry message.

    Adds all validation errors for the message to a list of all errors
    discovered by this validator."""

        tele = telemetry.Telemetry(message)
        entity_name = tele.attributes[DEVICE_ID]

        # Telemetry message received for an entity not in building config
        if entity_name not in self.entities_with_translation.keys():
            #TODO(charbull): refactor warning class
            self.AddWarning(
                telemetry_warning.TelemetryWarning(
                    entity_name, None,
                    'Telemetry message received for an entity not '
                    'in building config'))
            message.ack()
            return

        if entity_name in self.validated_entities:
            # Already validated telemetry for this entity,
            # so the message can be skipped.
            message.ack()
            return
        self.validated_entities[entity_name] = True

        entity = self.entities_with_translation[entity_name]

        print(
            'Validating telemetry message for entity: {0}'.format(entity_name))

        for field_translation in entity.translation.values():
            if field_translation.raw_field_name not in tele.points.keys():
                if not tele.is_partial:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Field missing from telemetry message'))
                continue

            point = tele.points[field_translation.raw_field_name]
            pv = point.present_value
            if pv is None:
                if field_translation.states:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Missing state in telemetry '
                            'message: {}'.format(pv)))
                elif field_translation.units:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Missing number in telemetry '
                            'message: {}'.format(pv)))
                else:
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Present value missing from '
                            'telemetry message'))
                continue

            if field_translation.states:
                if pv not in field_translation.states.values():
                    self.AddError(
                        telemetry_error.TelemetryError(
                            entity_name, field_translation.raw_field_name,
                            'Invalid state in telemetry message: {}'.format(
                                pv)))

                    continue

            if field_translation.units and not self.ValueIsNumeric(pv):
                self.AddError(
                    telemetry_error.TelemetryError(
                        entity_name, field_translation.raw_field_name,
                        'Invalid number in telemetry message: {}'.format(pv)))

        message.ack()
        self.CallbackIfCompleted()