public ActionResult GetProductionUnitDetailPopUp(int id)
        {
            var db = new ApplicationDbContext();

            var productionUnits = db.ProductionUnits.Where(p => p.Id == id)
                                  .Include(p => p.owner.preferedMoment)
                                  .Include(p => p.productionUnitType)
                                  .Include(p => p.productionUnitStatus).ToList();

            var productionUnit = productionUnits.FirstOrDefault();

            if (productionUnit == null)
            {
                return(Json(""));
            }

            var options = db.OptionLists.Include(o => o.productionUnit)
                          .Include(o => o.option)
                          .Where(o => o.productionUnit.Id == productionUnit.Id)
                          .Select(o => o.option);

            var optionList = string.Empty;

            if (options.Count() > 0)
            {
                options.ToList().ForEach(o => { optionList += o.name + "/"; });
            }

            var averageMonthlyProduction = PerformanceManager.GetEstimatedMonthlyProduction(productionUnit.productionUnitType.Id);
            var onlineSinceWeeks         = Math.Round((DateTime.Now - productionUnit.startDate).TotalDays / 7);
            var averageMonthlySparedCO2  = PerformanceManager.GetEstimatedMonthlySparedCO2(averageMonthlyProduction);

            var results = new object();

            results = new
            {
                AverageMonthlySparedCO2  = averageMonthlySparedCO2,
                OnlineSinceWeeks         = onlineSinceWeeks,
                AverageMonthlyProduction = averageMonthlyProduction,
                PioneerCitizenName       = productionUnit.owner.pioneerCitizenName,
                PioneerCitizenNumber     = productionUnit.owner.pioneerCitizenNumber,
                ProductionUnitStartDate  = productionUnit.startDate,
                ProductionUnitInfo       = productionUnit.info,
                ProductionUnitTypeName   = productionUnit.productionUnitType.name,
                ProductionUnitTypeImage  = productionUnit.productionUnitType.imagePath,
                ProductionUnitStatus     = productionUnit.productionUnitStatus.name,

                PhoneNumber = productionUnit.owner.phoneNumber == null ? "00 33 3 67 37 00 56" : productionUnit.owner.phoneNumber,
                ContactMail = productionUnit.owner.contactMail == null ? "*****@*****.**" : productionUnit.owner.contactMail,
                PicturePath = productionUnit.picturePath == null ? "NoImage.png" : productionUnit.picturePath,

                PreferedMoment = productionUnit.owner.preferedMoment == null ? "" : productionUnit.owner.preferedMoment.name,
                Location       = productionUnit.owner.location == null ? "" : productionUnit.owner.location,

                ProductionUnitOptions = optionList,
            };

            return(Json(results));
        }
        public ActionResult GetProductionUnitDetail(int id)
        {
            ApplicationDbContext db             = new ApplicationDbContext();
            MeasureService       measureService = new MeasureService(db);

            var currentProductionUnit = db.ProductionUnits.Include(p => p.owner.user)
                                        .Include(p => p.productionUnitType)
                                        .Include(p => p.options)
                                        .Where(p => p.Id == id).FirstOrDefault();

            if (currentProductionUnit == null)
            {
                return(Json(""));
            }

            var averageMonthlyProduction = PerformanceManager.GetEstimatedMonthlyProduction(currentProductionUnit.productionUnitType.Id);
            var onlineSinceWeeks         = Math.Round((DateTime.Now - currentProductionUnit.startDate).TotalDays / 7);
            var averageMonthlySparedCO2  = averageMonthlyProduction * 0.3;

            var pHSensorValueSet        = SensorValueManager.GetSensorValueSet(currentProductionUnit.Id, SensorTypeEnum.ph, db);
            var waterTempSensorValueSet = SensorValueManager.GetSensorValueSet(currentProductionUnit.Id, SensorTypeEnum.waterTemperature, db);
            var airTempSensorValueSet   = SensorValueManager.GetSensorValueSet(currentProductionUnit.Id, SensorTypeEnum.airTemperature, db);
            var humiditySensorValueSet  = SensorValueManager.GetSensorValueSet(currentProductionUnit.Id, SensorTypeEnum.humidity, db);

            if (db.ProductionUnitOwners.FirstOrDefault(o => o.user.UserName == this.User.Identity.Name).hasFahrenheitSetting == true)
            {
                if (waterTempSensorValueSet.CurrentCaptureTime != "-")
                {
                    waterTempSensorValueSet.AverageDayValue  = Math.Round(waterTempSensorValueSet.AverageDayValue * 9 / 5, 1) + 32;
                    waterTempSensorValueSet.AverageHourValue = Math.Round(waterTempSensorValueSet.AverageHourValue * 9 / 5, 1) + 32;
                    waterTempSensorValueSet.CurrentValue     = Math.Round(waterTempSensorValueSet.CurrentValue * 9 / 5, 1) + 32;
                }
                if (airTempSensorValueSet.CurrentCaptureTime != "-")
                {
                    airTempSensorValueSet.AverageDayValue  = Math.Round(airTempSensorValueSet.AverageDayValue * 9 / 5, 1) + 32;
                    airTempSensorValueSet.AverageHourValue = Math.Round(airTempSensorValueSet.AverageHourValue * 9 / 5, 1) + 32;
                    airTempSensorValueSet.CurrentValue     = Math.Round(airTempSensorValueSet.CurrentValue * 9 / 5, 1) + 32;
                }
            }

            var options = db.OptionLists.Include(o => o.productionUnit)
                          .Include(o => o.option)
                          .Where(p => p.productionUnit.Id == id)
                          .Select(p => p.option);

            var optionList = string.Empty;

            if (options.Count() > 0)
            {
                options.ToList().ForEach(o => { optionList += o.name + " / "; });
            }


            return(Json(new
            {
                PioneerCitizenName = currentProductionUnit.owner.pioneerCitizenName,
                PioneerCitizenNumber = currentProductionUnit.owner.pioneerCitizenNumber,
                ProductionUnitVersion = currentProductionUnit.version,
                ProductionUnitType = currentProductionUnit.productionUnitType.name,
                PicturePath = currentProductionUnit.picturePath,

                ProductionUnitOptions = optionList,
                OnlineSinceWeeks = onlineSinceWeeks,

                AverageMonthlyProduction = averageMonthlyProduction,
                AverageMonthlySparedCO2 = averageMonthlySparedCO2,

                CurrentPhValue = pHSensorValueSet.CurrentValue,
                CurrentPhCaptureTime = pHSensorValueSet.CurrentCaptureTime,
                AverageHourPhValue = pHSensorValueSet.AverageHourValue,
                AverageDayPhValue = pHSensorValueSet.AverageDayValue,
                LastDayPhCaptureTime = pHSensorValueSet.LastDayCaptureTime,

                CurrentWaterTempValue = waterTempSensorValueSet.CurrentValue,
                CurrentWaterTempCaptureTime = waterTempSensorValueSet.CurrentCaptureTime,
                AverageHourWaterTempValue = waterTempSensorValueSet.AverageHourValue,
                AverageDayWaterTempValue = waterTempSensorValueSet.AverageDayValue,
                LastDayWaterTempCaptureTime = waterTempSensorValueSet.LastDayCaptureTime,

                CurrentAirTempValue = airTempSensorValueSet.CurrentValue,
                CurrentAirTempCaptureTime = airTempSensorValueSet.CurrentCaptureTime,
                AverageHourAirTempValue = airTempSensorValueSet.AverageHourValue,
                AverageDayAirTempValue = airTempSensorValueSet.AverageDayValue,
                LastDayAirTempCaptureTime = airTempSensorValueSet.LastDayCaptureTime,

                CurrentHumidityValue = humiditySensorValueSet.CurrentValue,
                CurrentHumidityCaptureTime = humiditySensorValueSet.CurrentCaptureTime,
                AverageHourHumidityValue = humiditySensorValueSet.AverageHourValue,
                AverageDayHumidityValue = humiditySensorValueSet.AverageDayValue,
                LastDayHumidityCaptureTime = humiditySensorValueSet.LastDayCaptureTime,
            }, JsonRequestBehavior.AllowGet));
        }