private async static Task ReceiveMessagesFromMonitorAsync(string partition) { var eventHubReceiver = operHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.Now); while (true) { EventData eventData = null; try { eventData = await eventHubReceiver.ReceiveAsync(); Trace.TraceInformation("RAW MONITOR RECEIVED:{1}: {0}", Encoding.UTF8.GetString(eventData.GetBytes()), DateTime.Now); Console.WriteLine("RAW MONITOR RECEIVED:{1}: {0}", Encoding.UTF8.GetString(eventData.GetBytes()), DateTime.Now); } catch (Exception e) { Trace.TraceWarning("{0}:TIMEOUT at MONITOR occurred", DateTime.Now); Console.WriteLine("{0}:TIMEOUT at MONITOR occurred", DateTime.Now); } if (eventData == null) { continue; } IoTHubMessage msg = new IoTHubMessage(); msg.enqueuedTime = eventData.EnqueuedTimeUtc; msg.message = Encoding.UTF8.GetString(eventData.GetBytes()); //Trace.TraceInformation("RECEIVED: {0}", msg.message); //Console.WriteLine("RECEIVED: {0}", msg.message); ProcessMonitorMessageAsync(msg); } }
static async void ProcessMonitorMessageAsync(IoTHubMessage msg) { monitormessage monitor; monitor = JsonConvert.DeserializeObject <monitormessage>(msg.message); Trace.TraceInformation("{0}:MONITOR-> CATEGORY:{1}, OPERATION:{2}, DEVICEID:{3}", DateTime.Now, monitor.category, monitor.operationName, monitor.deviceId); Console.WriteLine("{0}:MONITOR-> CATEGORY:{1}, OPERATION:{2}, DEVICEID:{3}", DateTime.Now, monitor.category, monitor.operationName, monitor.deviceId); }
static async void ProcessMessageAsync(IoTHubMessage msg) { //Console.WriteLine("Enter: Processing command received! - {0}", msg.message); //string[] timing = msg.message.Split(','); //string timing = msg.message; jsonmessage timing; try { timing = JsonConvert.DeserializeObject <jsonmessage>(msg.message); } catch (Exception e) { return; } if ((timing.DeviceID != deviceId) || (startTimeString != timing.StartTime)) { } else //(startTimeString == timing[1]) { //sent = false; DateTime finishTime = DateTime.Now; string finish = finishTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.ffZ"); DateTime devTime; string devTimeString = timing.DeviceTime; DateTime.TryParseExact(devTimeString, "yyyy-MM-ddTHH:mm:ss.ffZ", null, System.Globalization.DateTimeStyles.None, out devTime); TimeSpan elapsedTime = finishTime - startTime; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Elapsed Time:{1}:{0}", elapsedTime, ++messageCount); Console.ResetColor(); //file.WriteLine("{0},{1},{2}", commandReceived, finish, elapsedTime.TotalMilliseconds); //file.FlushAsync(); //var cmd = conn.CreateCommand(); //cmd.CommandText = @" //INSERT dbo.PerfLogs (DeviceID, ServiceSendTime, DeviceTime, IoTHubReceiveTime, ServiceReceiveTime, ElapsedTime, TimeOut, Success, ServiceSDKversion, DeviceSDKversion, InstanceStartTime, Description) //VALUES (@DeviceID, @ServiceSendTime, @DeviceTime, @IoTHubReceiveTime, @ServiceReceiveTime, @ElapsedTime, @TimeOut, @Success, @ServiceSDKversion, @DeviceSDKversion, @InstanceStartTime, @Description)"; //cmd.Parameters.AddWithValue("@DeviceID", deviceId); //cmd.Parameters.AddWithValue("@ServiceSendTime", startTime.ToUniversalTime()); //cmd.Parameters.AddWithValue("@ServiceReceiveTime", finishTime.ToUniversalTime()); //cmd.Parameters.AddWithValue("@ElapsedTime", elapsedTime.TotalMilliseconds); //cmd.Parameters.AddWithValue("@TimeOut", 30000); //cmd.Parameters.AddWithValue("@Success", 1); //cmd.Parameters.AddWithValue("@ServiceSDKversion", svcSDKver); //cmd.Parameters.AddWithValue("@DeviceSDKversion", dvcSDKver); //cmd.Parameters.AddWithValue("@DeviceTime", devTime.ToUniversalTime()); //cmd.Parameters.AddWithValue("@IoTHubReceiveTime", msg.enqueuedTime); //cmd.Parameters.AddWithValue("@InstanceStartTime", instanceTime.ToUniversalTime()); //cmd.Parameters.AddWithValue("@Description", desc); //try //{ // cmd.ExecuteScalar(); //} //catch (Exception e) //{ // Trace.TraceError("{0}: SQL Exception - {1}", DateTime.Now, e.Message); // Console.WriteLine("{0}: SQL Exception - {1}", DateTime.Now, e.Message); //} Trace.TraceInformation("Processed:{3}: {0},{1},{2}", msg.message, finish, elapsedTime.TotalMilliseconds, DateTime.Now); //Console.WriteLine("Processed:{3}: {0},{1},{2}", msg.message, finish, elapsedTime.TotalMilliseconds, DateTime.Now); startTimeString = ""; Thread.Sleep(5000); while (true) { try { await SendCloudToDeviceMessageAsync(); break; } catch (Exception e) { Trace.TraceError("{0}: Exception - {1}", DateTime.Now, e.Message); Console.WriteLine("{0}: Exception - {1}", DateTime.Now, e.Message); Thread.Sleep(1000); } } } }
private async static Task ReceiveMessagesFromDeviceAsync(string partition) { var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.Now); while (true) { EventData eventData = null; try { eventData = await eventHubReceiver.ReceiveAsync(TimeSpan.FromSeconds(1)); Trace.TraceInformation("RAW RECEIVED:{1}: {0}", Encoding.UTF8.GetString(eventData.GetBytes()), DateTime.Now); Console.WriteLine("RAW RECEIVED:{1}: {0}", Encoding.UTF8.GetString(eventData.GetBytes()), DateTime.Now); } catch (Exception e) { if (isfirst) { isfirst = false; if ((timeout < (DateTime.Now - startTime).TotalSeconds) && startTimeString != "") { Trace.TraceWarning("{0}:TIMEOUT occurred", DateTime.Now); Console.WriteLine("{0}:TIMEOUT occurred", DateTime.Now); //sent = false; DateTime finishTime = DateTime.Now; string finish = finishTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.ffZ"); // var cmd = conn.CreateCommand(); // cmd.CommandText = @" //INSERT dbo.PerfLogs (DeviceID, ServiceSendTime, ServiceReceiveTime, TimeOut, Success, ServiceSDKversion, DeviceSDKversion, InstanceStartTime, Description) //VALUES (@DeviceID, @ServiceSendTime, @ServiceReceiveTime, @TimeOut, @Success, @ServiceSDKversion, @DeviceSDKversion, @InstanceStartTime, @Description)"; // cmd.Parameters.AddWithValue("@DeviceID", deviceId); // cmd.Parameters.AddWithValue("@ServiceSendTime", startTime.ToUniversalTime()); // cmd.Parameters.AddWithValue("@ServiceReceiveTime", finishTime.ToUniversalTime()); // //cmd.Parameters.AddWithValue("@ElapsedTime", elapsedTime.TotalMilliseconds); // cmd.Parameters.AddWithValue("@TimeOut", timeout); // cmd.Parameters.AddWithValue("@Success", 0); // cmd.Parameters.AddWithValue("@ServiceSDKversion", svcSDKver); // cmd.Parameters.AddWithValue("@DeviceSDKversion", dvcSDKver); // //cmd.Parameters.AddWithValue("@DeviceTime", devTime); // //cmd.Parameters.AddWithValue("@IoTHubReceiveTime", commandReceivedTime); // cmd.Parameters.AddWithValue("@InstanceStartTime", instanceTime.ToUniversalTime()); // cmd.Parameters.AddWithValue("@Description", desc); // cmd.ExecuteScalar(); startTimeString = ""; await SendCloudToDeviceMessageAsync(); isfirst = true; } } } if (eventData == null) { continue; } IoTHubMessage msg = new IoTHubMessage(); msg.enqueuedTime = eventData.EnqueuedTimeUtc; msg.message = Encoding.UTF8.GetString(eventData.GetBytes()); //Trace.TraceInformation("RECEIVED: {0}", msg.message); //Console.WriteLine("RECEIVED: {0}", msg.message); ProcessMessageAsync(msg); } }