Exemple #1
0
 public void TestGetCompassDirection_Wrapping()
 {
     Assert.AreEqual(CompassDirection.W, Compass.GetCompassDirection(-450));
     Assert.AreEqual(CompassDirection.N, Compass.GetCompassDirection(-360));
     Assert.AreEqual(CompassDirection.E, Compass.GetCompassDirection(-270));
     Assert.AreEqual(CompassDirection.S, Compass.GetCompassDirection(-180));
     Assert.AreEqual(CompassDirection.W, Compass.GetCompassDirection(-90));
     Assert.AreEqual(CompassDirection.N, Compass.GetCompassDirection(0));
     Assert.AreEqual(CompassDirection.E, Compass.GetCompassDirection(90));
     Assert.AreEqual(CompassDirection.S, Compass.GetCompassDirection(180));
     Assert.AreEqual(CompassDirection.W, Compass.GetCompassDirection(270));
     Assert.AreEqual(CompassDirection.N, Compass.GetCompassDirection(360));
     Assert.AreEqual(CompassDirection.E, Compass.GetCompassDirection(450));
     Assert.AreEqual(CompassDirection.S, Compass.GetCompassDirection(540));
     Assert.AreEqual(CompassDirection.W, Compass.GetCompassDirection(630));
     Assert.AreEqual(CompassDirection.N, Compass.GetCompassDirection(720));
     Assert.AreEqual(CompassDirection.E, Compass.GetCompassDirection(810));
 }
Exemple #2
0
        public void SetValue(string fieldName, string newValue, ref bool changed)
        {
            if (!Props.TryGetValue(fieldName, out string existingValue) || newValue != existingValue)
            {
                Props[fieldName] = newValue;
                if (fieldName == "time")
                {
                    ProcessNewTime(Updated);
                }
                else if (fieldName == "wind_dir_deg" && double.TryParse(newValue, out double windDirDeg))
                {
                    CompassDirection dir = Compass.GetCompassDirection((int)Math.Round(windDirDeg));
                    Props["wind_dir_abbr"] = dir.ToString();
                    Props["wind_dir_full"] = Compass.GetCompassDirectionName(dir);
                }
                else if (fieldName == "humidity" || fieldName == "temperature_C" || fieldName == "temperature_F")
                {
                    if (fieldName == "temperature_C")
                    {
                        Props["temperature_F"] = Convert_C_to_F(newValue);
                    }
                    else if (fieldName == "temperature_F")
                    {
                        Props["temperature_C"] = Convert_F_to_C(newValue);
                    }

                    if (Props.TryGetValue("humidity", out string strHumidity) && double.TryParse(strHumidity, out double humidity) &&
                        Props.TryGetValue("temperature_C", out string strC) && double.TryParse(strC, out double C))
                    {
                        double absoluteHumidityGramsPerCubicMeter = (6.112 * Math.Pow(Math.E, (17.67 * C) / (C + 243.5)) * (humidity * 2.1674)) / (273.15 + C);
                        Props["humdity_abs_gpm3"] = absoluteHumidityGramsPerCubicMeter.ToString("0.#");
                    }
                }
                changed = true;
            }
        }
Exemple #3
0
        /// <summary>
        /// Process an updated timestamp.  This does things like parse the time string and update wind low and high values.
        /// </summary>
        /// <param name="newTimeStr"></param>
        private void ProcessNewTime(DateTime?updated)
        {
            if (updated == null)
            {
                Props["time_epoch_ms"] = "0";
                Props["time_local"]    = "";
                return;
            }
            Props["time_epoch_ms"] = TimeUtil.GetTimeInMsSinceEpoch(updated.Value).ToString();
            Props["time_local"]    = updated.Value.ToString("yyyy-MM-dd hh:mm:ss tt");
            SetTimeout.OnBackground(() =>
            {
                try
                {
                    updated = Updated;

                    bool hasAnyWindData = false;
                    // Compute wind averages over time
                    double wind_dir_deg = 0;
                    if (!Props.TryGetValue("wind_dir_deg", out string str_wind_dir_deg) || !double.TryParse(str_wind_dir_deg, out wind_dir_deg))
                    {
                        wind_dir_deg = 0;
                    }
                    if (Props.TryGetValue("wind_avg_mi_h", out string str_wind_speed_mph) && double.TryParse(str_wind_speed_mph, out double wind_speed_mph))
                    {
                        hasAnyWindData = true;
                        windTrackerMi.Add(wind_speed_mph, wind_dir_deg);

                        RollingMinMaxAvg.StoredValue low   = windTrackerMi.GetMinimum();
                        CompassDirection dir               = Compass.GetCompassDirection((int)Math.Round(low.directionDegrees));
                        Props["wind_5min_low_mi_h"]        = low.speed.ToString();
                        Props["wind_5min_low_mi_dir_abbr"] = dir.ToString();
                        Props["wind_5min_low_mi_dir_full"] = Compass.GetCompassDirectionName(dir);

                        RollingMinMaxAvg.StoredValue high = windTrackerMi.GetMaximum();
                        dir = Compass.GetCompassDirection((int)Math.Round(high.directionDegrees));
                        Props["wind_5min_high_mi_h"]        = high.speed.ToString();
                        Props["wind_5min_high_mi_dir_abbr"] = dir.ToString();
                        Props["wind_5min_high_mi_dir_full"] = Compass.GetCompassDirectionName(dir);

                        RollingMinMaxAvg.StoredValue avg = windTrackerMi.GetAverage();
                        dir = Compass.GetCompassDirection((int)Math.Round(avg.directionDegrees));
                        Props["wind_5min_avg_mi_h"]        = avg.speed.ToString();
                        Props["wind_5min_avg_mi_dir_abbr"] = dir.ToString();
                        Props["wind_5min_avg_mi_dir_full"] = Compass.GetCompassDirectionName(dir);
                    }
                    if (Props.TryGetValue("wind_avg_km_h", out string str_wind_speed_kph) && double.TryParse(str_wind_speed_kph, out double wind_speed_kph))
                    {
                        windTrackerKm.Add(wind_speed_kph, wind_dir_deg);

                        RollingMinMaxAvg.StoredValue low   = windTrackerKm.GetMinimum();
                        CompassDirection dir               = Compass.GetCompassDirection((int)Math.Round(low.directionDegrees));
                        Props["wind_5min_low_km_h"]        = low.speed.ToString();
                        Props["wind_5min_low_km_dir_abbr"] = dir.ToString();
                        Props["wind_5min_low_km_dir_full"] = Compass.GetCompassDirectionName(dir);

                        RollingMinMaxAvg.StoredValue high = windTrackerKm.GetMaximum();
                        dir = Compass.GetCompassDirection((int)Math.Round(high.directionDegrees));
                        Props["wind_5min_high_km_h"]        = high.speed.ToString();
                        Props["wind_5min_high_km_dir_abbr"] = dir.ToString();
                        Props["wind_5min_high_km_dir_full"] = Compass.GetCompassDirectionName(dir);

                        RollingMinMaxAvg.StoredValue avg = windTrackerKm.GetAverage();
                        dir = Compass.GetCompassDirection((int)Math.Round(avg.directionDegrees));
                        Props["wind_5min_avg_km_h"]        = avg.speed.ToString();
                        Props["wind_5min_avg_km_dir_abbr"] = dir.ToString();
                        Props["wind_5min_avg_km_dir_full"] = Compass.GetCompassDirectionName(dir);
                    }
                    if (hasAnyWindData)
                    {
                        int speed            = (int)Math.Round(WindSpeedMph);
                        int gust             = (int)Math.Round(WindGustMph);
                        StringBuilder sbWind = new StringBuilder();
                        if (speed == 0 && gust == 0)
                        {
                            sbWind.Append("Calm");
                        }
                        else
                        {
                            if (speed > 0)
                            {
                                sbWind.Append(speed + " MPH " + WindDirAbbr);
                                if (gust > speed)
                                {
                                    sbWind.Append(", ");
                                }
                            }
                            if (gust > speed)
                            {
                                sbWind.Append("gusting to " + gust);
                                if (speed == 0)
                                {
                                    sbWind.Append(" MPH " + WindGustDirAbbr);
                                }
                            }
                        }
                        Props["wind_desc_mi"] = sbWind.ToString();
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        OnBackgroundError(this, ex);
                    }
                    catch (Exception ex2)
                    {
                        Logger.Debug(ex2, "MqttDevice OnBackgroundError event");
                    }
                }
                try
                {
                    OnUpdate(this, new EventArgs());
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "MqttDevice OnUpdate event");
                }
            }, 250);
        }
Exemple #4
0
        public SensorBase(string url)
        {
            originalUrl = url;
            dateUtc     = DateTime.UtcNow;
            DateTime now = DateTime.Now;

            ParseQueryStringArguments(url);
            raw_arguments["date"] = now.ToString("yyyy-MM-dd");
            raw_arguments["time"] = now.ToString("hh:mm:ss tt");

            // Assumed to be in all packets
            MAC      = GetString("id");
            ID       = GetString("sensor");
            UniqueID = MAC + "_" + ID;
            Model    = GetString("mt");
            Battery  = GetString("battery");
            if (string.IsNullOrWhiteSpace(Battery))
            {
                Battery = GetString("sensorbattery");
            }
            HubBattery         = GetString("hubbattery");
            SignalStrength     = GetInt("rssi");
            BarometricPressure = GetDouble("baromin");

            // Not in all packets
            IndoorHumidity = GetInt("indoorhumidity");
            IndoorTempF    = GetDouble("indoortempf");
            Humidity       = GetInt("humidity");
            TempF          = GetDouble("tempf");
            Probe          = GetInt("probe");
            Check          = GetInt("check");
            Water          = GetInt("water");
            WindSpeed      = GetInt("windspeedmph");
            if (raw_arguments.ContainsKey("windspeedkph"))
            {
                WindSpeedKph = GetInt("windspeedkph");
            }
            else
            {
                WindSpeedKph = (int)(WindSpeed * 1.60934);
                raw_arguments["windspeedkph"] = WindSpeedKph.ToString();
            }
            WindDirection = GetInt("winddir");
            WindGustSpeed = GetInt("windgustmph");
            if (raw_arguments.ContainsKey("windgustkph"))
            {
                WindGustSpeedKph = GetInt("windgustkph");
            }
            else
            {
                WindGustSpeedKph             = (int)(WindGustSpeed * 1.60934);
                raw_arguments["windgustkph"] = WindGustSpeedKph.ToString();
            }
            WindGustDirection = GetInt("windgustdir");
            WindSpeedAverage  = GetInt("windspeedavgmph");
            if (raw_arguments.ContainsKey("windspeedavgkph"))
            {
                WindSpeedKph = GetInt("windspeedavgkph");
            }
            else
            {
                WindSpeedAverageKph = (int)(WindSpeedAverage * 1.60934);
                raw_arguments["windspeedavgkph"] = WindSpeedAverageKph.ToString();
            }
            RainRecent = GetDouble("rainin");
            RainDaily  = GetDouble("dailyrainin");
            DewPointF  = GetInt("dewptf");

            WindDirectionCompass            = Compass.GetCompassDirection(WindDirection);
            raw_arguments["winddircompass"] = WindDirectionCompass.ToString();

            WindGustDirectionCompass            = Compass.GetCompassDirection(WindGustDirection);
            raw_arguments["windgustdircompass"] = WindGustDirectionCompass.ToString();


            StringBuilder windDescriptionMph = new StringBuilder();
            StringBuilder windDescriptionKph = new StringBuilder();
            bool          hasDirection       = false;

            if (WindSpeed > 0)
            {
                windDescriptionMph.Append(WindSpeed).Append(" MPH");
                windDescriptionKph.Append(WindSpeedKph).Append(" KPH");
                string direction = "";
                if (raw_arguments.ContainsKey("winddir"))
                {
                    direction    = " " + WindDirectionCompass.ToString();
                    hasDirection = true;
                }
                windDescriptionMph.Append(direction);
                windDescriptionKph.Append(direction);
            }
            if (WindGustSpeed > 0)
            {
                if (windDescriptionMph.Length > 0)
                {
                    windDescriptionMph.Append(", ");
                    windDescriptionKph.Append(", ");
                }
                windDescriptionMph.Append("gusting to ").Append(WindGustSpeed);
                windDescriptionKph.Append("gusting to ").Append(WindGustSpeedKph);
                if (raw_arguments.ContainsKey("windgustdir"))
                {
                    if (!hasDirection || WindGustDirectionCompass != WindDirectionCompass)
                    {
                        windDescriptionMph.Append(" ").Append(WindGustDirectionCompass.ToString());
                        windDescriptionKph.Append(" ").Append(WindGustDirectionCompass.ToString());
                    }
                }
                else if (!hasDirection && raw_arguments.ContainsKey("winddir"))
                {
                    windDescriptionMph.Append(" ").Append(WindDirectionCompass.ToString());
                    windDescriptionKph.Append(" ").Append(WindDirectionCompass.ToString());
                }
            }
            if (windDescriptionMph.Length == 0)
            {
                windDescriptionMph.Append("Calm");
                windDescriptionKph.Append("Calm");
            }
            raw_arguments["winddescriptionmph"] = windDescriptionMph.ToString();
            raw_arguments["winddescriptionkph"] = windDescriptionKph.ToString();
        }
Exemple #5
0
 public void TestGetCompassDirection_Precision()
 {
     for (int i = 0; i < 360; i++)
     {
         CompassDirection d = Compass.GetCompassDirection(i);
         if (i < 12)
         {
             Assert.AreEqual(CompassDirection.N, d);
         }
         else if (i < 34)
         {
             Assert.AreEqual(CompassDirection.NNE, d);
         }
         else if (i < 57)
         {
             Assert.AreEqual(CompassDirection.NE, d);
         }
         else if (i < 79)
         {
             Assert.AreEqual(CompassDirection.ENE, d);
         }
         else if (i < 102)
         {
             Assert.AreEqual(CompassDirection.E, d);
         }
         else if (i < 124)
         {
             Assert.AreEqual(CompassDirection.ESE, d);
         }
         else if (i < 147)
         {
             Assert.AreEqual(CompassDirection.SE, d);
         }
         else if (i < 169)
         {
             Assert.AreEqual(CompassDirection.SSE, d);
         }
         else if (i < 192)
         {
             Assert.AreEqual(CompassDirection.S, d);
         }
         else if (i < 214)
         {
             Assert.AreEqual(CompassDirection.SSW, d);
         }
         else if (i < 237)
         {
             Assert.AreEqual(CompassDirection.SW, d);
         }
         else if (i < 259)
         {
             Assert.AreEqual(CompassDirection.WSW, d);
         }
         else if (i < 282)
         {
             Assert.AreEqual(CompassDirection.W, d);
         }
         else if (i < 304)
         {
             Assert.AreEqual(CompassDirection.WNW, d);
         }
         else if (i < 327)
         {
             Assert.AreEqual(CompassDirection.NW, d);
         }
         else if (i < 349)
         {
             Assert.AreEqual(CompassDirection.NNW, d);
         }
         else
         {
             Assert.AreEqual(CompassDirection.N, d);
         }
     }
 }