//###########################################################################
        //######################## 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 static TaskArguments mapTo(MeasurementModel measurementModel)
        {
            TaskArguments taskArguments = new TaskArguments();
            taskArguments.MeasurementId = measurementModel.Id;
            taskArguments.Filename = measurementModel.Filename;
            taskArguments.TargetDuration = measurementModel.Setting.TargetDuration;
            taskArguments.StartOffsetDuration = measurementModel.Setting.StartOffsetDuration;

            taskArguments.IsUsedEvaluation = measurementModel.Setting.IsUsedEvaluation;
            taskArguments.IsRecordSamplesEvaluation = measurementModel.Setting.IsRecordSamplesEvaluation;
            taskArguments.SampleBufferSize = measurementModel.Setting.SampleBufferSize;
            taskArguments.AccelerometerThreshold = measurementModel.Setting.AccelerometerThreshold;
            taskArguments.GyrometerThreshold = measurementModel.Setting.GyrometerThreshold;
            taskArguments.StepDistance = measurementModel.Setting.StepDistance;
            taskArguments.PeakJoinDistance = measurementModel.Setting.PeakJoinDistance;

            taskArguments.IsUsedAccelerometer = measurementModel.Setting.IsUsedAccelerometer;
            taskArguments.IsRecordSamplesAccelerometer = measurementModel.Setting.IsRecordSamplesAccelerometer;
            taskArguments.ReportIntervalAccelerometer = measurementModel.Setting.ReportIntervalAccelerometer;

            taskArguments.IsUsedGyrometer = measurementModel.Setting.IsUsedGyrometer;
            taskArguments.IsRecordSamplesGyrometer = measurementModel.Setting.IsRecordSamplesGyrometer;
            taskArguments.ReportIntervalGyrometer = measurementModel.Setting.ReportIntervalGyrometer;

            taskArguments.IsUsedQuaternion = measurementModel.Setting.IsUsedQuaternion;
            taskArguments.IsRecordSamplesQuaternion = measurementModel.Setting.IsRecordSamplesQuaternion;
            taskArguments.ReportIntervalQuaternion = measurementModel.Setting.ReportIntervalQuaternion;

            taskArguments.IsUsedGeolocation = measurementModel.Setting.IsUsedGeolocation;
            taskArguments.IsRecordSamplesGeolocation = measurementModel.Setting.IsRecordSamplesGeolocation;
            taskArguments.ReportIntervalGeolocation = measurementModel.Setting.ReportIntervalGeolocation;
            return taskArguments;
        }
        //###########################################################################
        //########################### GeolocationSensor #############################
        //###########################################################################

        private void InitGeolocationSensor(TaskArguments taskArguments)
        {
            if (taskArguments.IsUsedGeolocation)
            {
                _geolocator = new Geolocator();
                if (_geolocator != null && _geolocator.LocationStatus != PositionStatus.Disabled)
                {
                    // force gps quality readings
                    _geolocator.DesiredAccuracy = PositionAccuracy.High;
                    _geolocator.MovementThreshold = 0;
                    //_geolocator.ReportInterval = taskArguments.ReportIntervalGeolocation;
                    //_geolocationEventHandler = new TypedEventHandler<Geolocator, PositionChangedEventArgs>(GeolocationPositionChanged);
                    //_geolocator.PositionChanged += _geolocationEventHandler;

                    TimeSpan period = TimeSpan.FromSeconds(taskArguments.ReportIntervalGeolocation);

                    _periodicUpdateTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
                    {
                        Debug.WriteLine("Position read with status: {0}", _geolocator.LocationStatus);

                        if (_geolocator != null && _geolocator.LocationStatus != PositionStatus.Disabled)
                        {
                            Geoposition currentGeoposition = await _geolocator.GetGeopositionAsync();
                            if (currentGeoposition != null)
                            {
                                Debug.WriteLine("Current coordianates: {0}:{1}", currentGeoposition.Coordinate.Point.Position.Latitude, currentGeoposition.Coordinate.Point.Position.Longitude);
                                _measurementData.AddGeolocationReading(currentGeoposition.Coordinate);
                            }
                        }
                        else if (_periodicUpdateTimer != null)
                        {
                            _periodicUpdateTimer.Cancel();
                        }
                    }, period);
                }
            }
        }
        //###########################################################################
        //########################### OrientationSensor #############################
        //###########################################################################

        private void InitOrientationSensor(TaskArguments taskArguments)
        {
            if (taskArguments.IsUsedQuaternion)
            {
                _orientationSensor = OrientationSensor.GetDefault();
                if (_orientationSensor != null)
                {
                    uint targetSensorReportInterval = taskArguments.ReportIntervalQuaternion;
                    _orientationSensor.ReportInterval = targetSensorReportInterval >= _orientationSensor.MinimumReportInterval ? targetSensorReportInterval : _orientationSensor.MinimumReportInterval;
                    _orientationSensorEventHandler = new TypedEventHandler<OrientationSensor, OrientationSensorReadingChangedEventArgs>(OrientationSensorReadingChanged);
                    _orientationSensor.ReadingChanged += _orientationSensorEventHandler;
                }
            }
        }
        //###########################################################################
        //########################### Gyrometer #####################################
        //###########################################################################

        private void InitGyrometer(TaskArguments taskArguments)
        {
            if (taskArguments.IsUsedGyrometer) 
            { 
                _gyrometer = Gyrometer.GetDefault();
                if (_gyrometer != null)
                {
                    uint targetSensorReportInterval = taskArguments.ReportIntervalGyrometer;
                    _gyrometer.ReportInterval = targetSensorReportInterval >= _gyrometer.MinimumReportInterval ? targetSensorReportInterval : _gyrometer.MinimumReportInterval;
                    _gyrometerEventHandler = new TypedEventHandler<Gyrometer, GyrometerReadingChangedEventArgs>(GyrometerReadingChanged);
                    _gyrometer.ReadingChanged += _gyrometerEventHandler;
                }
            }
        }
        //###########################################################################
        //########################### Accelerometer #################################
        //###########################################################################

        private void InitAccelerometer(TaskArguments taskArguments)
        {
            if (taskArguments.IsUsedAccelerometer) 
            { 
                _accelerometer = Accelerometer.GetDefault();
                if (_accelerometer != null)
                {
                    uint targetSensorReportInterval = taskArguments.ReportIntervalAccelerometer;
                    _accelerometer.ReportInterval = targetSensorReportInterval >= _accelerometer.MinimumReportInterval ? targetSensorReportInterval : _accelerometer.MinimumReportInterval; 
                    _accelerometerEventHandler = new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(AccelerometerReadingChanged);
                    _accelerometer.ReadingChanged += _accelerometerEventHandler;
                }
            }
        }
        //##################################################################################################################################
        //################################################## Save Evaluation data ##########################################################
        //##################################################################################################################################

        /// <summary>
        /// 
        /// </summary>
        /// <param name="evaluationResultModel"></param>
        /// <returns></returns>
        public static async Task AppendEvaluationDataToFileAsync(TaskArguments taskArguments, EvaluationResultModel evaluationResultModel)
        {
            if (taskArguments.IsUsedEvaluation && taskArguments.IsRecordSamplesEvaluation && 
                taskArguments.Filename != null && taskArguments.Filename != String.Empty && 
                evaluationResultModel.EvaluationResultList.Count > 0)
            {
                // convert data into byte array
                byte[] bytes = evaluationResultModel.ToEvaluationBytes();
                if (bytes != null && bytes.Length > 0)
                {
                    // find folder
                    StorageFolder folder = await FindStorageFolder(_evaluationPath);
                    // save byte array
                    await SaveBytesToEndOfFileAsync(bytes, folder, taskArguments.Filename);
                }
            }
            return;
        }