Ejemplo n.º 1
0
        private void QueryHiveData(List <SensorReading> readings, List <Alert> alerts)
        {
            try
            {
                if (settings.hive != null)
                {
                    HiveService service = new HiveService();

                    if (service.SignIn(settings.hive.username, settings.hive.password))
                    {
                        var hiveReadings = QueryHiveData(service);

                        readings.AddRange(hiveReadings);
                    }
                    else
                    {
                        alerts.Add(new Alert {
                            deviceName = "Hive", alertText = "Sign-in failed."
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying Hive data. {0}", ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Main work method
        /// </summary>
        /// <param name="settings">Settings.</param>
        public void ProcessTags(Settings settings)
        {
            this.settings = settings;
            Uri esPath = new UriBuilder
            {
                Host = settings.elasticserver,
                Port = 9200
            }.Uri;

            EsClient = ElasticUtils.getElasticClient(esPath, settings.indexname, false);

            try
            {
                var weatherReadings = QueryWeatherData(settings.weatherUnderground);

                StoreReadings(weatherReadings, settings.weatherUnderground.IndexName);
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying Weather data. {0}", ex);
            }

            List <Reading>      allReadings = new List <Reading>();
            List <SensorDevice> allDevices  = new List <SensorDevice>();

            try
            {
                if (settings.hive != null)
                {
                    HiveService service = new HiveService();

                    if (service.SignIn(settings.hive.username, settings.hive.password))
                    {
                        var hiveReadings = QueryHiveData(service, allDevices);

                        allReadings.AddRange(hiveReadings);
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying Hive data. {0}", ex);
            }

            try
            {
                if (settings.wirelesstag != null)
                {
                    WirelessSensorTagAPI tagService = new WirelessSensorTagAPI(settings.wirelesstag.WirelessTagServiceUrl);

                    if (tagService.SignIn(settings.wirelesstag.username, settings.wirelesstag.password))
                    {
                        var wirelessTagReadings = QuerySensorTags(tagService, allDevices);

                        allReadings.AddRange(wirelessTagReadings);
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying SensorTag data. {0}", ex);
            }

            if (allReadings.Any() && settings.email != null)
            {
                try
                {
                    StoreReadings(allReadings, settings.indexname);

                    ElasticUtils.DeleteDuplicates(EsClient, settings.indexname);
                }
                catch (Exception ex)
                {
                    Utils.Log("Exception ingesting data in ES. {0}", ex);
                }

                try
                {
                    List <Alert> alerts = new List <Alert>();

                    // See if any of the data we got back indicated a drained battery.
                    CheckBatteryStatus(allReadings, settings.lowBatteryThreshold, alerts);
                    CheckMissingData(allDevices, alerts);

                    if (alerts.Any())
                    {
                        Utils.SendAlertEmail(settings.email, alerts);
                    }
                }
                catch (Exception ex)
                {
                    Utils.Log("Exception checking missing data. {0}", ex);
                }
            }

            Utils.Log("Run complete.");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Main work method
        /// </summary>
        /// <param name="settings">Settings.</param>
        public void ProcessTags(Settings settings)
        {
            this.settings = settings;
            Uri esPath = new UriBuilder
            {
                Host = settings.elasticserver,
                Port = 9200
            }.Uri;

            EsClient = ElasticUtils.getElasticClient(esPath, settings.indexname, false);
            List <Alert> alerts = new List <Alert>();

            try
            {
                QueryWeatherData(settings.weatherUnderground);
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying Weather data. {0}", ex);
            }


            var allReadings = new List <SensorReading>();
            var allDevices  = new List <SensorDevice>();

            try
            {
                if (settings.hive != null)
                {
                    HiveService service = new HiveService();

                    if (service.SignIn(settings.hive.username, settings.hive.password))
                    {
                        var hiveReadings = QueryHiveData(service);

                        allReadings.AddRange(hiveReadings);
                    }
                    else
                    {
                        alerts.Add(new Alert {
                            deviceName = "Hive", alertText = "Sign-in failed."
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying Hive data. {0}", ex);
            }

            try
            {
                if (settings.wirelesstag != null)
                {
                    WirelessSensorTagAPI tagService = new WirelessSensorTagAPI(settings.wirelesstag.WirelessTagServiceUrl);

                    if (tagService.SignIn(settings.wirelesstag.username, settings.wirelesstag.password))
                    {
                        var wirelessTagReadings = QuerySensorTags(tagService, allDevices);

                        allReadings.AddRange(wirelessTagReadings);
                    }
                    else
                    {
                        alerts.Add(new Alert {
                            deviceName = "Wireless Tags", alertText = "Sign-in failed."
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception querying SensorTag data. {0}", ex);
            }

            if (allReadings.Any())
            {
                try
                {
                    StoreReadings(allReadings, settings.indexname);
                }
                catch (Exception ex)
                {
                    Utils.Log("Exception ingesting data in ES. {0}", ex);
                }

                // See if any of the data we got back indicated a drained battery.
                CheckBatteryStatus(allReadings, settings.lowBatteryThresholdVolts, alerts);
            }

            try
            {
                // Now check for any missing data - i.e., long gaps since we last saw anything
                CheckMissingData(allDevices, alerts);

                // And send any alerts we saw.
                if (alerts.Any())
                {
                    Utils.Log("Sending {0} alerts.", alerts.Count);

                    if (settings.email != null)
                    {
                        Utils.SendAlertEmail(settings.email, alerts);
                    }
                    if (settings.push != null)
                    {
                        Utils.SendPushAlert(settings.push, alerts);
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.Log("Exception checking missing data. {0}", ex);
            }

            Utils.Log("Run complete.");
        }