BeeHoneyt  1.1
BTS SNIR LaSalle Avignon 2021
Historique.java
Aller à la documentation de ce fichier.
1 package com.example.bee_honeyt;
2 
3 import android.os.AsyncTask;
4 import android.util.Log;
5 
6 import java.io.BufferedInputStream;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.StringReader;
12 import java.net.HttpURLConnection;
13 import java.net.MalformedURLException;
14 import java.net.URL;
15 
16 import javax.net.ssl.HttpsURLConnection;
17 
27 public class Historique
28 {
29  // Constantes
30  private static final String TAG = "_Historique";
31  // Attributs
32  private boolean chargement = false;
33  private AsyncTaskHTTP tache = null;
35 
39  public Historique()
40  {
41  Log.d(TAG, "new Historique()");
42  }
43 
49  {
50  this.callback = callback;
51  }
52 
57  public boolean estCharge()
58  {
59  Log.d(TAG, "estCharge() " + this.chargement);
60  return this.chargement;
61  }
62 
69  public void charger(String strUrl, String key, String deviceID, String duree)
70  {
71  if(strUrl != null && !strUrl.isEmpty())
72  {
73  String url;
74  if(deviceID.isEmpty())
75  url = strUrl + "/api/v2/query";
76  else
77  url = strUrl + "/api/v2/query/" + deviceID;
78  if(!duree.isEmpty()) // par défaut 1 heure
79  url += "?last=" + duree;
80  Log.d(TAG, "charger() " + strUrl);
81  this.tache = new AsyncTaskHTTP();
82  tache.execute(url, key);
83  }
84  }
85 
89  public class AsyncTaskHTTP extends AsyncTask<String, Void, String>
90  {
91  public AsyncTaskHTTP()
92  {
93  Log.d(TAG, "<AsyncTaskHTTP> new AsyncTaskHTTP()");
94  }
95 
96  @Override
97  protected String doInBackground(String... strings)
98  {
99  URL url = null;
100  HttpsURLConnection connexionHTTPS = null;
101  InputStream in = null;
102  String contenu = null;
103 
104  Log.d(TAG, "<AsyncTaskHTTP> doInBackground() url : " + strings[0] + " - key : " + strings[1]);
105  try
106  {
107  url = new URL(strings[0]);
108  connexionHTTPS = (HttpsURLConnection) url.openConnection();
109  connexionHTTPS.setRequestProperty("Accept", "application/json");
110  connexionHTTPS.setRequestProperty("Authorization", "key " + strings[1]);
111  int codeReponse = connexionHTTPS.getResponseCode();
112  if (codeReponse != HttpURLConnection.HTTP_OK)
113  {
114  Log.d(TAG,"<AsyncTaskHTTP> Erreur code réponse = " + codeReponse);
115  return contenu;
116  }
117 
118  in = new BufferedInputStream(connexionHTTPS.getInputStream());
119  BufferedReader r = new BufferedReader(new InputStreamReader(in));
120  StringBuilder sb = new StringBuilder();
121  String ligne = null;
122  while(true)
123  {
124  try
125  {
126  if (!((ligne = r.readLine()) != null))
127  break;
128  }
129  catch (IOException e)
130  {
131  e.printStackTrace();
132  }
133  sb.append(ligne);
134  sb.append("\r\n");
135  }
136  contenu = sb.toString();
137  Log.d(TAG,"<AsyncTaskHTTP> données récupérées : " + contenu.length() + " octet(s)");
138  }
139  catch (MalformedURLException e)
140  {
141  Log.d(TAG,"MalformedURLException");
142  e.printStackTrace();
143  }
144  catch (IOException e)
145  {
146  Log.d(TAG,"IOException");
147  e.printStackTrace();
148  }
149  finally
150  {
151  if (connexionHTTPS != null)
152  connexionHTTPS.disconnect();
153  }
154 
155  return contenu;
156  }
157 
158  @Override
159  protected void onPostExecute(String contenu)
160  {
161  Log.d(TAG,"<AsyncTaskHTTP> onPostExecute()");
162  if(contenu == null)
163  {
164  chargement = false; // pas de chargement
165  if(callback != null)
166  {
167  callback.onError();
168  }
169  return;
170  }
171 
172  StringReader sin = new StringReader(contenu);
173  if(sin != null)
174  {
175  chargement = true;
176 
177  if(chargement)
178  {
179  if(callback != null)
180  {
181  callback.onLoad(contenu);
182  }
183  }
184  }
185  }
186  }
187 }
com.example.bee_honeyt.Historique.AsyncTaskHTTP.AsyncTaskHTTP
AsyncTaskHTTP()
Definition: Historique.java:91
com.example.bee_honeyt.Historique.AsyncTaskHTTP.doInBackground
String doInBackground(String... strings)
Definition: Historique.java:97
com.example.bee_honeyt.Historique.callback
HistoriqueEventListener callback
Pour les fonctions de rappel sur évènement (onLoad, onError, ...)
Definition: Historique.java:34
com.example.bee_honeyt.HistoriqueEventListener.onLoad
void onLoad(String contenu)
com.example.bee_honeyt.Historique.setCallback
void setCallback(HistoriqueEventListener callback)
Installe les fonctions de rappel pour les évènements onLoad, ...
Definition: Historique.java:48
com.example.bee_honeyt.Historique.TAG
static final String TAG
TAG pour les logs.
Definition: Historique.java:30
com.example.bee_honeyt.HistoriqueEventListener.onError
void onError()
com.example.bee_honeyt.Historique.Historique
Historique()
Constructeur par défaut de la classe Historique.
Definition: Historique.java:39
com.example.bee_honeyt.Historique
Déclaration de la classe Historique.
Definition: Historique.java:27
com.example.bee_honeyt.Historique.tache
AsyncTaskHTTP tache
Tâche d'arrière plan pour récupérer le contenu de l'historique.
Definition: Historique.java:33
com.example.bee_honeyt.Historique.estCharge
boolean estCharge()
Retourne l'état de chargement du contenu.
Definition: Historique.java:57
com.example.bee_honeyt.HistoriqueEventListener
Déclaration de l'interface HistoriqueEventListener.
Definition: HistoriqueEventListener.java:12
com.example.bee_honeyt.Historique.AsyncTaskHTTP
Classe permettant de lancer la tâche de chargement du contenu de l'historique en arrière plan.
Definition: Historique.java:89
com.example.bee_honeyt.Historique.AsyncTaskHTTP.onPostExecute
void onPostExecute(String contenu)
Definition: Historique.java:159
com.example.bee_honeyt.Historique.chargement
boolean chargement
Indique si le contenu a été chargé
Definition: Historique.java:32
com.example.bee_honeyt.Historique.charger
void charger(String strUrl, String key, String deviceID, String duree)
Charge à partir d'une URL vers Data Storage TTN.
Definition: Historique.java:69