public WeatherReports getWeatherDetails(string city_name, string state_name, int UserName) { string key = "4b47e918045b2342"; string output; WeatherReports wp = new WeatherReports(); using (WebClient client = new WebClient()) { string input = "http://api.wunderground.com/api/" + key + "/conditions/q/" + state_name + "/" + city_name + ".json"; output = client.DownloadString(input); wp = JsonConvert.DeserializeObject <WeatherReports>(output); wp.current_observation.weather = "Not Available"; return(wp); } }
public static async Task ListenForMongoDbChanges() { Report("Starting Listener"); Running = true; // Task that runs SQL Syncing var timer = new System.Threading.Timer(async(e) => { await SqlSyncing(); }, null, Zero, SyncingPeriod); // Helper class for connection to MongoDb var orionContext = new OrionContext(); // Change Stream API of MongoDB (based on replica set oplog) // This is an async task and creates a thread for each change using (var cursor = await orionContext.Entities.WatchAsync()) { Report("Cursor Watching started"); try { foreach (var change in cursor.ToEnumerable()) { Report("Cursor Watch received update"); if (change.UpdateDescription == null) { Report("Update description missing, skipping cursor update"); continue; } // Getting id of device in safe manner // Helps determine entity of receiving object if (!(BsonSerializer.Deserialize <dynamic>(change.DocumentKey) is IDictionary <string, object> documentKey)) { continue; } var hasId = documentKey.TryGetValue("_id", out var _id); if (!hasId) { continue; } if (!(_id is IDictionary <string, object> idRow)) { continue; } var hasEntityType = idRow.TryGetValue("type", out var entityType); if (!hasEntityType) { continue; } // Turns out this enum doesn't do all that much EntityTypeEnum entityTypeEnum; switch (entityType) { case "weatherReport": entityTypeEnum = EntityTypeEnum.WeatherReport; // Interlocked ensures atomicity of each incrementation Interlocked.Increment(ref WeatherChangesReceived); break; case "roadTrafficReport": entityTypeEnum = EntityTypeEnum.RoadTrafficReport; // Interlocked ensures atomicity of each incrementation Interlocked.Increment(ref RoadTrafficChangesReceived); break; default: continue; } idRow.TryGetValue("id", out var deviceId); // creates instace of DTO Object and adds to ConcurrentQueue if (entityTypeEnum == EntityTypeEnum.WeatherReport) { Report($"Received Weather Report from device {deviceId}"); // Deserialize to Typed object var weatherUpdate = BsonSerializer.Deserialize <WeatherReportUpdate>(change.UpdateDescription.UpdatedFields); if (weatherUpdate == null) { continue; } var uow = await getUnitOfWorkAsync(); // Delay to ensure nothing gets added to lists while SQL Save Operation is happening while (_unitOfWorkLock || _unitOfWork?.IsObjectsSaving == true || uow.IsObjectsSaving) { Report("Lists are locked, waiting"); await Task.Delay(50); } var weatherReport = new WeatherReport(uow, weatherUpdate) { DeviceId = deviceId?.ToString() }; WeatherReports.Enqueue(weatherReport); } else if (entityTypeEnum == EntityTypeEnum.RoadTrafficReport) { Report($"Received Road Traffic Report from device {deviceId}"); // Deserialize to Typed object var roadTrafficUpdate = BsonSerializer.Deserialize <RoadTrafficReportUpdate>(change.UpdateDescription.UpdatedFields); if (roadTrafficUpdate == null) { continue; } var uow = await getUnitOfWorkAsync(); // Delay to ensure nothing gets added to lists while SQL Save Operation is happening while (_unitOfWorkLock || _unitOfWork?.IsObjectsSaving == true || uow.IsObjectsSaving) { Report("Lists are locked, waiting"); await Task.Delay(50); } var roadTrafficReport = new RoadTrafficReport(uow, roadTrafficUpdate) { DeviceId = deviceId?.ToString() }; RoadTrafficReports.Enqueue(roadTrafficReport); } } } catch (Exception ex) { Running = false; Report($"EXCEPTION: {ex.Message}"); throw new Exception(ex.Message); } Running = false; } Report("Exiting Listener"); }