Skip to content

Commit fecda4d

Browse files
committed
Multi Client and Instances of MQTT working
1 parent 7649bdb commit fecda4d

38 files changed

+901
-77
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<UserSecretsId>dotnet-ClientSampleBackgroundService-09C4A66B-2D89-4956-B8E3-14EF4DBB3A20</UserSecretsId>
6+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
7+
<DockerfileContext>..\CorefluxCSharpAPI</DockerfileContext>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\CorefluxCSharpAPI\CorefluxCSharpAPI.csproj" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace ClientSampleBackgroundService
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
CreateHostBuilder(args).Build().Run();
15+
}
16+
17+
public static IHostBuilder CreateHostBuilder(string[] args) =>
18+
Host.CreateDefaultBuilder(args)
19+
.ConfigureServices((hostContext, services) =>
20+
{
21+
services.AddHostedService<Worker>();
22+
});
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"ClientSampleBackgroundService": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": "true",
6+
"environmentVariables": {
7+
"DOTNET_ENVIRONMENT": "Development"
8+
}
9+
}
10+
}
11+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Logging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Coreflux.API.Networking.MQTT;
9+
10+
namespace ClientSampleBackgroundService
11+
{
12+
public class Worker : BackgroundService
13+
{
14+
private readonly ILogger<Worker> _logger;
15+
private MQTTControllerInstance MQTTControllerInstance;
16+
public bool isConnected;
17+
public Worker(ILogger<Worker> logger)
18+
{
19+
_logger = logger;
20+
MQTTControllerInstance = new MQTTControllerInstance();
21+
MQTTControllerInstance.OnConnect += MQTTControllerInstance_OnConnect;
22+
MQTTControllerInstance.OnDisconnect += MQTTControllerInstance_OnDisconnect;
23+
MQTTControllerInstance.NewPayload += MQTTControllerInstance_NewPayload;
24+
isConnected = false;
25+
}
26+
27+
private void MQTTControllerInstance_NewPayload(MQTTNewPayload obj)
28+
{
29+
_logger.LogInformation("received" + obj.topic+ " , "+ obj.payload +" @ {time} ", DateTimeOffset.Now);
30+
}
31+
32+
private void MQTTControllerInstance_OnDisconnect()
33+
{
34+
_logger.LogInformation("Disconnected of broker {time}", DateTimeOffset.Now);
35+
isConnected = false;
36+
// ReConnect();
37+
38+
}
39+
40+
private void MQTTControllerInstance_OnConnect()
41+
{
42+
_logger.LogInformation("Connected to broker {time}", DateTimeOffset.Now);
43+
isConnected = true;
44+
}
45+
46+
private async void ReConnect()
47+
{
48+
try
49+
{
50+
await MQTTControllerInstance.StartAsync("127.0.0.1");
51+
}
52+
catch
53+
{
54+
_logger.LogInformation("Failed to find the broker {time}", DateTimeOffset.Now);
55+
}
56+
}
57+
58+
59+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
60+
{
61+
try {
62+
await MQTTControllerInstance.StartAsync("127.0.0.1");
63+
}
64+
catch
65+
{
66+
_logger.LogInformation("Failed to find the broker {time}", DateTimeOffset.Now);
67+
}
68+
while (!stoppingToken.IsCancellationRequested)
69+
{
70+
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
71+
if(isConnected)
72+
{
73+
74+
await MQTTControllerInstance.SetDataAsync("teste", "1234");
75+
var t=MQTTControllerInstance.GetDataAsync("teste").GetAwaiter();
76+
var q= t.GetResult();
77+
_logger.LogInformation("received" + q + " @ {time} ", DateTimeOffset.Now);
78+
}
79+
else
80+
{
81+
try
82+
{
83+
await MQTTControllerInstance.StartAsync("127.0.0.1");
84+
}
85+
catch
86+
{
87+
_logger.LogInformation("Failed to find the broker {time}", DateTimeOffset.Now);
88+
}
89+
}
90+
await Task.Delay(10000, stoppingToken);
91+
}
92+
}
93+
}
94+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<RootNamespace>Coreflux.API.Networking.MQTT.UTest</RootNamespace>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
13+
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
14+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\CorefluxCSharpAPI\CorefluxCSharpAPI.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

CorefluxCSharpAPI-UTest/MQTT.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
6+
namespace Coreflux.API.Networking.MQTT.UTest
7+
{
8+
[TestClass]
9+
public class MQTTTest
10+
{
11+
12+
[TestMethod]
13+
public async void StaticMqttControllerStartLocalMQTT()
14+
{
15+
var teste=await StaticMqttControllerStart("127.0.0.1");
16+
Assert.AreEqual(true, teste);
17+
}
18+
19+
20+
private async Task<bool> StaticMqttControllerStart(string IP, int port = 1883, string user = "", string password = "", bool mqttSecure = false, bool usingWebSocket = false)
21+
{
22+
bool isConnectedByEvent = false;
23+
bool isConnectedByCheckConnect = false;
24+
25+
26+
MQTTController.OnConnect += () => {
27+
isConnectedByEvent = false;
28+
};
29+
30+
MQTTController.Start(IP,port,user,password,mqttSecure,usingWebSocket);
31+
32+
await Task.Delay(2000);
33+
isConnectedByCheckConnect = MQTTController.Connected();
34+
35+
36+
Assert.AreEqual(true, isConnectedByCheckConnect);
37+
38+
return isConnectedByCheckConnect && isConnectedByEvent;
39+
40+
}
41+
}
42+
}
Binary file not shown.
Binary file not shown.

CorefluxCSharpAPI/CorefluxCSharpAPI.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AssemblyName>CorefluxMQTTcSharpAPI</AssemblyName>
66
<ApplicationIcon></ApplicationIcon>
7-
<Version>1.2.0.0</Version>
7+
<Version>1.2.0.2</Version>
88
<Company>Coreflux</Company>
99
<Product>CorefluxMQTT</Product>
1010
<Authors>Setlevel</Authors>
1111
<Copyright>Setlevel 2019 - 2021</Copyright>
1212
<PackageIcon>CF.png</PackageIcon>
13-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
14-
<FileVersion>1.2.0.0</FileVersion>
13+
<AssemblyVersion>1.2.0.2</AssemblyVersion>
14+
<FileVersion>1.2.0.2</FileVersion>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
<PackageProjectUrl>coreflux.org</PackageProjectUrl>

CorefluxCSharpAPI/CorefluxCSharpAPI.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30711.63
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32112.339
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CorefluxCSharpAPI", "CorefluxCSharpAPI.csproj", "{166CEC83-23EC-4E4E-BEDA-89C0CE34A877}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientSampleBackgroundService", "..\ClientSampleBackgroundService\ClientSampleBackgroundService.csproj", "{915CD5CC-EC68-4835-AACE-A2B26936B78C}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{166CEC83-23EC-4E4E-BEDA-89C0CE34A877}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{166CEC83-23EC-4E4E-BEDA-89C0CE34A877}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{166CEC83-23EC-4E4E-BEDA-89C0CE34A877}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{915CD5CC-EC68-4835-AACE-A2B26936B78C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{915CD5CC-EC68-4835-AACE-A2B26936B78C}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{915CD5CC-EC68-4835-AACE-A2B26936B78C}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{915CD5CC-EC68-4835-AACE-A2B26936B78C}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)