/// <summary> /// Envoie une requête http au serveur contenant les données de localisation en format json. /// </summary> /// <param name="json">Json string</param> public static async Task <bool> SendJsonData(string json) { var handler = new HttpClientHandler() { CookieContainer = App.CookieManager.GetAllCookies() }; using (var httpClient = new HttpClient(handler, true)) { var parameters = new Dictionary <string, string> { { "pheidiaction", "sendLocationData" }, { "pheidiparams", "value**:**" + json + "**,**" } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string responseContent = await response.Content.ReadAsStringAsync(); Debug.WriteLine("Reponse:" + responseContent); return("Good" == responseContent); } } Debug.WriteLine("Problème de connexion au serveur, veuillez réessayer plus tard"); return(false); } }
static public async Task <string> SendNewLanguage() { string p = ""; var dic = new Dictionary <string, string>(); dic.Add("VALUE", App.Language); foreach (var d in dic) { p += d.Key + "**:**" + d.Value + "**,**"; } var parameters = new Dictionary <string, string> { { "pheidiaction", "SET_CON_A_LANGUAGE" }, { "pheidiparams", p } }; var response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 240)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string responseContent = response.Content.ReadAsStringAsync().Result; Debug.WriteLine("Reponse:" + responseContent); } return(GoodResult); } return(BadResult); }
static public async Task <string> SetPMH(string noseq) { var parameters = new Dictionary <string, string> { { "pheidiaction", "complexAction" }, { "pheidiparams", "action**:**setPMH**,**value**:**" + noseq + "**,**" } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 10)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string content = await response.Content.ReadAsStringAsync(); Debug.WriteLine(content); string setLanguageResult = ""; int setLanguageTriesCount = 0; while (setLanguageResult != GoodResult && setLanguageTriesCount < 10) { setLanguageResult = await SendNewLanguage(); setLanguageTriesCount++; } return(GoodResult); } } return(BadResult); }
/// <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); }
/// <summary> ///Méthode qui envoie la requête http permettant de se connecter à partir de l'application mobile. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="rememberUser"></param> static public async Task <string> UserLogin(string username, string password, bool rememberUser) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || App.CurrentServer == null) { return(AppResources.Erreur_LaissezAucunChampVide); } var parameters = new Dictionary <string, string> { { "pheidiaction", "complexAction" }, { "pheidiparams", "action**:**getWebSession**,**Username**:**" + username + "**,**Password**:**" + password + "**,**" } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 10)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string rc = await response.Content.ReadAsStringAsync(); Debug.WriteLine("WEBSESSION: " + rc); App.WebSession = new Cookie() { Name = "WEBSESSION", Domain = App.CurrentServer.Domain, Value = rc }; var userAgent = App.AppName + " " + Xamarin.Forms.Device.RuntimePlatform; var uaCookie = new Cookie() { Name = "IPHEIDI_USERAGENT", Domain = App.CurrentServer.Domain, Value = userAgent }; if (!string.IsNullOrEmpty(rc) && Utilities.IsNumeric(rc)) { Debug.WriteLine(rc); if (rememberUser && !App.DeviceIsShared) { App.UserNoseq = App.CredentialsManager.SaveCredentials(username, password, App.SystemCredentials.Key); } App.CookieContainer.GetCookies(new Uri(App.CurrentServer.Address)); //Ajoute le cookie de WEBSESSION et envoie vers la page web. App.CookieManager.AddCookie(App.WebSession); App.CookieManager.AddCookie(uaCookie); string setLanguageResult = ""; int setLanguageTriesCount = 0; while (setLanguageResult != GoodResult && setLanguageTriesCount < 10) { setLanguageResult = await SendNewLanguage(); setLanguageTriesCount++; } App.IsInLogin = false; return(GoodResult); } return(AppResources.Erreur_MauvaisEmailOuMdp); } } return(AppResources.Erreur_ProblemeConnexionServeur); }
static async Task <List <Action> > GetActions() { return(await Task.Run(async() => { var list = new List <Action>(); var parameters = new Dictionary <string, string> { { "pheidiaction", "complexAction" }, { "pheidiparams", "action**:**GetIpheidiActions**,**" } }; 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 { List <Dictionary <string, object> > fields = PheidiNetworkManager.GetFields(responseContent); foreach (var f in fields) { var action = new Action() { NoSeq = f["ACO_A_NoSeq"].ToString(), Name = f["ACO_A_Action"].ToString(), Category = f["Category"].ToString(), Description = f["SousCategory"].ToString() }; list.Add(action); } list.OrderBy((arg) => arg.Category).ThenBy((arg) => arg.Description); } catch { var answer = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseContent); if (answer["STATUS"] == "Good") { if (answer.ContainsKey("VALUE")) { try { list = JsonConvert.DeserializeObject <List <Action> >(answer["VALUE"]); Debug.WriteLine("List Generated"); } catch (Exception e) { Debug.WriteLine(e.Message); } } } } } } return list; })); }
public static void ExecuteAction(Action action) { Task.Run(async() => { if (!action.Params.ContainsKey("action")) { action.Params.Add("action", action.Name); } action.Params["action"] = action.Name; if (!action.Params.ContainsKey("language")) { action.Params.Add("language", App.Language); } action.Params["language"] = App.Language; string param = ""; foreach (var data in action.Params) { param += data.Key + "**:**" + data.Value + "**,**"; } var parameters = new Dictionary <string, string> { { "pheidiaction", action.Name }, { "Pheidi_Param[pheidiEvent]", action.Event }, { "pheidiparams", param } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 240)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string responseContent = response.Content.ReadAsStringAsync().Result; Debug.WriteLine("Reponse:" + responseContent); action.ActionAnswer = responseContent; } } RunActionAnswer(action); }); }
static public async Task <string> GetPMH() { var parameters = new Dictionary <string, string> { { "pheidiaction", "complexAction" }, { "pheidiparams", "action**:**GetPMH**,**" } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 10)); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string content = await response.Content.ReadAsStringAsync(); Debug.WriteLine(content); App.PMH = new Dictionary <string, string>(); foreach (var fields in GetFields(content)) { App.PMH.Add(fields["PMH_A_Identifiant"].ToString(), fields["Noseq"].ToString()); } return(GoodResult); } } return(BadResult); }
/// <summary> ///Méthode qui envoie la requête http permettant de se connecter au systeme central et de recuperer la liste des serveur de l'usager. /// </summary> /// <param name="username"></param> /// <param name="password"></param> public static async Task <string> SystemLogin(string username, string password) { var list = new List <string>(); try { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return(AppResources.Erreur_LaissezAucunChampVide); } string websession = ""; string noseq = ""; string url = "https://system.pheidi.com"; string domain = "system.pheidi.com"; App.CurrentServer = new ServerInfo() { Domain = domain, Address = url }; var parameters = new Dictionary <string, string> { { "pheidiaction", "complexAction" }, { "pheidiparams", "action**:**getWebSession**,**Username**:**" + username + "**,**Password**:**" + password + "**,**" } }; HttpResponseMessage response = await PheidiNetworkManager.SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 10), App.CurrentServer.GetDefaultUrl()); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { string rc = await response.Content.ReadAsStringAsync(); if (!string.IsNullOrEmpty(rc) && Utilities.IsNumeric(rc)) { Debug.WriteLine(rc); websession = rc; App.WebSession = new Cookie() { Name = "WEBSESSION", Domain = domain, Value = rc }; App.CookieManager.AddCookie(App.WebSession); } } else { return(AppResources.Erreur_ProblemeConnexionServeur); } } else { return(AppResources.Erreur_ProblemeConnexionServeur); } if (string.IsNullOrEmpty(websession)) { return(AppResources.Erreur_MauvaisEmailOuMdp); } parameters = new Dictionary <string, string>(); parameters.Add("pheidiaction", "complexAction"); parameters.Add("pheidiparams", "action**:**Get_UserNoseqWithWebSession**,**WES_A_WebSession**:**" + websession + "**,**"); response = null; response = await SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30), App.CurrentServer.GetDefaultUrl()); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { var content = await response.Content.ReadAsStringAsync(); Debug.WriteLine("GET NOSEQ FOR WEBSESSION: " + content); noseq = GetFields(content)[0]["WES_CON_A_NoSeq"].ToString(); } } if (string.IsNullOrEmpty(noseq)) { return(AppResources.Erreur_ProblemeConnexionServeur); } parameters = new Dictionary <string, string>(); parameters.Add("pheidiaction", "complexAction"); parameters.Add("pheidiparams", "action**:**Get_IpheidiServerList**,**CON_A_NoSeq**:**" + noseq + "**,**"); response = null; response = await SendHttpRequestAsync(parameters, new TimeSpan(0, 0, 30), App.CurrentServer.GetDefaultUrl()); if (response != null) { if (response.StatusCode == HttpStatusCode.OK) { var content = await response.Content.ReadAsStringAsync(); Debug.WriteLine("GET SERVER LIST: " + content); App.ServerInfoList = new List <ServerInfo>(); foreach (var fields in GetFields(content)) { var server = new ServerInfo(); server.Address = fields["SEIN_A_ServerAddress"].ToString(); server.Name = fields["SEIN_A_serverName"].ToString(); server.Noseq = fields["SEIN_A_Noseq"].ToString(); server.Domain = new Uri(server.Address).Host; App.ServerInfoList.Add(server); if (server.Noseq == App.ServerInfoNoseq) { App.CurrentServer = server; } } if (App.CurrentServer == null) { App.ServerInfoNoseq = ""; } } } } catch (Exception e) { Debug.WriteLine(e.Message); return(AppResources.Erreur_ProblemeConnexionServeur); } return(GoodResult); }
/// <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); }