Example #1
0
        public Task ValidateConnectionAsync(MqttConnectionValidatorContext context)
        {
            try
            {
                var pythonContext = new PythonDictionary
                {
                    { "endpoint", context.Endpoint },
                    { "is_secure_connection", context.IsSecureConnection },
                    { "client_id", context.ClientId },
                    { "username", context.Username },
                    { "password", context.Password },
                    { "raw_password", new Bytes(context.RawPassword ?? new byte[0]) },
                    { "clean_session", context.CleanSession },
                    { "authentication_method", context.AuthenticationMethod },
                    { "authentication_data", new Bytes(context.AuthenticationData ?? new byte[0]) },

                    { "result", PythonConvert.Pythonfy(context.ReasonCode) }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_validate_client_connection", pythonContext);

                context.ReasonCode = PythonConvert.ParseEnum <MqttConnectReasonCode>((string)pythonContext["result"]);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while validating client connection.");

                context.ReasonCode = MqttConnectReasonCode.UnspecifiedError;
            }

            return(Task.CompletedTask);
        }
Example #2
0
        public Task InterceptUnsubscriptionAsync(MqttUnsubscriptionInterceptorContext context)
        {
            try
            {
                var sessionItems = (PythonDictionary)context.SessionItems[MqttServerConnectionValidator.WrappedSessionItemsKey];

                var pythonContext = new PythonDictionary
                {
                    { "client_id", context.ClientId },
                    { "session_items", sessionItems },
                    { "topic", context.Topic },
                    { "accept_unsubscription", context.AcceptUnsubscription },
                    { "close_connection", context.CloseConnection }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_intercept_unsubscription", pythonContext);

                context.AcceptUnsubscription = (bool)pythonContext["accept_unsubscription"];
                context.CloseConnection      = (bool)pythonContext["close_connection"];
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while intercepting unsubscription.");
            }

            return(Task.CompletedTask);
        }
        public Task InterceptSubscriptionAsync(MqttSubscriptionInterceptorContext context)
        {
            try
            {
                var pythonContext = new PythonDictionary
                {
                    { "accept_subscription", context.AcceptSubscription },
                    { "close_connection", context.CloseConnection },

                    { "client_id", context.ClientId },
                    { "topic", context.TopicFilter.Topic },
                    { "qos", (int)context.TopicFilter.QualityOfServiceLevel }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_intercept_subscription", pythonContext);

                context.AcceptSubscription = (bool)pythonContext["accept_subscription"];
                context.CloseConnection    = (bool)pythonContext["close_connection"];
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while intercepting subscription.");
            }

            return(Task.CompletedTask);
        }
        public Task InterceptApplicationMessagePublishAsync(MqttApplicationMessageInterceptorContext context)
        {
            try
            {
                var pythonContext = new PythonDictionary
                {
                    { "accept_publish", context.AcceptPublish },
                    { "close_connection", context.CloseConnection },
                    { "client_id", context.ClientId },
                    { "topic", context.ApplicationMessage.Topic },
                    { "qos", (int)context.ApplicationMessage.QualityOfServiceLevel },
                    { "retain", context.ApplicationMessage.Retain }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_intercept_application_message", pythonContext);

                context.AcceptPublish            = (bool)pythonContext.get("accept_publish", context.AcceptPublish);
                context.CloseConnection          = (bool)pythonContext.get("close_connection", context.CloseConnection);
                context.ApplicationMessage.Topic = (string)pythonContext.get("topic", context.ApplicationMessage.Topic);
                context.ApplicationMessage.QualityOfServiceLevel = (MqttQualityOfServiceLevel)(int)pythonContext.get("qos", (int)context.ApplicationMessage.QualityOfServiceLevel);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while intercepting application message.");
            }

            return(Task.CompletedTask);
        }
        public Task InterceptApplicationMessagePublishAsync(MqttApplicationMessageInterceptorContext context)
        {
            try
            {
                // This might be not set when a message was published by the server instead of a client.
                context.SessionItems.TryGetValue(MqttServerConnectionValidator.WrappedSessionItemsKey, out var sessionItems);

                var pythonContext = new PythonDictionary
                {
                    { "client_id", context.ClientId },
                    { "session_items", sessionItems },
                    { "retain", context.ApplicationMessage.Retain },
                    { "accept_publish", context.AcceptPublish },
                    { "close_connection", context.CloseConnection },
                    { "topic", context.ApplicationMessage.Topic },
                    { "qos", (int)context.ApplicationMessage.QualityOfServiceLevel }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_intercept_application_message", pythonContext);

                context.AcceptPublish            = (bool)pythonContext.get("accept_publish", context.AcceptPublish);
                context.CloseConnection          = (bool)pythonContext.get("close_connection", context.CloseConnection);
                context.ApplicationMessage.Topic = (string)pythonContext.get("topic", context.ApplicationMessage.Topic);
                context.ApplicationMessage.QualityOfServiceLevel = (MqttQualityOfServiceLevel)(int)pythonContext.get("qos", (int)context.ApplicationMessage.QualityOfServiceLevel);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while intercepting application message.");
            }

            return(Task.CompletedTask);
        }
        public Task ValidateConnectionAsync(MqttConnectionValidatorContext context)
        {
            try
            {
                var pythonContext = new PythonDictionary
                {
                    { "client_id", context.ClientId },
                    { "endpoint", context.Endpoint },
                    { "is_secure_connection", context.IsSecureConnection },
                    { "username", context.Username },
                    { "password", context.Password },
                    { "result", PythonConvert.Pythonfy(context.ReturnCode) }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_validate_client_connection", pythonContext);

                context.ReturnCode = PythonConvert.ParseEnum <MqttConnectReturnCode>((string)pythonContext["result"]);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while validating client connection.");
            }

            return(Task.CompletedTask);
        }
        public Task HandleClientConnectedAsync(MqttServerClientConnectedEventArgs eventArgs)
        {
            try
            {
                var pythonEventArgs = new PythonDictionary
                {
                    { "client_id", eventArgs.ClientId }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_client_connected", pythonEventArgs);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while handling client connected event.");
            }

            return(Task.CompletedTask);
        }
        public Task HandleClientUnsubscribedTopicAsync(MqttServerClientUnsubscribedTopicEventArgs eventArgs)
        {
            try
            {
                var pythonEventArgs = new PythonDictionary
                {
                    { "client_id", eventArgs.ClientId },
                    { "topic", eventArgs.TopicFilter }
                };

                _pythonScriptHostService.InvokeOptionalFunction("on_client_unsubscribed_topic", pythonEventArgs);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while handling client unsubscribed topic event.");
            }

            return(Task.CompletedTask);
        }