Skip to content

Commit

Permalink
Add AMQP sample pack
Browse files Browse the repository at this point in the history
- Add solution and projects for:
 + Azure IoT Hub client
 + Azure Service Bus client
- Update readme.

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes committed Dec 20, 2018
1 parent f1961b6 commit 3ce1749
Show file tree
Hide file tree
Showing 13 changed files with 700 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -20,7 +20,7 @@ Feel free to browse, take what you like and contribute back if you want.
<tr>
<td><a href="samples/SerialCommunication">SerialCommunication sample pack</a></td>
<td><a href="samples/CAN">CAN sample</a></td>
<td><!--<a href="Utility/util3">Utility Three</a>--></td>
<td><a href="samples/AMQP">AMQP sample pack</a></td>
</tr>
</table>

Expand Down
34 changes: 34 additions & 0 deletions samples/AMQP/Amqp.Samples.sln
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Azure-ServiceBus-Sender", "Azure-ServiceBus-Sender\Azure-ServiceBus-Sender.nfproj", "{366B583C-2BF4-4BEF-A598-9EF13D27EB69}"
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Azure-IoT-Hub", "Azure-IoT-Hub\Azure-IoT-Hub.nfproj", "{5B1F824C-8921-4C51-9E08-09CC3D0A7979}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{366B583C-2BF4-4BEF-A598-9EF13D27EB69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{366B583C-2BF4-4BEF-A598-9EF13D27EB69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{366B583C-2BF4-4BEF-A598-9EF13D27EB69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{366B583C-2BF4-4BEF-A598-9EF13D27EB69}.Release|Any CPU.Build.0 = Release|Any CPU
{366B583C-2BF4-4BEF-A598-9EF13D27EB69}.Release|Any CPU.Deploy.0 = Release|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Release|Any CPU.Build.0 = Release|Any CPU
{5B1F824C-8921-4C51-9E08-09CC3D0A7979}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {016D6E8B-6BC4-46E7-8F2A-4769AA3A5EE8}
EndGlobalSection
EndGlobal
71 changes: 71 additions & 0 deletions samples/AMQP/Azure-IoT-Hub/Azure-IoT-Hub.nfproj
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>5b1f824c-8921-4c51-9e08-09cc3d0a7979</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>AmqpSamples.AzureIoTHub</RootNamespace>
<AssemblyName>Azure.IoTHub</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="..\Azure-ServiceBus-Sender\NetworkHelpers.cs" Link="NetworkHelpers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Amqp.nanoFramework, Version=2.1.0.0, Culture=neutral, PublicKeyToken=905a7b1e6458e0c3">
<HintPath>..\packages\AMQPNetLite.nanoFramework.2.1.5-preview-0016\lib\Amqp.nanoFramework.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="mscorlib, Version=1.1.0.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.1.0\lib\mscorlib.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.Runtime.Events, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Runtime.Events.1.0.2\lib\nanoFramework.Runtime.Events.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.Runtime.Native, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Runtime.Native.1.0.2\lib\nanoFramework.Runtime.Native.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="System.Math, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Math.1.0.2\lib\System.Math.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="System.Net, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Net.1.0.2\lib\System.Net.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Devices.Gpio, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Devices.Gpio.1.0.2\lib\Windows.Devices.Gpio.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
</Project>
135 changes: 135 additions & 0 deletions samples/AMQP/Azure-IoT-Hub/Program.cs
@@ -0,0 +1,135 @@
//
// Copyright (c) 2018 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using Amqp;
using nanoFramework.Networking;
using System;
using System.Threading;
using Windows.Devices.Gpio;
using AmqpTrace = Amqp.Trace;

namespace AmqpSamples.AzureIoTHub
{
public class Program
{
/////////////////////////////////////////////////////////////////////
// Azure IoT Hub settings
const string iotHubName = "<replace-with-iothub-name>";
const string device = "<replace-with-iothub-device-id>";

// use a SaS token generated from Azure Device Explorer like the one bellow
// SharedAccessSignature sr=contoso.azure-devices.net%2Fdevices%2FCOFEEMACHINE001&sig=IOLn3cZi6zl473%2B4jPZpDC7mc1X5LOEIkVeJgqeVZSw%3D&se=1545420774
const string sasToken = "<replace-with-sas-token-for-iothub-device>";
/////////////////////////////////////////////////////////////////////

private static AutoResetEvent sendMessage = new AutoResetEvent(false);

private static int temperature;

private static GpioPin _userButton;

private static Random _random = new Random();

public static void Main()
{
// setup and connect network
NetworkHelpers.SetupAndConnectNetwork(true);

// wait for network and valid system date time
NetworkHelpers.IpAddressAvailable.WaitOne();
NetworkHelpers.DateTimeAvailable.WaitOne();

// setup user button
// F769I-DISCO -> USER_BUTTON is @ PA0 -> (0 * 16) + 0 = 0
_userButton = GpioController.GetDefault().OpenPin(0);
_userButton.SetDriveMode(GpioPinDriveMode.Input);
_userButton.ValueChanged += UserButton_ValueChanged;

// setup AMQP
// set trace level
AmqpTrace.TraceLevel = TraceLevel.Frame | TraceLevel.Information;

// enable trace
AmqpTrace.TraceListener = WriteTrace;

// disable server certificate validation
Connection.DisableServerCertValidation = true;

// launch worker thread
new Thread(WorkerThread).Start();

Thread.Sleep(Timeout.Infinite);
}

private static void WorkerThread()
{
// need to wait for network connection...
NetworkHelpers.IpAddressAvailable.WaitOne();

// ... and valid date time
NetworkHelpers.DateTimeAvailable.WaitOne();

// parse Azure IoT Hub Map settings to AMQP protocol settings
string hostName = iotHubName + ".azure-devices.net";
string userName = device + "@sas." + iotHubName;
string senderAddress = "devices/" + device + "/messages/events";
string receiverAddress = "devices/" + device + "/messages/deviceBound";

Connection connection = new Connection(new Address(hostName, 5671, userName, sasToken));
Session session = new Session(connection);
SenderLink sender = new SenderLink(session, "send-link", senderAddress);
ReceiverLink receiver = new ReceiverLink(session, "receive-link", receiverAddress);
receiver.Start(100, OnMessage);

while (true)
{
// wait for button press
sendMessage.WaitOne();

// compose message
Message message = new Message();
message.ApplicationProperties = new Amqp.Framing.ApplicationProperties();
message.ApplicationProperties["temperature"] = temperature;

// send message with temperature
sender.Send(message, null, null);
}

// the loop won't reach here, just kept here for the ceremony
sender.Close();
session.Close();
connection.Close();
}

private static void OnMessage(IReceiverLink receiver, Message message)
{
// command received
int setTemperature = (int)message.ApplicationProperties["settemp"];
OnAction(setTemperature);
}

private static void OnAction(int setTemperature)
{
Console.WriteLine($"received new temperature setting: {setTemperature}");
}

private static void UserButton_ValueChanged(object sender, GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
// user button pressed, generate a random temperature value
temperature = _random.Next(50);

// signal event
sendMessage.Set();
}
}

static void WriteTrace(TraceLevel level, string format, params object[] args)
{
Console.WriteLine(Fx.Format(format, args));
}
}
}
33 changes: 33 additions & 0 deletions samples/AMQP/Azure-IoT-Hub/Properties/AssemblyInfo.cs
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CSharp.BlankApplication")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharp.BlankApplication")]
[assembly: AssemblyCopyright("Copyright © ")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
10 changes: 10 additions & 0 deletions samples/AMQP/Azure-IoT-Hub/packages.config
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AMQPNetLite.nanoFramework" version="2.1.5-preview-0016" targetFramework="netnanoframework10" />
<package id="nanoFramework.CoreLibrary" version="1.1.0" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.0.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Native" version="1.0.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Math" version="1.0.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Net" version="1.0.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Devices.Gpio" version="1.0.2" targetFramework="netnanoframework10" />
</packages>
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>366b583c-2bf4-4bef-a598-9ef13d27eb69</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>AmqpSamples.AzureSB.Sender</RootNamespace>
<AssemblyName>AzureSB.Sender</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="NetworkHelpers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Amqp.nanoFramework, Version=2.1.0.0, Culture=neutral, PublicKeyToken=905a7b1e6458e0c3">
<HintPath>..\packages\AMQPNetLite.nanoFramework.2.1.5-preview-0016\lib\Amqp.nanoFramework.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="mscorlib, Version=1.1.0.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.1.0\lib\mscorlib.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.Runtime.Events, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Runtime.Events.1.0.2\lib\nanoFramework.Runtime.Events.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="nanoFramework.Runtime.Native, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Runtime.Native.1.0.2\lib\nanoFramework.Runtime.Native.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="System.Math, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Math.1.0.2\lib\System.Math.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="System.Net, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.System.Net.1.0.2\lib\System.Net.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Devices.Gpio, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Devices.Gpio.1.0.2\lib\Windows.Devices.Gpio.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
</Project>

0 comments on commit 3ce1749

Please sign in to comment.