Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

49 rindas
1.9 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using IronPython.Runtime;
  4. using Microsoft.Extensions.Logging;
  5. using MQTTnet.Server.Scripting;
  6. namespace MQTTnet.Server.Mqtt
  7. {
  8. public class MqttUnsubscriptionInterceptor : IMqttServerUnsubscriptionInterceptor
  9. {
  10. private readonly PythonScriptHostService _pythonScriptHostService;
  11. private readonly ILogger _logger;
  12. public MqttUnsubscriptionInterceptor(PythonScriptHostService pythonScriptHostService, ILogger<MqttUnsubscriptionInterceptor> logger)
  13. {
  14. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  15. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  16. }
  17. public Task InterceptUnsubscriptionAsync(MqttUnsubscriptionInterceptorContext context)
  18. {
  19. try
  20. {
  21. var sessionItems = (PythonDictionary)context.SessionItems[MqttServerConnectionValidator.WrappedSessionItemsKey];
  22. var pythonContext = new PythonDictionary
  23. {
  24. { "client_id", context.ClientId },
  25. { "session_items", sessionItems },
  26. { "topic", context.Topic },
  27. { "accept_unsubscription", context.AcceptUnsubscription },
  28. { "close_connection", context.CloseConnection }
  29. };
  30. _pythonScriptHostService.InvokeOptionalFunction("on_intercept_unsubscription", pythonContext);
  31. context.AcceptUnsubscription = (bool)pythonContext["accept_unsubscription"];
  32. context.CloseConnection = (bool)pythonContext["close_connection"];
  33. }
  34. catch (Exception exception)
  35. {
  36. _logger.LogError(exception, "Error while intercepting unsubscription.");
  37. }
  38. return Task.CompletedTask;
  39. }
  40. }
  41. }