static async Task SendMessagesAsync(int numberOfMessagesToSend) { try { for (var i = 0; i < numberOfMessagesToSend; i++) { IoTMessage iotmessage = new IoTMessage() { DeviceId = "000-000-00" + i.ToString(), TimeStamp = DateTime.Now, Status = IoTStatus.Alive.ToString(), message = "Testing Message " + i.ToString() }; // Create a new message to send to the queue. string messageBody = JsonConvert.SerializeObject(iotmessage, Formatting.Indented); var message = new Message(Encoding.UTF8.GetBytes(messageBody)); // Write the body of the message to the console. Console.WriteLine($"Sending message: {messageBody}"); // Send the message to the queue. await queueClient.SendAsync(message); } } catch (Exception exception) { Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}"); } }
public static IEnumerable <Telemetry> Parse(string frame) { if (string.IsNullOrEmpty(frame) || string.IsNullOrWhiteSpace(frame)) { throw new ArgumentException("Empty string is not a valid argument", nameof(frame)); } try { List <Telemetry> telemetries = new List <Telemetry>(); IoTMessage dynamicFrame = JsonConvert.DeserializeObject <IoTMessage>(frame); IEnumerable <DataObject> datas = dynamicFrame.Data; foreach (var item in datas) { if (dynamicFrame.DevEUI == null || string.IsNullOrEmpty(dynamicFrame.DevEUI)) { continue; } telemetries.Add(new Telemetry { Id = $"{dynamicFrame.DevEUI}-{item.Ts}", DeviceId = dynamicFrame.DevEUI, OccuredAt = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds((int)(item.Ts / 1_000)), BatteryVoltage = item.Battery, Humidity = item.Humidity, Pressure = item.Pressure, Temperature = item.Temperature }); }
public static async Task Run([EventHubTrigger("messages/events", Connection = "IoTHub")] EventData message, ILogger log) { try { // Device sends a message string messageBody = Encoding.UTF8.GetString(message.Body.Array); IoTMessage iotMessage = JsonConvert.DeserializeObject <IoTMessage>(messageBody); Game payloadGame; switch (iotMessage.Type) { case "game_update": payloadGame = JsonConvert.DeserializeObject <Game>(iotMessage.Payload); GameHelper.GameUpdated(payloadGame); break; case "game_end": payloadGame = JsonConvert.DeserializeObject <Game>(iotMessage.Payload); await GameHelper.GameEnded(payloadGame); break; default: break; } } catch (Exception ex) { throw ex; } }
public async void SendToCloudMessagesAsync(IoTMessage message) { var messageString = JsonConvert.SerializeObject(message); var request = new Message(Encoding.ASCII.GetBytes(messageString)); request.Properties.Add("speedAlert", (message.Speed > 60) ? "true" : "false"); await DeviceClient.SendEventAsync(request); }
private async void Timer_Tick(object sender, object e) { IoTMessage message = new IoTMessage() { humidity = String.Format($"{devices.Humidity.HumidityData:n2}"), temperature = String.Format($"{devices.IRTemperature.AmbientTemperature:n2}"), pressure = String.Format($"{devices.Pressure.PressureData:n2}"), time = DateTime.Now.ToString() }; var messageString = JsonConvert.SerializeObject(message); var messageOut = new Message(Encoding.ASCII.GetBytes(messageString)); try { await deviceClient.SendEventAsync(messageOut); } catch (Exception ex) { } }