Exemple #1
0
 public ActionResult Index([Bind(Include = "Location")] WarningViewModel warning)
 {
     if (ModelState.IsValid)
     {
         Airly            airly      = new Airly();
         NpgsqlConnection connection = new NpgsqlConnection(
             System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
         connection.Open();
         connection.TypeMapper.UseNetTopologySuite();
         using (var cmd = new NpgsqlCommand(
                    "SELECT \"Airly_CAQI\", \"Temperature\", \"Pressure\" " +
                    "FROM  \"Airly\" JOIN \"AirlySensor\" " +
                    "ON \"Airly\".\"SensorId\" = \"AirlySensor\".\"Id\" " +
                    "WHERE \"Location\" IS NOT NULL " +
                    "AND \"TimeStamp\" >= now() - interval '3h' " +
                    "AND ST_Distance(\"Location\", ST_GeomFromText('POINT(" + warning.Location + ")', 4326)) < 1000 " +
                    "ORDER BY ST_Distance(\"Location\", ST_GeomFromText('POINT(" + warning.Location + ")', 4326)), " +
                    "\"TimeStamp\" DESC LIMIT 1;",
                    connection))
             using (var reader = cmd.ExecuteReader())
             {
                 try
                 {
                     reader.Read();
                     warning.AirlyCAQI   = reader.GetFloat(0);
                     warning.Temperature = reader.GetFloat(1);
                     warning.Pressure    = reader.GetFloat(2);
                     warning.Quality     = GetQuality(warning.AirlyCAQI);
                     warning.Description = GetDescription(warning);
                     reader.Close();
                 }
                 catch (Exception ex)
                 {
                     warning.Description = "Wystąpiły problemy przy pobieraniu ostrzeżeń dla Twojej lokalizacji";
                     Console.WriteLine(ex);
                 }
             }
         connection.Close();
         return(RedirectToAction("Warning", "Warning", warning));
     }
     warning.Description = "Wystąpił błąd i nie udało się pobrać Twojej lokalizacji";
     return(RedirectToAction("Warning", "Warning", warning));
 }
        private static async Task <Measurements> GetRequest(Sensor sensor)
        {
            HttpClient          httpClient   = GetAirlyApiClient();
            Measurements        measurements = null;
            string              latitude     = sensor.Location.Latitude.ToString().Replace(',', '.');
            string              longitude    = sensor.Location.Longitude.ToString().Replace(',', '.');
            HttpResponseMessage response     = await httpClient.GetAsync("measurements/nearest?indexType=AIRLY_CAQI&lat=" + latitude + "&lng=" + longitude + "&maxDistanceKM=50");

            if (response.IsSuccessStatusCode)
            {
                measurements = await response.Content.ReadAsAsync <Measurements>();

                CardioCartaEntities db = new CardioCartaEntities();
                if (measurements.Current.Values.Count > 0 && measurements.Current.Indexes.First(item => item.Name == "AIRLY_CAQI").Value != null)
                {
                    try
                    {
                        Airly airly = GetCurrent(measurements, sensor.Id);
                        db.Airly.Add(airly);
                        LinkedList <AirlyForecast> airlyForecasts = GetForecast(measurements, sensor.Id);
                        db.AirlyForecast.AddRange(airlyForecasts);
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    Console.Write(sensor.Id + ": " + measurements.Current.Indexes.ElementAt(0).Description);
                }
            }
            else
            {
                Console.Write(response.StatusCode + ": " + response.ReasonPhrase);
            }
            return(measurements);
        }
Exemple #3
0
 public RESTManager(Airly airly, string apiKey)
 {
     ApiKey   = apiKey;
     Airly    = airly;
     Handlers = new RequestQueueHandler(this);
 }
Exemple #4
0
 public RESTManager(Airly airly) : this(airly, airly.ApiKey)
 {
 }
        private static Airly GetCurrent(Measurements measurements, int sensorId)
        {
            Airly airly = new Airly();

            try
            {
                airly.Airly_CAQI = (float)(measurements.Current.Indexes.First(item => item.Name == "AIRLY_CAQI").Value);
            }
            catch (InvalidOperationException)
            {
                airly.Airly_CAQI = null;
            }
            try
            {
                airly.PM1 = (float)(measurements.Current.Values.First(item => item.Name == "PM1").Value);
            }
            catch (InvalidOperationException)
            {
                airly.PM1 = null;
            }
            try
            {
                airly.PM10 = (float)(measurements.Current.Values.First(item => item.Name == "PM10").Value);
            }
            catch (InvalidOperationException)
            {
                airly.PM10 = null;
            }
            try
            {
                airly.PM25 = (float)(measurements.Current.Values.First(item => item.Name == "PM25").Value);
            }
            catch (InvalidOperationException)
            {
                airly.PM25 = null;
            }

            try
            {
                airly.Humidity = (float)(measurements.Current.Values.First(item => item.Name == "HUMIDITY").Value);
            }
            catch (InvalidOperationException)
            {
                airly.Humidity = null;
            }

            try
            {
                airly.Pressure = (float)(measurements.Current.Values.First(item => item.Name == "PRESSURE").Value);
            }
            catch (InvalidOperationException)
            {
                airly.Pressure = null;
            }

            try
            {
                airly.Temperature = (float)(measurements.Current.Values.First(item => item.Name == "TEMPERATURE").Value);
            }
            catch (InvalidOperationException)
            {
                airly.Temperature = null;
            }

            airly.TimeStamp = measurements.Current.TillDateTime
                              .AddMinutes(-measurements.Current.TillDateTime.Minute)
                              .AddSeconds(-measurements.Current.TillDateTime.Second)
                              .AddMilliseconds(-measurements.Current.TillDateTime.Millisecond);
            airly.SensorId = sensorId;
            return(airly);
        }
Exemple #6
0
 public Meta(Airly airly) : base(airly)
 {
 }
Exemple #7
0
 public Measurements(Airly airly) : base(airly)
 {
 }
Exemple #8
0
 public Installations(Airly airly) : base(airly)
 {
 }