-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMqttConsumer.cs
149 lines (122 loc) · 4.87 KB
/
MqttConsumer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Consumer.Data;
using Consumer.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Consumer
{
internal class MqttConsumer
{
private readonly ILogger<MqttConsumer> _logger;
private IMqttClient _mqttClient;
private IMqttClientOptions _mqttClientOptions;
private readonly IServiceScopeFactory _serviceScopeFactory;
private string _subTopic;
public MqttConsumer(ILogger<MqttConsumer> logger, IConfiguration configuration, IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
_logger = logger;
int port = 1883;
Int32.TryParse(configuration.GetSection("MqttServerPort").Value, out port);
// MQTT options
_mqttClientOptions = new MqttClientOptionsBuilder()
.WithClientId(configuration.GetSection("ClientId").Value)
.WithTcpServer(configuration.GetSection("MqttServerIp").Value, port)
.WithCleanSession()
.Build();
_subTopic = configuration.GetSection("MqttSubscribeTopic").Value;
var factory = new MqttFactory();
_mqttClient = factory.CreateMqttClient();
// Event handlers
_mqttClient.UseDisconnectedHandler(OnDisconnected);
_mqttClient.UseApplicationMessageReceivedHandler(OnMessageReceived);
_mqttClient.UseConnectedHandler(OnConnected);
}
private async void OnDisconnected(MqttClientDisconnectedEventArgs e)
{
_logger.LogWarning($"Disconnected from MQTT server");
if(e.Exception != null)
_logger.LogError($"Error: {e.Exception.Message}");
await Task.Delay(TimeSpan.FromSeconds(5));
try
{
await _mqttClient.ConnectAsync(_mqttClientOptions, CancellationToken.None);
}
catch
{
_logger.LogError($"Reconnection failed");
}
}
private void OnMessageReceived(MqttApplicationMessageReceivedEventArgs e)
{
_logger.LogInformation($"Received message");
_logger.LogInformation($"Topic = {e.ApplicationMessage.Topic}");
_logger.LogInformation($"Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
_logger.LogInformation($"QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
_logger.LogInformation($"Retain = {e.ApplicationMessage.Retain}");
var payload = JsonConvert.DeserializeObject<SensorData>(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
_logger.LogInformation($"Temperature {payload.Temperature}");
_logger.LogInformation($"Humidity {payload.Humidity}");
payload.MeasuredAt = DateTime.Now;
SavePayload(payload);
}
private async void OnConnected(MqttClientConnectedEventArgs e)
{
_logger.LogInformation($"Connected to MQTT server");
// Subscribe to a topic
await _mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(_subTopic).Build());
_logger.LogInformation($"Subscribed to topic: {_subTopic}");
}
private async void SavePayload(SensorData data)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
try
{
context.SensorData.Add(data);
await context.SaveChangesAsync();
_logger.LogInformation($"Saved successfully");
}
catch (Exception ex)
{
_logger.LogError("Failed to save payload data", ex);
return;
}
}
}
public async void Run()
{
try
{
_logger.LogInformation("Running");
await _mqttClient.ConnectAsync(_mqttClientOptions, CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogError($"An error occurred: {ex.Message}");
}
}
/// <summary>
/// Executes on worker shutdown
/// </summary>
public async void OnStopping()
{
await _mqttClient.DisconnectAsync();
_mqttClient.Dispose();
}
}
}