diff --git a/.vs/MyCore/v15/Server/sqlite3/storage.ide b/.vs/MyCore/v15/Server/sqlite3/storage.ide
index 9f0e186..03ed2c6 100644
Binary files a/.vs/MyCore/v15/Server/sqlite3/storage.ide and b/.vs/MyCore/v15/Server/sqlite3/storage.ide differ
diff --git a/.vs/MyCore/v15/Server/sqlite3/storage.ide-shm b/.vs/MyCore/v15/Server/sqlite3/storage.ide-shm
index 8d0e15f..e5f9e40 100644
Binary files a/.vs/MyCore/v15/Server/sqlite3/storage.ide-shm and b/.vs/MyCore/v15/Server/sqlite3/storage.ide-shm differ
diff --git a/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal b/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal
index 94859f2..a8ce292 100644
Binary files a/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal and b/.vs/MyCore/v15/Server/sqlite3/storage.ide-wal differ
diff --git a/MyCore/Controllers/MQTTController.cs b/MyCore/Controllers/MQTTController.cs
index 7b138bf..26006f8 100644
--- a/MyCore/Controllers/MQTTController.cs
+++ b/MyCore/Controllers/MQTTController.cs
@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using MQTTnet.Client;
+using MQTTnet.Client.Options;
namespace MyCore.Controllers
{
@@ -15,6 +16,7 @@ namespace MyCore.Controllers
[ApiController]
public class MQTTController : ControllerBase
{
+ private string _mqttServer = "192.168.0.8";
///
/// It's a mqtt publish test ! :)
///
@@ -22,7 +24,16 @@ namespace MyCore.Controllers
[HttpGet]
public void GetToPublishMqtt()
{
- Client_Publish();
+ Client_Publish().ContinueWith(res => {
+ if (res.Status == TaskStatus.RanToCompletion)
+ {
+ Console.WriteLine("It's a success !");
+ }
+ else
+ {
+ Console.WriteLine("We have an issue here.. ");
+ }
+ });
}
protected async Task Client_Publish()
@@ -32,7 +43,7 @@ namespace MyCore.Controllers
try
{
var client1 = new MqttFactory().CreateMqttClient();
- await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("192.168.0.8").Build());
+ await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer(_mqttServer).Build());
var message = new MqttApplicationMessageBuilder().WithPayload("It's a test").WithTopic("IpAddress").WithRetainFlag().Build();
await client1.PublishAsync(message);
diff --git a/MyCore/MyCore.csproj b/MyCore/MyCore.csproj
index 27ff158..29853f9 100644
--- a/MyCore/MyCore.csproj
+++ b/MyCore/MyCore.csproj
@@ -24,7 +24,7 @@
-
+
diff --git a/MyCore/Services/MQTTService.cs b/MyCore/Services/MQTTService.cs
new file mode 100644
index 0000000..c860f6c
--- /dev/null
+++ b/MyCore/Services/MQTTService.cs
@@ -0,0 +1,129 @@
+using MQTTnet;
+using MQTTnet.Client;
+using MQTTnet.Client.Options;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MyCore.Services
+{
+ public class MQTTService
+ {
+
+ private IMqttClient _client;
+ private IMqttClientOptions _options;
+ private string _mqttServer = "192.168.31.140";
+ private string _user = "mqtt";
+ private string _password = "mqtt";
+
+ // It's here to have the mqtt initialisation + logic for payload..
+
+ // Related to which event occurs, a specific action is done.
+
+ public MQTTService()
+ {
+ try
+ {
+ // Create a new MQTT client.
+ _client = new MqttFactory().CreateMqttClient();
+
+ _options = new MqttClientOptionsBuilder()
+ .WithClientId("ApiService")
+ .WithTcpServer(_mqttServer)
+ .WithCredentials(_user, _password)
+ .WithCleanSession()
+ .Build();
+
+ _client.ConnectAsync(_options, CancellationToken.None).ContinueWith(res => {
+ if (res.Status == TaskStatus.RanToCompletion)
+ {
+ Console.WriteLine("It's connected");
+ }
+ else
+ {
+ Console.WriteLine($"Error connecting to {_mqttServer}");
+ }
+ });
+
+ _client.UseDisconnectedHandler(async e =>
+ {
+ Console.WriteLine("### DISCONNECTED FROM SERVER ###");
+ await Task.Delay(TimeSpan.FromSeconds(5));
+
+ try
+ {
+ await _client.ConnectAsync(_options, CancellationToken.None); // Since 3.0.5 with CancellationToken
+ }
+ catch
+ {
+ Console.WriteLine("### RECONNECTING FAILED ###");
+ }
+ });
+
+ _client.UseConnectedHandler(async e =>
+ {
+ Console.WriteLine("### CONNECTED WITH SERVER ###");
+
+ // Subscribe to a topic
+ await _client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
+
+ Console.WriteLine("### SUBSCRIBED ###");
+ });
+
+
+ _client.UseApplicationMessageReceivedHandler(e =>
+ {
+ Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
+ Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
+ Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
+ Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
+ Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
+ Console.WriteLine();
+ });
+
+
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine($"Error during creation of MQTTService with {_mqttServer}, exceptions : {e}");
+ }
+
+ }
+
+ public async void PublishMessage(string topic, string message)
+ {
+ var mqttMessage = new MqttApplicationMessageBuilder()
+ .WithTopic(topic)
+ .WithPayload(message)
+ .WithExactlyOnceQoS()
+ .WithRetainFlag()
+ .Build();
+
+ await _client.PublishAsync(mqttMessage);
+ }
+
+ /*protected async Task Start()
+ {
+ var server = new MqttFactory().CreateMqttServer();
+
+ try
+ {
+ var client1 = new MqttFactory().CreateMqttClient();
+ await client1.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer(_mqttServer).Build());
+ var message = new MqttApplicationMessageBuilder().WithPayload("It's a test").WithTopic("IpAddress").WithRetainFlag().Build();
+ await client1.PublishAsync(message);
+
+ await Task.Delay(500);
+
+ }
+ finally
+ {
+ await server.StopAsync();
+ }
+ }*/
+
+ }
+}
diff --git a/MyCore/Startup.cs b/MyCore/Startup.cs
index 4b6122f..954c651 100644
--- a/MyCore/Startup.cs
+++ b/MyCore/Startup.cs
@@ -25,6 +25,8 @@ namespace MyCore
public Startup(IConfiguration configuration)
{
Configuration = configuration;
+
+ MQTTService mQTTService = new MQTTService();
}
public IConfiguration Configuration { get; }
diff --git a/MyCore/bin/Debug/netcoreapp2.1/MyCore.deps.json b/MyCore/bin/Debug/netcoreapp2.1/MyCore.deps.json
index 51edaf0..c93aeea 100644
--- a/MyCore/bin/Debug/netcoreapp2.1/MyCore.deps.json
+++ b/MyCore/bin/Debug/netcoreapp2.1/MyCore.deps.json
@@ -1,7 +1,7 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v2.1",
- "signature": "e9e277718dca4942134387f40abf615b4b7fc6e7"
+ "signature": "ed4bfd850661b8103b19e7b3da8cfe9ea4910f9c"
},
"compilationOptions": {
"defines": [
@@ -25,7 +25,7 @@
"MyCore/1.0.0": {
"dependencies": {
"AspNetCore.Security.Jwt": "1.6.0",
- "MQTTnet": "2.8.5",
+ "MQTTnet": "3.0.8",
"Microsoft.AspNetCore.App": "2.1.1",
"Microsoft.AspNetCore.Authentication.JwtBearer": "2.1.2",
"Microsoft.AspNetCore.Razor.Design": "2.1.2",
@@ -579,7 +579,7 @@
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
}
},
- "MQTTnet/2.8.5": {
+ "MQTTnet/3.0.8": {
"dependencies": {
"NETStandard.Library": "2.0.3",
"System.Net.Security": "4.3.2",
@@ -588,8 +588,8 @@
},
"runtime": {
"lib/netstandard2.0/MQTTnet.dll": {
- "assemblyVersion": "2.8.5.0",
- "fileVersion": "2.8.5.0"
+ "assemblyVersion": "3.0.8.0",
+ "fileVersion": "3.0.8.0"
}
},
"compile": {
@@ -4258,12 +4258,12 @@
"path": "mongodb.driver.core/2.8.0",
"hashPath": "mongodb.driver.core.2.8.0.nupkg.sha512"
},
- "MQTTnet/2.8.5": {
+ "MQTTnet/3.0.8": {
"type": "package",
"serviceable": true,
- "sha512": "sha512-scNRIWxjuceFixHkwUkofWw354Az/95T8SW3eAk+edH2caMzihDbzdl3IWQf/mSiEB+5pVnOEWMNMnAZ8d4YHA==",
- "path": "mqttnet/2.8.5",
- "hashPath": "mqttnet.2.8.5.nupkg.sha512"
+ "sha512": "sha512-Pb93BrzmIK2M1wtRM8LM54jF8JNPEm6ai8lz2m6TtKpj+sqkBjWrB/I8VyyieUgOfdjbBX70y4S7uucxnb+RUA==",
+ "path": "mqttnet/3.0.8",
+ "hashPath": "mqttnet.3.0.8.nupkg.sha512"
},
"NETStandard.Library/2.0.3": {
"type": "package",
diff --git a/MyCore/obj/Debug/netcoreapp2.1/MyCore.csproj.FileListAbsolute.txt b/MyCore/obj/Debug/netcoreapp2.1/MyCore.csproj.FileListAbsolute.txt
index eab1dda..f748001 100644
--- a/MyCore/obj/Debug/netcoreapp2.1/MyCore.csproj.FileListAbsolute.txt
+++ b/MyCore/obj/Debug/netcoreapp2.1/MyCore.csproj.FileListAbsolute.txt
@@ -30,5 +30,5 @@ C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\bin\Debug\netcoreapp2.1\
C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\bin\Debug\netcoreapp2.1\MyCore.xml
C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\MyCore.dll
C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\MyCore.pdb
-C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\UserSecretsAssemblyInfo.cs
C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\MyCore.csprojAssemblyReference.cache
+C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\UserSecretsAssemblyInfo.cs
diff --git a/MyCore/obj/project.assets.json b/MyCore/obj/project.assets.json
index 1e6adaf..c51ba1b 100644
--- a/MyCore/obj/project.assets.json
+++ b/MyCore/obj/project.assets.json
@@ -2866,7 +2866,7 @@
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
}
},
- "MQTTnet/2.8.5": {
+ "MQTTnet/3.0.8": {
"type": "package",
"dependencies": {
"NETStandard.Library": "2.0.0",
@@ -8325,31 +8325,23 @@
"mongodb.driver.core.nuspec"
]
},
- "MQTTnet/2.8.5": {
- "sha512": "scNRIWxjuceFixHkwUkofWw354Az/95T8SW3eAk+edH2caMzihDbzdl3IWQf/mSiEB+5pVnOEWMNMnAZ8d4YHA==",
+ "MQTTnet/3.0.8": {
+ "sha512": "Pb93BrzmIK2M1wtRM8LM54jF8JNPEm6ai8lz2m6TtKpj+sqkBjWrB/I8VyyieUgOfdjbBX70y4S7uucxnb+RUA==",
"type": "package",
- "path": "mqttnet/2.8.5",
+ "path": "mqttnet/3.0.8",
"files": [
".nupkg.metadata",
".signature.p7s",
+ "LICENSE",
"lib/net452/MQTTnet.dll",
- "lib/net461/MQTTnet.deps.json",
"lib/net461/MQTTnet.dll",
- "lib/net462/MQTTnet.deps.json",
- "lib/net462/MQTTnet.dll",
- "lib/net470/MQTTnet.deps.json",
- "lib/net470/MQTTnet.dll",
- "lib/net471/MQTTnet.deps.json",
- "lib/net471/MQTTnet.dll",
- "lib/net472/MQTTnet.deps.json",
- "lib/net472/MQTTnet.dll",
"lib/netstandard1.3/MQTTnet.deps.json",
"lib/netstandard1.3/MQTTnet.dll",
"lib/netstandard2.0/MQTTnet.deps.json",
"lib/netstandard2.0/MQTTnet.dll",
"lib/uap10.0/MQTTnet.dll",
"lib/uap10.0/MQTTnet.pri",
- "mqttnet.2.8.5.nupkg.sha512",
+ "mqttnet.3.0.8.nupkg.sha512",
"mqttnet.nuspec"
]
},
@@ -14140,7 +14132,7 @@
"projectFileDependencyGroups": {
".NETCoreApp,Version=v2.1": [
"AspNetCore.Security.Jwt >= 1.6.0",
- "MQTTnet >= 2.8.5",
+ "MQTTnet >= 3.0.8",
"Microsoft.AspNetCore.App >= 2.1.1",
"Microsoft.AspNetCore.Authentication.JwtBearer >= 2.1.2",
"Microsoft.AspNetCore.Razor.Design >= 2.1.2",
@@ -14198,7 +14190,7 @@
},
"MQTTnet": {
"target": "Package",
- "version": "[2.8.5, )"
+ "version": "[3.0.8, )"
},
"Microsoft.AspNetCore.App": {
"suppressParent": "All",