예제 #1
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 SendCurrentLocation()
        {
            try
            {
                var accessStatus = await Geolocator.RequestAccessAsync();

                if (accessStatus != GeolocationAccessStatus.Allowed)
                {
                    await client.Location.NotifyErrorAsync(new LocationConfigurationError()
                    {
                        Denied   = true,
                        Disabled = false
                    });

                    return;
                }
                var locator = new Geolocator();
                if (locator.LocationStatus == PositionStatus.Disabled)
                {
                    await client.Location.NotifyErrorAsync(new LocationConfigurationError()
                    {
                        Denied   = false,
                        Disabled = true
                    });
                }
                var res = await locator.GetGeopositionAsync(new TimeSpan(0, 10, 0), new TimeSpan(0, 0, 23));

                await client.Location.AddLocationAsync(new Location()
                {
                    Latitude  = res.Coordinate.Point.Position.Latitude,
                    Longitude = res.Coordinate.Point.Position.Longitude,
                    Accuracy  = res.Coordinate.Accuracy,
                    Timestamp = res.Coordinate.Timestamp.UtcDateTime
                });
            }
            catch (DigitServiceException e)
            {
                await client.LogAsync($"Could not update location information: {e.Message}", 3);
            }
        }
예제 #3
0
        public async void RegisterPushChannel()
        {
            var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            var oldChannel = (localSettings.Values["PushChannelUri"] as string);

            if (null == oldChannel || oldChannel != channel.Uri)
            {
                var hub    = new NotificationHub(notificationHubName, notificationHubEndpoint);
                var result = await hub.RegisterNativeAsync(channel.Uri);

                if (result.RegistrationId != null)
                {
                    var pushChannelRegistration = new PushChannelRegistration()
                    {
                        Uri = result.RegistrationId
                    };
                    try //retry here because it's one of the calls where the user is active
                    {
                        await client.SetupPushChannelAsync(pushChannelRegistration);
                    }
                    catch (UnauthorizedException)
                    {
                        var authenticationProvider = DigitServiceBuilder.AuthenticationProvider();
                        await authenticationProvider.AuthenticateUser();

                        try
                        {
                            await client.SetupPushChannelAsync(pushChannelRegistration);
                        }
                        catch (UnauthorizedException e)
                        {
                            await client.LogAsync($"Authorization error while push channel registration: {e.Message}", 3);

                            return;
                        }
                    }
                    catch (DigitServiceException s)
                    {
                        await client.LogAsync($"Failed to register notification channel: {s.Message}", 3);

                        return;
                    }
                    localSettings.Values["PushChannelUri"] = channel.Uri;
                    await client.LogAsync($"Successfully registered notification channel {result.RegistrationId}", 1);
                }
                else
                {
                    await client.LogAsync($"Failed to register channel at hub", -1);
                }
            }
        }