예제 #1
0
        /// <summary>
        /// Gets the pending geofence request.
        /// </summary>
        /// <returns>The pending geofence request.</returns>
        public Geofence GetPendingGeofenceRequest()
        {
            var geo = PendingValidationGeofence;

            PendingValidationGeofence = null;
            return(geo);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Ipheidi.GeofenceCreatePage"/> class.
 /// </summary>
 /// <param name="geofence">Geofence.</param>
 public GeofenceCreatePage(Geofence _geofence)
 {
     geofence = _geofence;
     Title    = AppResources.GeofenceCreatePageTitle;
     Initialize();
     nameEntry.Text = geofence.Name;
 }
예제 #3
0
        /// <summary>
        /// Sends the geofence to server.
        /// </summary>
        /// <returns><c>true</c>, if geofence was sent to the server , <c>false</c> otherwise.</returns>
        /// <param name="geofence">Geofence.</param>
        async Task <bool> SendGeofenceToServer(Geofence geofence)
        {
            var json       = JsonConvert.SerializeObject(geofence);
            var parameters = new Dictionary <string, string> {
                { "pheidiaction", "sendGeofence" }, { "pheidiparams", "value**:**" + json + "**,**" }
            };
            HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30));

            if (response != null)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string responseContent = response.Content.ReadAsStringAsync().Result;
                    Debug.WriteLine("Reponse:" + responseContent);
                    var answer = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseContent);
                    if (answer["STATUS"] == "Good")
                    {
                        if (string.IsNullOrEmpty(geofence.NoSeq) && answer.ContainsKey("NOSEQ"))
                        {
                            geofence.NoSeq = answer["NOSEQ"];
                        }
                        geofence.LastModification = DateTime.UtcNow;
                        return(true);
                    }
                }
            }
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Adds the geofence.
        /// </summary>
        /// <param name="geofence">Fence.</param>
        public async Task <bool> AddGeofence(Geofence geofence)
        {
            try
            {
                geofence.User        = App.UserNoseq;
                geofence.ServerNoseq = App.ServerInfoNoseq;
                await DatabaseHelper.Database.SaveItemAsync(geofence);

                Geofences.Add(geofence);
                RefreshClosePositionGeofencesList();
                if (!await SendGeofenceToServer(geofence))
                {
                    if (RescheduledGeofenceUpdates.Any((arg) => arg.NoSeq == geofence.NoSeq))
                    {
                        RescheduledGeofenceUpdates.Remove(RescheduledGeofenceUpdates.First((arg) => arg.NoSeq == geofence.NoSeq));
                    }
                    RescheduledGeofenceUpdates.Add(geofence);
                }
                return(true);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            return(false);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Ipheidi.GeofenceCreatePage"/> class.
 /// </summary>
 /// <param name="loc">Location.</param>
 public GeofenceCreatePage(Location loc)
 {
     geofence = new Geofence()
     {
         Latitude = loc.Latitude, Longitude = loc.Longitude
     };
     geofence.Radius = ApplicationConst.DefaultGeofenceRadius;
     Initialize();
 }
예제 #6
0
 /// <summary>
 /// Updates the geofence.
 /// </summary>
 /// <param name="geofence">Geofence.</param>
 public void UpdateGeofence(Geofence geofence)
 {
     Task.Run(async() =>
     {
         await DatabaseHelper.Database.UpdateItem(geofence);
         Geofences[Geofences.IndexOf(Geofences.First(g => g.NoSeq == geofence.NoSeq))] = geofence;
         if (!await SendGeofenceToServer(geofence))
         {
             if (RescheduledGeofenceUpdates.Any((arg) => arg.NoSeq == geofence.NoSeq))
             {
                 RescheduledGeofenceUpdates.Remove(RescheduledGeofenceUpdates.First((arg) => arg.NoSeq == geofence.NoSeq));
             }
             RescheduledGeofenceUpdates.Add(geofence);
         }
     });
 }
예제 #7
0
 /// <summary>
 /// Update the geofence localy.
 /// </summary>
 /// <param name="geofence">Geofence.</param>
 public void LocalGeofenceUpdate(Geofence geofence)
 {
     RefreshClosePositionGeofencesList();
     Task.Run(async() =>
     {
         if (geofence.DeleteFlag == 1)
         {
             await DatabaseHelper.Database.DeleteItemAsync <Geofence>(await DatabaseHelper.Database.GetItem <Geofence>(geofence.NoSeq));
             Geofences.Remove(Geofences.First(g => g.NoSeq == geofence.NoSeq));
         }
         else
         {
             await DatabaseHelper.Database.UpdateItem(geofence);
             Geofences[Geofences.IndexOf(Geofences.First(g => g.NoSeq == geofence.NoSeq))] = geofence;
         }
     });
 }
예제 #8
0
 /// <summary>
 /// Deletes the geofence.
 /// </summary>
 /// <param name="geofence">Fence.</param>
 public void DeleteGeofence(Geofence geofence)
 {
     Geofences.Remove(geofence);
     RefreshClosePositionGeofencesList();
     geofence.DeleteFlag = 1;
     Task.Run(async() =>
     {
         if (await SendGeofenceToServer(geofence))
         {
             var toDelete = await DatabaseHelper.Database.GetItem <Geofence>(geofence.NoSeq);
             await DatabaseHelper.Database.DeleteItemAsync <Geofence>(toDelete);
         }
         else
         {
             await DatabaseHelper.Database.UpdateItem(geofence);
         }
     });
 }
예제 #9
0
        public void ExecuteAction(string actionName, GeofenceEvent ev, Geofence geofence)
        {
            if (!string.IsNullOrEmpty(actionName))
            {
                Task.Run(() =>
                {
                    var pheidiParams = new Dictionary <string, string>();

                    pheidiParams.Add("GeofenceName", geofence.Name);
                    pheidiParams.Add("GeofenceEvent", ev.ToString());
                    pheidiParams.Add("GeofenceLatitude", geofence.Latitude.ToString());
                    pheidiParams.Add("GeofenceLongitude", geofence.Longitude.ToString());
                    pheidiParams.Add("GeofenceNoseq", geofence.NoSeq);

                    var action = new Action()
                    {
                        Name   = actionName,
                        Params = pheidiParams
                    };
                    ActionManager.ExecuteAction(action);
                });
            }
        }
예제 #10
0
        /// <summary>
        /// Creates the geofence at current location.
        /// </summary>
        /// <param name="geofence">Geofence.</param>
        public void CreateGeofenceAtCurrentLocation(Geofence geofence)
        {
            if (geofence != null)
            {
                geofence.Name = geofence.Name.Length > ApplicationConst.GeofenceNameMaxSize ? geofence.Name.Substring(0, ApplicationConst.GeofenceNameMaxSize) : geofence.Name;
                List <Geofence> overlappingList = GetOverlappingGeofences(geofence);

                if (overlappingList.Count > 0)
                {
                    string list = "";
                    foreach (var g in overlappingList)
                    {
                        list += g.Name + "\n";
                    }
                    string        Title     = AppResources.Alerte_PlusieurLieuxPositionNouvelleLocalisation_Title;
                    string        Message   = AppResources.Alerte_PlusieurLieuxPositionNouvelleLocalisation_Message;
                    string        confirm   = AppResources.Oui;
                    string        cancel    = AppResources.Non;
                    System.Action onConfirm = () =>
                    {
                        var geo = new Geofence()
                        {
                            Name      = geofence.Name,
                            Latitude  = geofence.Latitude,
                            Longitude = geofence.Longitude
                        };
                        App.Instance.PushPage(new GeofenceCreatePage(geo));
                    };
                    App.NotificationManager.DisplayAlert(Message, Title, confirm, cancel, onConfirm, () => { });
                }
                else
                {
                    App.Instance.PushPage(new GeofenceCreatePage(geofence));
                }
            }
        }
예제 #11
0
        public GeofenceEditPage(Geofence geofence)
        {
            data   = geofence;
            Title  = geofence.Name;
            layout = new StackLayout()
            {
                Spacing = 15
            };
            layout.Padding = new Thickness(20, 20);
            entryLayout    = new StackLayout()
            {
                Orientation = StackOrientation.Vertical, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            lblLayout = new StackLayout()
            {
                Orientation = StackOrientation.Vertical, VerticalOptions = LayoutOptions.FillAndExpand
            };
            namelbl = new Label()
            {
                Text = AppResources.NomLabel, VerticalTextAlignment = TextAlignment.Center
            };
            nameEntry = new Entry()
            {
                Placeholder = AppResources.NomPlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            nameEntry.TextChanged += (object sender, TextChangedEventArgs e) =>
            {
                if (nameEntry.Text.Length > 50)
                {
                    nameEntry.Text = nameEntry.Text.Substring(0, 50);
                }
                else if (geofence.Name != nameEntry.Text && nameEntry.Text.Length < 50)
                {
                    geofence.Name = nameEntry.Text;
                    Title         = geofence.Name;
                    didChange     = true;
                }
            };
            lblLayout.Children.Add(namelbl);
            entryLayout.Children.Add(nameEntry);
            latitudelbl = new Label()
            {
                Text = AppResources.LatitudeLabel, VerticalTextAlignment = TextAlignment.Center
            };
            latitudeEntry = new Entry()
            {
                Text = geofence.Latitude + "", Placeholder = AppResources.LatitudePlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            latitudeEntry.Unfocused += (sender, e) =>
            {
                double value = 0;
                if (double.TryParse(latitudeEntry.Text, out value) || string.IsNullOrEmpty(latitudeEntry.Text))
                {
                    if (geofence.Latitude > value || geofence.Latitude < value)
                    {
                        geofence.Latitude = value;
                        didChange         = true;
                    }
                }
            };
            lblLayout.Children.Add(latitudelbl);
            entryLayout.Children.Add(latitudeEntry);


            longitudelbl = new Label()
            {
                Text = AppResources.LongitudeLabel, VerticalTextAlignment = TextAlignment.Center
            };
            longitudeEntry = new Entry()
            {
                Text = geofence.Longitude + "", Placeholder = AppResources.LongitudePlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            longitudeEntry.Unfocused += (sender, e) =>
            {
                double value = 0;
                if (double.TryParse(longitudeEntry.Text, out value) || string.IsNullOrEmpty(longitudeEntry.Text))
                {
                    if (geofence.Longitude > value || geofence.Longitude < value)
                    {
                        geofence.Longitude = value;
                        didChange          = true;
                    }
                }
            };
            lblLayout.Children.Add(longitudelbl);
            entryLayout.Children.Add(longitudeEntry);

            radiuslbl = new Label()
            {
                Text = AppResources.RayonLabel, VerticalTextAlignment = TextAlignment.Center
            };
            radiusPicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            foreach (var rad in GeofenceManager.GeofenceRadius)
            {
                radiusPicker.Items.Add(rad.ToString());
            }
            radiusPicker.SelectedItem          = geofence.Radius.ToString();
            radiusPicker.SelectedIndexChanged += (sender, e) =>
            {
                double val = geofence.Radius;
                double.TryParse(radiusPicker.SelectedItem.ToString(), out val);
                val = val <= 0 ? geofence.Radius : val > ApplicationConst.GeofenceMaxRadius ? ApplicationConst.GeofenceMaxRadius : val;
                radiusPicker.SelectedItem = val.ToString();
                if (geofence.Radius > val || geofence.Radius < val)
                {
                    geofence.Radius = val;
                    didChange       = true;
                }
            };

            entryLayout.Children.Add(radiusPicker);
            lblLayout.Children.Add(radiuslbl);
            formLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal
            };
            formLayout.Children.Add(lblLayout);
            formLayout.Children.Add(entryLayout);

            typeLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal
            };
            typeEnterLayout = new StackLayout();
            typeEnterLayout.Children.Add(new Label()
            {
                Text = AppResources.ActionEntreeLabel, VerticalTextAlignment = TextAlignment.Center
            });

            EnterTypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var EnterSoustypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            foreach (var t in ActionManager.GetActionTypes())
            {
                EnterTypePicker.Items.Add(t);
            }

            EnterTypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = EnterTypePicker.Items[EnterTypePicker.SelectedIndex];

                EnterSoustypePicker.Items.Clear();
                foreach (var t in ActionManager.GetActionList().Where((Action a) => a.Category == value))
                {
                    EnterSoustypePicker.Items.Add(t.Description);
                }
                EnterSoustypePicker.IsEnabled = EnterSoustypePicker.Items.Count != 0;
                if (EnterSoustypePicker.IsEnabled)
                {
                    if (!string.IsNullOrEmpty(geofence.EnterActionName))
                    {
                        var list = ActionManager.GetActionList();
                        EnterSoustypePicker.SelectedIndex = EnterSoustypePicker.Items.IndexOf(list.First(a => a.Name == geofence.EnterActionName).Description);
                    }
                    else
                    {
                        EnterSoustypePicker.SelectedIndex = 0;
                    }
                }
            };


            EnterSoustypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = EnterSoustypePicker.SelectedIndex >= 0 ? EnterSoustypePicker.Items[EnterSoustypePicker.SelectedIndex] : "";
                if (value != "")
                {
                    var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                    if (geofence.EnterActionName != action.Name)
                    {
                        geofence.EnterActionName = action.Name;
                        didChange = true;
                    }
                }
                else
                {
                    geofence.EnterActionName = "";
                }
            };

            typeExitLayout = new StackLayout();
            typeExitLayout.Children.Add(new Label()
            {
                Text = AppResources.ActionSortieLabel, VerticalTextAlignment = TextAlignment.Center
            });

            var ExitTypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var ExitSoustypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            foreach (var t in ActionManager.GetActionTypes())
            {
                ExitTypePicker.Items.Add(t);
            }
            ExitTypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = ExitTypePicker.Items[ExitTypePicker.SelectedIndex];
                ExitSoustypePicker.Items.Clear();

                foreach (var t in ActionManager.GetActionList().Where(a => a.Category == value))
                {
                    ExitSoustypePicker.Items.Add(t.Description);
                }
                ExitSoustypePicker.IsEnabled = ExitSoustypePicker.Items.Count != 0;
                if (ExitSoustypePicker.IsEnabled)
                {
                    if (!string.IsNullOrEmpty(geofence.ExitActionName))
                    {
                        ExitSoustypePicker.SelectedIndex = ExitSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.ExitActionName).Description);
                    }
                    else
                    {
                        ExitSoustypePicker.SelectedIndex = 0;
                    }
                }
            };


            ExitSoustypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = ExitSoustypePicker.SelectedIndex >= 0 ? ExitSoustypePicker.Items[ExitSoustypePicker.SelectedIndex] : "";
                if (value != "")
                {
                    var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                    if (geofence.ExitActionName != action.Name)
                    {
                        geofence.ExitActionName = action.Name;
                        didChange = true;
                    }
                }
                else
                {
                    geofence.ExitActionName = "";
                }
            };

            string category = ActionManager.Null;

            if (!string.IsNullOrEmpty(geofence.EnterActionName))
            {
                if (ActionManager.GetActionList().Any(a => geofence.EnterActionName == a.Name))
                {
                    var enterAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = enterAction.Category;
                }
                else
                {
                    geofence.EnterActionName = string.Empty;
                    category = ActionManager.Null;
                }
            }
            EnterTypePicker.SelectedItem = category;


            category = ActionManager.Null;

            if (!string.IsNullOrEmpty(geofence.ExitActionName))
            {
                var list = (ActionManager.GetActionList());
                if (list.Any(a => geofence.ExitActionName == a.Name))
                {
                    var exitAction = list.First(a => geofence.ExitActionName == a.Name);
                    category = exitAction.Category;
                }
                else
                {
                    geofence.ExitActionName = string.Empty;
                    category = ActionManager.Null;
                }
            }
            ExitTypePicker.SelectedItem = category;

            typeEnterLayout.Children.Add(EnterTypePicker);
            typeEnterLayout.Children.Add(EnterSoustypePicker);

            typeExitLayout.Children.Add(ExitTypePicker);
            typeExitLayout.Children.Add(ExitSoustypePicker);

            typeLayout.Children.Add(typeEnterLayout);
            typeLayout.Children.Add(typeExitLayout);
            delaylbl = new Label()
            {
                Text = AppResources.DelaiNotificationLabel, VerticalTextAlignment = TextAlignment.Center
            };
            delayLayout = new StackLayout();
            delayLayout.Children.Add(delaylbl);

            delayPicker = new HMSTimePicker {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            delayPicker.Time       = new TimeSpan(0, 0, (int)geofence.NotificationDelay);
            delayPicker.Unfocused += (sender, e) =>
            {
                var time = (uint)delayPicker.Time.TotalSeconds;
                if (time > uint.MaxValue)
                {
                    time = uint.MaxValue;
                }
                if (time != geofence.NotificationDelay)
                {
                    geofence.NotificationDelay = time;
                    didChange = true;
                }
            };
            delayLayout.Children.Add(delayPicker);



            map = new Button()
            {
                WidthRequest = App.Width / 2, Text = AppResources.CarteButton
            };
            map.Clicked += (sender, e) =>
            {
                var loc = new Location()
                {
                    Latitude = geofence.Latitude, Longitude = geofence.Longitude
                };
                App.Instance.PushPage(new MapPage(loc));
            };

            layout.Children.Add(formLayout);
            layout.Children.Add(map);
            layout.Children.Add(typeLayout);
            layout.Children.Add(delayLayout);


            if (geofence.PublicFlag == 0)
            {
                accesSwitch = new ChoiceSwitch()
                {
                    LeftOption = AppResources.AccesGeofencePrivate, RightOption = AppResources.AccesGeofencePublic
                };

                accesSwitch.SelectedIndexChanged += (sender, e) =>
                {
                    geofence.PublicFlag = accesSwitch.SelectedIndex;
                };
                layout.Children.Add(accesSwitch);
            }
            ScrollView scrollview = new ScrollView();

            scrollview.Content = layout;
            Content            = scrollview;
            didChange          = false;
        }
        /// <summary>
        /// Initialize this instance.
        /// </summary>
        private void Initialize()
        {
            if (geofence == null)
            {
                var data = App.GeofenceManager.GetPendingGeofenceRequest();
                geofence = data ?? new Geofence()
                {
                    Latitude = 0, Longitude = 0
                };
            }

            InitializeComponent();
            switchNotification.IsToggled = true;
            labelLongitude.Text          = AppResources.LongitudeLabel;
            entryLongitude.Placeholder   = AppResources.LongitudePlaceHolder;
            labelLatitude.Text           = AppResources.LatitudeLabel;
            entryLatitude.Placeholder    = AppResources.LatitudePlaceHolder;
            btnMap.Text            = AppResources.CarteButton;
            nameEntry.Placeholder  = AppResources.NomLieuPlaceHolder;
            notificationLabel.Text = AppResources.ActiverNotificationLabel;
            enterActionLabel.Text  = AppResources.ActionEntreeLabel;
            exitActionLabel.Text   = AppResources.ActionSortieLabel;
            btnSave.Text           = AppResources.EnregistrerBouton;
            btnCancel.Text         = AppResources.AnnulerBouton;


            if (geofence != null)
            {
                entryLatitude.Text  = geofence.Latitude.ToString();
                entryLongitude.Text = geofence.Longitude.ToString();

                btnMap.Clicked += (sender, e) =>
                {
                    var loc = new Location()
                    {
                        Latitude = geofence.Latitude, Longitude = geofence.Longitude
                    };
                    var map = new MapPage(loc);
                    Navigation.PushAsync(map);
                };


                btnSave.Clicked += (sender, e) =>
                {
                    if (!string.IsNullOrEmpty(nameEntry.Text))
                    {
                        geofence.Name = nameEntry.Text;
                        geofence.NotificationEnabled = switchNotification.IsToggled;
                        geofence.Radius      = ApplicationConst.DefaultGeofenceRadius;
                        geofence.User        = App.UserNoseq;
                        geofence.ServerNoseq = App.ServerInfoList.First((arg) => App.ServerInfoNoseq == arg.Noseq).Domain;
                        App.GeofenceManager.AddGeofence(geofence);
                        Navigation.PopAsync();
                    }
                    else
                    {
                        nameEntry.PlaceholderColor = Color.Red;
                    }
                };

                btnCancel.Clicked += (sender, e) =>
                {
                    Navigation.PopAsync();
                };

                nameEntry.Focused += (sender, e) =>
                {
                    nameEntry.PlaceholderColor = Color.Gray;
                };

                nameEntry.Unfocused += (sender, e) =>
                {
                    if (nameEntry.Text.Length > ApplicationConst.GeofenceNameMaxSize)
                    {
                        nameEntry.Text = nameEntry.Text.Substring(0, ApplicationConst.GeofenceNameMaxSize);
                    }
                    else if (string.IsNullOrEmpty(nameEntry.Text))
                    {
                        nameEntry.PlaceholderColor = Color.Red;
                    }
                };
                entryLatitude.Unfocused += (sender, e) =>
                {
                    double lat = 0;
                    if (double.TryParse(entryLatitude.Text, out lat))
                    {
                        geofence.Latitude = lat;
                    }
                    else
                    {
                        entryLatitude.Text = geofence.Latitude.ToString();
                    }
                };

                entryLongitude.Unfocused += (sender, e) =>
                {
                    double lon = 0;
                    if (double.TryParse(entryLongitude.Text, out lon))
                    {
                        geofence.Longitude = lon;
                    }
                    else
                    {
                        entryLongitude.Text = geofence.Longitude.ToString();
                    }
                };



                //Type Picker
                foreach (var t in ActionManager.GetActionTypes())
                {
                    EnterTypePicker.Items.Add(t);
                }

                EnterTypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = EnterTypePicker.Items[EnterTypePicker.SelectedIndex];

                    EnterSoustypePicker.Items.Clear();
                    foreach (var t in ActionManager.GetActionList().Where((Action a) => a.Category == value))
                    {
                        EnterSoustypePicker.Items.Add(t.Description);
                    }
                    EnterSoustypePicker.IsEnabled = EnterSoustypePicker.Items.Count != 0;
                    if (EnterSoustypePicker.IsEnabled)
                    {
                        if (!string.IsNullOrEmpty(geofence.EnterActionName))
                        {
                            EnterSoustypePicker.SelectedIndex = EnterSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.EnterActionName).Description);
                        }
                        else
                        {
                            EnterSoustypePicker.SelectedIndex = 0;
                        }
                    }
                };


                EnterSoustypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = EnterSoustypePicker.SelectedIndex >= 0 ? EnterSoustypePicker.Items[EnterSoustypePicker.SelectedIndex] : "";
                    if (value != "")
                    {
                        var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                        geofence.EnterActionName = action.Name;
                    }
                    else
                    {
                        geofence.EnterActionName = "";
                    }
                };

                foreach (var t in ActionManager.GetActionTypes())
                {
                    ExitTypePicker.Items.Add(t);
                }
                ExitTypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = ExitTypePicker.Items[ExitTypePicker.SelectedIndex];
                    ExitSoustypePicker.Items.Clear();

                    foreach (var t in ActionManager.GetActionList().Where(a => a.Category == value))
                    {
                        ExitSoustypePicker.Items.Add(t.Description);
                    }
                    ExitSoustypePicker.IsEnabled = ExitSoustypePicker.Items.Count != 0;
                    if (ExitSoustypePicker.IsEnabled)
                    {
                        if (!string.IsNullOrEmpty(geofence.ExitActionName))
                        {
                            ExitSoustypePicker.SelectedIndex = ExitSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.ExitActionName).Description);
                        }
                        else
                        {
                            ExitSoustypePicker.SelectedIndex = 0;
                        }
                    }
                };


                ExitSoustypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = ExitSoustypePicker.SelectedIndex >= 0 ? ExitSoustypePicker.Items[ExitSoustypePicker.SelectedIndex] : "";
                    if (value != "")
                    {
                        var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                        geofence.ExitActionName = action.Name;
                    }
                    else
                    {
                        geofence.ExitActionName = "";
                    }
                };

                string category = ActionManager.Null;

                if (!string.IsNullOrEmpty(geofence.EnterActionName))
                {
                    var enterAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = enterAction.Category;
                }
                EnterTypePicker.SelectedItem = category;

                category = ActionManager.Null;

                if (!string.IsNullOrEmpty(geofence.ExitActionName))
                {
                    var exitAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = exitAction.Category;
                }
                ExitTypePicker.SelectedItem = category;


                delayLabel = new Label()
                {
                    Text = AppResources.DelaiNotificationLabel, VerticalTextAlignment = TextAlignment.Center
                };
                delayLayout.Children.Add(delayLabel);
                delayPicker = new HMSTimePicker {
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                try
                {
                    delayPicker.Time = new TimeSpan(0, ApplicationConst.DefaultGeofenceTriggerTime / 60, ApplicationConst.DefaultGeofenceTriggerTime % 60);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
                delayPicker.Unfocused += (sender, e) =>
                {
                    var time = (uint)delayPicker.Time.TotalSeconds;
                    if (time > uint.MaxValue)
                    {
                        time = uint.MaxValue;
                    }
                    if (time != geofence.NotificationDelay)
                    {
                        geofence.NotificationDelay = time;
                    }
                };
                delayLayout.Children.Add(delayPicker);

                labelRadius.Text = AppResources.RayonLabel;
                foreach (var rad in GeofenceManager.GeofenceRadius)
                {
                    radiusPicker.Items.Add(rad.ToString());
                }
                radiusPicker.SelectedIndexChanged += (sender, e) => CheckEntryRadius();
                radiusPicker.SelectedItem          = ApplicationConst.DefaultGeofenceRadius.ToString();

                ChoiceSwitch accesSwitch = new ChoiceSwitch()
                {
                    LeftOption = AppResources.AccesGeofencePrivate, RightOption = AppResources.AccesGeofencePublic
                };

                accesSwitch.SelectedIndexChanged += (sender, e) =>
                {
                    geofence.PublicFlag = accesSwitch.SelectedIndex;
                };
                AccesLayout.Children.Add(accesSwitch);
            }
        }
예제 #13
0
        /// <summary>
        /// Gets the geofence from server.
        /// </summary>
        /// <returns><c>true</c>, if geofence from server was gotten, <c>false</c> otherwise.</returns>
        public async Task <bool> GetGeofenceUpdateFromServer()
        {
            if (!Application.Current.Properties.ContainsKey("LastGeofenceSync"))
            {
                Application.Current.Properties["LastGeofenceSync"] = "2000-01-01 00:00:00";
            }
            var parameters = new Dictionary <string, string> {
                { "pheidiaction", "complexaction" }, { "pheidiparams", "action**:**GetGeofenceUpdate**,**Last_Update_Date**:**" + Application.Current.Properties["LastGeofenceSync"] + "**,**" }
            };
            HttpResponseMessage response = PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30)).Result;

            if (response != null)
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string responseContent = response.Content.ReadAsStringAsync().Result;
                    Debug.WriteLine("Reponse:" + responseContent);
                    try
                    {
                        if (Geofences == null)
                        {
                            GetGeofenceFromDatabase();
                        }
                        List <Geofence> list = new List <Geofence>();
                        var             geo  = new Geofence();
                        try
                        {
                            var fields = PheidiNetworkManager.GetFields(responseContent);
                            foreach (var field in fields)
                            {
                                geo = new Geofence();

                                geo.NoSeq = field.ContainsKey("GEO_A_NoSeq") ? field["GEO_A_NoSeq"]?.ToString() : string.Empty;

                                geo.EnterActionName = field.ContainsKey("GAR_ACO_A_Action_EnterAction") ? field["GAR_ACO_A_Action_EnterAction"]?.ToString() : string.Empty;

                                geo.ExitActionName = field.ContainsKey("GAR_ACO_A_Action_ExitAction") ? field["GAR_ACO_A_Action_ExitAction"]?.ToString() : string.Empty;

                                geo.Name = field.ContainsKey("GEO_A_Name") ? field["GEO_A_Name"]?.ToString() : string.Empty;

                                geo.DeleteFlag = field.ContainsKey("GEO_B_DeleteFlag") ? (bool.Parse(field["GEO_B_DeleteFlag"]?.ToString() ?? false.ToString()) ? 1 : 0) : 0;

                                geo.CreationDate = field.ContainsKey("GEO_S_CrDate") ? DateTime.Parse(field["GEO_S_CrDate"]?.ToString()) : DateTime.Now;

                                geo.LastModification = field.ContainsKey("GEO_S_LastModDate") ? DateTime.Parse(field["GEO_S_LastModDate"]?.ToString()) : DateTime.Now;

                                geo.Latitude = field.ContainsKey("GEO_N_Latitude") ? double.Parse(field["GEO_N_Latitude"]?.ToString()) : 0;

                                geo.Longitude = field.ContainsKey("GEO_N_Longitude") ? double.Parse(field["GEO_N_Longitude"]?.ToString()) : 0;

                                geo.NotificationEnabled = field.ContainsKey("GAR_B_NotificationFlag") ? bool.Parse(field["GAR_B_NotificationFlag"]?.ToString() ?? false.ToString()) : true;

                                geo.PublicFlag = field.ContainsKey("GEO_B_PublicFlag") ? bool.Parse(field["GEO_B_PublicFlag"]?.ToString() ?? false.ToString()) ? 1 : 0 : 0;

                                geo.Radius = field.ContainsKey("GEO_N_Radius") ? double.Parse(field["GEO_N_Radius"]?.ToString()) : ApplicationConst.DefaultGeofenceRadius;

                                geo.ServerNoseq = App.ServerInfoNoseq;

                                geo.User = App.UserNoseq;

                                list.Add(geo);
                            }
                            Debug.WriteLine("List Generated");
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine("GetGeofenceUpdate - Creating List: " + e.Message);
                        }
                        List <Geofence> toRemove = new List <Geofence>();
                        foreach (var geofence in list)
                        {
                            try
                            {
                                if (Geofences.Any(g => g.NoSeq == geofence.NoSeq))
                                {
                                    //Delete la copie local si celle au serveur à déjà été supprimée.
                                    if (geofence.DeleteFlag == 1)
                                    {
                                        var data = await DatabaseHelper.Database.GetItem <Geofence>(geofence.NoSeq);

                                        await DatabaseHelper.Database.DeleteItemAsync <Geofence>(data);

                                        toRemove.Add(geofence);
                                    }
                                    //Update la copie local pour correspondre à celle du serveur.
                                    else
                                    {
                                        var data = Geofences.First(g => g.NoSeq == geofence.NoSeq);
                                        data.DeleteFlag          = geofence.DeleteFlag;
                                        data.Latitude            = geofence.Latitude;
                                        data.Longitude           = geofence.Longitude;
                                        data.Name                = geofence.Name;
                                        data.Radius              = geofence.Radius;
                                        data.LastModification    = DateTime.Now;
                                        data.EnterActionName     = geofence.EnterActionName;
                                        data.ExitActionName      = geofence.ExitActionName;
                                        data.NotificationEnabled = geofence.NotificationEnabled;
                                        await DatabaseHelper.Database.UpdateItem(data);
                                    }
                                }
                                else
                                {
                                    //Ajoute une nouvelle geofence si la copie locale n'existe pas.
                                    if (geofence.DeleteFlag == 0)
                                    {
                                        await DatabaseHelper.Database.SaveItemAsync(geofence);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine("GetGeofenceUpdate - Updating geofence: " + e.Message);
                            }
                        }
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            foreach (var geofence in toRemove)
                            {
                                try
                                {
                                    Geofences.Remove(Geofences.FirstOrDefault((arg) => arg.NoSeq == geofence.NoSeq));
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("GetGeofenceUpdate - Removing deleted: " + e.Message);
                                }
                            }
                        });

                        Application.Current.Properties["LastGeofenceSync"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        Debug.WriteLine("Geofence: Synch done");
                        await Application.Current.SavePropertiesAsync();

                        return(true);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("GetGeofenceUpdate: " + e.Message);
                    }
                }
            }
            return(false);
        }
예제 #14
0
 /// <summary>
 /// Gets the overlapping geofences.
 /// </summary>
 /// <returns>The overlapping geofences.</returns>
 /// <param name="geofence">Geofence.</param>
 public List <Geofence> GetOverlappingGeofences(Geofence geofence)
 {
     return(GetOverlappingGeofences(geofence.Latitude, geofence.Longitude));
 }
예제 #15
0
        /// <summary>
        /// Creates or selects a geofence at the current location.
        /// </summary>
        /// <param name="geofence">Geofence.</param>
        /// <param name="TypeSpecific">If set to <c>true</c> type specific.</param>
        public string CreateOrSelectGeofenceAtCurrentLocation(Geofence geofence)
        {
            string noseq     = "";
            bool   selecting = true;

            if (geofence != null)
            {
                geofence.Name = geofence.Name.Length > ApplicationConst.GeofenceNameMaxSize ? geofence.Name.Substring(0, ApplicationConst.GeofenceNameMaxSize) : geofence.Name;
                List <Geofence> overlappingList = GetOverlappingGeofences(geofence);

                ObservableCollection <Geofence> list = new ObservableCollection <Geofence>();
                foreach (var g in overlappingList)
                {
                    list.Add(g);
                }
                var p = new ListPickingPage();
                p.Title  = "Localisation";
                p.Header = "Choisissez le lieux correpondant à votre position:";

                var geofenceCell = new DataTemplate(typeof(BasicTextCellView));
                geofenceCell.SetBinding(BasicTextCellView.TextProperty, "Name");
                p.SetData(list, geofenceCell);


                p.AddOnButtonAddEvent((sender, e) =>
                {
                    Device.BeginInvokeOnMainThread(() => App.Instance.PushPage(new GeofenceCreatePage(geofence)));
                });
                p.AddOnSelectItemEvent((sender, e) =>
                {
                    noseq = ((Geofence)p.GetSelectedItem()).NoSeq;
                });
                p.AddOnButtonConfirmationEvent((sender, e) =>
                {
                    selecting = false;
                });
                p.AddOnButtonCancelEvent((sender, e) =>
                {
                    noseq     = "";
                    selecting = false;
                });
                p.Appearing += (sender, e) =>
                {
                    overlappingList = GetOverlappingGeofences(geofence);
                    foreach (var g in overlappingList)
                    {
                        if (!list.Any((arg) => arg.NoSeq == g.NoSeq))
                        {
                            list.Add(g);
                        }
                    }
                };
                if (Device.RuntimePlatform == Device.Android)
                {
                    Device.BeginInvokeOnMainThread(() => App.Instance.PushPage(p));
                    while (selecting)
                    {
                        Task.Delay(100).Wait();
                    }
                }
                else
                {
                    Device.BeginInvokeOnMainThread(() => App.Instance.PushPage(p));

                    while (selecting)
                    {
                        Task.Delay(100).Wait();
                    }
                }
            }
            return(noseq);
        }