private static void CheckHighsAndLows(Observation observation)
        {
            MainWindow.LogMessage("Checking Highs and Lows");
            HighLowObservation highsAndLows = new HighLowObservation();
            bool exists = DatabaseManager.GetHighsAndLows(observation.ObservationDate, ref highsAndLows);

            if (exists && highsAndLows.CompareToObservation(observation))
            {
                MainWindow.LogMessage("New high/low values found, updating");
                DatabaseManager.UpdateHighsAndLows(highsAndLows);
                MainWindow.LogMessage("New high/low update complete");
            }
            else if (!exists)
            {
                MainWindow.LogMessage("No high/low entry found, creating");
                highsAndLows.Initialize(observation);
                DatabaseManager.CreateHighsAndLows(highsAndLows);
                MainWindow.LogMessage("New high/low entry created");
            }
            else
            {
                MainWindow.LogMessage("No changes to highs and lows detected");
            }
        }
        private static void HandleXMLFileAdded(object source, FileSystemEventArgs e)
        {
            MainWindow.LogMessage("New XML file added at " + e.FullPath);

            //File watcher will trigger multiple events. Sleep for a bit.
            XMLFileWatcher.EnableRaisingEvents = false;
            Thread.Sleep(1000);
            XMLFileWatcher.EnableRaisingEvents = true;

            Observation observation = new Observation();
            bool        save        = true;

            try
            {
                observation = XMLParser.getObservationFromXML(e.FullPath);
            }
            catch (Exception ex)
            {
                MainWindow.LogError("Unable to parse observation.", ex);

                //Copy the XML file with a timestamp so we can look at it later
                try
                {
                    string directory = Path.GetDirectoryName(e.FullPath) + @"\";
                    File.Copy(e.FullPath, directory + "Parse Error - " + DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".xml");
                }
                catch (Exception ex2)
                {
                    MainWindow.LogError("Unable to copy error XML file for review", ex2);
                }
                save = false;
            }
            if (save)
            {
                try
                {
                    MainWindow.LogMessage("Sending current observation to server");
                    string results = WebManager.SendCurrentObservationToServer(observation);
                    MainWindow.LogMessage(results);
                }
                catch (Exception ex)
                {
                    MainWindow.LogError("Unable to send the observation to the server", ex);
                }

                if (observation.SaveToDatabase)
                {
                    try
                    {
                        MainWindow.LogMessage("Saving observation to the database");
                        DatabaseManager.LogObservation(observation);
                        MainWindow.LogMessage("Observation saved to the database");
                    }
                    catch (Exception ex)
                    {
                        MainWindow.LogError("Unable to save the observation to the database", ex, false, false);
                    }

                    try
                    {
                        MainWindow.LogMessage("Uploading today's data to the server");
                        string results = WebManager.UploadTodayObservations();
                        MainWindow.LogMessage(results);
                    }
                    catch (Exception ex)
                    {
                        MainWindow.LogError("Unable to save the observation to the database", ex, false, false);
                    }
                }

                try
                {
                    CheckHighsAndLows(observation);
                }
                catch (Exception ex)
                {
                    MainWindow.LogError("Unable to update highs and lows", ex);
                }

                CheckPeriodicUploads(observation.ObservationDate);
            }
        }
Exemple #3
0
        public bool CompareToObservation(Observation observation)
        {
            bool changes = false;

            if (observation.Temperature > HighTemp)
            {
                HighTemp = observation.Temperature;
                changes  = true;
            }

            if (observation.Temperature < LowTemp)
            {
                LowTemp = observation.Temperature;
                changes = true;
            }

            if (observation.HeatIndex > HighHeatIndex)
            {
                HighHeatIndex = observation.Temperature;
                changes       = true;
            }

            if (observation.HeatIndex < LowHeatIndex)
            {
                LowHeatIndex = observation.Temperature;
                changes      = true;
            }

            if (observation.WindChill > HighWindChill)
            {
                HighWindChill = observation.Temperature;
                changes       = true;
            }

            if (observation.WindChill < LowWindChill)
            {
                LowWindChill = observation.Temperature;
                changes      = true;
            }

            if (observation.WindSpeed > HighWindSpeed)
            {
                HighWindSpeed = observation.Temperature;
                changes       = true;
            }

            if (observation.Humidity > HighHumidity)
            {
                HighHumidity = observation.Temperature;
                changes      = true;
            }

            if (observation.Humidity < LowHumidity)
            {
                LowHumidity = observation.Temperature;
                changes     = true;
            }

            if (observation.DewPoint > HighDewPoint)
            {
                HighDewPoint = observation.Temperature;
                changes      = true;
            }

            if (observation.DewPoint < LowDewPoint)
            {
                LowDewPoint = observation.Temperature;
                changes     = true;
            }

            if (observation.Pressure > HighPressure)
            {
                HighPressure = observation.Temperature;
                changes      = true;
            }

            if (observation.Pressure < LowPressure)
            {
                LowPressure = observation.Temperature;
                changes     = true;
            }

            if (observation.TodayRain > DayRain)
            {
                DayRain = observation.Temperature;
                changes = true;
            }

            if (observation.RainRate > HighRainRate)
            {
                HighRainRate = observation.Temperature;
                changes      = true;
            }

            return(changes);
        }
Exemple #4
0
        internal static Observation getObservationFromXML(string file)
        {
            Observation   observation = new Observation();
            XmlTextReader reader      = new XmlTextReader(file);

            string date = "";
            string time = "";

            try
            {
                while (reader.Read())
                {
                    switch (reader.Name)
                    {
                    case "uploadDate":
                        date = reader.ReadElementContentAsString();
                        break;

                    case "uploadTime":
                        time = reader.ReadElementContentAsString();
                        break;

                    case "temp":
                        observation.Temperature = reader.ReadElementContentAsFloat();
                        break;

                    case "hiTemp":
                        observation.HiTemperature = reader.ReadElementContentAsFloat();
                        break;

                    case "hiTempTime":
                        observation.HiTemperatureTime = ParseWeatherLinkDate(date, reader.ReadElementContentAsString());
                        break;

                    case "lowTemp":
                        observation.LowTemperature = reader.ReadElementContentAsFloat();
                        break;

                    case "lowTempTime":
                        observation.LowTemperatureTime = ParseWeatherLinkDate(date, reader.ReadElementContentAsString());
                        break;

                    case "windChill":
                        observation.WindChill = reader.ReadElementContentAsFloat();
                        break;

                    case "lowWindChill":
                        observation.LowWindChill = reader.ReadElementContentAsFloat();
                        break;

                    case "lowWindChillTime":
                        observation.LowWindChillTime = ParseWeatherLinkDate(date, reader.ReadElementContentAsString());
                        break;

                    case "heatIndex":
                        observation.HeatIndex = reader.ReadElementContentAsFloat();
                        break;

                    case "hiHeatIndex":
                        observation.HiHeatIndex = reader.ReadElementContentAsFloat();
                        break;

                    case "hiHeatIndexTime":
                        observation.HiHeatIndexTime = ParseWeatherLinkDate(date, reader.ReadElementContentAsString());
                        break;

                    case "windSpeed":
                        observation.WindSpeed = reader.ReadElementContentAsFloat();
                        break;

                    case "wind10MinAvg":
                        //for some odd reason, weatherlink decided to use ---- for a full 10 minutes of zero wind...
                        string avgwind = reader.ReadElementContentAsString();
                        try { observation.TenMinAvgWindSpeed = float.Parse(avgwind); }
                        catch { observation.TenMinAvgWindSpeed = 0; }
                        break;

                    case "hiWindSpeed":
                        observation.HiWindSpeed = reader.ReadElementContentAsFloat();
                        break;

                    case "hiWindSpeedTime":
                        string hiWindSpeedTime = reader.ReadElementContentAsString();
                        if (hiWindSpeedTime != "----")
                        {
                            observation.HiWindSpeedTime = ParseWeatherLinkDate(date, hiWindSpeedTime);
                        }
                        break;

                    case "windDirectionDeg":
                        observation.WindDirection = reader.ReadElementContentAsInt();
                        break;

                    case "windDirectionSector":
                        observation.WindDirectionLetter = reader.ReadElementContentAsString();
                        break;

                    case "humidity":
                        observation.Humidity = reader.ReadElementContentAsFloat();
                        break;

                    case "dewPoint":
                        observation.DewPoint = reader.ReadElementContentAsFloat();
                        break;

                    case "pressure":
                        observation.Pressure = reader.ReadElementContentAsFloat();
                        break;

                    case "todayRain":
                        observation.TodayRain = reader.ReadElementContentAsFloat();
                        break;

                    case "stormRain":
                        observation.StormRain = reader.ReadElementContentAsFloat();
                        break;

                    case "monthRain":
                        observation.MonthRain = reader.ReadElementContentAsFloat();
                        break;

                    case "yearRain":
                        observation.YearRain = reader.ReadElementContentAsFloat();
                        break;

                    case "rainRate":
                        observation.RainRate = reader.ReadElementContentAsFloat();
                        break;

                    case "hiRainRate":
                        observation.HiRainRate = reader.ReadElementContentAsFloat();
                        break;

                    case "hiRainRateTime":
                        string rainRateTime = reader.ReadElementContentAsString();
                        if (rainRateTime != "----")
                        {
                            observation.HiRainRateTime = ParseWeatherLinkDate(date, rainRateTime);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                reader.Close();
                throw new Exception("XML Parse Failure", e);
            }

            reader.Close();

            if (!string.IsNullOrWhiteSpace(date) && !string.IsNullOrWhiteSpace(time))
            {
                observation.ObservationDate = ParseWeatherLinkDate(date, time);
                observation.SaveToDatabase  = GetShouldSaveToDB(observation.ObservationDate);
            }

            return(observation);
        }
Exemple #5
0
        internal static string GenerateJSONCurrentConditions(Observation observation)
        {
            string json = new JavaScriptSerializer().Serialize(observation.getSerializableObservationFull());

            return(json);
        }