-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
160 lines (135 loc) · 4.55 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
using System;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Devices;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace devicetwins
{
class Program
{
static string IoTHubConnectionString = "";
static string DeviceConnectionString = "";
static string DeviceID = "";
static string desiredProperty = "";
static string reportedProperty = "";
static DeviceClient Client = null;
static int desiredPropertyValue = 30;
static int ReportedPropertyValue = 0;
static async Task Main(string[] args)
{
Console.WriteLine("***********************************************");
Console.WriteLine("Welcome to the Azure IoT Hub Device Twin Tester");
Console.WriteLine();
Console.WriteLine("Author: Pete Gallagher");
Console.WriteLine("Twitter: @pete_codes");
Console.WriteLine("Date: 16th November 2020");
Console.WriteLine();
Console.WriteLine("***********************************************");
Console.WriteLine();
try
{
Console.WriteLine("Enter the IoT Hub Connection String");
IoTHubConnectionString = Console.ReadLine();
Console.WriteLine("Enter the Device Connection String");
DeviceConnectionString = Console.ReadLine();
Console.WriteLine("Enter the Device ID");
DeviceID = Console.ReadLine();
Console.WriteLine("Enter the Desired Property Key");
desiredProperty = Console.ReadLine();
Console.WriteLine("Enter the Reported Property Key");
reportedProperty = Console.ReadLine();
InitClient();
GetDeviceTwinAsync().Wait();
Client.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChanged, null).Wait();
var reportedPropertyValueString = "";
while (true)
{
Console.WriteLine($"Set value for {reportedProperty} Reported Property (Enter to exit)");
reportedPropertyValueString = Console.ReadLine();
if (reportedPropertyValueString != null && reportedPropertyValueString != "")
{
ReportedPropertyValue = Int32.Parse(reportedPropertyValueString);
await ReportProperty();
}
else
{
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
private static async Task GetDeviceTwinAsync()
{
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(IoTHubConnectionString);
try
{
var twin = await registryManager.GetTwinAsync(DeviceID);
Console.WriteLine(twin.ToJson());
}
catch (Exception ex)
{
Console.WriteLine("Error : {0}", ex);
}
}
public static async void InitClient()
{
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt);
Console.WriteLine("Retrieving twin");
await Client.GetTwinAsync();
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
public static async Task ReportProperty()
{
try
{
Console.WriteLine($"Sending reported property {reportedProperty} value");
TwinCollection reportedProperties;
reportedProperties = new TwinCollection();
reportedProperties["coolerOn"] = ReportedPropertyValue;
await Client.UpdateReportedPropertiesAsync(reportedProperties);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Errors: {0}", ex.Message);
}
}
private static async Task OnDesiredPropertyChanged(TwinCollection desiredProperties, object userContext)
{
try
{
Console.WriteLine("Desired property change:");
Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
desiredPropertyValue = desiredProperties[desiredProperty];
Console.WriteLine($"Desired Property {desiredProperty} is now: {desiredPropertyValue}");
}
catch (AggregateException ex)
{
foreach (Exception exception in ex.InnerExceptions)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", exception);
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
}
}