Beispiel #1
0
        public string ExecuteAction(string pheidiparams, string objectAction)
        {
            PheidiParams pp = new PheidiParams();

            pp.Load(pheidiparams);

            if (pp.ContainsKey("FILECHOOSER"))
            {
                PheidiWebChromeClient.FileChooserPheidiParams = pp;
            }
            else if (objectAction == "geofenceAutoCreate")
            {
                try
                {
                    string answer = "";
                    string p      = "";
                    var    dic    = new Dictionary <string, string>();
                    dic.Add("OTHERFIELD", pp["FIELD"]);
                    dic.Add("NOSEQ", pp["NOSEQ"]);
                    dic.Add("FIELD", "geofence");

                    foreach (var d in dic)
                    {
                        p += d.Key + "**:**" + d.Value + "**,**";
                    }
                    var parameters = new Dictionary <string, string> {
                        { "pheidiaction", "GetFieldValueFromOtherField" }, { "pheidiparams", p }
                    };
                    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;
                            System.Diagnostics.Debug.WriteLine("Reponse:" + responseContent);
                            try
                            {
                                answer = PheidiNetworkManager.GetFields(responseContent)[0][dic["FIELD"]] as string;
                            }
                            catch (Exception e)
                            {
                                System.Diagnostics.Debug.WriteLine(e.Message);
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(answer))
                    {
                        string val = "";
                        if (pp.ContainsKey("VALUE"))
                        {
                            val = pp["VALUE"];
                        }
                        bool   EndOfProcess = false;
                        string message      = "Voulez-vous associez \"" + val + "\" comme étant le lieux actuel?";
                        var    a            = new System.Action(() =>
                        {
                            try
                            {
                                var location      = App.LocationService.GetLocation();
                                string noseq      = "";
                                bool createNewGeo = true;
                                if (location != null)
                                {
                                    var potentialGeofences = App.GeofenceManager.GetOverlappingGeofences(location.Latitude, location.Longitude);
                                    if (potentialGeofences.Any(g => g.Name.ToLower() == val.ToLower()))
                                    {
                                        var geo      = potentialGeofences.First(g => g.Name.ToLower() == val.ToLower());
                                        noseq        = geo.NoSeq;
                                        createNewGeo = false;
                                    }
                                }
                                if (createNewGeo)
                                {
                                    var geo = new Geofence()
                                    {
                                        Latitude            = location.Latitude,
                                        Longitude           = location.Longitude,
                                        NotificationEnabled = true,
                                        User              = App.UserNoseq,
                                        ServerNoseq       = App.CurrentServer.Noseq,
                                        NotificationDelay = ApplicationConst.DefaultGeofenceTriggerTime,
                                        Name              = val,
                                        Radius            = ApplicationConst.DefaultGeofenceRadius
                                    };
                                    geo.SetIsInside(true);
                                    if (pp.ContainsKey("ENTERACTIONNAME"))
                                    {
                                        geo.EnterActionName = pp["ENTERACTIONNAME"];
                                    }
                                    if (pp.ContainsKey("EXITACTIONNOSEQ"))
                                    {
                                        geo.ExitActionName = pp["EXITACTIONNAME"];
                                    }
                                    geo.Radius = ApplicationConst.DefaultGeofenceRadius;
                                    App.GeofenceManager.AddGeofence(geo);
                                    noseq = geo.NoSeq;
                                }
                                pheidiparams = PheidiParams.InsertValueInString(pheidiparams, "IPheidi_Params", noseq);
                            }
                            catch (Exception e)
                            {
                                System.Diagnostics.Debug.WriteLine(e.Message);
                            }
                            finally
                            {
                                EndOfProcess = true;
                            }
                        });

                        App.NotificationManager.DisplayAlert(message, "", "Oui", "Non", a, () => { EndOfProcess = true; });

                        while (!EndOfProcess)
                        {
                            Task.Delay(500);
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
            return(pheidiparams);
        }
        public static new NSUrlRequest GetCanonicalRequest(NSUrlRequest request)
        {
            string data = "";

            if (request.Body != null)
            {
                string[] param = request.Body.ToString().Split('&');

                if (request.Url.ToString().Contains("localisation"))
                {
                    for (int i = 0; i < param.Length; i++)
                    {
                        if (param[i].Contains("pheidiparams"))
                        {
                            var    location = App.LocationService.GetLocation();
                            string values   = "";
                            string str      = "";
                            if (location != null)
                            {
                                values = "Longitude%2A%2A%3A%2A%2A" + location.Longitude + "%2A%2A%2C%2A%2ALatitude%2A%2A%3A%2A%2A" + location.Latitude;
                            }
                            else
                            {
                                values = "Longitude%2A%2A%3A%2A%2Anull%2A%2A%2C%2A%2ALatitude%2A%2A%3A%2A%2Anull";
                            }
                            string[] keyValue = param[i].Split('=');
                            str      = keyValue[1] == "null" ? keyValue[0] + "=" + values : param[i] + values;
                            param[i] = str;
                        }
                    }
                    foreach (var str in param)
                    {
                        data += data.Length > 0 ? "&" + str : str;
                    }
                    return(GenerateRequest(request, data));
                }
                else if (request.Url.ToString().Contains("geofenceAutoCreate"))
                {
                    param = request.Body.ToString().Split('&');
                    PheidiParams pp    = new PheidiParams();
                    int          index = 0;
                    for (int i = 0; i < param.Length; i++)
                    {
                        if (param[i].Contains("pheidiparams"))
                        {
                            pp.Load(WebUtility.UrlDecode(param[i]));
                            index = i;
                        }
                    }
                    if (!CurrentlyProcessingRequestLocaly)
                    {
                        CurrentlyProcessingRequestLocaly = true;
                        try
                        {
                            Task.Run(async() =>
                            {
                                string answer = "";
                                string p      = "";
                                var dic       = new Dictionary <string, string>();
                                dic.Add("OTHERFIELD", pp["FIELD"]);
                                dic.Add("NOSEQ", pp["NOSEQ"]);
                                dic.Add("FIELD", "geofence");

                                foreach (var d in dic)
                                {
                                    p += d.Key + "**:**" + d.Value + "**,**";
                                }
                                var parameters = new Dictionary <string, string> {
                                    { "pheidiaction", "GetFieldValueFromOtherField" }, { "pheidiparams", p }
                                };
                                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);
                                        try
                                        {
                                            answer = PheidiNetworkManager.GetFields(responseContent)[0][dic["FIELD"]] as string;
                                        }
                                        catch (Exception e)
                                        {
                                            Debug.WriteLine(e.Message);
                                        }
                                    }
                                }
                                if (string.IsNullOrEmpty(answer))
                                {
                                    string val = "";
                                    if (pp.ContainsKey("VALUE"))
                                    {
                                        val = pp["VALUE"];
                                    }
                                    string message = string.Format(AppResources.Alerte_VoulezVousAssociezXCommelieuMessage, val);
                                    var a          = new System.Action(() =>
                                    {
                                        try
                                        {
                                            var location        = App.LocationService.GetLocation();
                                            string noseq        = "";
                                            string pheidiParams = "";
                                            data = "";
                                            bool createNewGeo = true;
                                            if (location != null)
                                            {
                                                var potentialGeofences = App.GeofenceManager.GetOverlappingGeofences(location.Latitude, location.Longitude);
                                                if (potentialGeofences.Any(g => g.Name.ToLower() == val.ToLower()))
                                                {
                                                    var geo      = potentialGeofences.First(g => g.Name.ToLower() == val.ToLower());
                                                    noseq        = geo.NoSeq;
                                                    createNewGeo = false;
                                                }
                                            }
                                            if (createNewGeo)
                                            {
                                                var geo = new Geofence()
                                                {
                                                    Latitude            = location.Latitude,
                                                    Longitude           = location.Longitude,
                                                    NotificationEnabled = true,
                                                    User              = App.UserNoseq,
                                                    ServerNoseq       = App.CurrentServer.Domain,
                                                    NotificationDelay = ApplicationConst.DefaultGeofenceTriggerTime,
                                                    Radius            = ApplicationConst.DefaultGeofenceRadius,
                                                    Name              = val
                                                };
                                                geo.SetIsInside(true);
                                                if (pp.ContainsKey("ENTERACTIONNAME"))
                                                {
                                                    geo.EnterActionName = pp["ENTERACTIONNAME"];
                                                }
                                                if (pp.ContainsKey("EXITACTIONNAME"))
                                                {
                                                    geo.ExitActionName = pp["EXITACTIONNAME"];
                                                }
                                                geo.Radius = ApplicationConst.DefaultGeofenceRadius;
                                                App.GeofenceManager.AddGeofence(geo);
                                                noseq = geo.NoSeq;
                                            }
                                            pheidiParams = PheidiParams.InsertValueInString(param[index], "IPheidi_Params", noseq);

                                            for (int i = 0; i < param.Length; i++)
                                            {
                                                string str = i != index ? param[i] : pheidiParams;
                                                data      += data.Length > 0 ? "&" + str : str;
                                            }
                                            Debug.WriteLine(data);
                                            SendRequest(GenerateRequest(request, data));
                                            Task.Delay(100);
                                        }
                                        catch (Exception e)
                                        {
                                            Debug.WriteLine(e.Message);
                                        }
                                        finally
                                        {
                                            CurrentlyProcessingRequestLocaly = false;
                                        }
                                    });

                                    App.NotificationManager.DisplayAlert(message, "", AppResources.Oui, AppResources.Non, a, () => { CurrentlyProcessingRequestLocaly = false; });
                                }
                                else
                                {
                                    CurrentlyProcessingRequestLocaly = false;
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.Message);
                            CurrentlyProcessingRequestLocaly = false;
                        }

                        return(null);
                    }
                }
            }
            return(request);
        }