Ejemplo n.º 1
0
        public async Task <ActionResult> UpdateRuleProperties(EditLocationRuleModel editModel)
        {
            LocationRule updatedRule = CreateLocationRuleFromEditModel(editModel);
            TableStorageResponse <LocationRule> result = await _locationRulesLogic.SaveLocationRuleAsync(updatedRule);

            return(BuildRuleUpdateResponse(result));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieve the default location rule.
        /// </summary>
        /// <returns></returns>
        public async Task <LocationRule> GetDefaultLocationRuleAsync()
        {
            LocationRule defaultRule = await GetLocationRuleAsync("default", "default");

            if (defaultRule == null)
            {
                defaultRule = new LocationRule("default")
                {
                    RegionId          = "default",
                    Region            = "All",
                    RegionLatitude    = 300.0,
                    RegionLongitude   = 300.0,
                    VerticalThreshold = 25.05,
                    LateralThreshold  = 20.0,
                    ForwardThreshold  = 15.8,
                    RuleOutput        = "DefaultJerk",
                    Etag         = "",
                    EnabledState = true
                };

                await SaveLocationRuleAsync(defaultRule);
            }

            return(defaultRule);
        }
Ejemplo n.º 3
0
 private string GetIncorrectInputDetails(LocationRule rule)
 {
     if (string.IsNullOrWhiteSpace(rule.RuleId))
     {
         return(Strings.EmptyRuleId);
     }
     return(Strings.IncorrectRegionID);
 }
Ejemplo n.º 4
0
        public async Task <ActionResult> GetRuleProperties(string regionId, string ruleId)
        {
            LocationRule rule = await _locationRulesLogic.GetLocationRuleAsync(regionId, ruleId);

            EditLocationRuleModel editModel = CreateEditModelFromLocationRule(rule);

            editModel.IsCreateRequest = false;
            return(PartialView("_LocationRuleProperties", editModel));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Удаляет из цели правило выбора географической локации
        /// </summary>
        /// <param name="target">Цель геолокации</param>
        /// <param name="location">Географическая локация</param>
        public void RemoveLocationFromTarget(Target target, Location location)
        {
            LocationRule rule = LocationRuleRepository.FindByTargetAndLocation(target, location);

            if (rule != null)
            {
                LocationRuleRepository.Delete(rule);
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> Default()
        {
            LocationRule rule = await _locationRulesLogic.GetDefaultLocationRuleAsync();

            EditLocationRuleModel editModel = CreateEditModelFromLocationRule(rule);

            editModel.IsCreateRequest = false;

            return(View("EditLocationRuleProperties", editModel));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Retrieve a single rule from AzureTableStorage or default if none exists.
        /// A distinct rule is defined by the combination key regionID/ruleId
        /// </summary>
        /// <param name="regionLatitude"></param>
        /// <param name="regionLongitude"></param>
        /// <param name="ruleId"></param>
        /// <returns></returns>
        public async Task <LocationRule> GetLocationRuleAsync(string regionId, string ruleId)
        {
            TableOperation query = TableOperation.Retrieve <LocationRuleTableEntity>(regionId, ruleId);

            TableResult response = await Task.Run(() =>
                                                  _azureTableStorageClient.Execute(query)
                                                  );

            LocationRule result = BuildRuleFromTableEntity((LocationRuleTableEntity)response.Result);

            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Generate a new rule with bare-bones configuration. This new rule can then be configured and sent
        /// back through the SaveDeviceRuleAsync method to persist.
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        /// <returns></returns>
        public async Task <LocationRule> GetNewRuleAsync(string regionId, double latitude, double longitude)
        {
            //making sure we have only 2 decimal places counted
            double regionLatitude  = Math.Truncate(latitude * 100) / 100;
            double regionLongitude = Math.Truncate(longitude * 100) / 100;

            return(await Task.Run(() =>
            {
                var rule = new LocationRule();
                rule.InitializeNewRule(regionId, regionLatitude, regionLongitude);

                return rule;
            }));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            LocationRule = await _context.LocationRule.FirstOrDefaultAsync(m => m.LocationRuleID == id);

            if (LocationRule == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Selects all records from the LocationRule table.
        /// </summary>
        public virtual List <LocationRule> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "LocationRuleSelectAll"))
            {
                List <LocationRule> locationRuleList = new List <LocationRule>();
                while (dataReader.Read())
                {
                    LocationRule locationRule = MakeLocationRule(dataReader);
                    locationRuleList.Add(locationRule);
                }
                dataReader.Close();

                return(locationRuleList);
            }
        }
Ejemplo n.º 11
0
        public async Task <TableStorageResponse <LocationRule> > DeleteLocationRuleAsync(string regionId, string ruleId)
        {
            LocationRule found = await _locationRulesRepository.GetLocationRuleAsync(regionId, ruleId);

            if (found == null)
            {
                var response = new TableStorageResponse <LocationRule>();
                response.Entity = found;
                response.Status = TableStorageResponseStatus.NotFound;

                return(response);
            }

            return(await _locationRulesRepository.DeleteLocationRuleAsync(found));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            LocationRule = await _context.LocationRule.FindAsync(id);

            if (LocationRule != null)
            {
                _context.LocationRule.Remove(LocationRule);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates a new instance of the LocationRule class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual LocationRule MakeLocationRule(SqlDataReader dataReader)
        {
            LocationRule locationRule = new LocationRule();

            locationRule.ID              = SqlClientUtility.GetInt32(dataReader, "ID", 0);
            locationRule.Code            = SqlClientUtility.GetString(dataReader, "Code", String.Empty);
            locationRule.LocationID      = SqlClientUtility.GetInt32(dataReader, "LocationID", 0);
            locationRule.RuleID          = SqlClientUtility.GetInt32(dataReader, "RuleID", 0);
            locationRule.IsActive        = SqlClientUtility.GetBoolean(dataReader, "IsActive", false);
            locationRule.Comments        = SqlClientUtility.GetString(dataReader, "Comments", String.Empty);
            locationRule.InternalComment = SqlClientUtility.GetString(dataReader, "InternalComment", String.Empty);
            locationRule.CreatedBy       = SqlClientUtility.GetString(dataReader, "CreatedBy", String.Empty);
            locationRule.CreatedOn       = SqlClientUtility.GetDateTime(dataReader, "CreatedOn", DateTime.Now);
            locationRule.AuditActionBy   = SqlClientUtility.GetString(dataReader, "AuditActionBy", String.Empty);
            locationRule.AuditActionOn   = SqlClientUtility.GetDateTime(dataReader, "AuditActionOn", DateTime.Now);

            return(locationRule);
        }
Ejemplo n.º 14
0
        public async Task <ActionResult> CreateOrUpdate(double lat, double lng)
        {
            var          regionId = $"{Math.Truncate(lat * 10)/10}_{Math.Truncate(lng*10)/10}";
            LocationRule rule     = await _locationRulesLogic.GetLocationRuleOrDefaultAsync(regionId, lat, lng);

            EditLocationRuleModel editModel = CreateEditModelFromLocationRule(rule);

            if (string.IsNullOrWhiteSpace(rule.RuleId))
            {
                editModel.IsCreateRequest = true;
            }
            else
            {
                editModel.IsCreateRequest = false;
            }

            return(View("EditLocationRuleProperties", editModel));
        }
Ejemplo n.º 15
0
        private EditLocationRuleModel CreateEditModelFromLocationRule(LocationRule rule)
        {
            EditLocationRuleModel model = new EditLocationRuleModel()
            {
                RegionId          = rule.RegionId,
                Region            = rule.Region,
                RuleId            = rule.RuleId,
                RegionLatitude    = rule.RegionLatitude,
                RegionLongitude   = rule.RegionLongitude,
                VerticalThreshold = rule.VerticalThreshold,
                LateralThreshold  = rule.LateralThreshold,
                ForwardThreshold  = rule.ForwardThreshold,
                EnabledState      = rule.EnabledState,
                RuleOutput        = rule.RuleOutput,
                Etag = rule.Etag
            };

            return(model);
        }
Ejemplo n.º 16
0
        private LocationRule CreateLocationRuleFromEditModel(EditLocationRuleModel editModel)
        {
            LocationRule rule = new LocationRule()
            {
                EnabledState      = editModel.EnabledState,
                RegionId          = editModel.RegionId,
                RegionLatitude    = editModel.RegionLatitude,
                RegionLongitude   = editModel.RegionLongitude,
                Region            = editModel.Region,
                RuleId            = editModel.RuleId,
                VerticalThreshold = editModel.VerticalThreshold,
                LateralThreshold  = editModel.LateralThreshold,
                ForwardThreshold  = editModel.ForwardThreshold,
                RuleOutput        = editModel.RuleOutput,
                Etag = editModel.Etag
            };

            return(rule);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Selects all records from the LocationRule table by a foreign key.
        /// </summary>
        public virtual List <LocationRule> SelectAllByRuleID(int ruleID)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@RuleID", ruleID)
            };

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "LocationRuleSelectAllByRuleID", parameters))
            {
                List <LocationRule> locationRuleList = new List <LocationRule>();
                while (dataReader.Read())
                {
                    LocationRule locationRule = MakeLocationRule(dataReader);
                    locationRuleList.Add(locationRule);
                }

                return(locationRuleList);
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Saves a record to the LocationRule table.
        /// </summary>
        public virtual void Insert(LocationRule locationRule)
        {
            ValidationUtility.ValidateArgument("locationRule", locationRule);

            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Code", locationRule.Code),
                new SqlParameter("@LocationID", locationRule.LocationID),
                new SqlParameter("@RuleID", locationRule.RuleID),
                new SqlParameter("@IsActive", locationRule.IsActive),
                new SqlParameter("@Comments", locationRule.Comments),
                new SqlParameter("@InternalComment", locationRule.InternalComment),
                new SqlParameter("@CreatedBy", locationRule.CreatedBy),
                new SqlParameter("@CreatedOn", locationRule.CreatedOn),
                new SqlParameter("@AuditActionBy", locationRule.AuditActionBy),
                new SqlParameter("@AuditActionOn", locationRule.AuditActionOn)
            };

            locationRule.ID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "LocationRuleInsert", parameters));
        }
Ejemplo n.º 19
0
        // <summary>
        /// Navigate to the LocationReport screen
        /// </summary>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        /// <returns></returns>

        public async Task <ActionResult> LocationReport(LocationPropertiesModel locationProperties)
        {
            var errorMessage = locationProperties.CheckForErrorMessage();

            if (!String.IsNullOrWhiteSpace(errorMessage))
            {
                return(Json(new { error = errorMessage }));
            }

            var regionLatitude  = Math.Truncate((double)locationProperties.Latitude * 100) / 100;
            var regionLongitude = Math.Truncate((double)locationProperties.Longitude * 100) / 100;
            var regionId        = $"{Math.Truncate(regionLatitude * 10)/10}_{Math.Truncate(regionLongitude * 10)/10}";

            LocationRule rule = await _locationRulesLogic.GetLocationRuleOrDefaultAsync(regionId, regionLatitude, regionLongitude);

            locationProperties.RuleId                 = rule.RuleId;
            locationProperties.RegionLatitude         = rule.RegionLatitude;
            locationProperties.RegionLongitude        = rule.RegionLongitude;
            locationProperties.JerkedDeviceSelectList = GetSelectListFromJsonList(locationProperties.JsonList);
            return(View("LocationReport", locationProperties));
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Добавляет к цели правило выбора географической локации
        /// </summary>
        /// <param name="target">Цель геолокации</param>
        /// <param name="location">Географическая локация</param>
        /// <param name="kind">Вид правила включения</param>
        public void AddLocationToTarget(Target target, Location location, RuleKind kind)
        {
            LocationRule rule = LocationRuleRepository.FindByTargetAndLocation(target, location);

            if (rule == null)
            {
                rule = new LocationRule()
                {
                    TargetId   = target.Id,
                    LocationId = location.Id,
                    Kind       = kind
                };

                LocationRuleRepository.Create(rule);
            }
            else if (rule.Kind != kind)
            {
                rule.Kind = kind;
                LocationRuleRepository.Save(rule);
            }
        }
Ejemplo n.º 21
0
        private LocationRuleTableEntity BuildTableEntityFromRule(LocationRule incomingRule)
        {
            LocationRuleTableEntity tableEntity =
                new LocationRuleTableEntity(incomingRule.RegionId, incomingRule.RuleId)
            {
                Enabled           = incomingRule.EnabledState,
                Region            = incomingRule.Region,
                RegionLatitude    = incomingRule.RegionLatitude,
                RegionLongitude   = incomingRule.RegionLongitude,
                VerticalThreshold = incomingRule.VerticalThreshold,
                LateralThreshold  = incomingRule.LateralThreshold,
                ForwardThreshold  = incomingRule.ForwardThreshold,
                RuleOutput        = incomingRule.RuleOutput
            };

            if (!string.IsNullOrWhiteSpace(incomingRule.Etag))
            {
                tableEntity.ETag = incomingRule.Etag;
            }

            return(tableEntity);
        }
Ejemplo n.º 22
0
        private LocationRule BuildRuleFromTableEntity(LocationRuleTableEntity tableEntity)
        {
            if (tableEntity == null)
            {
                return(null);
            }

            var updatedRule = new LocationRule(tableEntity.RuleId)
            {
                RegionId          = tableEntity.RegionId,
                Region            = tableEntity.Region,
                EnabledState      = tableEntity.Enabled,
                RegionLatitude    = tableEntity.RegionLatitude,
                RegionLongitude   = tableEntity.RegionLongitude,
                VerticalThreshold = tableEntity.VerticalThreshold,
                LateralThreshold  = tableEntity.LateralThreshold,
                ForwardThreshold  = tableEntity.ForwardThreshold,
                RuleOutput        = tableEntity.RuleOutput,
                Etag = tableEntity.ETag
            };

            return(updatedRule);
        }
Ejemplo n.º 23
0
        public async Task <TableStorageResponse <LocationRule> > DeleteLocationRuleAsync(LocationRule ruleToDelete)
        {
            LocationRuleTableEntity incomingEntity = BuildTableEntityFromRule(ruleToDelete);

            TableStorageResponse <LocationRule> result =
                await _azureTableStorageClient.DoDeleteAsync <LocationRule, LocationRuleTableEntity>(incomingEntity, BuildRuleFromTableEntity);

            if (result.Status == TableStorageResponseStatus.Successful)
            {
                // Build up a new blob to push up for ASA job ref data
                List <LocationRuleBlobEntity> blobList = await BuildBlobEntityListFromTableRows();
                await PersistRulesToBlobStorageAsync(blobList);
            }

            return(result);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Save a rule to the data store. This method should be used for new rules as well as updating existing rules
        /// </summary>
        /// <param name="newRule"></param>
        /// <returns></returns>
        public async Task <TableStorageResponse <LocationRule> > SaveLocationRuleAsync(LocationRule newRule)
        {
            if (newRule.RegionId == "default")
            {
                return(await _locationRulesRepository.SaveLocationRuleAsync(newRule));
            }

            string regionId = $"{Math.Truncate(newRule.RegionLatitude * 10)/10}_{Math.Truncate(newRule.RegionLongitude *10)/10}";

            if (newRule.RegionId != regionId || string.IsNullOrWhiteSpace(newRule.RuleId))
            {
                var response = new TableStorageResponse <LocationRule>();
                response.Entity = newRule;
                response.Status = TableStorageResponseStatus.IncorrectEntry;

                return(response);
            }

            //Enforce single instance of a rule for a data field for a given device
            List <LocationRule> foundForRegion = await _locationRulesRepository.GetAllRulesForRegionAsync(newRule.RegionId);

            foreach (LocationRule rule in foundForRegion)
            {
                if ((rule.RuleId != newRule.RuleId) &&
                    (rule.RegionLatitude == newRule.RegionLatitude) && (rule.RegionLongitude == newRule.RegionLongitude))
                {
                    var response = new TableStorageResponse <LocationRule>();
                    response.Entity = rule;
                    response.Status = TableStorageResponseStatus.DuplicateInsert;

                    return(response);
                }
            }

            return(await _locationRulesRepository.SaveLocationRuleAsync(newRule));
        }
Ejemplo n.º 25
0
 public void AddRule(LocationRule r)
 {
     rules.Add(r);
 }
Ejemplo n.º 26
0
 public void RemoveRule(LocationRule r)
 {
     rules.Remove(r);
 }