Page 21 - MSDN Magazine, June 2017
P. 21

multicolor
hit-
highlighting
easy
options
®
Figure 10 Sending Device Information
public async Task SendDeviceInfo() {
var deviceInfo = new DeviceInfo() {
IsSimulatedDevice = false,
ObjectType = "DeviceInfo",
Version = "1.0",
DeviceProperties = new DeviceProperties(Configuration.DeviceId),
// Commands collection Commands = new Command[] {
CommandHelper.CreateCameraPreviewStatusCommand() }
};
await SendMessage(deviceInfo); }
of the DeviceAuthenticationWithRegistrySymmetricKey class. I use this to simplify creation of the DeviceClient class instance.
Once the connection is made, all you need to do is to send the DeviceInfo, as shown in Figure 10.
The RemoteCamera app sends the device info, which describes the real hardware, so IsSimulatedDevice property is set to false. As I men- tioned, ObjectType is set to DeviceInfo. Moreover, I set the Version property to 1.0. For DeviceProperties I use arbitrary values, which mostly consist of static strings (see the SetDefaultValues method of the DeviceProperties class). I also define one remote command, Update camera preview, which enables remote control of the camera preview. This command has one Boolean parameter, IsPreviewActive, which specifies whether the camera preview should be started or stopped (see the CommandHelper.cs file under AzureHelpers folder).
To establish communication between your IoT device and the IoT Hub you use the Microsoft.Azure.Devices.Client NuGet package.
To actually send data to the cloud I implement the SendMessage method:
private async Task SendMessage(Object message) {
var serializedMessage = MessageHelper.Serialize(message);
await deviceClient.SendEventAsync(serializedMessage); }
Basically, you need to serialize your C# object to a byte array that contains JSON-formatted objects (see the MessageHelper static class from AzureHelpers subfolder):
public static Message Serialize(object obj) {
ArgumentCheck.IsNull(obj, "obj");
var jsonData = JsonConvert.SerializeObject(obj);
return new Message(Encoding.UTF8.GetBytes(jsonData)); }
Instantly Search Terabytes of Data
across a desktop, network, Internet or Intranet site with dtSearch enterprise and developer products
Over 25 search features, with
dtSearch’s document filters support popular file types, emails with multilevel attachments, databases, web data
Developers:
• APIs for .NET, Java and C++
• SDKs for Windows, UWP, Linux, Mac and Android
• See dtSearch.com for articles on faceted search, advanced data classification, working with SQL, NoSQL & other DBs, MS Azure, etc.
Visit dtSearch.com for
• hundreds of reviews and case studies • fully-functional evaluations
The Smart Choice for Text Retrieval® since 1991
dtSearch.com 1-800-IT-FINDS
msdnmagazine.com
























































   19   20   21   22   23