private void OnPositionChangedForeground(object sender, CustomPositionChangedEventArgs args) { IGPSTracker senderTracker = sender as IGPSTracker; double? argSpeed = args.Speed; if (argSpeed.HasValue) { double speed = (double) argSpeed; //speed is NaN if connection is weak bool connectionIsWeak = double.IsNaN(speed); double totalPeriodCosts = 0; if (!connectionIsWeak) { //Ignore very small speeds which usually aren't resulted by driving if (speed < 1) { speed = 0; } //Calculate distances // ReSharper disable once PossibleLossOfFraction if (senderTracker != null) { _periodDistanceKm += (speed * (senderTracker.Interval / 1000)) / 1000; } //unit of interval is millisecond _totalDistKm += _periodDistanceKm; totalPeriodCosts = _periodDistanceKm * CostsPerKm; //reset period distance because cost of it is added to users' bill _periodDistanceKm = 0; } //Raise BillsChangedEvent via dispatcher if (BillsChanged != null) { Action action = delegate { BillsChanged(this, new BillsChangedEventArgs(totalPeriodCosts, speed, _totalDistKm, connectionIsWeak)); }; _dispatcher.Invoke(action); } } }
private void OnPositionChangedBackground(object sender, CustomPositionChangedEventArgs args) { //muutokset tulee tallentaa säännöllisesti siltä varalta, että käyttäjä //ei palaa enää ohjelmaan. Päivitysväli on yksi kaksi minuuttia if(_backgroundUpdateCounter >= 60) { _backgroundUpdateCounter = 0; OnPositionChangedForeground(sender, args); } else { double? argSpeed = args.Speed; if (argSpeed.HasValue) { double speed = (double)argSpeed; if (!double.IsNaN(speed)) { // ReSharper disable once PossibleLossOfFraction double distKm = (speed * (_gpsTracker.Interval / 1000)) / 1000; //interval is milliseconds //increase period distance so it can be added to users' bill when app is on foreground _periodDistanceKm += distKm; } } } }