Last active
March 10, 2020 02:01
-
-
Save chrismckelt/4a0769a626f433fb25903318b88c5311 to your computer and use it in GitHub Desktop.
Azure IoT Edge Demo DataGenerator.cs
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
var connectionSettings = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); // setup connection to hubs MQTT broker | |
ITransportSettings[] settings = { connectionSettings }; | |
ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(); // inbuilt SDK magic to connect using environment variables | |
await ioTHubModuleClient.OpenAsync(); // connect | |
var chance = new Chance(42); // random data generator | |
var payload = chance.Object<Payload>(); | |
var currentTemp = 20d; | |
for (int i = 0; i < 1000; i++) | |
{ | |
currentTemp = currentTemp + chance.Double(0, 1); | |
payload.Temperature = currentTemp; | |
payload.IsAirConditionerOn = chance.Bool(payload.Temperature); | |
payload.TagKey = "dotnet"; | |
payload.TimeStamp = DateTime.Now; // just display in local time for demo | |
var msg = JsonConvert.SerializeObject(payload); | |
var messageBytes = Encoding.UTF8.GetBytes(msg); | |
using (var pipeMessage = new Message(messageBytes)) | |
{ | |
await ioTHubModuleClient.SendEventAsync("output1", pipeMessage); | |
Log.Information("sent: " + msg); | |
} | |
Thread.Sleep(1000); | |
i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment