Ejemplo n.º 1
0
        private async void DatasetImportDialog_ImportButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            string[] colorList =
            {
                Colors.Red.ToString(),
                Colors.Blue.ToString(),
                Colors.Green.ToString(),
                Colors.BlueViolet.ToString(),
                Colors.Aqua.ToString(),
                Colors.Fuchsia.ToString(),
                Colors.Orange.ToString(),
                Colors.DeepSkyBlue.ToString(),
                Colors.Olive.ToString(),
                Colors.MediumVioletRed.ToString(),
                Colors.Pink.ToString(),
                Colors.SlateGray.ToString(),
                Colors.PaleGreen.ToString(),
                Colors.RosyBrown.ToString(),
                Colors.Sienna.ToString(),
                Colors.YellowGreen.ToString(),
                Colors.Bisque.ToString(),
                Colors.SpringGreen.ToString(),
                Colors.LightSteelBlue.ToString()
            };
            // Disable all controls
            DatasetImportDialog_DisableControls();
            progressBarImport.Visibility       = Visibility.Visible;
            txtProgressImportStatus.Visibility = Visibility.Visible;
            // Show Status Bar
            txtProgressImportStatus.Text = "Loading File: " + txtDatasetFilePath.Text;
            // Populate All Sensors and Activities in the dataset file
            HashSet <string> SensorDictionary   = new HashSet <string>();
            HashSet <string> ActivityDictionary = new HashSet <string>();
            // Get Streams to Store Reorganized events ready
            StorageFile eventFile = await this._targetFolder.CreateFileAsync("events.csv", CreationCollisionOption.ReplaceExisting);

            IRandomAccessStream eventStream = await eventFile.OpenAsync(FileAccessMode.ReadWrite);

            StreamWriter eventStreamWriter = new StreamWriter(eventStream.AsStreamForWrite());
            StorageFile  temperatureFile   = await this._targetFolder.CreateFileAsync("temperature.csv", CreationCollisionOption.ReplaceExisting);

            IRandomAccessStream temperatureStream = await temperatureFile.OpenAsync(FileAccessMode.ReadWrite);

            StreamWriter temperatureStreamWriter = new StreamWriter(temperatureStream.AsStreamForWrite());
            StorageFile  powerFile = await this._targetFolder.CreateFileAsync("power_useage.csv", CreationCollisionOption.ReplaceExisting);

            IRandomAccessStream powerStream = await powerFile.OpenAsync(FileAccessMode.ReadWrite);

            StreamWriter powerStreamWriter = new StreamWriter(powerStream.AsStreamForWrite());

            // Get Storage File and a Read Stream
            using (var inputStream = await this._selectedFile.OpenReadAsync())
                using (var classicStream = inputStream.AsStreamForRead())
                    using (var streamReader = new StreamReader(classicStream))
                    {
                        int lineNum = 0;
                        // Read Line by Line
                        while (streamReader.Peek() >= 0)
                        {
                            lineNum++;
                            string   curEventString = streamReader.ReadLine();
                            string[] tokens         = curEventString.Split(new char[] { ' ' });
                            string   eventString    = tokens[0] + "," + tokens[1].Split(new char[] { '.' })[0];
                            // Sensor IDs
                            if (tokens.Length > 2)
                            {
                                // Add Sensor ID to Sensor Dictionary
                                if (!SensorDictionary.Contains(tokens[2]))
                                {
                                    SensorDictionary.Add(tokens[2]);
                                }
                                eventString += "," + tokens[2];
                            }
                            eventString += "," + tokens[3];
                            // Activity List
                            if (tokens.Length > 4)
                            {
                                // Add Activity Label to Activity Dictionary
                                if (!ActivityDictionary.Contains(tokens[4]))
                                {
                                    ActivityDictionary.Add(tokens[4]);
                                }
                                eventString += ",," + tokens[4];
                            }
                            if (lineNum % 1000 == 0)
                            {
                                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Parsing " + lineNum.ToString() + " events"));
                            }
                            // Rewrite current event
                            switch (SensorType.GuessSensorTypeFromName(tokens[2]))
                            {
                            case "Motion":
                            case "Door":
                            case "Item":
                            case "Light":
                                eventStreamWriter.WriteLine(eventString);
                                break;

                            case "Temperature":
                                temperatureStreamWriter.WriteLine(eventString);
                                break;

                            case "Power":
                                powerStreamWriter.WriteLine(eventString);
                                break;

                            default:
                                break;
                            }
                        }
                    }
            // Release all Stream Resources
            eventStreamWriter.Dispose();
            eventStream.Dispose();
            temperatureStreamWriter.Dispose();
            temperatureStream.Dispose();
            powerStreamWriter.Dispose();
            powerStream.Dispose();
            // Populate Home Structure
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Update Dataset Structure..."));

            // Set Dataset Name
            _homeViewModel.DatasetName = txtDatasetName.Text;
            // Set Folder and Path
            _homeViewModel.dataset.Folder = this._targetFolder;
            _homeViewModel.dataset.Path   = this._targetFolder.Path;
            // Copy Floor Plan
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Copy Floor Plan..."));

            StorageFile newfloorPlanFile = await this._floorPlanFile.CopyAsync(this._targetFolder, this._floorPlanFile.Name, NameCollisionOption.ReplaceExisting);

            _homeViewModel.dataset.pathToFloorPlan = this._floorPlanFile.Name;
            // Configure Sensors
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Configure Sensors..."));

            foreach (string sensorName in SensorDictionary)
            {
                // Add Sensor To Dataset
                _homeViewModel.dataset.AddSensor(sensorName);
            }
            // Configure Activity Labels
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Configure Activities..."));

            int colorIndex = 0;

            foreach (string activityLabel in ActivityDictionary)
            {
                // Add Activity To Dataset
                _homeViewModel.AddActivity(new ActivityViewModel(new Activity(activityLabel, colorList[colorIndex], false, false)));
                colorIndex++;
            }
            // Save Dataset to File
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Save Dataset To File..."));

            await _homeViewModel.dataset.SaveToFolder();

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, (() => txtProgressImportStatus.Text = "Done..."));

            // Show Loading Status
            progressBarImport.Visibility       = Visibility.Collapsed;
            txtProgressImportStatus.Visibility = Visibility.Collapsed;
            DatasetImportDialog_EnableControls();
            finishImport = true;
            this.Hide();
        }