Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MqttClientSubscribedTopicHandler.cs 1.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 MqttClientSubscribedTopicHandler : IMqttServerClientSubscribedTopicHandler
  9. {
  10. private readonly PythonScriptHostService _pythonScriptHostService;
  11. private readonly ILogger _logger;
  12. public MqttClientSubscribedTopicHandler(PythonScriptHostService pythonScriptHostService, ILogger<MqttClientSubscribedTopicHandler> logger)
  13. {
  14. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  15. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  16. }
  17. public Task HandleClientSubscribedTopicAsync(MqttServerClientSubscribedTopicEventArgs eventArgs)
  18. {
  19. try
  20. {
  21. var pythonEventArgs = new PythonDictionary
  22. {
  23. { "client_id", eventArgs.ClientId },
  24. { "topic", eventArgs.TopicFilter.Topic },
  25. { "qos", (int)eventArgs.TopicFilter.QualityOfServiceLevel }
  26. };
  27. _pythonScriptHostService.InvokeOptionalFunction("on_client_subscribed_topic", pythonEventArgs);
  28. }
  29. catch (Exception exception)
  30. {
  31. _logger.LogError(exception, "Error while handling client subscribed topic event.");
  32. }
  33. return Task.CompletedTask;
  34. }
  35. }
  36. }