// add geofence to listbox
        private void AddGeofenceToRegisteredGeofenceListBox(Geofence geofence)
        {
            GeofenceItem item = new GeofenceItem(geofence);

            // the registered geofence listbox is data bound
            // to the collection stored in the root page
            geofenceCollection.Insert(0, item);
        }
Example #2
0
        private void LoadGeofences()
        {
            foreach (var(serverId, serverConfig) in _config.Instance.Servers)
            {
                serverConfig.Geofences.Clear();

                var geofenceFiles = serverConfig.GeofenceFiles;
                var geofences     = new List <GeofenceItem>();

                if (geofenceFiles != null && geofenceFiles.Any())
                {
                    foreach (var file in geofenceFiles)
                    {
                        var filePath = Path.Combine(Strings.GeofenceFolder, file);

                        try
                        {
                            var fileGeofences = GeofenceItem.FromFile(filePath);

                            geofences.AddRange(fileGeofences);

                            _logger.Info($"Successfully loaded {fileGeofences.Count} geofences from {file}");
                        }
                        catch (Exception ex)
                        {
                            _logger.Error($"Could not load Geofence file {file} (for server {serverId}):");
                            _logger.Error(ex);
                        }
                    }
                }

                serverConfig.Geofences.AddRange(geofences);
            }
        }
Example #3
0
        public NotificationProcessor(DiscordClient client, IDatabase db, Config config, IEventLogger logger)
        {
            _client = client;
            _db     = db;
            _config = config;
            _logger = logger;

            _filters          = new Filters(_logger);
            _uniqueMessageIds = new Dictionary <string, ulong>();

            GeofenceSvc = new GeofenceService(GeofenceItem.Load(_config.GeofenceFolder, _config.CityRoles));
            _builder    = new NotificationBuilder(_client, _config, _db, _logger, GeofenceSvc);
        }
Example #4
0
        /// <summary>
        /// Load geofences from the `/Geofences` folder
        /// </summary>
        /// <returns>Returns parsed geofence list</returns>
        public List <GeofenceItem> LoadGeofence()
        {
            if (string.IsNullOrEmpty(GeofenceFile))
            {
                return(null);
            }

            var path = Path.Combine(Strings.GeofenceFolder, GeofenceFile);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"Geofence file {path} not found.", path);
            }

            return(Geofences = GeofenceItem.FromFile(path));
        }
        private void RefreshControlsFromGeofenceItem(GeofenceItem item)
        {
            if (null != item)
            {
                Id.Text = item.Id;
                Latitude.Text = item.Latitude.ToString();
                Longitude.Text = item.Longitude.ToString();
                Radius.Text = item.Radius.ToString();
                SingleUse.IsChecked = item.SingleUse;

                if (0 != item.DwellTime.Ticks)
                {
                    DwellTime.Text = item.DwellTime.ToString();
                }
                else
                {
                    DwellTime.Text = "";
                }

                if (0 != item.Duration.Ticks)
                {
                    Duration.Text = item.Duration.ToString();
                }
                else
                {
                    Duration.Text = "";
                }

                if (0 != item.StartTime.Ticks)
                {
                    DateTimeOffset dt = item.StartTime;

                    StartTime.Text = formatterShortDateLongTime.Format(dt);
                }
                else
                {
                    StartTime.Text = "";
                }

                // Update flags used to enable Create Geofence button
                OnIdTextChanged(null, null);
                OnLongitudeTextChanged(null, null);
                OnLatitudeTextChanged(null, null);
                OnRadiusTextChanged(null, null);
            }
        }
Example #6
0
        private static IEnumerable <GeofenceItem> LoadGeofences(string geofencesFolder)
        {
            var geofences = new List <GeofenceItem>();

            foreach (var file in Directory.EnumerateFiles(geofencesFolder))
            {
                try
                {
                    var fileGeofences = GeofenceItem.FromFile(file);

                    geofences.AddRange(fileGeofences);
                }
                catch (Exception ex)
                {
                    TestContext.Error.WriteLine($"Could not load Geofence file {file}:");
                    TestContext.Error.WriteLine(ex);
                }
            }

            return(geofences);
        }
Example #7
0
        /// <summary>
        /// Load geofences from the `/Geofences` folder
        /// </summary>
        /// <returns>Returns parsed geofence list</returns>
        public List <GeofenceItem> LoadGeofence()
        {
            var geofences = new List <GeofenceItem>();

            if (GeofenceFiles.Count == 0)
            {
                return(geofences);
            }

            foreach (var geofenceFile in GeofenceFiles)
            {
                var path = Path.Combine(Strings.GeofenceFolder, geofenceFile);
                if (!File.Exists(path))
                {
                    throw new FileNotFoundException($"Geofence file {path} not found.", path);
                }

                // Only return the first geofence
                var geofence = GeofenceItem.FromFile(path).FirstOrDefault();
                geofences.Add(geofence);
            }
            return(Geofences = geofences);
        }
Example #8
0
        private AlarmList LoadAlarms(ulong forGuildId, string alarmsFilePath)
        {
            _logger.Trace($"WebhookManager::LoadAlarms [AlarmsFilePath={alarmsFilePath}]");

            if (!File.Exists(alarmsFilePath))
            {
                _logger.Error($"Failed to load file alarms file '{alarmsFilePath}'...");
                return(null);
            }

            var alarmData = File.ReadAllText(alarmsFilePath);

            if (string.IsNullOrEmpty(alarmData))
            {
                _logger.Error($"Failed to load '{alarmsFilePath}', file is empty...");
                return(null);
            }

            var alarms = JsonConvert.DeserializeObject <AlarmList>(alarmData);

            if (alarms == null)
            {
                _logger.Error($"Failed to deserialize the alarms file '{alarmsFilePath}', make sure you don't have any json syntax errors.");
                return(null);
            }

            _logger.Info($"Alarms file {alarmsFilePath} was loaded successfully.");

            foreach (var alarm in alarms.Alarms)
            {
                if (alarm.Geofences != null)
                {
                    foreach (var geofenceName in alarm.Geofences)
                    {
                        lock (_geofencesLock)
                        {
                            // First try and find loaded geofences for this server by name or filename (so we don't have to parse already loaded files again)
                            var server    = _config.Instance.Servers[forGuildId];
                            var geofences = server.Geofences.Where(g => g.Name.Equals(geofenceName, StringComparison.OrdinalIgnoreCase) ||
                                                                   g.Filename.Equals(geofenceName, StringComparison.OrdinalIgnoreCase)).ToList();

                            if (geofences.Any())
                            {
                                alarm.GeofenceItems.AddRange(geofences);
                            }
                            else
                            {
                                // Try and load from a file instead
                                var filePath = Path.Combine(Strings.GeofenceFolder, geofenceName);

                                if (!File.Exists(filePath))
                                {
                                    _logger.Warn($"Could not find Geofence file \"{geofenceName}\" for alarm \"{alarm.Name}\"");
                                    continue;
                                }

                                var fileGeofences = GeofenceItem.FromFile(filePath);

                                alarm.GeofenceItems.AddRange(fileGeofences);
                                _logger.Info($"Successfully loaded {fileGeofences.Count} geofences from {geofenceName}");
                            }
                        }
                    }
                }

                alarm.LoadAlerts();
                alarm.LoadFilters();
            }

            return(alarms);
        }
Example #9
0
 public void ParseGeofenceFile()
 {
     Geofence = GeofenceItem.FromFile(GeofenceFile);
 }
Example #10
0
        //private string SanitizeCityName(string city)
        //{
        //    return city
        //        .Replace("Rancho Cucamonga", "Upland")
        //        .Replace("La Verne", "Pomona")
        //        .Replace("Diamond Bar", "Pomona")
        //        .Replace("Los Angeles", "EastLA")
        //        .Replace("East Los Angeles", "EastLA")
        //        .Replace("Commerce", "EastLA")
        //        .Replace("Santa Fe Springs", "Whittier");
        //}

        #endregion

        #region Filter Checks

        private bool MatchesGeofenceFilter(GeofenceItem geofence, Location location)
        {
            return(GeofenceSvc.Contains(geofence, location));
        }