Example #1
0
        public async Task <IActionResult> AddBatteryMeasurement(string id, [FromBody] BatteryMeasurement batteryMeasurement)
        {
            if (!await deviceService.HasAccessAsync(id, User.GetId()))
            {
                return(Unauthorized());
            }

            await deviceService.AddBatteryMeasurementAsync(id, batteryMeasurement);

            return(Ok());
        }
Example #2
0
        public async Task StoreBatteryMeasurementAsync(string deviceId, BatteryMeasurement batteryStatus)
        {
            var mes = new StoredBatteryMeasurement()
            {
                DeviceId        = deviceId,
                RawValue        = batteryStatus.RawValue,
                MeasurementTime = batteryStatus.MeasurementTime
            };
            await digitServiceContext.BatteryMeasurements.AddAsync(mes);

            await digitServiceContext.SaveChangesAsync();
        }
Example #3
0
                public async Task AddMeasurementAsync(BatteryMeasurement measurement)
                {
                    var client = await clientFactory();

                    var json = JsonConvert.SerializeObject(measurement);
                    HttpResponseMessage res = await client.PostAsync($"api/device/{deviceId}/battery",
                                                                     new StringContent(json, Encoding.UTF8, "application/json"));

                    if (!res.IsSuccessStatusCode)
                    {
                        throw new DigitServiceException($"Battery measurement post error {res.StatusCode}");
                    }
                }
Example #4
0
        public async Task <bool> AddBatteryMeasurement()
        {
            try
            {
                var batt = await digitBLEClient.ReadBatteryAsync();

                if (!batt.HasValue)
                {
                    await digitServiceClient.LogAsync($"Device was not reachable to read battery");
                }
                else
                {
                    var value       = (batt.Value / 100.0) * 1023;
                    var measurement = new BatteryMeasurement()
                    {
                        MeasurementTime = DateTime.Now,
                        RawValue        = (uint)value
                    };
                    try
                    {
                        await digitServiceClient.Device["12345"].Battery.AddMeasurementAsync(measurement);
                        ApplicationData.Current.LocalSettings.Values[BatteryMeasurementTimerKey] = EveryNthTime;
                        return(true);
                    }
                    catch (DigitServiceException e)
                    {
                        await digitServiceClient.LogAsync($"Could not post battery value {batt.Value}: {e.Message}", 3);
                    }
                }
            }
            catch (DigitBLEExpcetion ex)
            {
                await digitServiceClient.LogAsync($"Could not read battery: {ex.Message}", 3);
            }
            return(false);
        }
 public async Task AddBatteryMeasurementAsync(string deviceId, BatteryMeasurement batteryMeasurement)
 {
     await deviceRepository.StoreBatteryMeasurementAsync(deviceId, batteryMeasurement);
 }