public ActionResult Index()
        {
            IrrigationAdvisorContext.Instance();

            FarmConfiguration lFarmConfiguration;
            UserConfiguration lUserConfiguration;
            User        lLoggedUser;
            List <Farm> lFarmList;


            #region Configuration - Instance
            lUserConfiguration = new UserConfiguration();
            lFarmConfiguration = new FarmConfiguration();
            #endregion

            lLoggedUser = lUserConfiguration.GetUserByName(ManageSession.GetUserName());
            lFarmList   = lFarmConfiguration.GetFarmWithActiveCropIrrigationWeathersListBy(lLoggedUser);

            return(View("~/Views/Reports/CropIrrigationWeathersActive/CropIrrigationWeathersActive.cshtml", lFarmList));
        }
Ejemplo n.º 2
0
 public ActionResult Index()
 {
     IrrigationAdvisorContext.Instance();
     return(View("~/Views/Reports/CropIrrigationWeathersActive/CropIrrigationWeathersActive.cshtml"));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Return Grid Irrigation Unit for Home
        /// </summary>
        /// <returns></returns>
        public List <GridPivotHome> GetGridPivotHome()
        {
            #region Local Variables
            String lSomeData = "";
            List <GridPivotHome>       lGridIrrigationUnitList = new List <GridPivotHome>();
            GridPivotHome              lGridIrrigationUnit;
            List <GridPivotDetailHome> lGridIrrigationUnitDetailRow;
            GridPivotDetailHome        lGridIrrigationUnitRow;
            DateTime      lDateOfReference;
            FarmViewModel lFarmViewModel;
            User          lLoggedUser;
            List <Farm>   lFarmList;
            // Farm lCurrentFarm;
            List <IrrigationUnit>        lIrrigationUnitList;
            List <CropIrrigationWeather> lCropIrrigationWeatherList;
            String      lFirstPivotName = "";
            List <Rain> lRainList;
            List <Models.Water.Irrigation> lIrrigationList;
            List <DailyRecord>             lDailyRecordList;
            String        lSowingDate;
            String        lPhenologicalStageToday;
            Double        lHydricBalancePercentage;
            DailyRecord   lDailyRecord;
            String        lCropCoefficient;
            List <Double> lETcList;
            Double        lETcItem;
            HomeViewModel lHomeViewModel;
            #endregion

            #region Configuration Variables
            UserConfiguration                  lUserConfiguration;
            FarmConfiguration                  lFarmConfiguration;
            IrrigationUnitConfiguration        lIrrigationUnitConfiguration;
            CropIrrigationWeatherConfiguration lCropIrrigationWeatherConfiguration;
            IrrigationAdvisorContext           lIrrigationAdvisorContext;
            #endregion

            try
            {
                #region Configuration - Instance
                lUserConfiguration                  = new UserConfiguration();
                lFarmConfiguration                  = new FarmConfiguration();
                lIrrigationUnitConfiguration        = new IrrigationUnitConfiguration();
                lCropIrrigationWeatherConfiguration = new CropIrrigationWeatherConfiguration();
                lIrrigationAdvisorContext           = IrrigationAdvisorContext.Instance();
                #endregion

                lDateOfReference = ManageSession.GetNavigationDate();
                lSomeData        = lSomeData + "Date: " + lDateOfReference.Date + "-";

                //Obtain logged user
                lLoggedUser = lUserConfiguration.GetUserByName(ManageSession.GetUserName());
                lSomeData   = lSomeData + "User: "******"-";

                //Get list of Farms from User
                lFarmList = lFarmConfiguration.GetFarmWithActiveCropIrrigationWeathersListBy(lLoggedUser);

                //Create IrrigationQuantity Units List
                lIrrigationUnitList = new List <IrrigationUnit>();
                foreach (Farm lCurrentFarm in lFarmList)
                {
                    lSomeData      = lSomeData + "Farm: " + lCurrentFarm.Name + "-";
                    lFarmViewModel = new FarmViewModel(lCurrentFarm);

                    lIrrigationUnitList = lIrrigationUnitConfiguration.GetIrrigationUnitListBy(lCurrentFarm);

                    lCropIrrigationWeatherList = new List <CropIrrigationWeather>();
                    lDailyRecordList           = new List <DailyRecord>();

                    foreach (var lIrrigationUnit in lIrrigationUnitList)
                    {
                        lCropIrrigationWeatherList = lIrrigationUnitConfiguration.GetCropIrrigationWeatherListIncludeCropMainWeatherStationRainListIrrigationListBy(lIrrigationUnit, lDateOfReference);

                        lFirstPivotName = "";
                        lETcList        = new List <Double>();
                        foreach (CropIrrigationWeather lCropIrrigationWeather in lCropIrrigationWeatherList)
                        {
                            lSomeData = lSomeData + "CropIrrigationWeather: " + lCropIrrigationWeather.CropIrrigationWeatherName + "-";

                            lDailyRecordList = lCropIrrigationWeatherConfiguration.GetDailyRecordListIncludeDailyRecordListBy(lIrrigationUnit, lDateOfReference, lCropIrrigationWeather.Crop);

                            lRainList       = lCropIrrigationWeather.RainList;
                            lIrrigationList = lCropIrrigationWeather.IrrigationList;
                            lSowingDate     = lCropIrrigationWeather.SowingDate.Day.ToString()
                                              + "/" + lCropIrrigationWeather.SowingDate.Month.ToString();
                            //Grid of irrigation data
                            lGridIrrigationUnitDetailRow = new List <GridPivotDetailHome>();

                            lCropCoefficient         = String.Empty;
                            lPhenologicalStageToday  = String.Empty;
                            lHydricBalancePercentage = 0;

                            for (int i = -InitialTables.MIN_DAY_SHOW_IN_GRID_BEFORE_TODAY; i <= InitialTables.MAX_DAY_SHOW_IN_GRID_AFTER_TODAY; i++)
                            {
                                //Day i
                                lGridIrrigationUnitRow = AddGridIrrigationUnit(lDateOfReference, lDateOfReference.AddDays(i), lIrrigationList, lRainList, lDailyRecordList);
                                lGridIrrigationUnitDetailRow.Add(lGridIrrigationUnitRow);
                                if (i == 0) //TODAY
                                {
                                    //Obtain All data for today from DailyRecord
                                    lPhenologicalStageToday = lGridIrrigationUnitRow.Phenology;
                                    lDailyRecord            = lGridIrrigationUnitRow.DailyRecord;
                                    if (lDailyRecord != null)
                                    {
                                        lCropCoefficient         = lDailyRecord.CropCoefficient.ToString();
                                        lPhenologicalStageToday  = lDailyRecord.PhenologicalStage.Stage.ShortName;
                                        lHydricBalancePercentage = lDailyRecord.PercentageOfHydricBalance;
                                    }
                                }
                                if (lGridIrrigationUnitRow.DailyRecord == null)
                                {
                                    if (lCropIrrigationWeather.MainWeatherStationId > 0)
                                    {
                                        lETcItem = lIrrigationAdvisorContext.WeatherDatas
                                                   .Where(wd => wd.Date == lGridIrrigationUnitRow.DateOfData.Date &&
                                                          wd.WeatherStationId == lCropIrrigationWeather.MainWeatherStationId)
                                                   .Select(wd => wd.Evapotranspiration).FirstOrDefault();
                                    }
                                    else
                                    {
                                        lETcItem = lIrrigationAdvisorContext.WeatherDatas
                                                   .Where(wd => wd.Date == lGridIrrigationUnitRow.DateOfData.Date &&
                                                          wd.WeatherStationId == lCropIrrigationWeather.AlternativeWeatherStationId)
                                                   .Select(wd => wd.Evapotranspiration).FirstOrDefault();
                                    }
                                }
                                else
                                {
                                    lETcItem = Math.Round(lGridIrrigationUnitRow.DailyRecord.EvapotranspirationCrop.Output / lGridIrrigationUnitRow.DailyRecord.CropCoefficient, 2);
                                }
                                lETcList.Add(Math.Round(lETcItem, 2));
                            }
                            if (String.IsNullOrEmpty(lFirstPivotName))
                            {
                                lFirstPivotName = lCropIrrigationWeather.IrrigationUnit.ShortName;
                            }
                            else if (lFirstPivotName == lCropIrrigationWeather.IrrigationUnit.ShortName)
                            {
                                lFirstPivotName = "";
                            }

                            lHomeViewModel = ManageSession.GetHomeViewModel();

                            //Add all the days for the IrrigationUnit
                            lGridIrrigationUnit = new GridPivotHome(lCurrentFarm.Name + " | " + lFirstPivotName,
                                                                    lCropIrrigationWeather.Crop.ShortName,
                                                                    lSowingDate,
                                                                    lPhenologicalStageToday,
                                                                    lHydricBalancePercentage.ToString() + " %",
                                                                    lCropCoefficient,
                                                                    lHomeViewModel.IsUserAdministrator,
                                                                    lETcList,
                                                                    lGridIrrigationUnitDetailRow,
                                                                    lCropIrrigationWeather.CropIrrigationWeatherId);

                            lGridIrrigationUnitList.Add(lGridIrrigationUnit);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.LogError(ex, "Exception in HomeController.GetGridPivotHome \n {0} ", lSomeData);
                throw ex;
            }

            return(lGridIrrigationUnitList);
        }
        /// <summary>
        /// Add Grid Irrigation Unit with all columns data
        /// Using DailyRecords for obtain Irrigation, Rain information.
        /// </summary>
        /// <param name="pDayOfReference"></param>
        /// <param name="pDayOfData"></param>
        /// <param name="pIrrigationList"></param>
        /// <param name="pRainList"></param>
        /// <param name="pDailyRecordList"></param>
        /// <returns></returns>
        private GridPivotDetailHome AddGridIrrigationUnit(DateTime pDayOfReference, DateTime pDayOfData, List <Models.Water.Irrigation> pIrrigationList,
                                                          List <Rain> pRainList, List <DailyRecord> pDailyRecordList)
        {
            #region local variables
            String lSomeData            = "";
            GridPivotDetailHome lReturn = null;

            Double   lIrrigationQuantity        = 0;
            Double   lRainQuantity              = 0;
            Double   lForcastIrrigationQuantity = 0;
            Double   lWaterQuantity             = 0;
            DateTime lDateOfData     = Utils.MIN_DATETIME;
            DateTime lDayOfReference = Utils.MIN_DATETIME;
            bool     lIsToday        = false;
            Utils.IrrigationStatus lIrrigationStatus = Utils.IrrigationStatus.Default;
            String lPhenology = "";

            Rain        lRain        = null;
            DailyRecord lDailyRecord = null;
            #endregion

            try
            {
                lDateOfData     = pDayOfData.Date;
                lDayOfReference = pDayOfReference.Date;
                lSomeData       = lSomeData + "DayOfReference: " + lDayOfReference.Date + "-";
                lSomeData       = lSomeData + "DateOfData: " + lDateOfData.Date + "-";

                //Find Daily Record of the Date of Data
                lDailyRecord = pDailyRecordList.Where(dr => dr.DailyRecordDateTime.Date == lDateOfData.Date).FirstOrDefault();

                #region Irrigation in the past
                if (lDailyRecord != null && lDateOfData < lDayOfReference)
                {
                    if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Input > 0)
                    {
                        lIrrigationQuantity += lDailyRecord.Irrigation.Input;
                    }
                    else if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.ExtraInput > 0)
                    {
                        lIrrigationQuantity += lDailyRecord.Irrigation.ExtraInput;
                    }
                }
                #endregion

                lWaterQuantity = lIrrigationQuantity;
                lSomeData      = lSomeData + "IrrigationQuantity: " + lIrrigationQuantity + "-";

                #region Rain
                //Find Rain of the Date of Data
                lRain = pRainList.Where(r => r.Date.Date == lDateOfData || r.ExtraDate.Date == lDateOfData).FirstOrDefault();
                if (lRain != null && lRain.GetTotalInput() > 0)
                {
                    lRainQuantity = lRain.GetTotalInput();
                }
                #endregion

                lWaterQuantity += lRainQuantity;
                lSomeData       = lSomeData + "RainQuantity: " + lRainQuantity + "-";

                #region Irrigation for today or in the future
                //Find Daily Record of the Date of Data
                lDailyRecord = pDailyRecordList.Where(dr => dr.DailyRecordDateTime.Date == lDateOfData).FirstOrDefault();
                if (lDailyRecord != null && lDateOfData >= lDayOfReference)
                {
                    if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Input > 0)
                    {
                        lForcastIrrigationQuantity += lDailyRecord.Irrigation.Input;
                    }
                    else if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.ExtraInput > 0)
                    {
                        lForcastIrrigationQuantity += lDailyRecord.Irrigation.ExtraInput;
                    }
                }
                #endregion

                lWaterQuantity += lForcastIrrigationQuantity;
                lSomeData       = lSomeData + "ForcastIrrigationQuantity: " + lForcastIrrigationQuantity + "-";

                lIsToday = lDateOfData == lDayOfReference;

                if (lIsToday && lDailyRecord != null)
                {
                    lPhenology = lDailyRecord.PhenologicalStage.Stage.ShortName;
                }

                #region IrrigationStatus
                if (lRainQuantity > 0 && lRainQuantity >= (lWaterQuantity - lRainQuantity))
                {
                    lIrrigationStatus = Utils.IrrigationStatus.Rain;
                }
                else if (lIrrigationQuantity > 0 && lIrrigationQuantity > lRainQuantity)
                {
                    lIrrigationStatus = Utils.IrrigationStatus.Irrigated;
                }
                else if (lForcastIrrigationQuantity > 0 && lForcastIrrigationQuantity > lRainQuantity)
                {
                    lIrrigationStatus = Utils.IrrigationStatus.NextIrrigation;
                }
                else if (lDailyRecord != null && (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Type == Utils.WaterInputType.CantIrrigate))
                {
                    lIrrigationStatus = Utils.IrrigationStatus.CantIrrigate;
                }
                else if (lDailyRecord != null && (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Type == Utils.WaterInputType.IrrigationWasNotDecided))
                {
                    lIrrigationStatus = Utils.IrrigationStatus.IrrigationWasNotDecided;
                }
                else
                {
                    lIrrigationStatus = Utils.IrrigationStatus.Default;
                }
                #endregion

                lSomeData = lSomeData + "IrrigationStatus: " + lIrrigationStatus.ToString() + "-";

                var lContext = IrrigationAdvisorContext.Instance();

                bool lIsIrrigationConfirmated = false;

                if (lDailyRecord.Irrigation != null && (lDailyRecord.Irrigation.Type == Utils.WaterInputType.IrrigationByETCAcumulated || lDailyRecord.Irrigation.Type == Utils.WaterInputType.IrrigationByHydricBalance))
                {
                    lIsIrrigationConfirmated = lContext
                                               .Irrigations
                                               .Where(n => n.Type == Utils.WaterInputType.Confirmation && n.ExtraDate == lDateOfData && n.CropIrrigationWeatherId == lDailyRecord.CropIrrigationWeatherId)
                                               .Any();
                }
                else
                {
                    lIsIrrigationConfirmated = true;
                }

                lReturn = new GridPivotDetailHome(lIrrigationQuantity, lRainQuantity, lForcastIrrigationQuantity,
                                                  lDateOfData, lIsToday, lIrrigationStatus,
                                                  lPhenology, lIsIrrigationConfirmated, lDailyRecord);
            }
            catch (Exception ex)
            {
                Utils.LogError(ex, "Exception in HomeController.AddGridIrrigationUnit \n {0}", lSomeData);
                throw ex;
            }

            return(lReturn);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            string config = System.Configuration.ConfigurationManager.AppSettings["Status"];

            try
            {
                using (Entities entities = new Entities())
                {
                    bool isExecuting = entities.CalculationByCropIrrigationWeathers
                                       .Any(n => n.IsExecuting);

                    DateTime referenceDate = entities.Status.First(n => n.Name == config).DateOfReference;

                    if (!isExecuting)
                    {
                        logger.Log(LogLevel.Info, "Buscando CIW pendientes de procesar.");

                        var pendings = entities.CalculationByCropIrrigationWeathers
                                       .Where(n => !n.IsCompleted)
                                       .Select(n => n.CropIrrigationWeatherId)
                                       .Distinct()
                                       .ToList();

                        logger.Log(LogLevel.Info, "Fin de búsqueda CIW pendientes de procesar.");

                        var lIrrigationUnitConfiguration = new IrrigationUnitConfiguration();

                        logger.Log(LogLevel.Info, "Buscando los CIW para procesar.");

                        var cropIrrigationWeathers = lIrrigationUnitConfiguration.GetCropIrrigationWeatherListBy(pendings);

                        logger.Log(LogLevel.Info, "Fin de obtención de ciw.");

                        var lIrrigationAdvisorContext = IrrigationAdvisorContext.Instance();

                        if (cropIrrigationWeathers.Any())
                        {
                            Utils.SetStatusAsMaintenaince(config);
                        }

                        foreach (var item in cropIrrigationWeathers)
                        {
                            var executionProcess = entities.CalculationByCropIrrigationWeathers
                                                   .Where(n => n.CropIrrigationWeatherId == item.CropIrrigationWeatherId)
                                                   .ToList();

                            foreach (var e in executionProcess)
                            {
                                e.IsExecuting = true;
                            }

                            entities.SaveChanges();

                            logger.Log(LogLevel.Info, "Procesando CIW " + item.CropIrrigationWeatherId);

                            item.AddInformationToIrrigationUnits(referenceDate.AddDays(-5), referenceDate, lIrrigationAdvisorContext);
                            lIrrigationAdvisorContext.SaveChanges();

                            foreach (var e in executionProcess)
                            {
                                e.IsExecuting = false;
                                e.IsCompleted = true;
                            }

                            entities.SaveChanges();

                            logger.Log(LogLevel.Info, "Fin de procesamiento de CIW " + item.CropIrrigationWeatherId);
                        }

                        logger.Log(LogLevel.Info, "Fin de procesamiento general.");
                    }
                    else
                    {
                        logger.Log(LogLevel.Info, "No se puede comenzar un nuevo procesamiento ya que hay un procesamiento en ejecución.");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, ex);
            }
            finally
            {
                Utils.SetStatusAsOnline(config);
            }
        }