public async Task <ActionResult> EditCondition(MatcherViewModel model, string updateButton)
        {
            string previousPage = CacheHelper.GetPreviousPage(_cache);

            if (updateButton.Equals("Cancel"))
            {
                return(Redirect(previousPage));
            }

            var matcher = await DigitalTwinsHelper.GetMatcherAsync(model.SelectedMatcher.Id, _cache, Loggers.SilentLogger, false);

            if (matcher != null)
            {
                var index = matcher.Conditions.FindIndex(m => m.Id == model.SelectedMatcherCondition.Id);

                if (updateButton.Equals("Update"))
                {
                    matcher.Conditions[index] = model.SelectedMatcherCondition;
                }

                if (updateButton.Equals("Delete"))
                {
                    matcher.Conditions.RemoveAt(index);
                }

                CacheHelper.AddInCache(_cache, matcher, matcher.Id, Context.Matcher);
            }
            return(Redirect(previousPage));
        }
        public async Task <ActionResult> Edit(MatcherViewModel model, string updateButton)
        {
            string previousPage = CacheHelper.GetPreviousPage(_cache);

            if (updateButton.Equals("Cancel"))
            {
                return(Redirect(previousPage));
            }

            // we load the matcher in cache to restore the conditions previously defined
            var matcher = await DigitalTwinsHelper.GetMatcherAsync(model.SelectedMatcher.Id, _cache, Loggers.SilentLogger, false);

            matcher.Name = model.SelectedMatcher.Name;

            // we update the matcher
            await DigitalTwinsHelper.UpdateMatcherAsync(matcher, _cache, Loggers.SilentLogger);

            // we associate the matcher with the UDF
            //var udf = await DigitalTwinsHelper.GetUserDefinedFunction(model.UdfId, _cache, Loggers.SilentLogger, false);
            //udf.Matchers.Add(matcher);
            //await DigitalTwinsHelper.UpdateUserDefinedFunctionAsync(_cache, Loggers.SilentLogger, udf);

            //we remove the matcher from the cache
            CacheHelper.AddInCache(_cache, null, Guid.Empty, Context.Matcher);

            return(RedirectToAction("Edit", "UDF", new { id = model.UdfId }));
        }
        public async Task <ActionResult> AddCondition(MatcherViewModel model, string createButton)
        {
            string previousPage = CacheHelper.GetPreviousPage(_cache);

            var matcher = await DigitalTwinsHelper.GetMatcherAsync(model.SelectedMatcher.Id, _cache, Loggers.SilentLogger, false);

            if (matcher != null)
            {
                matcher.Conditions.Add(model.SelectedMatcherCondition);
                CacheHelper.AddInCache(_cache, matcher, matcher.Id, Context.Matcher);
            }
            return(Redirect(previousPage));
        }
        private async Task LoadAsync(Guid?id = null, Guid?spaceId = null)
        {
            ConditionTargetList = new List <string>
            {
                "Sensor",
                "SensorDevice",
                "SensorSpace"
            };

            ConditionComparisonList = new List <string>
            {
                "Equals",
                "NotEquals",
                "Contains"
            };

            SensorDataTypeList = await DigitalTwinsHelper.GetTypesAsync(Types.SensorDataType, _cache, Loggers.SilentLogger, true);

            if (id != null)
            {
                this.SelectedMatcher = await DigitalTwinsHelper.GetMatcherAsync((Guid)id, _cache, Loggers.SilentLogger, false);
            }
            else
            {
                // if we are back from the creation of a MatcherCondition, we reload the working Matcher
                Matcher matcher = (Matcher)CacheHelper.GetFromCache(_cache, Guid.Empty, Context.Matcher);
                if (matcher == null)
                {
                    matcher = new Matcher()
                    {
                        SpaceId = (Guid)spaceId
                    };
                    CacheHelper.AddInCache(_cache, matcher, matcher.Id, Context.Matcher);
                }
                this.SelectedMatcher          = matcher;
                this.SelectedMatcherCondition = new MatcherCondition();
            }
        }