internal void Update(WeatherLinkHandler weather) { WeatherLoopData weatherLoopData = weather.Data; if (weatherLoopData == null) { return; } WeatherDataItems.Find(x => x.Name == "BarometricTrend").RawValueUpdate(weatherLoopData.BarometerTrend); WeatherDataItems.Find(x => x.Name == "Barometer").RawValueUpdate(weatherLoopData.Barometer); WeatherDataItems.Find(x => x.Name == "InsideTemperature").RawValueUpdate(weatherLoopData.InsideTemperature); WeatherDataItems.Find(x => x.Name == "InsideHumidity").RawValueUpdate(weatherLoopData.InsideHumidity); WeatherDataItems.Find(x => x.Name == "OutsideTemperature").RawValueUpdate(weatherLoopData.OutsideTemperature); WeatherDataItems.Find(x => x.Name == "WindSpeed").RawValueUpdate(weatherLoopData.WindSpeed); WeatherDataItems.Find(x => x.Name == "WindSpeed10MinAvg").RawValueUpdate(weatherLoopData.WindSpeed10MinAvg); WeatherDataItems.Find(x => x.Name == "WindDirectionNamed").RawValueUpdate(weatherLoopData.WindDirection); WeatherDataItems.Find(x => x.Name == "WindDirection").RawValueUpdate(weatherLoopData.WindDirection); WeatherDataItems.Find(x => x.Name == "OutsideHumidity").RawValueUpdate(weatherLoopData.OutsideHumidity); WeatherDataItems.Find(x => x.Name == "RainRate").RawValueUpdate(weatherLoopData.RainRate); WeatherDataItems.Find(x => x.Name == "UV").RawValueUpdate(weatherLoopData.UV); WeatherDataItems.Find(x => x.Name == "SolarRadiation").RawValueUpdate(weatherLoopData.SolarRadiation); WeatherDataItems.Find(x => x.Name == "StormRain").RawValueUpdate(weatherLoopData.StormRain); WeatherDataItems.Find(x => x.Name == "RainDay").RawValueUpdate(weatherLoopData.RainDay); WeatherDataItems.Find(x => x.Name == "RainMonth").RawValueUpdate(weatherLoopData.RainMonth); WeatherDataItems.Find(x => x.Name == "RainYear").RawValueUpdate(weatherLoopData.RainYear); WeatherDataItems.Find(x => x.Name == "DayET").RawValueUpdate(weatherLoopData.DayET); WeatherDataItems.Find(x => x.Name == "MonthET").RawValueUpdate(weatherLoopData.MonthET); WeatherDataItems.Find(x => x.Name == "YearET").RawValueUpdate(weatherLoopData.YearET); WeatherDataItems.Find(x => x.Name == "ConsoleBatteryVoltage").RawValueUpdate(weatherLoopData.ConsoleBatteryVoltage); }
static void Main(string[] args) { // Read config string MqttBroker = ConfigurationManager.AppSettings["MqttBroker"].ToString(); string MqttBasePath = ConfigurationManager.AppSettings["MqttBasePath"].ToString(); string SerialPortName = ConfigurationManager.AppSettings["SerialPortName"].ToString(); int UpdateIntervalSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateIntervalSeconds"]); bool OnlyPublishIfUpdated = Convert.ToBoolean(ConfigurationManager.AppSettings["OnlyPublishIfUpdated"]); // Create MQTT object MqttHandler mqttHandler = null; bool retry = true; do { try { Console.WriteLine("[{0}] INFO: Connecting to MQTT broker {1}, Path {2}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), MqttBroker, MqttBasePath); mqttHandler = new MqttHandler(MqttBroker, MqttBasePath); Console.WriteLine("[{0}] INFO: Connected!", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")); retry = false; } catch (Exception ex) { Console.WriteLine("[{0}] ERROR: {1}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), ex.Message); Thread.Sleep(1000); } } while (retry); WeatherLinkHandler weather = new WeatherLinkHandler(); WeatherData weatherData = new WeatherData(); while (true) { Stopwatch Watch = Stopwatch.StartNew(); Console.WriteLine("[{0}] INFO: Executing", DateTime.Now); try { Console.WriteLine("[{0}] INFO: Connecting to Davis on port {1}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), SerialPortName); weather.GetWeatherLinkData(SerialPortName); weatherData.Update(weather); } catch (Exception ex) { Console.WriteLine("[{0}] ERROR: {1}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), ex.Message); } foreach (WeatherDataItem weatherDataItem in weatherData.WeatherDataItems) { if (weatherDataItem.Published && OnlyPublishIfUpdated) { continue; } weatherDataItem.Published = true; Console.WriteLine("[{0}] INFO: Publishing {1} Value {2} (RawValue {3})", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"), weatherDataItem.Name, weatherDataItem.Value, weatherDataItem.RawValue); mqttHandler.Publish(weatherDataItem.Name, weatherDataItem.Value); } Watch.Stop(); int ElapsedMs = Convert.ToInt32(Watch.ElapsedMilliseconds); int TimeToSleepMs = (UpdateIntervalSeconds * 1000) - ElapsedMs; Console.WriteLine("[{0}] INFO: Sleeping for {1} milliseconds", DateTime.Now, TimeToSleepMs); Thread.Sleep(TimeToSleepMs); } }