Ejemplo n.º 1
0
        private async Task UpsertDeviceAsync(MeteringDevice device)
        {
            var existingDevice = registeredDevices.FirstOrDefault(p => p.DeviceId == device.Id);

            if (existingDevice == null)
            {
                existingDevice = new RegisteredMeter()
                {
                    DeviceId = device.Id
                };
            }
            existingDevice.DeviceIp = device.Ip;
            await metersDatabase.UpsertAsync(existingDevice);

            await LoadRegisterdDevicesAsync();
        }
        private async Task <IEnumerable <Reading> > DownloadDeviceDataAsync(RegisteredMeter registeredMeter)
        {
            try
            {
                using (var response = await httpClient.GetAsync($"http://{registeredMeter.DeviceIp}:8080/v1/readings"))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        return(Enumerable.Empty <Reading>());
                    }

                    var content = await response.Content.ReadAsStringAsync();

                    var readings = JsonConvert.DeserializeObject <ReadingDto[]>(content);
                    return(readings.Select(p => new Reading()
                    {
                        PM25 = p.PM25,
                        PM10 = p.PM10,
                        DeviceName = p.DeviceName,
                        ClientId = registeredMeter.DeviceId
                    }));
                }
            }
            catch (Exception)
            {
                // TODO display toast error or something
                return(new[]
                {
                    new Reading()
                    {
                        PM25 = "-2",
                        PM10 = "-2",
                        DeviceName = "Error",
                        ClientId = registeredMeter.DeviceId
                    }
                });
            }
        }
Ejemplo n.º 3
0
 public async Task <int> RemoveMeterAsync(RegisteredMeter registeredMeter)
 => await _database.DeleteAsync(registeredMeter);
Ejemplo n.º 4
0
 public async Task UpsertAsync(RegisteredMeter registeredMeter)
 => await _database.InsertOrReplaceAsync(registeredMeter);