public CalcDeviceTaggingSets GetDeviceTaggingSets([NotNull] Simulator sim, int personCount)
        {
            CalcDeviceTaggingSets cs = new CalcDeviceTaggingSets
            {
                AllCalcDeviceTaggingSets = new List <DeviceTaggingSetInformation>()
            };

            foreach (var deviceTaggingSet in sim.DeviceTaggingSets.MyItems)
            {
                var calcset = new DeviceTaggingSetInformation(deviceTaggingSet.Name);
                foreach (var entry in deviceTaggingSet.Entries)
                {
                    if (entry.Device == null)
                    {
                        throw new LPGException("Device was null");
                    }

                    if (entry.Tag == null)
                    {
                        throw new LPGException("Tag was null");
                    }

                    var devname = entry.Device.Name;
                    //sim.MyGeneralConfig.CSVCharacter);
                    var tagname = CalcAffordanceFactory.FixAffordanceName(entry.Tag.Name,
                                                                          _calcParameters.CSVCharacter);
                    calcset.AddTag(devname, tagname);
                }

                foreach (var reference in
                         deviceTaggingSet.References.Where(x => x.PersonCount == personCount))
                {
                    if (reference.Tag == null)
                    {
                        throw new LPGException("Tag was null");
                    }

                    calcset.AddRefValue(reference.Tag.Name, reference.ReferenceValue, reference.LoadType.Name);
                }

                foreach (var loadType in deviceTaggingSet.LoadTypes)
                {
                    if (loadType.LoadType == null)
                    {
                        throw new LPGException("Loadtype was null");
                    }

                    if (_ltDict.SimulateLoadtype(loadType.LoadType))
                    {
                        var clt = _ltDict.GetLoadtypeDtoByLoadType(loadType.LoadType);
                        calcset.AddLoadType(clt.ConvertToLoadTypeInformation());
                    }
                }

                cs.AllCalcDeviceTaggingSets.Add(calcset);
            }

            return(cs);
        }
Beispiel #2
0
        public void PostProcessingTestSingleActivation()
        {
            using (var wd = new WorkingDir(Utili.GetCurrentMethodAndClass())) {
                wd.InputDataLogger.AddSaver(new CalcParameterLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ColumnEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new ResultFileEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new DeviceActivationEntryLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new HouseholdKeyLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcLoadTypeDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new CalcPersonDtoLogger(wd.SqlResultLoggingService));
                wd.InputDataLogger.AddSaver(new DeviceTaggingSetLogger(wd.SqlResultLoggingService));
                var calculationProfiler = new CalculationProfiler();

                var startdate      = new DateTime(2018, 1, 1);
                var enddate        = startdate.AddMinutes(1000);
                var calcParameters = CalcParametersFactory.MakeGoodDefaults().SetStartDate(startdate).SetEndDate(enddate).EnableShowSettlingPeriod();

                var rnd = new Random(1);
                var nr  = new NormalRandom(0, 1, rnd);

                calcParameters.Enable(CalcOption.HouseSumProfilesFromDetailedDats);
                calcParameters.Enable(CalcOption.DeviceProfilesIndividualHouseholds);
                calcParameters.Enable(CalcOption.TotalsPerDevice);
                calcParameters.Enable(CalcOption.TotalsPerLoadtype);
                calcParameters.Enable(CalcOption.DetailedDatFiles);
                calcParameters.Enable(CalcOption.DeviceActivations);
                var key = new HouseholdKey("hh1");

                wd.InputDataLogger.Save(calcParameters);

                using (var fft = new FileFactoryAndTracker(wd.WorkingDirectory, "hhname", wd.InputDataLogger)) {
                    //wd.InputDataLogger.AddSaver(new CalcDeviceDtoLogger(wd.SqlResultLoggingService));
                    var dsc = new DateStampCreator(calcParameters);
                    using (IOnlineLoggingData old = new OnlineLoggingData(dsc, wd.InputDataLogger, calcParameters)) {
                        var calcPersonDto = new CalcPersonDto("blub", "personguid".ToStrGuid(), 1, PermittedGender.Male, key, new List <DateSpan>(),
                                                              new List <DateSpan>(), 1, "traittag", "householdname");
                        var persons = new List <CalcPersonDto> {
                            calcPersonDto
                        };
                        wd.InputDataLogger.SaveList(persons.ConvertAll(x => (IHouseholdKey)x));

                        using (var lf = new LogFile(calcParameters, fft)) {
                            fft.RegisterHousehold(key, "test hh", HouseholdKeyType.Household, "Description", null, null);
                            fft.RegisterHousehold(Constants.GeneralHouseholdKey, "General", HouseholdKeyType.General, "Description", null, null);
                            var odap         = new OnlineDeviceActivationProcessor(old, calcParameters, fft);
                            var clt          = new CalcLoadType("lt1", "W", "kWh", 3, true, Guid.NewGuid().ToStrGuid());
                            var loadTypeDtos = new List <CalcLoadTypeDto> {
                                clt.ConvertToDto()
                            };
                            wd.InputDataLogger.Save(loadTypeDtos);
                            //var loadTypes = new List<CalcLoadType> {clt};
                            var cdl         = new CalcDeviceLoad("devload1", 10, clt, 666, 0);
                            var deviceLoads = new List <CalcDeviceLoad> {
                                cdl
                            };
                            var cloc = new CalcLocation("locname", Guid.NewGuid().ToStrGuid());
                            var deviceCategoryGuid = Guid.NewGuid().ToStrGuid();
                            var calcDeviceDto      = new CalcDeviceDto("devicename", deviceCategoryGuid, key, OefcDeviceType.Device, "category",
                                                                       string.Empty, Guid.NewGuid().ToStrGuid(), cloc.Guid, cloc.Name);
                            var calcDeviceDtos = new List <CalcDeviceDto> {
                                calcDeviceDto
                            };
                            wd.InputDataLogger.SaveList(calcDeviceDtos.ConvertAll(x => (IHouseholdKey)x));
                            //device tagging set for the post processing
                            var cdts = new List <DeviceTaggingSetInformation>();
                            var dtsi = new DeviceTaggingSetInformation("myset");
                            dtsi.AddTag(calcDeviceDto.Name, "testTag ");
                            cdts.Add(dtsi);
                            wd.InputDataLogger.Save(cdts);
                            //device
                            using (var calcRepo = new CalcRepo(calcParameters: calcParameters, odap: odap, rnd: rnd, normalRandom: nr)) {
                                var device = new CalcDevice(deviceLoads, cloc, calcDeviceDto, calcRepo);
                                //var devices = new List<CalcDevice> {device};
                                double[] resultValues = { 0, 100.0, 0, 0, 0, 0, 0, 0, 0, 0 };
                                var      cp           = new CalcProfile("profile1", Guid.NewGuid().ToStrGuid(), new TimeSpan(0, 1, 0), ProfileType.Absolute,
                                                                        "custom");
                                cp.AddNewTimepoint(new TimeSpan(0), 0);
                                cp.AddNewTimepoint(new TimeSpan(0, 1, 0), 10);
                                cp.AddNewTimepoint(new TimeSpan(0, 2, 0), 0);
                                cp.ConvertToTimesteps();
                                //var locations = new List<CalcLocation> {cloc};
                                var ts1 = new TimeStep(0, calcParameters);
                                device.SetTimeprofile(cp, ts1, clt, "affordanceName", "activatorName", 10, false);
                                for (var i = 0; i < 10; i++)
                                {
                                    var ts       = new TimeStep(i, calcParameters);
                                    var filerows = odap.ProcessOneTimestep(ts);
                                    filerows.Count.Should().Be(1);
                                    filerows[0].EnergyEntries.Count.Should().Be(1);
                                    Logger.Info(filerows[0].EnergyEntries[0].ToString(CultureInfo.CurrentCulture));
                                    filerows[0].EnergyEntries[0].Should().Be(resultValues[i]);
                                    foreach (var fileRow in filerows)
                                    {
                                        fileRow.Save(odap.BinaryOutStreams[fileRow.LoadType]);
                                    }
                                }
                            }
                            old.FinalSaveToDatabase();
                            fft.Dispose();
                            lf.Dispose(); // needed to free the file access
                            //var autoDevs = new List<CalcAutoDev>();
                            //var ps = new Postprocessor(lf.FileFactoryAndTracker, calculationProfiler,calcParameters );
                            //var householdKeys = new HashSet<string> {"1"};
                            //var calcAffordanceTaggingSets = new List<CalcAffordanceTaggingSet>();
                            //var calcDeviceTaggingSets = new List<CalcDeviceTaggingSet>();
                            //var calcDeviceTaggingSets = new List<DeviceTaggingSetInformation>();
                            //var householdPlans = new List<CalcHouseholdPlan>();
                            //var householdNamesByNumber = new Dictionary<string, string> {["HH1"] = "household"};
                            //var affordanceEnergyUseFile = new AffordanceEnergyUseFile(lf.FileFactoryAndTracker,calcParameters);

                            //var results = new Dictionary<string, double>();
                            //BitArray isSick = new BitArray(calcParameters.InternalTimesteps);
                            //BitArray isOnVacation = new BitArray(calcParameters.InternalTimesteps);
                            //CalcPersonDto cpd = new CalcPersonDto("personname", Guid.NewGuid().ToStrGuid(),18,PermittedGender.Female,key,new List<DateSpan>(),new List<DateSpan>(),1,"traittag","hhname"  );
                            //var persons = new List<CalcPerson> {new CalcPerson(cpd, new Random(), lf,cloc,calcParameters,isSick,isOnVacation)};
                            //var deviceNamesToCategory = new Dictionary<string, string>();
                            //CalcDeviceTaggingSets calcDeviceTaggingSets = new CalcDeviceTaggingSets();
                            var cpp = new PostProcessingManager(calculationProfiler, fft);
                            cpp.Run(wd.WorkingDirectory);

                            /*ps.EndOfSimulationProcessing(devices, locations, autoDevs, loadTypes, odap.Oefc, householdKeys
                             *  ,calcAffordanceTaggingSets, calcDeviceTaggingSets, householdPlans, householdNamesByNumber
                             *  ,affordanceEnergyUseFile, results, CalcObjectType.ModularHousehold, persons, deviceNamesToCategory,10);*/
                            //var dstpath = Path.Combine(wd.WorkingDirectory,DirectoryNames.CalculateTargetdirectory(TargetDirectory.Reports),"DeviceSums." + clt.Name + ".csv");
                            lf.Dispose(); // needed to free the file access
                            var di  = new DirectoryInfo(wd.WorkingDirectory);
                            var fis = di.GetFiles("DeviceSums.*", SearchOption.AllDirectories);

                            if (fis.Length == 0)
                            {
                                throw new LPGException("No Sum File was generated");
                            }
                            fft.Dispose();
                            using (var sr = new StreamReader(fis[0].FullName)) {
                                sr.ReadLine();              //header
                                var result = sr.ReadLine(); //0
                                if (result == null)
                                {
                                    throw new LPGException("Result was null");
                                }

                                var arr = result.Split(';');
                                Assert.Equal("300", arr[1]);
                            }
                        }
                    }
                }

                Logger.Info(wd.WorkingDirectory);
                wd.CleanUp();
            }
        }