/// <summary>
        /// Insert a new seekios
        /// </summary>
        public void InsertSeekiosSignalR(string uidDevice, string seekiosJson)
        {
            if (string.IsNullOrEmpty(seekiosJson))
            {
                return;
            }
            if (uidDevice == App.UidDevice)
            {
                return;
            }
            var seekiosToAdd = JsonConvert.DeserializeObject <SeekiosDTO>(seekiosJson);

            if (seekiosToAdd != null)
            {
                App.CurrentUserEnvironment.LsSeekios.Add(seekiosToAdd);
                App.Locator.ListSeekios.ActivityNeedsUIToBeUpdated   = true;
                App.Locator.DetailSeekios.ActivityNeedsUIToBeUpdated = true;
                if (_dispatcherService != null)
                {
                    _dispatcherService.Invoke(() =>
                    {
                        App.RaiseSeekiosInformationChangedEverywhere(seekiosToAdd.Idseekios);
                    });
                }
            }
        }
Exemplo n.º 2
0
 private void FlightsLoaded(Result <IEnumerable <Flight> > result)
 {
     if (result.HasError())
     {
         HandleException(result.Error);
     }
     else
     {
         _dispatcher.Invoke(() => LoadFlights(result.Value));
     }
 }
Exemplo n.º 3
0
        public void SeekiosInstructionTaken(string uidSeekios
                                            , Tuple <int, int> batteryAndSignal
                                            , DateTime dateCommunication)
        {
            // Get the concerned seekios
            var seekios = App.CurrentUserEnvironment.LsSeekios.FirstOrDefault(el => el.UIdSeekios == uidSeekios);

            if (seekios == null)
            {
                return;
            }

            // Update the seekios
            seekios.HasGetLastInstruction = true;
            if (batteryAndSignal != null)
            {
                seekios.BatteryLife   = batteryAndSignal.Item1;
                seekios.SignalQuality = batteryAndSignal.Item2;
            }
            seekios.DateLastCommunication = dateCommunication.ToLocalTime();
            seekios.IsRefreshingBattery   = false;

            // Update the mode
            var mode = App.CurrentUserEnvironment.LsMode.FirstOrDefault(x => x.Seekios_idseekios == seekios.Idseekios);

            if (mode != null)
            {
                mode.DateModeActivation = DateTime.Now;
            }

            // Update UI
            if (_dispatcherService == null)
            {
                return;
            }
            _dispatcherService.Invoke(() =>
            {
                App.RaiseSeekiosInformationChangedEverywhere(seekios.Idseekios);
            });
        }
        private void FireEvent(Action <Position> callback)
        {
            Thread.Sleep(25);


            _dispatcher.Invoke(() =>
            {
                var position              = new Position();
                position.Coords.Latitude  = _currentLocation.Latitude;
                position.Coords.Longitude = _currentLocation.Longitude;
                callback(position);
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Update the user
        /// </summary>
        public void UpdateUserSignalR(string uidDevice, string userJson)
        {
            if (string.IsNullOrEmpty(userJson))
            {
                return;
            }
            if (uidDevice == App.UidDevice)
            {
                return;
            }
            var user = JsonConvert.DeserializeObject <UserDTO>(userJson);

            if (user != null)
            {
                App.CurrentUserEnvironment.User = user;
                if (_dispatcherService != null)
                {
                    _dispatcherService.Invoke(() =>
                    {
                        OnUserChanged?.Invoke(null, null);
                    });
                }
            }
        }
        /// <summary>
        /// Delete seekios
        /// </summary>
        public void DeleteSeekiosSignalR(string uidDevice, string idseekiosStr)
        {
            if (string.IsNullOrEmpty(idseekiosStr))
            {
                return;
            }
            if (uidDevice == App.UidDevice)
            {
                return;
            }
            int idseekios = 0;

            if (!int.TryParse(idseekiosStr, out idseekios))
            {
                return;
            }
            if (!App.CurrentUserEnvironment.LsSeekios.Any(x => x.Idseekios == idseekios))
            {
                return;
            }

            var modes = App.CurrentUserEnvironment.LsMode.Where(x => x.Seekios_idseekios == idseekios);

            if (modes?.Count() > 0)
            {
                foreach (var mode in modes)
                {
                    var alerts = App.CurrentUserEnvironment.LsAlert.Where(x => x.IdMode == mode.Idmode);
                    if (alerts?.Count() > 0)
                    {
                        foreach (var alert in alerts)
                        {
                            // Remove recipients
                            App.CurrentUserEnvironment.LsAlertRecipient.RemoveAll(x => x.IdAlert == alert.IdAlert);
                        }
                    }
                    // Remove alerts
                    App.CurrentUserEnvironment.LsAlert.RemoveAll(x => x.IdMode == mode.Idmode);
                }
                // Remove modes
                App.CurrentUserEnvironment.LsMode.RemoveAll(x => x.Seekios_idseekios == idseekios);
            }
            // Remove locations
            App.CurrentUserEnvironment.LsLocations.RemoveAll(x => x.Seekios_idseekios == idseekios);
            // Remove seekios
            App.CurrentUserEnvironment.LsSeekios.RemoveAll(x => x.Idseekios == idseekios);
            App.Locator.ListSeekios.ActivityNeedsUIToBeUpdated   = true;
            App.Locator.DetailSeekios.ActivityNeedsUIToBeUpdated = true;
            if (_dispatcherService != null)
            {
                _dispatcherService.Invoke(() =>
                {
                    if (_navigationService.CurrentPageKey != "-- ROOT --")
                    {
                        // Navigate to the list seekios
                        _navigationService.NavigateTo(App.LIST_SEEKIOS_PAGE);
                    }
                    else
                    {
                        App.RaiseSeekiosInformationChangedEverywhere(0);
                    }
                });
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Modify SOS alert, depending of the SOSAlert variable
        /// </summary>
        public void InsertOrUpdateAlertSOSSignalR(string uidDevice, string idseekiosStr, string alertWithRecipientJson)
        {
            if (string.IsNullOrEmpty(idseekiosStr))
            {
                return;
            }
            if (uidDevice == App.UidDevice)
            {
                return;
            }
            int idseekios = 0;

            if (!int.TryParse(idseekiosStr, out idseekios))
            {
                return;
            }
            if (string.IsNullOrEmpty(alertWithRecipientJson))
            {
                return;
            }
            var alertWithRecipient = JsonConvert.DeserializeObject <AlertWithRecipientDTO>(alertWithRecipientJson);

            if (alertWithRecipient != null)
            {
                var seekios = App.CurrentUserEnvironment.LsSeekios.FirstOrDefault(x => x.Idseekios == idseekios);
                if (seekios != null)
                {
                    // Update Alert SOS
                    if (seekios.AlertSOS_idalert.HasValue)
                    {
                        // Remove old data
                        var alert = App.CurrentUserEnvironment.LsAlert.FirstOrDefault(x => x.IdAlert == seekios.AlertSOS_idalert);
                        App.CurrentUserEnvironment.LsAlertRecipient.RemoveAll(x => x.IdAlert == alert.IdAlert);
                        App.CurrentUserEnvironment.LsAlert.Remove(alert);
                        App.Locator.AlertSOS.LsRecipients.Clear();

                        // Add new data
                        CurrentAlertSOS = alertWithRecipient;
                        App.CurrentUserEnvironment.LsAlert.Add(alertWithRecipient);
                        seekios.AlertSOS_idalert = alertWithRecipient.IdAlert;
                        foreach (var recipient in alertWithRecipient.LsRecipients)
                        {
                            recipient.IdAlert = alertWithRecipient.IdAlert;
                        }
                        App.CurrentUserEnvironment.LsAlertRecipient.AddRange(alertWithRecipient.LsRecipients);
                    }
                    // Insert Alert SOS
                    else
                    {
                        CurrentAlertSOS          = alertWithRecipient;
                        seekios.AlertSOS_idalert = alertWithRecipient.IdAlert;
                        App.CurrentUserEnvironment.LsAlert.RemoveAll(r => r.IdAlert == alertWithRecipient.IdAlert);
                        App.CurrentUserEnvironment.LsAlert.Add(alertWithRecipient);
                        App.CurrentUserEnvironment.LsAlertRecipient.RemoveAll(r => r.IdAlert == alertWithRecipient.IdAlert);
                        App.Locator.AlertSOS.LsRecipients.Clear();
                        if (alertWithRecipient.LsRecipients?.Count > 0)
                        {
                            foreach (var recipient in alertWithRecipient.LsRecipients)
                            {
                                recipient.IdAlert = alertWithRecipient.IdAlert;
                            }
                            App.CurrentUserEnvironment.LsAlertRecipient.AddRange(alertWithRecipient.LsRecipients);
                        }
                    }
                    // Update the view
                    if (_dispatcherService != null)
                    {
                        _dispatcherService.Invoke(() =>
                        {
                            OnAlertSosChanged?.Invoke(null, null);
                        });
                    }
                }
            }
        }
        /// <summary>
        /// Select a mode
        /// </summary>
        public async Task <bool> SelectMode(ModeDefinitionEnum modeDefinition)
        {
            try
            {
                // User is out of requests
                if (int.Parse(CreditHelper.TotalCredits) <= 0)
                {
                    await _dialogService.ShowMessage(Resources.UserNoRequestLeft
                                                     , Resources.UserNoRequestLeftTitle);

                    return(false);
                }

                // User has no internet
                if (!App.DeviceIsConnectedToInternet)
                {
                    await _dialogService.ShowMessage(Resources.NoInternetMessage
                                                     , Resources.NoInternetTitle);

                    return(false);
                }

                // If a mode is already active on the seekios, we display custom popup
                var modeActive = App.CurrentUserEnvironment.LsMode.FirstOrDefault(el => el.Seekios_idseekios == App.Locator.DetailSeekios.SeekiosSelected.Idseekios);
                if (modeActive != null)
                {
                    if (!await _dialogService.ShowChangeModePopup(Resources.ModeChangeTitle
                                                                  , Resources.ModeChangePowerSaving
                                                                  , modeActive.ModeDefinition_idmodeDefinition
                                                                  , (int)modeDefinition
                                                                  , SeekiosUpdated.IsInPowerSaving))
                    {
                        return(false);
                    }
                }

                // Display loading layout
                _dialogService.ShowLoadingLayout();

                // Settings to save offline the preferences for mode zone / don't move
                var trackingSetting = App.Locator.ModeSelection.LsTrackingSetting.FirstOrDefault(x => x.IdSeekios == App.Locator.DetailSeekios.SeekiosSelected.Idseekios && x.ModeDefinition == modeDefinition);
                if (trackingSetting == null)
                {
                    trackingSetting = new TrackingSetting()
                    {
                        IdSeekios      = SeekiosUpdated.Idseekios,
                        ModeDefinition = modeDefinition,
                        RefreshTime    = MapViewModelBase.RefreshTime,
                    };
                    App.Locator.ModeSelection.LsTrackingSetting.Add(trackingSetting);
                }
                else
                {
                    trackingSetting.RefreshTime = MapViewModelBase.RefreshTime;
                }

                // Create the new mode to add
                var modeToAddInDb = new ModeDTO
                {
                    Seekios_idseekios = SeekiosUpdated.Idseekios,
                    Trame             = string.Empty,
                    Device_iddevice   = App.CurrentUserEnvironment.Device != null ? App.CurrentUserEnvironment.Device.Iddevice : 0,
                };

                // Save the mode in database
                int idMode        = 0;
                var timeDiffHours = Math.Ceiling((DateTime.Now - DateTime.UtcNow).TotalHours);
                switch (modeDefinition)
                {
                case ModeDefinitionEnum.ModeDontMove:
                    trackingSetting.IsEnable             = App.Locator.ModeDontMove.IsTrackingSettingEnable;
                    trackingSetting.IsPowerSavingEnabled = App.Locator.ModeDontMove.IsPowerSavingEnabled;
                    modeToAddInDb.Trame = string.Format("{0};{1}", timeDiffHours, MapViewModelBase.RefreshTime == 0 ? string.Empty : MapViewModelBase.RefreshTime.ToString());
                    modeToAddInDb.IsPowerSavingEnabled = App.Locator.ModeDontMove.IsPowerSavingEnabled;
                    idMode = await _dataService.InsertModeDontMove(modeToAddInDb, App.Locator.ModeDontMove.LsAlertsModeDontMove);

                    break;

                case ModeDefinitionEnum.ModeZone:
                    trackingSetting.IsEnable             = App.Locator.ModeZone.IsTrackingSettingEnable;
                    trackingSetting.IsPowerSavingEnabled = App.Locator.ModeZone.IsPowerSavingEnabled;
                    modeToAddInDb.Trame = string.Format("{0};{1}", timeDiffHours, App.Locator.ModeZone.CodeTrame(App.Locator.ModeZone.LsAreaCoordsMap));
                    modeToAddInDb.IsPowerSavingEnabled = App.Locator.ModeZone.IsPowerSavingEnabled;
                    idMode = await _dataService.InsertModeZone(modeToAddInDb, App.Locator.ModeZone.LsAlertsModeZone);

                    break;

                case ModeDefinitionEnum.ModeTracking:
                    modeToAddInDb.Trame = string.Format("{0};{1}", timeDiffHours, MapViewModelBase.RefreshTime == 0 ? string.Empty : MapViewModelBase.RefreshTime.ToString());
                    modeToAddInDb.IsPowerSavingEnabled   = App.Locator.ModeTracking.IsPowerSavingEnabled;
                    trackingSetting.IsPowerSavingEnabled = App.Locator.ModeTracking.IsPowerSavingEnabled;
                    idMode = await _dataService.InsertModeTracking(modeToAddInDb);

                    break;

                default:
                    return(false);
                }

                // Update power saving state only if the current state of power saving was off
                if (!App.CurrentUserEnvironment.LsSeekios.First(x => x.Idseekios == SeekiosUpdated.Idseekios).IsInPowerSaving)
                {
                    SeekiosUpdated.IsInPowerSaving = trackingSetting.IsPowerSavingEnabled;
                    App.CurrentUserEnvironment.LsSeekios.First(x => x.Idseekios == SeekiosUpdated.Idseekios).IsInPowerSaving = trackingSetting.IsPowerSavingEnabled;
                }

                // Save the setting tracking offline
                _saveDataService.SaveData(App.TrackingSetting, JsonConvert.SerializeObject(LsTrackingSetting));

                // Update the seekios
                if (idMode <= 0)
                {
                    return(false);
                }
                SeekiosUpdated.HasGetLastInstruction = false;
                if (MapViewModelBase.Seekios != null)
                {
                    MapViewModelBase.Seekios.HasGetLastInstruction = false;
                }

                // We locally delete the last mode and the alerts associate
                foreach (var mode in App.CurrentUserEnvironment.LsMode.Where(x => x.Seekios_idseekios == SeekiosUpdated.Idseekios))
                {
                    var idAlerts = App.CurrentUserEnvironment.LsAlert.Where(x => x.IdMode == mode.Idmode).Select(x => x.IdAlert).ToList();
                    foreach (var idAlert in idAlerts)
                    {
                        App.CurrentUserEnvironment.LsAlertRecipient.RemoveAll(x => x.IdAlert == idAlert);
                        App.CurrentUserEnvironment.LsAlert.RemoveAll(x => x.IdAlert == idAlert);
                    }
                }
                App.CurrentUserEnvironment.LsMode.RemoveAll(x => x.Seekios_idseekios == SeekiosUpdated.Idseekios);

                // Add the mode in local data
                var modeToAddInLocal = new ModeDTO()
                {
                    Idmode = idMode,
                    CountOfTriggeredAlert = 0,
                    DateModeCreation      = DateTime.UtcNow,
                    Device_iddevice       = modeToAddInDb.Device_iddevice,
                    Seekios_idseekios     = modeToAddInDb.Seekios_idseekios,
                    StatusDefinition_idstatusDefinition = (int)StatutDefinitionEnum.RAS,
                    Trame = modeToAddInDb.Trame,
                    ModeDefinition_idmodeDefinition = (int)modeDefinition,
                    IsPowerSavingEnabled            = modeToAddInDb.IsPowerSavingEnabled
                };
                App.CurrentUserEnvironment.LsMode.Add(modeToAddInLocal);

                // Handle alerts if it's required
                switch (modeDefinition)
                {
                case ModeDefinitionEnum.ModeDontMove:
                    // configure a bool to execute 2 back actions
                    App.Locator.ListAlert.Seekios = SeekiosUpdated;
                    var dontMoveAlerts = await _dataService.AlertsByMode(modeToAddInDb);

                    if (dontMoveAlerts != null)
                    {
                        foreach (AlertDTO a in dontMoveAlerts)
                        {
                            App.CurrentUserEnvironment.LsAlert.Add(a);
                        }
                    }
                    // Pourquoi on utilise le ModeZoneViewModel pour le DontMove???
                    App.Locator.ModeDontMove.LsAlertsModeDontMove?.Clear();
                    break;

                case ModeDefinitionEnum.ModeZone:
                    // il faut aussi aller chercher les alertes pour le nouveau mode avec leur nouveaux ids...
                    // vider la liste dans ModeZoneVM + ajouter les nlles alertes dans LsAlertes
                    var zoneAlerts = await _dataService.AlertsByMode(modeToAddInDb);

                    if (zoneAlerts != null)
                    {
                        foreach (AlertDTO a in zoneAlerts)
                        {
                            App.CurrentUserEnvironment.LsAlert.Add(a);
                        }
                    }
                    App.Locator.ModeZone.LsAlertsModeZone?.Clear();
                    break;
                }

                // Delete the seekios tracking object because it's a new mode
                var seekiosOnTrackingToDelete = App.Locator.Map.LsSeekiosOnTracking.FirstOrDefault(x => x.Seekios.Idseekios == SeekiosUpdated.Idseekios);
                if (seekiosOnTrackingToDelete != null)
                {
                    if (seekiosOnTrackingToDelete.Timer.IsRunning)
                    {
                        seekiosOnTrackingToDelete.Timer.Stop();
                    }
                    App.Locator.Map.LsSeekiosOnTracking.RemoveAll(x => x.Seekios.Idseekios == SeekiosUpdated.Idseekios);
                }

                // Setup a new seekios tracking object for a new timer
                if (modeToAddInLocal.ModeDefinition_idmodeDefinition == (int)ModeDefinitionEnum.ModeTracking)
                {
                    App.Locator.Map.AddSeekiosOnTracking(SeekiosUpdated, modeToAddInLocal);
                }

                // Hide loading layout
                _dialogService.HideLoadingLayout();
                return(true);
            }
            catch (TimeoutException)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.TimeoutError
                                              , Resources.TimeoutErrorTitle
                                              , Resources.Close, null));
            }
            catch (WebException)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.TimeoutError
                                              , Resources.TimeoutErrorTitle
                                              , Resources.Close, null));
            }
            catch (Exception)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.UnexpectedError
                                              , Resources.UnexpectedErrorTitle
                                              , Resources.Close, null));
            }
            _dialogService.HideLoadingLayout();
            return(false);
        }
        /// <summary>
        /// Refresh seekios position
        /// </summary>
        public async Task <bool> RefreshSeekiosPosition()
        {
            var seekiosToRefresh = App.Locator.DetailSeekios.SeekiosSelected;

            try
            {
                // If the seekios is already in refresh state
                if (LsSeekiosOnDemand.Any(x => x.Seekios.Idseekios == seekiosToRefresh.Idseekios))
                {
                    return(false);
                }

                // If user has enough credits
                int creditsDispo = 0;
                if (!int.TryParse(Helper.CreditHelper.TotalCredits, out creditsDispo))
                {
                    return(false);
                }
                if (creditsDispo <= 0)
                {
                    var msg   = Resources.UserNoRequestLeft;
                    var title = Resources.UserNoRequestLeftTitle;
                    await _dialogService.ShowMessage(msg, title);

                    return(false);
                }

                // If the seekios is in power saving, cancel the request
                if (App.CurrentUserEnvironment.LsSeekios.First(x => x.Idseekios == seekiosToRefresh.Idseekios).IsInPowerSaving == true)
                {
                    await _dialogService.ShowMessage(Resources.PowerSavingOn
                                                     , Resources.PowerSavingOnTitle
                                                     , Resources.PowerSavingTuto
                                                     , Resources.Close, (result2) => { if (result2)
                                                                                       {
                                                                                           App.Locator.Parameter.GoToTutorialPowerSaving();
                                                                                       }
                                                     });

                    return(false);
                }

                // Make the request in BDD
                _dialogService.ShowLoadingLayout();
                var result = await _dataService.RefreshSeekiosLocation(seekiosToRefresh.Idseekios);

                if (result != 1)
                {
                    var msg   = Resources.RefreshSeekiosFailed;
                    var title = Resources.RefreshSeekiosFailedTitle;
                    _dispatcherService.Invoke(async() => await _dialogService.ShowMessage(msg, title));
                }
                _dialogService.HideLoadingLayout();

                // Udpate the seekios
                var index = App.CurrentUserEnvironment.LsSeekios.IndexOf(App.CurrentUserEnvironment.LsSeekios.First(x => x.Idseekios == seekiosToRefresh.Idseekios));
                App.CurrentUserEnvironment.LsSeekios[index].DateLastOnDemandRequest = DateTime.Now;
                App.CurrentUserEnvironment.LsSeekios[index].HasGetLastInstruction   = false;
                App.Locator.DetailSeekios.SeekiosSelected = App.CurrentUserEnvironment.LsSeekios[index];

                // Add the new seekios is the refresh seekios list
                AddSeekiosOnDemand(seekiosToRefresh, DateTime.Now.AddSeconds(App.TIME_FOR_REFRESH_SEEKIOS_IN_SECOND));

                // Raise event for update UI
                OnSeekiosRefreshRequestSent?.Invoke(null, null);
                return(true);
            }

            catch (TimeoutException)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.TimeoutError
                                              , Resources.TimeoutErrorTitle
                                              , Resources.Close, null));
            }
            catch (WebException)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.TimeoutError
                                              , Resources.TimeoutErrorTitle
                                              , Resources.Close, null));
            }
            catch (Exception)
            {
                _dispatcherService.Invoke(async() => await _dialogService.ShowError(
                                              Resources.UnexpectedError
                                              , Resources.UnexpectedErrorTitle
                                              , Resources.Close, null));
            }
            _dialogService.HideLoadingLayout();
            return(false);
        }