Beispiel #1
0
        private async Task <bool> AddPointOnMapTask(Location location, UserToken token, Guid bidGuid)
        {
            using (var client = new HttpClient(new CustomAndroidClientHandler()))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.Token);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var encodedContent = new Dictionary <string, string>
                {
                    {
                        "latitude", location.Latitude.ToString(CultureInfo.InvariantCulture)
                    },
                    {
                        "longitude", location.Longitude.ToString(CultureInfo.InvariantCulture)
                    }
                };

                if (bidGuid != Guid.Empty)
                {
                    encodedContent.Add("uid", bidGuid.ToString());
                }

                var response = await client.PostAsync("http://lk.asb-security.ru/api/pointOnMap", new FormUrlEncodedContent(encodedContent));

                return(await Task.FromResult(response.IsSuccessStatusCode));
            }
        }
Beispiel #2
0
        private async Task <bool> UpdateCurrentLocation(Location location, UserToken token, Guid bidGuid)
        {
            using (var client = new HttpClient(new CustomAndroidClientHandler()))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.TokenType, token.Token);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = await client.PostAsync(UpdateCurrentLocationUri,
                                                      new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    {
                        "latitude", location.Latitude.ToString(CultureInfo.InvariantCulture)
                    },
                    {
                        "longitude", location.Longitude.ToString(CultureInfo.InvariantCulture)
                    }
                }));

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

                System.Diagnostics.Debug.WriteLine(jsonString);

                var result = await Task.FromResult(response.IsSuccessStatusCode);


                return(result);
            }
        }
Beispiel #3
0
        private async void Update()
        {
            if (_bidGuid.Equals(Guid.Empty))
            {
                Log.Error(_tag, "Bid Guid is empty.");
            }

            if (Connectivity.NetworkAccess == NetworkAccess.Internet)
            {
                if (Looper.MyLooper() == null)
                {
                    Looper.Prepare();
                }

                try
                {
                    _locationManager.RequestLocationUpdates(_locationProvider, Milliseconds, (float)MinimumDistance, _locationListener);
                }
                catch (SecurityException ex)
                {
                    Log.Error(_tag, "fail to request location update, ignore", ex);
                }
                catch (IllegalArgumentException ex)
                {
                    Log.Error(_tag, "network provider does not exist, " + ex.Message);
                }

                var location = _locationManager.GetLastKnownLocation(_locationProvider);

                if (location == null || _token == null)
                {
                    return;
                }

                bool res = false;
                var  loc = new Location(location.Latitude, location.Longitude);
                try
                {
                    if (_isGuard)
                    {
                        res = await UpdateCurrentLocation(loc, _token, _bidGuid);
                    }
                    else
                    {
                        res = await AddPointOnMapTask(loc, _token, _bidGuid);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(_tag, "Network error" + e.Message);
                    Console.WriteLine(e);
                }

                Log.Debug(_tag, res ? $"Update location is SUCCESS at {DateTime.Now};" : $"Update location is FAIL at {DateTime.Now}; Error: {_locationService.LastError}");
            }
        }
Beispiel #4
0
        private async void UpdateLocation(UserToken token, LocationUpdatedEventArgs e)
        {
            // Handle foreground updates
            var location = e.Location;

            var service = new LocationService();
            var loc     = new Location(location.Coordinate.Longitude, location.Coordinate.Latitude);

            if (App.User.IsGuard)
            {
                await service.UpdateCurrentLocationTask(loc, token, _bidGuid);
            }
            else
            {
                await service.AddPointOnMapTask(loc, token, _bidGuid);
            }
        }
Beispiel #5
0
        private async void SendAlarm(BidType type)
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            if (Device.RuntimePlatform == Device.Android)
            {
                if (!await CheckPermission(Permission.Location, "Для отправки тревоги, необходимо разрешение на использование геоданных."))
                {
                    IsBusy = false;
                    return;
                }
            }

            var          guid    = Guid.NewGuid();
            var          user    = App.User;
            bool         res     = false;
            IBidsService service = new BidsService
            {
                AccessToken = (string)user.UserToken.Token.Clone(),
                TokenType   = (string)user.UserToken.TokenType.Clone()
            };

            try
            {
                res = await service.CreateBid(new Bid
                {
                    Guid     = guid,
                    Client   = user,
                    Location = await Location.GetCurrentGeolocationAsync(GeolocationAccuracy.Best),
                    Status   = BidStatus.PendingAcceptance,
                    Type     = type
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            if (res)
            {
                if (type == BidType.Call)
                {
                    App.Call("+7 911 447-11-83");
                }
                else if (type == BidType.Alert)
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", "Тревога отправлена", "OK");
                }

                await Task.Run(() =>
                {
                    DependencyService.Get <ILocationTrackingService>()
                    .StartService(guid);
                });
            }
            else
            {
                if (string.IsNullOrEmpty(service.LastError))
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", "Ошибка сервера.", "Ок");
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", service.LastError, "Ок");
                }
            }

            IsBusy = false;
        }