//##################################################################################################################################
 //################################################## Save Accelerometer data #######################################################
 //##################################################################################################################################
 
 /// <summary>
 /// Saves accelerometer tuples form the given accelerometer tuples list to the end of file.
 /// Importent: Await Task to be sure all accelerometer tuples has been saved.
 /// </summary>
 /// <param name="filename"></param>
 /// <param name="measurementData"></param>
 /// <param name="isActiveListChoosen"></param>
 /// <returns></returns>
 public static async Task AppendAccelerometerDataToFileAsync(String filename, MeasurementData measurementData, bool isActiveListChoosen)
 {
     if (filename != null && filename != String.Empty)
     {
         // convert data into byte array
         byte[] bytes = measurementData.ToAccelerometerBytes(isActiveListChoosen);
         if (bytes != null && bytes.Length > 0)
         {
             // find folder
             StorageFolder accelerometerFolder = await FindStorageFolder(_measurementAccelerometerPath);
             // save byte array
             await SaveBytesToEndOfFileAsync(bytes, accelerometerFolder, filename);
         }
     }
     return;
 }
        //###########################################################################
        //######################## Init MeasurementTask #############################
        //###########################################################################

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine(
                "####################################################\n" +
                "############### Run MeasurementTask ################\n" +
                "####################################################");

            _taskInstance = taskInstance;
            // GET TASKARGRUMENTS
            _taskArguments = GetTaskArgumentsFromTriggerDetails((DeviceUseDetails)_taskInstance.TriggerDetails);

            if (_taskArguments != null)
            {
                // init different sensors
                InitAccelerometer(_taskArguments);
                InitGyrometer(_taskArguments);
                InitOrientationSensor(_taskArguments);
                InitGeolocationSensor(_taskArguments);

                taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

                // create new measurement data model
                _measurementData = new MeasurementData(_taskArguments.Filename, _taskArguments.SampleBufferSize);
                _measurementData.ListsHasSwitched += MeasurementData_ReadingsListsHasSwitched;

                //set evaluation setting model
                _evaluationSettingModel = new EvaluationSettingModel(_taskArguments.SampleBufferSize, _taskArguments.AccelerometerThreshold, 
                    _taskArguments.GyrometerThreshold, _taskArguments.StepDistance, _taskArguments.PeakJoinDistance);

                _deferral = _taskInstance.GetDeferral();
                Debug.WriteLine(
                    "####################################################\n" +
                    "########## MeasurementTask initialisiert ###########\n" +
                    "####################################################");
            }
            else
            {
                Debug.WriteLine(
                    "####################################################\n" +
                    "############# MeasurementTask aborted ##############\n" +
                    "####################################################");
            }
        }
        public static async Task AppendMeasurementDataToFileAsync(TaskArguments taskArguments, MeasurementData measurementData, bool isActiveListChoosen)
        {
            Task accelerometerDataTask = null;
            Task gyrometerDataTask = null;
            Task quaterionDataTask = null;
            Task geolocationDataTask = null;

            if (taskArguments.IsUsedAccelerometer && taskArguments.IsRecordSamplesAccelerometer) 
            { 
                accelerometerDataTask = AppendAccelerometerDataToFileAsync(taskArguments.Filename, measurementData, isActiveListChoosen);
            }
            if (taskArguments.IsUsedGyrometer && taskArguments.IsRecordSamplesGyrometer)
            {
                gyrometerDataTask = AppendGyrometerDataToFileAsync(taskArguments.Filename, measurementData, isActiveListChoosen);
            }
            if (taskArguments.IsUsedQuaternion && taskArguments.IsRecordSamplesQuaternion)
            {
                quaterionDataTask = AppendQuaternionDataToFileAsync(taskArguments.Filename, measurementData, isActiveListChoosen);
            }
            if (taskArguments.IsUsedGeolocation && taskArguments.IsRecordSamplesGeolocation)
            {
                geolocationDataTask = AppendGeolocationDataToFileAsync(taskArguments.Filename, measurementData, isActiveListChoosen);
            }

            if (accelerometerDataTask != null)
            {
                await accelerometerDataTask;
            }
            if (gyrometerDataTask != null)
            {
                await gyrometerDataTask;
            }
            if (quaterionDataTask != null)
            {
                await quaterionDataTask;
            }
            if (geolocationDataTask != null)
            {
                await geolocationDataTask;
            }
        }
 private async Task ProcessAnalysis(MeasurementData measurementData, bool isActiveListChoosen)
 {
     if (_taskArguments.IsUsedEvaluation)
     {
         EvaluationDataModel evaluationDataModel = new EvaluationDataModel();
         if (isActiveListChoosen)
         {
             evaluationDataModel.AddAllAccelerometerAnalysisFromSampleList(measurementData.GetActivAccelerometerList());
             evaluationDataModel.AddAllGyrometerAnalysisFromSampleList(measurementData.GetActivGyrometerList());
         }
         else
         {
             evaluationDataModel.AddAllAccelerometerAnalysisFromSampleList(measurementData.GetPassivAccelerometerList());
             evaluationDataModel.AddAllGyrometerAnalysisFromSampleList(measurementData.GetPassivGyrometerList());
         }
         EvaluationResultModel evaluationResultModel = await _measurementEvaluationService.RunEvaluationDuringMeasurementAsync(evaluationDataModel, _evaluationSettingModel);
         _totalSteps += evaluationResultModel.DetectedSteps;
         await TaskFileService.AppendEvaluationDataToFileAsync(_taskArguments, evaluationResultModel);
         _taskInstance.Progress = _totalSteps;
     }
 }
        //##################################################################################################################################
        //################################################## Save Geolocation data #########################################################
        //##################################################################################################################################

        /// <summary>
        /// Saves quaternion tuples form the given geolocation data to the end of file.
        /// Importent: Await Task to be sure all geolocation data has been saved.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="accelerometerTupleList"></param>
        /// <returns></returns>
        public static async Task AppendGeolocationDataToFileAsync(String filename, MeasurementData measurementData, bool isActiveListChoosen)
        {
            if (filename != null && filename != String.Empty)
            {
                // convert data into byte array
                byte[] bytes = measurementData.ToGeolocationBytes(isActiveListChoosen);
                if (bytes != null && bytes.Length > 0)
                {
                    // find folder
                    StorageFolder folder = await FindStorageFolder(_measurementGeolocationPath);
                    // save csv string
                    await SaveBytesToEndOfFileAsync(bytes, folder, filename);
                }
            }
            return;
        }