mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
MQTTService WIP connection to Hassio server
This commit is contained in:
parent
68e0338fe7
commit
e49daefae3
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
|
using MQTTnet.Client.Options;
|
||||||
|
|
||||||
namespace MyCore.Controllers
|
namespace MyCore.Controllers
|
||||||
{
|
{
|
||||||
@ -15,6 +16,7 @@ namespace MyCore.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class MQTTController : ControllerBase
|
public class MQTTController : ControllerBase
|
||||||
{
|
{
|
||||||
|
private string _mqttServer = "192.168.0.8";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// It's a mqtt publish test ! :)
|
/// It's a mqtt publish test ! :)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -22,7 +24,16 @@ namespace MyCore.Controllers
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public void GetToPublishMqtt()
|
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()
|
protected async Task Client_Publish()
|
||||||
@ -32,7 +43,7 @@ namespace MyCore.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var client1 = new MqttFactory().CreateMqttClient();
|
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();
|
var message = new MqttApplicationMessageBuilder().WithPayload("It's a test").WithTopic("IpAddress").WithRetainFlag().Build();
|
||||||
await client1.PublishAsync(message);
|
await client1.PublishAsync(message);
|
||||||
|
|
||||||
|
|||||||
@ -24,7 +24,7 @@
|
|||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.0.2105168" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.8.0" />
|
||||||
<PackageReference Include="MQTTnet" Version="2.8.5" />
|
<PackageReference Include="MQTTnet" Version="3.0.8" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
129
MyCore/Services/MQTTService.cs
Normal file
129
MyCore/Services/MQTTService.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -25,6 +25,8 @@ namespace MyCore
|
|||||||
public Startup(IConfiguration configuration)
|
public Startup(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
Configuration = configuration;
|
Configuration = configuration;
|
||||||
|
|
||||||
|
MQTTService mQTTService = new MQTTService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"runtimeTarget": {
|
"runtimeTarget": {
|
||||||
"name": ".NETCoreApp,Version=v2.1",
|
"name": ".NETCoreApp,Version=v2.1",
|
||||||
"signature": "e9e277718dca4942134387f40abf615b4b7fc6e7"
|
"signature": "ed4bfd850661b8103b19e7b3da8cfe9ea4910f9c"
|
||||||
},
|
},
|
||||||
"compilationOptions": {
|
"compilationOptions": {
|
||||||
"defines": [
|
"defines": [
|
||||||
@ -25,7 +25,7 @@
|
|||||||
"MyCore/1.0.0": {
|
"MyCore/1.0.0": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"AspNetCore.Security.Jwt": "1.6.0",
|
"AspNetCore.Security.Jwt": "1.6.0",
|
||||||
"MQTTnet": "2.8.5",
|
"MQTTnet": "3.0.8",
|
||||||
"Microsoft.AspNetCore.App": "2.1.1",
|
"Microsoft.AspNetCore.App": "2.1.1",
|
||||||
"Microsoft.AspNetCore.Authentication.JwtBearer": "2.1.2",
|
"Microsoft.AspNetCore.Authentication.JwtBearer": "2.1.2",
|
||||||
"Microsoft.AspNetCore.Razor.Design": "2.1.2",
|
"Microsoft.AspNetCore.Razor.Design": "2.1.2",
|
||||||
@ -579,7 +579,7 @@
|
|||||||
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
|
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"MQTTnet/2.8.5": {
|
"MQTTnet/3.0.8": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"NETStandard.Library": "2.0.3",
|
"NETStandard.Library": "2.0.3",
|
||||||
"System.Net.Security": "4.3.2",
|
"System.Net.Security": "4.3.2",
|
||||||
@ -588,8 +588,8 @@
|
|||||||
},
|
},
|
||||||
"runtime": {
|
"runtime": {
|
||||||
"lib/netstandard2.0/MQTTnet.dll": {
|
"lib/netstandard2.0/MQTTnet.dll": {
|
||||||
"assemblyVersion": "2.8.5.0",
|
"assemblyVersion": "3.0.8.0",
|
||||||
"fileVersion": "2.8.5.0"
|
"fileVersion": "3.0.8.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"compile": {
|
"compile": {
|
||||||
@ -4258,12 +4258,12 @@
|
|||||||
"path": "mongodb.driver.core/2.8.0",
|
"path": "mongodb.driver.core/2.8.0",
|
||||||
"hashPath": "mongodb.driver.core.2.8.0.nupkg.sha512"
|
"hashPath": "mongodb.driver.core.2.8.0.nupkg.sha512"
|
||||||
},
|
},
|
||||||
"MQTTnet/2.8.5": {
|
"MQTTnet/3.0.8": {
|
||||||
"type": "package",
|
"type": "package",
|
||||||
"serviceable": true,
|
"serviceable": true,
|
||||||
"sha512": "sha512-scNRIWxjuceFixHkwUkofWw354Az/95T8SW3eAk+edH2caMzihDbzdl3IWQf/mSiEB+5pVnOEWMNMnAZ8d4YHA==",
|
"sha512": "sha512-Pb93BrzmIK2M1wtRM8LM54jF8JNPEm6ai8lz2m6TtKpj+sqkBjWrB/I8VyyieUgOfdjbBX70y4S7uucxnb+RUA==",
|
||||||
"path": "mqttnet/2.8.5",
|
"path": "mqttnet/3.0.8",
|
||||||
"hashPath": "mqttnet.2.8.5.nupkg.sha512"
|
"hashPath": "mqttnet.3.0.8.nupkg.sha512"
|
||||||
},
|
},
|
||||||
"NETStandard.Library/2.0.3": {
|
"NETStandard.Library/2.0.3": {
|
||||||
"type": "package",
|
"type": "package",
|
||||||
|
|||||||
@ -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\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.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\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\MyCore.csprojAssemblyReference.cache
|
||||||
|
C:\Users\thoma\OneDrive\Documents\Travail\MyCore\MyCore\obj\Debug\netcoreapp2.1\UserSecretsAssemblyInfo.cs
|
||||||
|
|||||||
@ -2866,7 +2866,7 @@
|
|||||||
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
|
"lib/netstandard1.5/MongoDB.Driver.Core.dll": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"MQTTnet/2.8.5": {
|
"MQTTnet/3.0.8": {
|
||||||
"type": "package",
|
"type": "package",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"NETStandard.Library": "2.0.0",
|
"NETStandard.Library": "2.0.0",
|
||||||
@ -8325,31 +8325,23 @@
|
|||||||
"mongodb.driver.core.nuspec"
|
"mongodb.driver.core.nuspec"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"MQTTnet/2.8.5": {
|
"MQTTnet/3.0.8": {
|
||||||
"sha512": "scNRIWxjuceFixHkwUkofWw354Az/95T8SW3eAk+edH2caMzihDbzdl3IWQf/mSiEB+5pVnOEWMNMnAZ8d4YHA==",
|
"sha512": "Pb93BrzmIK2M1wtRM8LM54jF8JNPEm6ai8lz2m6TtKpj+sqkBjWrB/I8VyyieUgOfdjbBX70y4S7uucxnb+RUA==",
|
||||||
"type": "package",
|
"type": "package",
|
||||||
"path": "mqttnet/2.8.5",
|
"path": "mqttnet/3.0.8",
|
||||||
"files": [
|
"files": [
|
||||||
".nupkg.metadata",
|
".nupkg.metadata",
|
||||||
".signature.p7s",
|
".signature.p7s",
|
||||||
|
"LICENSE",
|
||||||
"lib/net452/MQTTnet.dll",
|
"lib/net452/MQTTnet.dll",
|
||||||
"lib/net461/MQTTnet.deps.json",
|
|
||||||
"lib/net461/MQTTnet.dll",
|
"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.deps.json",
|
||||||
"lib/netstandard1.3/MQTTnet.dll",
|
"lib/netstandard1.3/MQTTnet.dll",
|
||||||
"lib/netstandard2.0/MQTTnet.deps.json",
|
"lib/netstandard2.0/MQTTnet.deps.json",
|
||||||
"lib/netstandard2.0/MQTTnet.dll",
|
"lib/netstandard2.0/MQTTnet.dll",
|
||||||
"lib/uap10.0/MQTTnet.dll",
|
"lib/uap10.0/MQTTnet.dll",
|
||||||
"lib/uap10.0/MQTTnet.pri",
|
"lib/uap10.0/MQTTnet.pri",
|
||||||
"mqttnet.2.8.5.nupkg.sha512",
|
"mqttnet.3.0.8.nupkg.sha512",
|
||||||
"mqttnet.nuspec"
|
"mqttnet.nuspec"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -14140,7 +14132,7 @@
|
|||||||
"projectFileDependencyGroups": {
|
"projectFileDependencyGroups": {
|
||||||
".NETCoreApp,Version=v2.1": [
|
".NETCoreApp,Version=v2.1": [
|
||||||
"AspNetCore.Security.Jwt >= 1.6.0",
|
"AspNetCore.Security.Jwt >= 1.6.0",
|
||||||
"MQTTnet >= 2.8.5",
|
"MQTTnet >= 3.0.8",
|
||||||
"Microsoft.AspNetCore.App >= 2.1.1",
|
"Microsoft.AspNetCore.App >= 2.1.1",
|
||||||
"Microsoft.AspNetCore.Authentication.JwtBearer >= 2.1.2",
|
"Microsoft.AspNetCore.Authentication.JwtBearer >= 2.1.2",
|
||||||
"Microsoft.AspNetCore.Razor.Design >= 2.1.2",
|
"Microsoft.AspNetCore.Razor.Design >= 2.1.2",
|
||||||
@ -14198,7 +14190,7 @@
|
|||||||
},
|
},
|
||||||
"MQTTnet": {
|
"MQTTnet": {
|
||||||
"target": "Package",
|
"target": "Package",
|
||||||
"version": "[2.8.5, )"
|
"version": "[3.0.8, )"
|
||||||
},
|
},
|
||||||
"Microsoft.AspNetCore.App": {
|
"Microsoft.AspNetCore.App": {
|
||||||
"suppressParent": "All",
|
"suppressParent": "All",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user