Esempio n. 1
0
    def _handle_pipeline_event(self, event):
        """
        Pipeline Event handler function to convert incoming MQTT messages into the appropriate DPS
        events, based on the topic of the message
        """
        if isinstance(event, pipeline_events_mqtt.IncomingMQTTMessageEvent):
            topic = event.topic

            if mqtt_topic.is_dps_response_topic(topic):
                logger.info(
                    "Received payload:{payload} on topic:{topic}".format(
                        payload=event.payload, topic=topic))
                key_values = mqtt_topic.extract_properties_from_topic(topic)
                status_code = mqtt_topic.extract_status_code_from_topic(topic)
                request_id = key_values["rid"][0]
                if event.payload is not None:
                    response = event.payload.decode("utf-8")
                # Extract pertinent information from mqtt topic
                # like status code request_id and send it upwards.
                operation_flow.pass_event_to_previous_stage(
                    self,
                    pipeline_events_provisioning.RegistrationResponseEvent(
                        request_id, status_code, key_values, response),
                )
            else:
                logger.warning(
                    "Unknown topic: {} passing up to next handler".format(
                        topic))
                operation_flow.pass_event_to_previous_stage(self, event)

        else:
            # all other messages get passed up
            operation_flow.pass_event_to_previous_stage(self, event)
Esempio n. 2
0
    def _handle_pipeline_event(self, event):
        """
        Pipeline Event handler function to convert incoming MQTT messages into the appropriate DPS
        events, based on the topic of the message
        """
        if isinstance(event, pipeline_events_mqtt.IncomingMQTTMessageEvent):
            topic = event.topic

            if mqtt_topic.is_dps_response_topic(topic):
                logger.info(
                    "Received payload:{payload} on topic:{topic}".format(
                        payload=event.payload, topic=topic))
                key_values = mqtt_topic.extract_properties_from_topic(topic)
                retry_after = mqtt_topic.get_optional_element(
                    key_values, "retry-after", 0)
                status_code = mqtt_topic.extract_status_code_from_topic(topic)
                request_id = key_values["rid"][0]

                self.send_event_up(
                    pipeline_events_base.ResponseEvent(
                        request_id=request_id,
                        status_code=int(status_code, 10),
                        response_body=event.payload,
                        retry_after=retry_after,
                    ))
            else:
                logger.warning(
                    "Unknown topic: {} passing up to next handler".format(
                        topic))
                self.send_event_up(event)

        else:
            # all other messages get passed up
            super(ProvisioningMQTTTranslationStage,
                  self)._handle_pipeline_event(event)