forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
163 lines (134 loc) · 6.04 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using Amqp;
using System;
using System.Device.Gpio;
using System.Diagnostics;
using System.Threading;
using AmqpTrace = Amqp.Trace;
#if HAS_WIFI
using System.Device.Wifi;
#endif
namespace AmqpSamples.AzureIoTHub
{
public class Program
{
/////////////////////////////////////////////////////////////////////
// Azure IoT Hub settings
// To make your life easier use a SaS token generated from Azure Device Explorer like the one bellow
// HostName=contoso.azure-devices.net;DeviceId=COFEEMACHINE001;SharedAccessSignature=SharedAccessSignature sr=contoso.azure-devices.net%2Fdevices%2FCOFEEMACHINE001&sig=tGeAGJeRgFUqIKEs%2BtYNLmLAGWGHiHT%2F2TIIsu8oQ%2F0%3D&se=1234656789
const string _hubName = "contoso";
const string _deviceId = "COFEEMACHINE001";
const string _sasToken = "SharedAccessSignature sr=contoso.azure-devices.net%2Fdevices%2FCOFEEMACHINE001&sig=tGeAGJeRgFUqIKEs%2BtYNLmLAGWGHiHT%2F2TIIsu8oQ%2F0%3D&se=1234656789";
#if HAS_WIFI
private static string MySsid = "ssid";
private static string MyPassword = "password";
#endif
/////////////////////////////////////////////////////////////////////
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()
{
Debug.WriteLine("Waiting for network up and IP address...");
bool success;
CancellationTokenSource cs = new(60000);
#if HAS_WIFI
success = WifiNetworkHelper.ConnectDhcp(MySsid, MyPassword, requiresDateTime: true, token: cs.Token);
#else
success = NetworkHelper.SetupAndConnectNetwork(cs.Token, true);
#endif
if (!success)
{
#if HAS_WIFI
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {WifiNetworkHelper.Status}.");
if (WifiNetworkHelper.HelperException != null)
{
Debug.WriteLine($"Exception: {WifiNetworkHelper.HelperException}");
}
#else
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.Status}.");
if (NetworkHelper.HelperException != null)
{
Debug.WriteLine($"Exception: {NetworkHelper.HelperException}");
}
#endif
return;
}
// setup user button
// F769I-DISCO -> USER_BUTTON is @ PA0 -> (0 * 16) + 0 = 0
var _gpioController = new GpioController();
_userButton = _gpioController.OpenPin(0, PinMode.Input);
_userButton.ValueChanged += _userButton_ValueChanged;
// setup AMQP
// set trace level
AmqpTrace.TraceLevel = TraceLevel.Frame | TraceLevel.Information;
// enable trace
AmqpTrace.TraceListener = WriteTrace;
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// *** CHECK README for details on this configuration **** //
Connection.DisableServerCertValidation = false;
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// launch worker thread
new Thread(WorkerThread).Start();
Thread.Sleep(Timeout.Infinite);
}
private static void WorkerThread()
{
// parse Azure IoT Hub Map settings to AMQP protocol settings
string hostName = _hubName + ".azure-devices.net";
string userName = _deviceId + "@sas." + _hubName;
string senderAddress = "devices/" + _deviceId + "/messages/events";
string receiverAddress = "devices/" + _deviceId + "/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)
{
Debug.WriteLine($"received new temperature setting: {setTemperature}");
}
private static void _userButton_ValueChanged(object sender, PinValueChangedEventArgs e)
{
if (e.ChangeType == PinEventTypes.Falling)
{
// 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)
{
Debug.WriteLine(Fx.Format(format, args));
}
}
}