public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters)
        {
            Module.SetPrint(0);
            //var relevantPotentials = _pvPotentials.Where(x => x.HouseGuid == houseComponent.HouseGuid);
            PvSystemEntry entry = (PvSystemEntry)parameters.HouseComponent;
            int           idx   = 0;

            foreach (var area in entry.PVAreas)
            {
                var key    = MakeKeyFromPVArea(area);
                var keystr = key.GetKey();
                //key has been checked in this run
                if (_checkedKeys.Contains(keystr))
                {
                    continue;
                }

                _checkedKeys.Add(keystr);
                bool isInDb = _saveableEntries.CheckForName(keystr, MyLogger);
                if (isInDb)
                {
                    continue;
                }

                Info("Missing pv profile for " + keystr + ", generating...");
                PVSystemSettings pvs = new PVSystemSettings(key, 1, 1, MyLogger, idx++);
                var profile          = pvs.Run(Services.RunningConfig);
                _saveableEntries.AddRow(profile);
                _saveableEntries.SaveDictionaryToDatabase(MyLogger);
            }

            return(true);
        }
        public void TestCreationSavingAndKeyCheck()
        {
            SqlConnectionPreparer ms = new SqlConnectionPreparer(Config);
            var     db = ms.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice);
            Profile p  = new Profile("myProfile", new List <double>().AsReadOnly(), EnergyOrPower.Power);
            SaveableEntry <Profile> sa = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.PVGeneration, Logger);

            sa.MakeCleanTableForListOfFields(true);
            sa.MakeTableForListOfFieldsIfNotExists(true);
            string myKey = p.Name;

            if (sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Key already exists in cleared db");
            }

            sa.AddRow(p);
            sa.SaveDictionaryToDatabase(Logger);
            if (!sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Saving failed. Key not in db");
            }
        }
        public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters)
        {
            CarDistanceEntry cde = (CarDistanceEntry)parameters.HouseComponent;
            Car myCar            = _dbDto.Cars.Single(x => x.Guid == cde.CarGuid);

            parameters.HouseComponentResultObject.CarStatus = "Gasoline, not analyzed further";

            if (myCar.RequiresProfile == CarProfileRequirement.NoProfile)
            {
                return(true);
            }

            Hausanschluss ha = _dbDto.Hausanschlusse.Single(x => x.Guid == cde.HausAnschlussGuid);

            parameters.HouseComponentResultObject.CarStatus            = "Electric";
            parameters.HouseComponentResultObject.CommutingDistance    = cde.CommutingDistance;
            parameters.HouseComponentResultObject.OtherDrivingDistance = cde.FreizeitDistance;
            Household flaHousehold = _dbDto.Households.FirstOrDefault(x => x.Guid == cde.HouseholdGuid);

            if (flaHousehold == null)
            {
                parameters.HouseComponentResultObject.CarStatus += ", no household";
                throw new FlaException("no household found");
            }

            var house = _dbDto.Houses.FirstOrDefault(x => x.HouseGuid == cde.HouseGuid);

            if (house == null)
            {
                throw new FlaException("no house found");
            }

            HouseholdData householdData = null;

            if (_housesToBeCreated.Count > 0)
            {
                var houseJob = _housesToBeCreated.FirstOrDefault(x => x.House.HouseGuid == ha.HouseGuid);
                if (houseJob == null)
                {
                    parameters.HouseComponentResultObject.CarStatus += ", no house";

                    throw new FlaException("no house found");
                }

                householdData = houseJob.House.Households.FirstOrDefault(x => x.HouseholdGuid == flaHousehold.HouseholdKey);
                if (householdData == null)
                {
                    parameters.HouseComponentResultObject.CarStatus += ", no household guid";
                    throw new FlaException("no household for household guid " + cde.HouseholdGuid);
                }


                //set lpg parameters
                householdData.UseElectricCar = ElectricCarUse.UseElectricCar;

                //find number of cars
                var householdCars = _dbDto.Cars.Where(x => x.HouseholdGuid == cde.HouseholdGuid).ToList();
                switch (householdCars.Count)
                {
                case 1:
                    //use lpg profile for a single car
                    householdData.TransportationDeviceSet = TransportationDevicesTwoCar;
                    break;

                case 2:
                    //use lpg profile for a single car
                    householdData.TransportationDeviceSet = TransportationDevicesTwoCar;
                    break;

                case 3:
                    // use lpg profile for a single car
                    // todo: fix this and put in the right transportation device set
                    householdData.TransportationDeviceSet = TransportationDevicesTwoCar;
                    break;

                case 4:
                    //use lpg profile for a single car
                    //todo: fix this and put in the right transportation device set
                    householdData.TransportationDeviceSet = TransportationDevicesTwoCar;
                    break;

                default: throw new FlaException("Household with " + householdCars.Count + " cars is missing");
                }

                householdData.TravelRouteSet     = TravelRouteSet;
                householdData.ChargingStationSet = ChargingStationSet;
                if (householdData.TransportationDistanceModifiers == null)
                {
                    householdData.TransportationDistanceModifiers = new List <TransportationDistanceModifier>();
                }

                householdData.TransportationDistanceModifiers.Add(new TransportationDistanceModifier("Work", "Car", cde.CommutingDistance * 1000));
                householdData.TransportationDistanceModifiers.Add(new TransportationDistanceModifier("Entertainment",
                                                                                                     "Car",
                                                                                                     cde.FreizeitDistance * 1000));
                parameters.HouseComponentResultObject.CarStatus += ", asking for repare with distances commuting: " + cde.CommutingDistance +
                                                                   ", free time: " + cde.FreizeitDistance + " km";
            }

            if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithFullLpgLoad)
            {
                Profile lpgProfile = null;
                try {
                    lpgProfile = _lpgProfileLoader.LoadLPGProfile(parameters,
                                                                  ha.Trafokreis,
                                                                  LoadtypetoSearchFor,
                                                                  _saveableEntry,
                                                                  flaHousehold.HouseholdKey,
                                                                  out _,
                                                                  house.ComplexName,
                                                                  Services.RunningConfig);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                    Error("trying to load lpg profile: " + ex.Message);
                    parameters.HouseComponentResultObject.LPGErrors = "trying to load lpg profile failed: " + ex.Message;
                }

                // ReSharper disable PossibleNullReferenceException
                if (_housesToBeCreated.Count > 0)
                {
                    if (lpgProfile != null)
                    {
                        householdData.IsCarProfileCalculated = true;
                        parameters.HouseComponentResultObject.ProcessingStatus = "Found LPG profile on full check";
                    }
                    else
                    {
                        householdData.IsCarProfileCalculated = false;
                        parameters.HouseComponentResultObject.ProcessingStatus = "Missing LPG profile on full check";
                    }
                }
                // ReSharper restore PossibleNullReferenceException
            }
            else if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithOnlyNamecheck)
            {
                // ReSharper disable PossibleNullReferenceException
                if (_housesToBeCreated.Count > 0)
                {
                    if (_saveableEntry.CheckForName(flaHousehold.HouseholdKey, Services.Logger))
                    {
                        householdData.IsHouseholdProfileCalculated             = true;
                        parameters.HouseComponentResultObject.ProcessingStatus = "Found LPG profile on name check";
                    }
                    else
                    {
                        householdData.IsHouseholdProfileCalculated             = false;
                        parameters.HouseComponentResultObject.ProcessingStatus = "Miissing LPG profile on name check";
                    }
                }
                // ReSharper restore PossibleNullReferenceException
            }
            else
            {
                throw new FlaException("Unknown lpg prepare mode");
            }

            return(true);
        }
        public bool PrepareLoadProfileIfNeeded([NotNull] ProviderParameterDto parameters)
        {
            if (parameters.HouseComponent.HouseComponentType != HouseComponentType.Household)
            {
                throw new FlaException("Was not household: " + parameters.HouseComponent.HouseComponentType);
            }

            Household     hh       = (Household)parameters.HouseComponent;
            Hausanschluss ha       = _dbDto.Hausanschlusse.Single(x => x.Guid == hh.HausAnschlussGuid);
            var           houseJob = _housesToBeCreated.FirstOrDefault(x => x.House.HouseGuid == hh.HouseGuid);

            if (houseJob == null)
            {
                var flahouse = _dbDto.Houses.Single(x => x.Guid == hh.HouseGuid);
                houseJob       = new HouseCreationAndCalculationJob(Slice.DstScenario.ToString(), Slice.DstYear.ToString(), ha.Trafokreis);
                houseJob.House = new HouseData(hh.HouseGuid, "HT01", 0, 0, flahouse.ComplexName);
                _housesToBeCreated.Add(houseJob);
            }

            HouseholdData hd = new HouseholdData(hh.HouseholdKey,
                                                 hh.EffectiveEnergyDemand,
                                                 ElectricCarUse.NoElectricCar,
                                                 hh.Name,
                                                 null,
                                                 null,
                                                 null,
                                                 null,
                                                 HouseholdDataSpecifictionType.ByPersons);

            hd.IsCarProfileCalculated           = true;
            hd.HouseholdDataPersonSpecification = new HouseholdDataPersonSpecification(new List <PersonData>());
            if (hh.Occupants.Count == 0)
            {
                throw new FlaException("No occupants in the household " + hh.Name);
            }

            foreach (var occupant in hh.Occupants)
            {
                hd.HouseholdDataPersonSpecification.Persons.Add(new PersonData(occupant.Age, (Gender)occupant.Gender));
            }

            House house = _dbDto.Houses.First(x => x.Guid == hh.HouseGuid);

            if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithFullLpgLoad)
            {
                Profile lpgProfile = null;
                try {
                    lpgProfile = _lpgloader.LoadLPGProfile(parameters,
                                                           ha.Trafokreis,
                                                           LoadtypetoSearchFor,
                                                           _saveableEntry,
                                                           hh.HouseholdKey,
                                                           out _,
                                                           house.ComplexName,
                                                           Services.RunningConfig);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex) {
#pragma warning restore CA1031 // Do not catch general exception types
                    parameters.HouseComponentResultObject.LPGErrors = "trying to load lpg profile failed: " + ex.Message;
                    Error("trying to load lpg profile: " + ex.Message);
                }

                if (lpgProfile != null)
                {
                    hd.IsHouseholdProfileCalculated = true;
                }
                else
                {
                    hd.IsHouseholdProfileCalculated = false;
                }
            }
            else if (Services.RunningConfig.LpgPrepareMode == LpgPrepareMode.PrepareWithOnlyNamecheck)
            {
                if (_saveableEntry.CheckForName(hh.HouseholdKey, Services.Logger))
                {
                    hd.IsHouseholdProfileCalculated = true;
                }
                else
                {
                    hd.IsHouseholdProfileCalculated = false;
                }
            }
            else
            {
                throw new FlaException("Unknown lpg prepare mode");
            }

            houseJob.House.Households.Add(hd);
            return(true);
        }