public TimeStep SetTimeprofile([NotNull] CalcProfile calcProfile, [NotNull] TimeStep startTimeStep, [NotNull] CalcLoadType loadType,
                                       [NotNull] string affordanceName, [NotNull] string activatingPersonName, double multiplier, bool activateDespiteBeingBusy)
        {
            if (calcProfile.StepValues.Count == 0)
            {
                throw new LPGException("Trying to set empty device profile. This is a bug. Please report.");
            }

            CalcDeviceLoad cdl = _deviceLoadsBy[loadType];

            if (cdl == null)
            {
                throw new LPGException("It was tried to activate the loadtype " + loadType.Name +
                                       " even though that one is not set for the device " + Name);
            }

            /*if (Math.Abs(cdl.Value) < 0.00000001 ) {
             *  throw new LPGException("Trying to calculate with a power consumption factor of 0. This is wrong.");
             * }*/
            if (CalcRepo.Odap == null && !Config.IsInUnitTesting)
            {
                throw new LPGException("ODAP was null. Please report");
            }
            var totalDuration = calcProfile.StepValues.Count; //.GetNewLengthAfterCompressExpand(timefactor);
            //calcProfile.CompressExpandDoubleArray(timefactor,allProfiles),
            var key = _keyByLoad[cdl.LoadType];

            RandomValueProfile rvp = GetRandomValueProfile(calcProfile.StepValues.Count, cdl, startTimeStep);
            StepValues         sv  = StepValues.MakeStepValues(calcProfile,
                                                               multiplier, rvp, cdl);

            CalcRepo.Odap.AddNewStateMachine(
                startTimeStep,
                cdl.LoadType.ConvertToDto(),
                affordanceName,
                activatingPersonName,
                key,
                _calcDeviceDto, sv);

            if (MatchingAutoDevs.Count > 0)
            {
                foreach (CalcAutoDev matchingAutoDev in MatchingAutoDevs)
                {
                    if (matchingAutoDev._keyByLoad.ContainsKey(cdl.LoadType))
                    {
                        var zerokey = matchingAutoDev._keyByLoad[cdl.LoadType];
                        CalcRepo.Odap.AddZeroEntryForAutoDev(zerokey,
                                                             startTimeStep,
                                                             totalDuration);
                    }
                }
            }
            SetBusy(startTimeStep, totalDuration, loadType, activateDespiteBeingBusy);
            return(startTimeStep.AddSteps(totalDuration));
        }
        private RandomValueProfile GetRandomValueProfile(int count, CalcDeviceLoad cdl, TimeStep timeStep)
        {
            var entry = _randomValues.FirstOrDefault(x =>
                                                     x.TimeStep == timeStep && Math.Abs(x.PowerStandardDeviation - cdl.PowerStandardDeviation) < 0.000000001 && x.RandomValueProfile.Values.Count == count);

            if (entry != null)
            {
                return(entry.RandomValueProfile);
            }
            var newrvp = RandomValueProfile.MakeStepValues(count, CalcRepo.NormalRandom,
                                                           cdl.PowerStandardDeviation);

            while (_randomValues.Count > 10)
            {
                _randomValues.RemoveAt(0);
            }
            var rvpe = new RandomValueProfileEntry(timeStep, cdl.PowerStandardDeviation, newrvp);

            _randomValues.Add(rvpe);
            return(newrvp);
        }