-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding v1 of IOT hub, simulated device code
- Loading branch information
Showing
2 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Devices; | ||
using Newtonsoft.Json; | ||
namespace ReadFileUploadNotification | ||
{ | ||
class Program | ||
{ | ||
static ServiceClient serviceClient; | ||
static string connectionString = "HostName=ImageHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=oJiZcgM9UYQD/jbTnk7xq3VrdOIgY/6ZDR9C7cVDw1Y="; | ||
|
||
private async static void ReceiveFileUploadNotificationAsync() | ||
{ | ||
var notificationReceiver = serviceClient.GetFileNotificationReceiver(); | ||
|
||
Console.WriteLine("\nReceiving file upload notification from service"); | ||
while (true) | ||
{ | ||
var fileUploadNotification = await notificationReceiver.ReceiveAsync(); | ||
if (fileUploadNotification == null) continue; | ||
|
||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
Console.WriteLine("Received file upload notification: {0}", | ||
string.Join(", ", new String[] { fileUploadNotification.BlobName, fileUploadNotification.BlobUri, fileUploadNotification.DeviceId })); | ||
|
||
Console.WriteLine("\nSending notification to device"); | ||
|
||
//var commandMessage = new | ||
// Message(Encoding.ASCII.GetBytes("Cloud to device message that file is uploaded")); | ||
|
||
var dataToSend = new | ||
{ | ||
imgURI = fileUploadNotification.BlobUri, | ||
deviceId = fileUploadNotification.DeviceId, | ||
location = new | ||
{ | ||
line1 = "100 Linn Street", | ||
line2 = "unit 1", | ||
city = "Seattle", | ||
state = "WA", | ||
zip = "98012" | ||
} | ||
}; | ||
var messageString = JsonConvert.SerializeObject(dataToSend); | ||
var commandMessage = new Message(Encoding.ASCII.GetBytes(messageString)); | ||
Console.WriteLine("Sending {0}", commandMessage); | ||
await serviceClient.SendAsync("VasundharasDevice", commandMessage); | ||
|
||
Console.ResetColor(); | ||
|
||
await notificationReceiver.CompleteAsync(fileUploadNotification); | ||
} | ||
} | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Receive file upload notifications\n"); | ||
serviceClient = ServiceClient.CreateFromConnectionString(connectionString); | ||
ReceiveFileUploadNotificationAsync(); | ||
Console.WriteLine("Press Enter to exit\n"); | ||
Console.ReadLine(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
// This application uses the Azure IoT Hub device SDK for .NET | ||
// For samples see: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/iothub/device/samples | ||
|
||
using System; | ||
using Microsoft.Azure.Devices.Client; | ||
using Newtonsoft.Json; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
namespace simulated_device | ||
{ | ||
class SimulatedDevice | ||
{ | ||
private static DeviceClient s_deviceClient; | ||
|
||
// The device connection string to authenticate the device with your IoT hub. | ||
// Using the Azure CLI: | ||
// az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyDotnetDevice --output table | ||
private readonly static string s_connectionString = "HostName=ImageHub.azure-devices.net;DeviceId=VasundharasDevice;SharedAccessKey=X1hnIA893PGve0zk0dq/GzJ4AzSknMBGUnTcnCCYGoA="; | ||
|
||
// Async method to send simulated telemetry | ||
private static async void SendDeviceToCloudMessagesAsync() | ||
{ | ||
// Initial telemetry values | ||
double minTemperature = 20; | ||
double minHumidity = 60; | ||
Random rand = new Random(); | ||
|
||
while (true) | ||
{ | ||
double currentTemperature = minTemperature + rand.NextDouble() * 15; | ||
double currentHumidity = minHumidity + rand.NextDouble() * 20; | ||
|
||
// Create JSON message | ||
var telemetryDataPoint = new | ||
{ | ||
temperature = currentTemperature, | ||
humidity = currentHumidity | ||
}; | ||
var messageString = JsonConvert.SerializeObject(telemetryDataPoint); | ||
var message = new Message(Encoding.ASCII.GetBytes(messageString)); | ||
|
||
// Add a custom application property to the message. | ||
// An IoT hub can filter on these properties without access to the message body. | ||
message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); | ||
|
||
// Send the telemetry message | ||
await s_deviceClient.SendEventAsync(message); | ||
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); | ||
|
||
await Task.Delay(1000); | ||
} | ||
} | ||
|
||
private static async void SendUploadDetailsInEvent(string msg) | ||
{ | ||
Console.WriteLine("sending event to iot hub for cosmos"); | ||
Random rand = new Random(); | ||
// Create JSON message | ||
var testData = new | ||
{ | ||
imgURI = msg, | ||
deviceId = rand.NextDouble() | ||
}; | ||
var messageString = JsonConvert.SerializeObject(testData); | ||
var message = new Message(Encoding.ASCII.GetBytes(messageString)); | ||
|
||
// Add a custom application property to the message. | ||
// An IoT hub can filter on these properties without access to the message body. | ||
//message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); | ||
|
||
// Send the telemetry message | ||
await s_deviceClient.SendEventAsync(message); | ||
Console.WriteLine("{0} > Sending message to cloud: {1}", DateTime.Now, messageString); | ||
|
||
} | ||
|
||
private static async void ReceiveC2dAsync() | ||
{ | ||
Console.WriteLine("\nReceiving cloud to device messages from service"); | ||
while (true) | ||
{ | ||
Message receivedMessage = await s_deviceClient.ReceiveAsync(); | ||
if (receivedMessage == null) continue; | ||
|
||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
|
||
string msg = Encoding.ASCII.GetString(receivedMessage.GetBytes()); | ||
|
||
Console.WriteLine("Received message: {0}", msg); | ||
Console.ResetColor(); | ||
|
||
await s_deviceClient.CompleteAsync(receivedMessage); | ||
|
||
SendUploadDetailsInEvent(msg); | ||
} | ||
} | ||
|
||
private static async void SendToBlobAsync() | ||
{ | ||
string fileName = "image1.jpg"; | ||
Console.WriteLine("Uploading file: {0}", fileName); | ||
var watch = System.Diagnostics.Stopwatch.StartNew(); | ||
|
||
using (var sourceData = new FileStream(@"image.jpg", FileMode.Open)) | ||
{ | ||
await s_deviceClient.UploadToBlobAsync(fileName, sourceData); | ||
} | ||
|
||
watch.Stop(); | ||
Console.WriteLine("Time to upload file: {0}ms\n", watch.ElapsedMilliseconds); | ||
} | ||
|
||
private static void Main(string[] args) | ||
{ | ||
Console.WriteLine("IoT Hub Quickstarts #1 - Simulated device. Ctrl-C to exit.\n"); | ||
|
||
// Connect to the IoT hub using the MQTT protocol | ||
s_deviceClient = DeviceClient.CreateFromConnectionString(s_connectionString, TransportType.Mqtt); | ||
// SendDeviceToCloudMessagesAsync(); | ||
|
||
SendToBlobAsync(); | ||
|
||
ReceiveC2dAsync(); | ||
|
||
|
||
Console.ReadLine(); | ||
} | ||
} | ||
} |