Ejemplo n.º 1
0
        void ApplyRoutingConfiguration(IMessage message, PublishPacket packet)
        {
            RouteDestinationType routeType;

            if (this.messageRouter.TryRouteIncomingMessage(packet.TopicName, message, out routeType))
            {
                // successfully matched topic against configured routes -> validate topic name
                string messageDeviceId;
                if (message.Properties.TryGetValue(TemplateParameters.DeviceIdTemplateParam, out messageDeviceId))
                {
                    if (!this.DeviceId.Equals(messageDeviceId, StringComparison.Ordinal))
                    {
                        throw new InvalidOperationException(
                                  $"Device ID provided in topic name ({messageDeviceId}) does not match ID of the device publishing message ({this.DeviceId})");
                    }
                    message.Properties.Remove(TemplateParameters.DeviceIdTemplateParam);
                }
            }
            else
            {
                if (!this.settings.PassThroughUnmatchedMessages)
                {
                    throw new InvalidOperationException($"Topic name `{packet.TopicName}` could not be matched against any of the configured routes.");
                }

                if (MqttIotHubAdapterEventSource.Log.IsWarningEnabled)
                {
                    MqttIotHubAdapterEventSource.Log.Warning("Topic name could not be matched against any of the configured routes. Falling back to default telemetry settings.", packet.ToString());
                }
                routeType = RouteDestinationType.Telemetry;
                message.Properties[this.settings.ServicePropertyPrefix + MessagePropertyNames.Unmatched] = bool.TrueString;
                message.Properties[this.settings.ServicePropertyPrefix + MessagePropertyNames.Subject]   = packet.TopicName;
            }

            // once we have different routes, this will change to tackle different aspects of route types
            switch (routeType)
            {
            case RouteDestinationType.Telemetry:
                break;

            default:
                throw new ArgumentOutOfRangeException($"Unexpected route type: {routeType}");
            }
        }
        void ApplyRoutingConfiguration(Message message, PublishPacket packet)
        {
            RouteDestinationType routeType;

            if (this.topicNameRouter.TryMapTopicNameToRoute(packet.TopicName, out routeType, message.Properties))
            {
                // successfully matched topic against configured routes -> validate topic name
                string messageDeviceId;
                if (message.Properties.TryGetValue(DeviceIdParam, out messageDeviceId))
                {
                    if (!this.identity.DeviceId.Equals(messageDeviceId, StringComparison.Ordinal))
                    {
                        throw new InvalidOperationException(
                                  string.Format("Device ID provided in topic name ({0}) does not match ID of the device publishing message ({1}). IoT Hub Name: {2}",
                                                messageDeviceId, this.identity.DeviceId, this.identity.IoTHubHostName));
                    }
                    message.Properties.Remove(DeviceIdParam);
                }
            }
            else
            {
                if (MqttIotHubAdapterEventSource.Log.IsWarningEnabled)
                {
                    MqttIotHubAdapterEventSource.Log.Warning("Topic name could not be matched against any of the configured routes. Falling back to default telemetry settings.", packet.ToString());
                }
                routeType = RouteDestinationType.Telemetry;
                message.Properties[UnmatchedFlagPropertyName] = bool.TrueString;
                message.Properties[SubjectPropertyName]       = packet.TopicName;
            }

            // once we have different routes, this will change to tackle different aspects of route types
            switch (routeType)
            {
            case RouteDestinationType.Telemetry:
                break;

            default:
                throw new ArgumentOutOfRangeException(string.Format("Unexpected route type: {0}", routeType));
            }
        }