Exemplo n.º 1
0
        public static bool ValidateRules(GroupedMeasure currentMeasures, int productionUnitId, string productionUnitOwnerMail)
        {
            Evaluator evaluator = new Evaluator();
            bool      isValid   = true;

            ApplicationDbContext db    = new ApplicationDbContext();
            ApplicationDbContext dbLog = new ApplicationDbContext();

            var data      = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Content/AquaponicsRules.json"));
            var rulesList = JsonConvert.DeserializeObject <List <Rule> >(data);

            var currentProductionUnit = db.ProductionUnits.Where(p => p.Id == productionUnitId).FirstOrDefault();
            var warningEventType      = db.EventTypes.Where(p => p.Id == 1).FirstOrDefault();

            foreach (var rule in rulesList)
            {
                try
                {
                    bool rslt = evaluator.Evaluate(rule.ruleEvaluator, currentMeasures);
                    if (rslt)
                    {
                        var bindingValue = currentMeasures.GetType().GetProperty(rule.bindingPropertyValue).GetValue(currentMeasures, null);
                        var message      = String.Format(rule.warningContent, bindingValue);

                        if (currentProductionUnit != null)
                        {
                            db.Events.Add(new Event()
                            {
                                date = DateTime.Now, description = message, productionUnit = currentProductionUnit, eventType = warningEventType
                            });
                            db.SaveChanges();
                        }

                        isValid = false;
                    }
                }
                catch (Exception ex)
                {
                    dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager Evaluator"), ex));
                    dbLog.SaveChanges();
                }
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager - Save Events"), ex));
                dbLog.SaveChanges();
            }

            return(isValid);
        }
Exemplo n.º 2
0
        public static List <RecommandationTemplaceObject> ValidateRules(GroupedMeasure currentMeasures, int productionUnitId)
        {
            Evaluator evaluator = new Evaluator();

            ApplicationDbContext db    = new ApplicationDbContext();
            ApplicationDbContext dbLog = new ApplicationDbContext();

            var data      = File.ReadAllText(HostingEnvironment.MapPath("~/Content/SmartGreenhouseRules.json"));
            var rulesList = JsonConvert.DeserializeObject <List <Rule> >(data);

            var currentProductionUnit = db.ProductionUnits.Include(p => p.owner.language).Where(p => p.Id == productionUnitId).FirstOrDefault();

            var issueEventType   = db.EventTypes.Where(p => p.Id == 2).FirstOrDefault();
            var warningEventType = db.EventTypes.Where(p => p.Id == 1).FirstOrDefault();
            var infoEventType    = db.EventTypes.Where(p => p.Id == 8).FirstOrDefault();

            var currentProductionUnitOwner = currentProductionUnit.owner;

            List <RecommandationTemplaceObject> reco = new List <RecommandationTemplaceObject>();

            foreach (var rule in rulesList)
            {
                var strTitle       = String.Empty;
                var strDescription = String.Empty;
                var strUrl         = String.Empty;

                var eventType = new EventType();

                bool rslt = false;

                try
                {
                    try
                    {
                        rslt = evaluator.Evaluate(rule.ruleEvaluator, currentMeasures);

                        strTitle       = rule.title;
                        strDescription = rule.description;
                        strUrl         = rule.url;

                        switch (rule.impactLevel)
                        {
                        case 0:
                            eventType = issueEventType;
                            break;

                        case 1:
                            eventType = warningEventType;
                            break;

                        case 2:
                            eventType = infoEventType;
                            break;

                        default:
                            eventType = infoEventType;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager Evaluator - {0}", rule.ruleEvaluator), ex));
                        dbLog.SaveChanges();
                    }

                    if (currentProductionUnitOwner != null && currentProductionUnitOwner.language != null)
                    {
                        switch (currentProductionUnitOwner.language.description)
                        {
                        case "fr":
                            strTitle       = rule.titleFR;
                            strDescription = rule.descriptionFR;
                            break;

                        case "de":
                            strTitle       = rule.titleDE;
                            strDescription = rule.descriptionDE;
                            strUrl         = String.Format("{0}?ljs=de", rule.url);
                            break;

                        default:
                            strTitle       = rule.title;
                            strDescription = rule.description;
                            strUrl         = String.Format("{0}?ljs=en", rule.url);
                            break;
                        }
                    }

                    if (rslt)
                    {
                        var bindingValue   = currentMeasures.GetType().GetProperty(rule.bindingPropertyValue).GetValue(currentMeasures, null);
                        var strFormatTitle = String.Format(strTitle, bindingValue);

                        if (currentProductionUnit != null)
                        {
                            reco.Add(new RecommandationTemplaceObject()
                            {
                                title = strFormatTitle, description = strDescription, url = strUrl, order = rule.impactLevel
                            });

                            var content = String.Format(@"<b>{0}</b> </br> {1} </br> <a style=""color:#515A5A;"" href=""{2}"">Link</a>", strFormatTitle, strDescription, strUrl);

                            db.Events.Add(new Event()
                            {
                                date = DateTime.Now, description = content, isOpen = false, productionUnit = currentProductionUnit, eventType = eventType, createdBy = "MyFood Bot"
                            });
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager Evaluator - {0}", rule.ruleEvaluator), ex));
                    dbLog.SaveChanges();
                }
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager - Save Events"), ex));
                dbLog.SaveChanges();
            }

            return(reco);
        }
Exemplo n.º 3
0
        public static bool ValidateRules(GroupedMeasure currentMeasures, int productionUnitId)
        {
            Evaluator evaluator = new Evaluator();
            bool      isValid   = true;

            ApplicationDbContext db    = new ApplicationDbContext();
            ApplicationDbContext dbLog = new ApplicationDbContext();

            var data      = File.ReadAllText(HostingEnvironment.MapPath("~/Content/AquaponicsRules.json"));
            var rulesList = JsonConvert.DeserializeObject <List <Rule> >(data);

            var currentProductionUnit = db.ProductionUnits.Include(p => p.owner.language).Where(p => p.Id == productionUnitId).FirstOrDefault();
            var warningEventType      = db.EventTypes.Where(p => p.Id == 1).FirstOrDefault();

            var currentProductionUnitOwner = currentProductionUnit.owner;

            foreach (var rule in rulesList)
            {
                var  warningContent = String.Empty;
                bool rslt           = false;

                try
                {
                    try
                    {
                        rslt           = evaluator.Evaluate(rule.ruleEvaluator, currentMeasures);
                        warningContent = rule.warningContent;
                    }
                    catch (Exception ex)
                    {
                        dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager Evaluator - {0}", rule.ruleEvaluator), ex));
                        dbLog.SaveChanges();
                    }

                    if (currentProductionUnitOwner != null && currentProductionUnitOwner.language != null)
                    {
                        switch (currentProductionUnitOwner.language.description)
                        {
                        case "fr":
                            warningContent = rule.warningContentFR;
                            break;

                        default:
                            break;
                        }
                    }

                    var bindingValue = currentMeasures.GetType().GetProperty(rule.bindingPropertyValue).GetValue(currentMeasures, null);
                    var message      = String.Format(warningContent, bindingValue);

                    if (rslt)
                    {
                        if (currentProductionUnit != null)
                        {
                            db.Events.Add(new Event()
                            {
                                date = DateTime.Now, description = message, isOpen = false, productionUnit = currentProductionUnit, eventType = warningEventType, createdBy = "MyFood Bot"
                            });
                            db.SaveChanges();
                        }

                        isValid = false;
                    }
                }
                catch (Exception ex)
                {
                    dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager Evaluator - {0}", rule.ruleEvaluator), ex));
                    dbLog.SaveChanges();
                }
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                dbLog.Logs.Add(Log.CreateErrorLog(String.Format("Error with Rule Manager - Save Events"), ex));
                dbLog.SaveChanges();
            }

            return(isValid);
        }