Skip to content

Commit

Permalink
Added source code and image folders
Browse files Browse the repository at this point in the history
  • Loading branch information
sundarnarasiman committed May 22, 2019
1 parent 4fc7fd3 commit 1697277
Show file tree
Hide file tree
Showing 42 changed files with 2,490 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*-perl-*-

package.Aws-iot-dotnet-app-mqtt-over-websockets-sigv4 = {
interfaces = (1.0);

deploy = {
generic = true;
};

build-environment = {
chroot = basic;
network-access = blocked;
};

# Use NoOpBuild. See https://w.amazon.com/index.php/BrazilBuildSystem/NoOpBuild
build-system = no-op;
build-tools = {
1.0 = {
NoOpBuild = 1.0;
};
};

# Use runtime-dependencies for when you want to bring in additional
# packages when deploying.
# Use dependencies instead if you intend for these dependencies to
# be exported to other packages that build against you.
dependencies = {
1.0 = {
};
};

runtime-dependencies = {
1.0 = {
};
};

};
Binary file added Dotnet core console app/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AwsIOTMqttOverWebsockets", "AwsIOTMqttOverWebsockets\AwsIOTMqttOverWebsockets.csproj", "{E52C2A04-5BED-49D6-B9A2-311935301432}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E52C2A04-5BED-49D6-B9A2-311935301432}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E52C2A04-5BED-49D6-B9A2-311935301432}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E52C2A04-5BED-49D6-B9A2-311935301432}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E52C2A04-5BED-49D6-B9A2-311935301432}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Utils\" />
<Folder Include="Messaging\" />
<Folder Include="Model\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MQTTnet" Version="2.8.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup>
<ItemGroup>
<None Remove="rolling.log" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Compile Remove="Model\ICloudConnectorConfig.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MQTTnet.Client;
using MQTTnet;
using AwsIOTMqttOverWebsockets.Utils;
using AwsIOTMqttOverWebsockets.Model;
using System.Threading;

namespace AwsIOTMqttOverWebsockets.Messaging
{
public class CloudConnector
{
private CloudConnectionConfig cloudConnectionConfig;
public IMqttClient mqttClient;
private IMqttClientOptions mqttClientOptions;
private bool IsConnected;


public CloudConnector(CloudConnectionConfig cloudConnectionConfig1)
{
cloudConnectionConfig = cloudConnectionConfig1;
}

public void ConnectToAwsIOT()
{
try
{


AwsMqttConnection awsMqttConnection = new AwsMqttConnection();
awsMqttConnection.Host = cloudConnectionConfig.Host;
awsMqttConnection.Region = cloudConnectionConfig.Region;
awsMqttConnection.AccessKey = cloudConnectionConfig.AccessKey;
awsMqttConnection.SecretKey = cloudConnectionConfig.SecretKey;

awsMqttConnection.ClientId = new Guid();

string requestUrl = awsMqttConnection.GetRequesturl();

var factory = new MqttFactory();
mqttClient = factory.CreateMqttClient();


mqttClientOptions = new MqttClientOptionsBuilder()
.WithWebSocketServer(requestUrl)
.Build();



mqttClient.ConnectAsync(mqttClientOptions).Wait();
IsConnected = true;
Logger.LogInfo("Connected successfully .....");

}
catch (Exception ex)
{
Logger.LogDebug(ex.Message);
}
}



public async void PublishMessage()
{
try
{

if (mqttClient==null)
{
ConnectToAwsIOT();


}

if (IsConnected)

{
await mqttClient.PublishAsync(cloudConnectionConfig.TopicToPublish, cloudConnectionConfig.MessageToPublish, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce, false);


Logger.LogInfo("Message published successfully");
}

else
{

Logger.LogInfo("Waiting for connection to complete");
}
}
catch (Exception ex)
{
Logger.LogDebug(ex.Message);
}
}

public async void SubscribeMessage()
{

try
{

if (mqttClient == null)
{
ConnectToAwsIOT();

}



if (IsConnected)

{
string topic = cloudConnectionConfig.TopicToSubscribe;
mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;

await mqttClient.SubscribeAsync(cloudConnectionConfig.TopicToSubscribe);
Logger.LogInfo("subscribed");

}

else
{
Logger.LogInfo("Waiting for connection to complete");
}
}
catch (Exception ex)
{
Logger.LogDebug(ex.Message);
}
}

private void MqttClient_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
{
AwsMqttConnection awsMqttConnection = new AwsMqttConnection();
string message=awsMqttConnection.ProcessReceivedMessages(e);
Logger.LogInfo(message);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using AwsIOTMqttOverWebsockets.Model;
namespace AwsIOTMqttOverWebsockets.Messaging
{
public static class ConnectionConfigManager
{
private static CloudConnectionConfig cloudConnectionConfig;

public static CloudConnectionConfig GetConnectionConfig()
{

if (cloudConnectionConfig==null)
{

cloudConnectionConfig = new CloudConnectionConfig();
}

return cloudConnectionConfig;


}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AwsIOTMqttOverWebsockets.Utils;
using MQTTnet.Client;
using MQTTnet;
namespace AwsIOTMqttOverWebsockets.Model
{
public class AwsMqttConnection
{
public string Host { get; set; }

public string Region { get; set; }

public string AccessKey { get; set; }

public string SecretKey { get; set; }

public string SubscribeTopic { get; set; }

public string PublishTopic { get; set; }


public Guid ClientId { get; set; }

public string RequestUrl { get; set; }

public string GetRequesturl()
{
string requestUrl = Sigv4util.getSignedurl(Host, Region, AccessKey, SecretKey);
return requestUrl;

}

public string ProcessReceivedMessages(MqttApplicationMessageReceivedEventArgs e)
{
StringBuilder stringBuilder = new StringBuilder();
string payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload, 0, e.ApplicationMessage.Payload.Length);

stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("### RECEIVED APPLICATION MESSAGE ###");
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("Topic " + e.ApplicationMessage.Topic);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("Pay load" + payload);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("QOS " + e.ApplicationMessage.QualityOfServiceLevel);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("QOS " + "Retain " + e.ApplicationMessage.Retain);

return stringBuilder.ToString();
}
}
}
Loading

0 comments on commit 1697277

Please sign in to comment.