-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MqttClientService.cs added in order to wrap the connection to the mqt…
…t client and keep reference to it
- Loading branch information
Showing
8 changed files
with
65 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using api.Core.Options; | ||
using Microsoft.Extensions.Options; | ||
using MQTTnet; | ||
using MQTTnet.Client; | ||
|
||
namespace api.Core.Services; | ||
|
||
public class MqttClientService | ||
{ | ||
private readonly IMqttClient _mqttClient; | ||
private readonly IOptions<MqttOptions> _options; | ||
|
||
public MqttClientService(IOptions<MqttOptions> options) | ||
{ | ||
_options = options; | ||
var factory = new MqttFactory(); | ||
_mqttClient = factory.CreateMqttClient(); | ||
} | ||
|
||
public async Task ConnectAsync() | ||
{ | ||
var mqttClientOptions = new MqttClientOptionsBuilder() | ||
.WithTcpServer(_options.Value.Server, _options.Value.Port) | ||
.WithCredentials(_options.Value.Username) | ||
.Build(); | ||
|
||
await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); | ||
} | ||
|
||
public IMqttClient GetClient() | ||
{ | ||
return _mqttClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters