private static string SendRandomTelemetry(ModuleClient ioTHubModuleClient, IoT_Device deviceData, string serializedData) { System.Threading.Thread.Sleep(1000); var sendMessageTask = new Task(() => SendDeviceToCloudMessagesAsync(ioTHubModuleClient, serializedData)); sendMessageTask.Start(); sendMessageTask.Wait(); serializedData = JsonConvert.SerializeObject(deviceData.GetDeviceData()); return(serializedData); }
public IoT_Device GetDeviceData() { //Building our device //Create list of properties Random rand = new Random(); List <DeviceProperty> properties = new List <DeviceProperty>(); DeviceProperty a = new DeviceProperty(); a.Name = "owner"; a.Value = "Rafal Warzycha"; properties.Add(a); DeviceProperty b = new DeviceProperty(); b.Name = "manufactured by"; b.Value = "ABB"; properties.Add(b); DeviceProperty g = new DeviceProperty(); g.Name = "inspired by"; g.Value = "Ewelina"; properties.Add(g); //Create a list of variables DeviceVariable c = new DeviceVariable(); c.Name = "temperature"; c.Value = rand.Next(25, 35).ToString(); c.Timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); DeviceVariable d = new DeviceVariable(); d.Name = "torque"; d.Value = rand.Next(500, 1500).ToString(); d.Timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); //Create a device IoT_Device deviceData = new IoT_Device(); deviceData.DeviceId = Guid.NewGuid().ToString(); deviceData.DeviceProperties = properties; if (rand.Next(0, 100) > 50) { deviceData.DeviceVariable = c; } else { deviceData.DeviceVariable = d; } return(deviceData); }
/// <summary> /// Initializes the ModuleClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init() { MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); ITransportSettings[] settings = { mqttSetting }; // Open a connection to the Edge runtime ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings); await ioTHubModuleClient.OpenAsync(); Console.WriteLine("IoT Hub module client initialized."); // Register a callback for messages that are received by the module. await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", LogMessage, ioTHubModuleClient); IoT_Device deviceData = new IoT_Device(); string serializedData = ""; for (int i = 0; i < 100; i++) { serializedData = SendRandomTelemetry(ioTHubModuleClient, deviceData, serializedData); } }