You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

54 lines
2.6 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using IronPython.Runtime;
  4. using Microsoft.Extensions.Logging;
  5. using MQTTnet.Protocol;
  6. using MQTTnet.Server.Scripting;
  7. namespace MQTTnet.Server.Mqtt
  8. {
  9. public class MqttApplicationMessageInterceptor : IMqttServerApplicationMessageInterceptor
  10. {
  11. private readonly PythonScriptHostService _pythonScriptHostService;
  12. private readonly ILogger _logger;
  13. public MqttApplicationMessageInterceptor(PythonScriptHostService pythonScriptHostService, ILogger<MqttApplicationMessageInterceptor> logger)
  14. {
  15. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  16. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  17. }
  18. public Task InterceptApplicationMessagePublishAsync(MqttApplicationMessageInterceptorContext context)
  19. {
  20. try
  21. {
  22. // This might be not set when a message was published by the server instead of a client.
  23. context.SessionItems.TryGetValue(MqttServerConnectionValidator.WrappedSessionItemsKey, out var sessionItems);
  24. var pythonContext = new PythonDictionary
  25. {
  26. { "client_id", context.ClientId },
  27. { "session_items", sessionItems },
  28. { "retain", context.ApplicationMessage.Retain },
  29. { "accept_publish", context.AcceptPublish },
  30. { "close_connection", context.CloseConnection },
  31. { "topic", context.ApplicationMessage.Topic },
  32. { "qos", (int)context.ApplicationMessage.QualityOfServiceLevel }
  33. };
  34. _pythonScriptHostService.InvokeOptionalFunction("on_intercept_application_message", pythonContext);
  35. context.AcceptPublish = (bool)pythonContext.get("accept_publish", context.AcceptPublish);
  36. context.CloseConnection = (bool)pythonContext.get("close_connection", context.CloseConnection);
  37. context.ApplicationMessage.Topic = (string)pythonContext.get("topic", context.ApplicationMessage.Topic);
  38. context.ApplicationMessage.QualityOfServiceLevel = (MqttQualityOfServiceLevel)(int)pythonContext.get("qos", (int)context.ApplicationMessage.QualityOfServiceLevel);
  39. }
  40. catch (Exception exception)
  41. {
  42. _logger.LogError(exception, "Error while intercepting application message.");
  43. }
  44. return Task.CompletedTask;
  45. }
  46. }
  47. }