public async Task <IActionResult> AddEnergyLabel(EnergyLabel model, string UserName, string indexConsumptionId)
        {
            EnergyLabel energyLabel = new EnergyLabel
            {
                MP        = model.MP,
                HouseType = model.HouseType,
            };

            energyLabel.Data = DateTime.Now;

            energyLabel.Id                 = Guid.NewGuid().ToString();
            energyLabel.SmallUserId        = _userRepository.GetIdByName(UserName);
            energyLabel.FinalConsumptionId = _userRepository.GetIdFinalConsumptionByIndexConsumption(indexConsumptionId);

            energyLabel.TotalConsumption = (_userRepository.GetTotalConsumptionByFinalId(energyLabel.FinalConsumptionId) * 12);

            energyLabel.kW_mpa      = (float)_applianceRepository.GetKwMpa(energyLabel.TotalConsumption, energyLabel.MP);
            energyLabel.Index       = (float)_applianceRepository.GetIndex(energyLabel.kW_mpa);
            energyLabel.EnergyClass = _applianceRepository.GetEnergyLabel(energyLabel.Index);

            await _consumptionContext.EnergyLabels.AddAsync(energyLabel);

            await _consumptionContext.SaveChangesAsync();

            return(Ok(energyLabel));
        }
        /// <summary>
        /// Recalculates the EnergyLabel property of this object.
        /// </summary>
        public void UpdateEei()
        {
            var calculations = CalculationManager.SelectCalculationStrategy(this);

            foreach (var calculation in calculations ?? Enumerable.Empty <IEEICalculation>())
            {
                EnergyLabel.Add(calculation?.CalculateEEI(this));
            }
        }
        public async Task <IActionResult> GetEnergyLabel(string UserName, string indexConsumptionId)
        {
            EnergyLabel energyLabel = new EnergyLabel();

            string userId             = _userRepository.GetIdByName(UserName);
            string finalConsumptionId = _userRepository.GetIdFinalConsumptionByIndexConsumption(indexConsumptionId);

            energyLabel = _consumptionContext.EnergyLabels.Where(x => x.SmallUserId.Equals(userId)).Where(x => x.FinalConsumptionId.Equals(finalConsumptionId)).FirstOrDefault();

            return(Ok(energyLabel));
        }
Example #4
0
        void ReleaseDesignerOutlets()
        {
            if (EnergyLabel != null)
            {
                EnergyLabel.Dispose();
                EnergyLabel = null;
            }

            if (HeartRateLabel != null)
            {
                HeartRateLabel.Dispose();
                HeartRateLabel = null;
            }

            if (ToggleWorkoutButton != null)
            {
                ToggleWorkoutButton.Dispose();
                ToggleWorkoutButton = null;
            }
        }
Example #5
0
 private void ResetUI()
 {
     ToggleWorkoutButton.SetTitle("Crawl Baby Crawl");
     EnergyLabel.SetText("Energy: 0.0");
     HeartRateLabel.SetText("Heart Rate:N/A");
 }
Example #6
0
        public void BeginWorkout(DateTime beginDate)
        {
            // Obtain the `HKObjectType` for active energy burned and the `HKUnit` for kilocalories.
            var activeEnergyType = HKQuantityType.Create(HKQuantityTypeIdentifier.ActiveEnergyBurned);

            if (activeEnergyType == null)
            {
                return;
            }
            // Obtain the `HKObjectType` for HeartRate.
            var heartRateType = HKQuantityType.Create(HKQuantityTypeIdentifier.HeartRate);

            if (heartRateType == null)
            {
                return;
            }


            var energyUnit    = HKUnit.Kilocalorie;
            var heartRateUnit = HKUnit.FromString("count/min");

            // Update properties.
            WorkoutBeginDate = beginDate;

            // Set up a predicate to obtain only samples from the local device starting from `beginDate`.

            var datePredicate = HKQuery.GetPredicateForSamples((NSDate)beginDate, null, HKQueryOptions.None);

            var devices         = new NSSet <HKDevice>(new HKDevice[] { HKDevice.LocalDevice });
            var devicePredicate = HKQuery.GetPredicateForObjectsFromDevices(devices);
            var predicate       = NSCompoundPredicate.CreateAndPredicate(new NSPredicate[] { datePredicate, devicePredicate });

            //Create a results handler to recreate the samples generated by a query of active energy samples so that they can be associated with this app in the move graph.It should be noted that if your app has different heuristics for active energy burned you can generate your own quantities rather than rely on those from the watch.The sum of your sample's quantity values should equal the energy burned value provided for the workout
            Action <List <HKSample> > sampleHandler;

            sampleHandler = (List <HKSample> samples) =>
            {
                DispatchQueue.MainQueue.DispatchAsync(delegate
                {
                    var accumulatedSamples = new List <HKQuantitySample>();

                    var initialActivityEnergy = CurrentActiveEnergyQuantity.GetDoubleValue(energyUnit);
                    double accumulatedValue   = initialActivityEnergy;
                    foreach (HKQuantitySample sample in samples)
                    {
                        accumulatedValue = accumulatedValue + sample.Quantity.GetDoubleValue(energyUnit);
                        var ourSample    = HKQuantitySample.FromType(activeEnergyType, sample.Quantity, sample.StartDate, sample.EndDate);
                        accumulatedSamples.Add(ourSample);
                    }

                    // Update the UI.
                    CurrentActiveEnergyQuantity = HKQuantity.FromQuantity(energyUnit, accumulatedValue);
                    EnergyLabel.SetText($"Energy: {accumulatedValue.ToString("F")}");

                    // Update our samples.
                    ActiveEnergySamples.AddRange(accumulatedSamples);
                });
            };

            // Create a query to report new Active Energy Burned samples to our app.
            var activeEnergyQuery = new HKAnchoredObjectQuery(activeEnergyType, predicate, null, HKSampleQuery.NoLimit, (query, addedObjects, deletedObjects, newAnchor, error) =>
            {
                if (error == null)
                {
                    // NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout
                    ActiveEnergySamples = new List <HKSample>(addedObjects);
                    sampleHandler(ActiveEnergySamples);
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            });

            // Assign the same handler to process future samples generated while the query is still active.
            activeEnergyQuery.UpdateHandler = (query, addedObjects, deletedObjects, newAnchor, error) =>
            {
                if (error == null)
                {
                    ActiveEnergySamples = new List <HKSample>(addedObjects);
                    sampleHandler(ActiveEnergySamples);
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            };

            // Start Query
            CurrentQuery = activeEnergyQuery;
            HealthStore.ExecuteQuery(activeEnergyQuery);

            Action <List <HKSample> > heartRateSampler = (List <HKSample> samples) =>
            {
                foreach (HKQuantitySample sample in samples)
                {
                    _dangerousHeartRate.OnNext(sample.Quantity.GetDoubleValue(heartRateUnit));
                }
                DispatchQueue.MainQueue.DispatchAsync(delegate
                {
                    var sample = samples.LastOrDefault() as HKQuantitySample;
                    if (sample != null)
                    {
                        HeartRateLabel.SetText($"Heart Rate: {sample.Quantity.GetDoubleValue(heartRateUnit).ToString()}");
                    }
                });
            };

            HeartRateQuery = new HKAnchoredObjectQuery(heartRateType, predicate, null, HKSampleQuery.NoLimit, (query, addedObjects, deletedObjects, newAnchor, error) =>
            {
                if (error == null)
                {
                    // NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout
                    sampleHandler(new List <HKSample>(addedObjects));
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            });
            // Assign the same handler to process future samples generated while the query is still active.
            (HeartRateQuery as HKAnchoredObjectQuery).UpdateHandler = (query, addedObjects, deletedObjects, newAnchor, error) =>
            {
                if (error == null)
                {
                    heartRateSampler(new List <HKSample>(addedObjects));
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            };
            HealthStore.ExecuteQuery(HeartRateQuery);
        }