Page 22 - MSDN Magazine, June 2017
P. 22
Then you wrap the resulting array into the Message class, which you send to the cloud with the SendEventAsync method of the DeviceClient class instance. The Message class is the object, which supplements raw data (a JSON object being transferred) with additional properties. These properties are used to track messages being sent between devices and the IoT Hub.
Once you connect to the cloud, you can also start sending telemetry data, much as you did when sending device information.
In the RemoteCamera app, establishing a connection with the cloud and sending device info is triggered by two buttons located on the Cloud tab: Connect and initialize and Send device info. The click event handler of the first button is bound to the Connect method of the RemoteCameraViewModel:
public async Task Connect() {
await CloudHelper.Initialize();
IsConnected = true; }
The click event handler of the second button is wired with the SendDeviceInfo method of the CloudHelper class instance. This method was discussed earlier.
Once you connect to the cloud, you can also start sending telemetry data, much as you did when sending device information.
Figure 11 Remote Messages Deserialization and Parsing
That is, you can use the SendMessage method to which you pass a telemetry object. Here, this object, an instance of the Telemetry- Data class, has just a single property, Brightness. The following shows a complete example of sending telemetry data to the cloud, implemented within the SendBrightness method of the CloudHelper class:
public async void SendBrightness(byte brightness) {
if (IsInitialized) {
// Construct TelemetryData
var telemetryData = new TelemetryData() {
Brightness = brightness };
// Serialize TelemetryData and send it to the cloud
await SendMessage(telemetryData); }
}
SendBrightness is invoked right after obtaining the bright- ness, calculated by the ImageProcessor. This is performed in the ProcessingDone event handler:
private void ImageProcessor_ProcessingDone(object sender, ImageProcessorEventArgs e) {
// Update display through dispatcher DisplayBrightness(e.Brightness);
// Send telemetry
if (remoteCameraViewModel.IsTelemetryActive) {
remoteCameraViewModel.CloudHelper.SendBrightness(e.Brightness);
}
So, if you now run the RemoteCamera app, start the preview and connect to the cloud, you’ll see that brightness values are displayed in the corresponding chart, as shown back in Figure 1. Note that, although simulated devices of the preconfigured solution are ded- icated to sending temperature and humidity as telemetry data, you can also send other values. Here, I’m sending brightness, which is automatically displayed in the appropriate chart.
Handling Remote Commands
The CloudHelper class also implements methods for handling remote commands received from the cloud. Similarly, as with the case of ImageProcessor, I handle commands in the background
Figure 12 Updating the Camera Preview
}
private async Task HandleIncomingMessage(Message message) {
try {
// Deserialize message to remote command
var remoteCommand = MessageHelper.Deserialize(message);
// Parse command ParseCommand(remoteCommand);
// Send confirmation to the cloud
await deviceClient.CompleteAsync(message); }
catch (Exception ex) {
Debug.WriteLine(ex.Message);
// Reject message, if it was not parsed correctly
await deviceClient.RejectAsync(message); }
}
private void ParseCommand(RemoteCommand remoteCommand) {
// Verify remote command name
if (string.Compare(remoteCommand.Name,
CommandHelper.CameraPreviewCommandName) == 0) {
// Raise an event, when the valid command was received UpdateCameraPreviewCommandReceived(this,
new UpdateCameraPreviewCommandEventArgs(
remoteCommand.Parameters.IsPreviewActive)); }
}
private async void CloudHelper_UpdateCameraPreviewCommandReceived( object sender, UpdateCameraPreviewCommandEventArgs e)
{
if (Dispatcher.HasThreadAccess) {
if (e.IsPreviewActive) {
await remoteCameraViewModel.PreviewStart(); }
else {
await remoteCameraViewModel.PreviewStop(); }
} else {
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
CloudHelper_UpdateCameraPreviewCommandReceived(sender, e); });
} }
18 msdn magazine
Internet of Things