public ContactsController() { db = new ApplicationDbContext(); cm = new ContactsManagement(); wm = new WeatherManagement(); }
public string[] GetInitialWeatherStream(int?id) { string descriptor = null; var contact = db.Contacts.Where(c => c.Id == id).SingleOrDefault(); var addressId = contact.AddressId; string[] noWeatherData = new string[2] { "", "Add An Address For The Current Temperature In Their Area" }; Address address = db.Addresses.Where(a => a.Id == addressId).Select(a => a).SingleOrDefault(); double latitude = 0; double longitude = 0; var rawLatitude = db.Addresses.Where(a => a.Id == addressId).Select(a => a.Latitude).SingleOrDefault(); var rawLongitude = db.Addresses.Where(a => a.Id == addressId).Select(a => a.Longitude).SingleOrDefault(); if (rawLatitude == null && rawLongitude == null) { if (address.StreetAddress != null && address.Region != null && address.Locality != null && address.ZipCode != null) { Address updatedAddress = SetLatLong(address); rawLatitude = db.Addresses.Where(a => a.Id == updatedAddress.Id).Select(a => a.Latitude).SingleOrDefault(); rawLongitude = db.Addresses.Where(a => a.Id == updatedAddress.Id).Select(a => a.Longitude).SingleOrDefault(); } if (address.Region != null && address.Locality != null) { latitude = Convert.ToDouble(FindLatLongFromLocalityAndRegion(address.Locality, address.Region)[0]); longitude = Convert.ToDouble(FindLatLongFromLocalityAndRegion(address.Locality, address.Region)[1]); } else if (address.Region != null) { ContactsManagement cm = new ContactsManagement(); var indexer = cm.stateList.FindIndex(n => n.Value.Equals(address.Region)); latitude = cm.latLongList[indexer][0]; longitude = cm.latLongList[indexer][1]; descriptor = "~"; } } if (rawLatitude != null) { latitude = Convert.ToDouble(rawLatitude.Value); latitude = Math.Round(latitude); } if (rawLongitude != null) { longitude = Convert.ToDouble(rawLongitude.Value); longitude = Math.Round(longitude); } string url = @"https://api.weather.gov/points/" + latitude + "," + longitude; if (latitude != 0 && longitude != 0) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = Access.email; request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; WebResponse response = request.GetResponse(); System.IO.Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); string responseFromServer = reader.ReadToEnd(); response.Close(); var root = JsonConvert.DeserializeObject <GovWeatherData.Context>(responseFromServer); string observationStations = root.properties.observationStations; string secondUrl = root.properties.forecastHourly; //int coordOne = root.properties.gridX; //int coordTwo = root.properties.gridY; return(FindCurrentTemperature(descriptor, secondUrl)); } return(noWeatherData); }