Ejemplo n.º 1
0
        private void filterAndSave(string line)
        {
            //split timestamp and value
            string[] substrings = line.Split(' ');
            if (substrings.Length > 1)
            {
                if (this.starttime != new DateTime(2000, 1, 1) && this.endtime != new DateTime(2000, 1, 1)) //check if time filter is set
                {
                    filterByTime(this.starttime, this.endtime);                                             //check if timestamp fits between start and endtime
                }
                else
                {
                    fitsTimeFilter = true;
                }

                if (this.types != null && fitsTimeFilter) //check if any type filters are set and if time is right
                {
                    filterByType();
                }
                else
                {
                    fitsTypeFilter = true;
                }

                if (this.application != null && fitsTimeFilter && fitsTypeFilter) //check if any application filters are set and if time and types are right
                {
                    filterByApplication();
                }
                else
                {
                    fitsApplicationFilter = true;
                }

                if (fitsTimeFilter && fitsTypeFilter && fitsApplicationFilter) //check if data fits all filters
                {
                    temp = new MeterData(REDDMeter.GetMeterId());
                    temp.SetTimestamp(substrings[0]);
                    temp.SetPowerP1(Convert.ToDouble(substrings[1], CultureInfo.InvariantCulture));
                    myDatabase.InsertMeterDataIntoDb(temp);
                }
                fitsTimeFilter        = false;
                fitsTypeFilter        = false;
                fitsApplicationFilter = false;
            }
        }
Ejemplo n.º 2
0
        protected override void processFile(String path)
        {
            //open file
            //get datastream
            //save data --> not implemented yet
            //close file
            StreamReader reader = new StreamReader(File.OpenRead(path));

            if (reader != null)
            {
                String line;
                while ((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                    Console.WriteLine();

                    String[] values = line.Split(',');

                    if (sort == false)
                    {
                        if (isU)
                        {
                            String[] voltage = new String[values.Count()];

                            for (int i = 0; i < values.Count(); i++)
                            {
                                voltage[i] = values[i];
                            }

                            int countFrequency = 0;
                            foreach (String value in voltage)
                            {
                                if ((countFrequency % frequenzy) == 0)
                                {
                                    MeterData temp = new MeterData(ADRESMeterId);
                                    temp.SetVoltage(Convert.ToDouble(value, CultureInfo.InvariantCulture));

                                    myDatabase.InsertMeterDataIntoDb(temp);
                                }
                                countFrequency++;
                            }
                        }
                        else
                        {
                            String[] power = new String[values.Count()];

                            for (int i = 0; i < values.Count(); i++)
                            {
                                power[i] = values[i];
                            }

                            int counter = 0;
                            foreach (String value in power)
                            {
                                if (this.frequenzy == 0)
                                {
                                    this.frequenzy = 1;
                                }

                                if ((counter % (2 * frequenzy) == 0))
                                {
                                    MeterData temp = new MeterData(ADRESMeterId);
                                    temp.SetPowerP1(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                                    myDatabase.InsertMeterDataIntoDb(temp);
                                }

                                counter++;
                            }
                        }
                    }
                    else
                    {
                        if (sortU)
                        {
                            if (isU)
                            {
                                String[] voltage = new String[values.Count()];

                                for (int i = 0; i < values.Count(); i++)
                                {
                                    voltage[i] = values[i];
                                }

                                int countFrequency = 0;
                                foreach (String value in voltage)
                                {
                                    if (this.frequenzy == 0)
                                    {
                                        this.frequenzy = 1;
                                    }

                                    if ((countFrequency % frequenzy) == 0)
                                    {
                                        MeterData temp = new MeterData(ADRESMeterId);
                                        temp.SetVoltage(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                                        myDatabase.InsertMeterDataIntoDb(temp);
                                    }
                                    countFrequency++;
                                }
                            }
                        }
                        if (sortP)
                        {
                            String[] power = new String[values.Count()];

                            for (int i = 0; i < values.Count(); i++)
                            {
                                power[i] = values[i];
                            }

                            int counter = 0;
                            foreach (String value in power)
                            {
                                if (this.frequenzy == 0)
                                {
                                    this.frequenzy = 1;
                                }

                                if ((counter % (2 * frequenzy)) == 0)
                                {
                                    MeterData temp = new MeterData(ADRESMeterId);
                                    temp.SetPowerP1(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                                    myDatabase.InsertMeterDataIntoDb(temp);
                                }

                                counter++;
                            }
                        }
                    }
                }
                reader.Close();
            }
        }
Ejemplo n.º 3
0
        private void workDisagregationFile(String path)
        {
            String[] paths = path.Split('\\');

            ///For later usage to find the household
            String household       = paths[paths.Count() - 2];
            int    householdNumber = (int)Char.GetNumericValue(household[household.Count() - 1]);

            ///Device identifier
            String device = paths[paths.Count() - 1];

            String[] deviceParts        = device.Split('.');
            String[] furtherDeviceParts = deviceParts[0].Split('_');
            int      deviceNumber       = Int32.Parse(furtherDeviceParts[1]);

            bool filterOk = false;

            ///Filter for appliance
            if (this.application != null)
            {
                foreach (ApplicationEnum currentApp in this.application)
                {
                    filterOk |= this.compareApplicationEnumWithCurrentHouseholdApp(currentApp, householdNumber, deviceNumber);
                }
            }
            else
            {
                filterOk = true;
            }

            if (filterOk == true)
            {
                using (StreamReader file = new StreamReader(path))
                {
                    if (file != null)
                    {
                        String line = "";
                        while ((line = file.ReadLine()) != null)
                        {
                            if (line != null)
                            {
                                String[] values = line.Split(' ');

                                String[] PowerValues = new String[(values.Count())];


                                if (!swapTimestampifSecondsPassed(values[0]))
                                {
                                    continue;
                                }

                                ///Only store data to database if the current timestamp is in the given boundries
                                if (Int32.Parse(values[0]) < this.getUnixTimestampFromDate(this.starttime) || Int32.Parse(values[0]) > this.getUnixTimestampFromDate(this.endtime) && this.filterByTimeEnabled)
                                {
                                }
                                else
                                {
                                    for (int i = 0; i < values.Count(); i++)
                                    {
                                        PowerValues[i] = values[i];
                                    }

                                    ///CHANGE TO REAL METERS
                                    MeterData temp = new MeterData(this.housesList[0].Item1);
                                    temp.SetTimestamp(values[0]);
                                    temp.SetPowerP1(Convert.ToDouble(values[1]));

                                    myDatabase.InsertMeterDataIntoDb(temp);
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("Loaded " + household + " " + device + " sucessfully!");
            }
        }
Ejemplo n.º 4
0
        private void workFile(String path)
        {
            String pathToVoltageFile = path;
            String pathToCurrentFile = "";

            String[] paths = pathToVoltageFile.Split('\\');

            //Gets the household number
            String household       = paths[paths.Count() - 2];
            int    householdNumber = (int)Char.GetNumericValue(household[household.Count() - 1]);

            for (int i = 0; i < (paths.Count() - 1); i++)
            {
                pathToCurrentFile += paths[i] + "\\";
            }
            String[] timestamp = paths[paths.Count() - 1].Split('-');

            String curentTimestamp = this.removeMilisFromTimestamp(timestamp[0]);

            pathToCurrentFile += timestamp[0] + "-current";

            int chunkSize = 1024;

            using (FileStream fsVoltage = new FileStream(pathToVoltageFile, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader binReaderVoltage = new BinaryReader(fsVoltage, new ASCIIEncoding()))
                {
                    using (FileStream fsCurrent = new FileStream(pathToCurrentFile, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader binReaderCurrent = new BinaryReader(fsCurrent, new ASCIIEncoding()))
                        {
                            byte[] chunkVolts;
                            byte[] chunkAmps;

                            int k = 0;
                            int l = 0;

                            while (binReaderVoltage.BaseStream.Position != binReaderVoltage.BaseStream.Length)
                            {
                                int realTimestamp = Int32.Parse(curentTimestamp) + l;

                                if (!swapTimestampifSecondsPassed(realTimestamp.ToString()))
                                {
                                    if (k == (16000 / chunkSize))
                                    {
                                        k = 0;
                                        l++;
                                    }
                                    k++;

                                    continue;
                                }

                                ///If value timestamp is out of timestampvalue / Filter by time
                                if (realTimestamp < this.getUnixTimestampFromDate(this.starttime) || realTimestamp > this.getUnixTimestampFromDate(this.endtime) && this.filterByTimeEnabled)
                                {
                                }
                                else
                                {
                                    if (this.frequenzy == 0)
                                    {
                                        chunkVolts = binReaderVoltage.ReadBytes(chunkSize);
                                        chunkAmps  = binReaderCurrent.ReadBytes(chunkSize);

                                        double[] voltValues = new double[chunkVolts.Length / 8];
                                        double[] ampValues  = new double[chunkAmps.Length / 8];
                                        for (int j = 0; j < voltValues.Length; j++)
                                        {
                                            voltValues[j]  = BitConverter.ToDouble(chunkVolts, j * 8);
                                            voltValues[j] *= Math.Pow(2, 31); //conversion factor
                                            voltValues[j] *= (1.90101491444 * Math.Pow(10, -7));
                                        }

                                        for (int j = 0; j < ampValues.Length; j++)
                                        {
                                            ampValues[j]  = BitConverter.ToDouble(chunkAmps, j * 8);
                                            ampValues[j] *= Math.Pow(2, 31); //conversion factor
                                            ampValues[j] *= (4.9224284384 * Math.Pow(10, -8));
                                        }

                                        double[] powerValue = new double[ampValues.Length];
                                        for (int i = 0; i < powerValue.Count(); i++)
                                        {
                                            powerValue[i] = voltValues[i] * ampValues[i];

                                            ///Filtering whole houses does not work yet
                                            MeterData temp = new MeterData(this.housesList[householdNumber - 1].Item1);

                                            ///Frequenzy = 16kHz
                                            ///all 16 tousand times, one second passes
                                            temp.SetTimestamp(realTimestamp.ToString());
                                            temp.SetPowerP1(powerValue[i]);
                                            temp.SetVoltage(voltValues[i]);

                                            myDatabase.InsertMeterDataIntoDb(temp);
                                        }
                                    }
                                    else
                                    {
                                        chunkVolts = binReaderVoltage.ReadBytes(chunkSize);
                                        chunkAmps  = binReaderCurrent.ReadBytes(chunkSize);

                                        double[] voltValues = new double[chunkVolts.Length / 8];
                                        double[] ampValues  = new double[chunkAmps.Length / 8];
                                        for (int j = 0; j < voltValues.Length; j++)
                                        {
                                            voltValues[j]  = BitConverter.ToDouble(chunkVolts, j * 8);
                                            voltValues[j] *= Math.Pow(2, 31); //conversion factor
                                            voltValues[j] *= (1.90101491444 * Math.Pow(10, -7));
                                        }

                                        for (int j = 0; j < ampValues.Length; j++)
                                        {
                                            ampValues[j]  = BitConverter.ToDouble(chunkAmps, j * 8);
                                            ampValues[j] *= Math.Pow(2, 31); //conversion factor
                                            ampValues[j] *= (4.9224284384 * Math.Pow(10, -8));
                                        }

                                        double[] powerValue = new double[ampValues.Length];
                                        for (int i = 0; i < 1; i++)
                                        {
                                            powerValue[i] = voltValues[i] * ampValues[i];

                                            MeterData temp = new MeterData(this.housesList[householdNumber - 1].Item1);

                                            ///Frequenzy = 16kHz
                                            ///all 16 tousand times, one second passes
                                            temp.SetTimestamp(realTimestamp.ToString());
                                            temp.SetPowerP1(powerValue[i]);
                                            temp.SetVoltage(voltValues[i]);

                                            myDatabase.InsertMeterDataIntoDb(temp);
                                        }
                                    }
                                }

                                if (k == (16000 / chunkSize))
                                {
                                    k = 0;
                                    l++;
                                }
                                k++;
                            }
                        }
                    }
                }
            }
        }
        private void workFile(string path)
        {
            String[] pathParts = path.Split('\\');
            String   household = pathParts[pathParts.Count() - 2];

            StreamReader file = new StreamReader(path);

            if (file != null)
            {
                //First line gets lost here on purpose
                String line = file.ReadLine();

                while ((line = file.ReadLine()) != null)
                {
                    if (line != null)
                    {
                        String[] values = line.Split(',');

                        ///The file reader can overflow. This protects that case
                        if (values[0] == "timestamp")
                        {
                            break;
                        }

                        String[] PowerValues = new String[(values.Count() - 1)];

                        for (int i = 1; i < values.Count(); i++)
                        {
                            PowerValues[i - 1] = values[i];
                        }

                        ///Aplication filter
                        List <int> foundIndicesWithDuplicates    = new List <int>();
                        List <int> foundIndicesWithoutDuplicates = new List <int>();
                        if (this.application != null)
                        {
                            foreach (ApplicationEnum currentCheckApplication in this.application)
                            {
                                int startAplicationList = this.currentHouseholdNumber * 9;
                                for (int i = startAplicationList; i < startAplicationList + 9; i++)
                                {
                                    if (currentCheckApplication == this.meterList[i].Item2)
                                    {
                                        foundIndicesWithDuplicates.Add(i - (9 * startAplicationList));
                                    }
                                }
                            }
                            foundIndicesWithoutDuplicates = foundIndicesWithDuplicates.Distinct().ToList();
                        }
                        int j = 0;
                        foreach (String value in PowerValues)
                        {
                            if (j == 0)
                            {
                                ///If value timestamp is out of timestampvalue / Filter by time
                                if ((Int32.Parse(removeMilisFromTimestamp(values[0])) < this.getUnixTimestampFromDate(this.starttime) || Int32.Parse(removeMilisFromTimestamp(values[0])) > this.getUnixTimestampFromDate(this.endtime)) && this.filterByTimeEnabled)
                                {
                                    break;
                                }

                                ///Checks if the given accuracy time has already passed. if not, break out.
                                if (!swapTimestampifSecondsPassed(values[0]))
                                {
                                    break;
                                }
                            }

                            if (foundIndicesWithoutDuplicates.Count != 0)
                            {
                                foreach (int searchedIndex in foundIndicesWithoutDuplicates)
                                {
                                    if (j == searchedIndex)
                                    {
                                        MeterData temp = new MeterData(getMeterId(j));
                                        temp.SetTimestamp(removeMilisFromTimestamp(values[0]));

                                        if (value == "NULL")
                                        {
                                            temp.SetPowerP1(Convert.ToDouble(0.0, CultureInfo.InvariantCulture));
                                        }
                                        else
                                        {
                                            temp.SetPowerP1(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                                        }

                                        myDatabase.InsertMeterDataIntoDb(temp);
                                    }
                                }
                            }
                            else
                            {
                                MeterData temp = new MeterData(getMeterId(j));
                                temp.SetTimestamp(removeMilisFromTimestamp(values[0]));

                                if (value == "NULL")
                                {
                                    temp.SetPowerP1(Convert.ToDouble(0.0, CultureInfo.InvariantCulture));
                                }
                                else
                                {
                                    temp.SetPowerP1(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                                }

                                myDatabase.InsertMeterDataIntoDb(temp);
                            }
                            j++;
                        }
                    }
                }
            }
        }