BeeHoneyt  1.1
BTS SNIR LaSalle Avignon 2021
Communication.java
Aller à la documentation de ce fichier.
1 package com.example.bee_honeyt;
2 
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.database.sqlite.SQLiteOpenHelper;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Message;
10 import android.util.Log;
11 
12 import org.eclipse.paho.android.service.MqttAndroidClient;
13 import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
14 import org.eclipse.paho.client.mqttv3.IMqttActionListener;
15 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
16 import org.eclipse.paho.client.mqttv3.IMqttToken;
17 import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
18 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
19 import org.eclipse.paho.client.mqttv3.MqttException;
20 import org.eclipse.paho.client.mqttv3.MqttMessage;
21 
35 public class Communication
36 {
40  private static final String TAG = "_Communication";
41 
45  private MqttAndroidClient mqttAndroidClient;
46  private Handler handler = null; // pour la communication entre classes
47 
51  public static final int TTN_CONNECTE = 1;
52  public static final int TTN_DECONNECTE = 2;
53  public static final int TTN_MESSAGE = 3;
54 
55  //private String username = "rucher"; //!< nom d'utilisateur
56  //private String password = "ttn-account-v2.a4GRsjloPzQ_4hswG-rmWaO9MzWkAtzCvggWeO2DvL4"; //!< mot de passe TTN
57  private String username = "";
58  private String password = "";
59 
67  public Communication(Context context, final Handler handler)
68  {
69  Log.v(TAG, "[Communication()] clientId = " + clientId);
70  this.handler = handler;
71 
72  creerClientMQTTT(context, handler);
73  }
74 
75  public String getUsername()
76  {
77  return this.username;
78  }
79 
80  public String getPassword()
81  {
82  return this.password;
83  }
84 
85  public void setClientId(String username)
86  {
88  }
89 
90  public void setUsername(String username)
91  {
92  if(username != null)
93  {
94  this.clientId = username;
95  this.username = username;
96  }
97  }
98 
99  public void setPassword(String password)
100  {
101  if(password != null)
102  this.password = password;
103  }
104 
105  private void creerClientMQTTT(Context context, Handler handler)
106  {
107  mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
108  mqttAndroidClient.setCallback(new MqttCallbackExtended()
109  {
110  @Override
111  public void connectComplete(boolean b, String s)
112  {
113  Log.w(TAG, "[connectComplete()] serverUri = " + s + " connecte = " + mqttAndroidClient.isConnected());
114  Message msg = Message.obtain();
115  Bundle bundle = new Bundle();
116  bundle.putInt("etat", TTN_CONNECTE); // clé -> valeur ici etat -> TTN_CONNECTE
117  msg.setData(bundle);
118  handler.sendMessage(msg);
119  }
120 
121  @Override
122  public void connectionLost(Throwable throwable)
123  {
124  Log.w(TAG, "[connectionLost()]");
125  Message msg = Message.obtain();
126  Bundle b = new Bundle();
127  b.putInt("etat", TTN_DECONNECTE);
128  msg.setData(b);
129  handler.sendMessage(msg);
130  }
131 
132  @Override
133  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
134  {
135  Log.w(TAG, "[messageArrived()] topic = " + topic + " message = " + mqttMessage.toString());
136  Message msg = Message.obtain();
137  Bundle b = new Bundle();
138  b.putInt("etat", TTN_MESSAGE);
139  b.putString("topic", topic);
140  b.putString("message", mqttMessage.toString());
141  msg.setData(b);
142  handler.sendMessage(msg);
143  }
144 
145  @Override
146  public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)
147  {
148  Log.w(TAG, "[deliveryComplete()]");
149  }
150  });
151  }
152 
159  public void setCallback(MqttCallbackExtended callback)
160  {
161  mqttAndroidClient.setCallback(callback);
162  }
163 
169  public void connecter()
170  {
171  MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
172  mqttConnectOptions.setAutomaticReconnect(true);
173  mqttConnectOptions.setCleanSession(false);
174  mqttConnectOptions.setUserName(username);
175  mqttConnectOptions.setPassword(password.toCharArray());
176 
177  try
178  {
179  Log.d(TAG, "[connecter()] serverUri = " + serverUri + " clientId = " + clientId);
180  mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
181  {
182  @Override
183  public void onSuccess(IMqttToken asyncActionToken)
184  {
185  DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
186  disconnectedBufferOptions.setBufferEnabled(true);
187  disconnectedBufferOptions.setBufferSize(100);
188  disconnectedBufferOptions.setPersistBuffer(false);
189  disconnectedBufferOptions.setDeleteOldestMessages(false);
190  mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
191  Log.d(TAG, "[onSuccess()] serverUri = " + serverUri + " clientId = " + clientId);
192  }
193 
194  @Override
195  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
196  {
197  Log.d(TAG, "[onFailure()] serverUri = " + serverUri + " clientId = " + clientId + " exception = " + exception.toString());
198  }
199  });
200  }
201  catch (MqttException e)
202  {
203  e.printStackTrace();
204  }
205  }
206 
212  public void deconnecter()
213  {
214  if(!estConnecte())
215  return;
216  Log.d(TAG, "[deconnecter()] serverUri = " + serverUri + " clientId = " + clientId);
217  try
218  {
219  IMqttToken disconToken = mqttAndroidClient.disconnect();
220  disconToken.setActionCallback(new IMqttActionListener()
221  {
222  @Override
223  public void onSuccess(IMqttToken asyncActionToken)
224  {
225  Log.d(TAG, "[onSuccess()] serverUri = " + serverUri + " clientId = " + clientId);
226  }
227 
228  @Override
229  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
230  {
231  Log.d(TAG, "[onFailure()] serverUri = " + serverUri + " clientId = " + clientId + " exception = " + exception.toString());
232  }
233  });
234  }
235  catch (MqttException e)
236  {
237  e.printStackTrace();
238  }
239  }
240 
247  public boolean estConnecte()
248  {
249  Log.w(TAG, "[estConnecte()] " + mqttAndroidClient.isConnected());
250 
251  return mqttAndroidClient.isConnected();
252  }
253 
260  public boolean souscrireTopic(String deviceID)
261  {
262  // Vérifications
263  if(mqttAndroidClient == null && !mqttAndroidClient.isConnected())
264  {
265  return false;
266  }
267 
268  final String topicTTN = clientId + "/devices/" + deviceID + "/up";
269  Log.w(TAG, "[souscrireTopic()] topic = " + topicTTN);
270  try
271  {
272  final boolean[] retour = {false};
273  mqttAndroidClient.subscribe(topicTTN, 0, null, new IMqttActionListener()
274  {
275  @Override
276  public void onSuccess(IMqttToken asyncActionToken)
277  {
278  Log.w(TAG, "[onSuccess()] topic = " + topicTTN);
279  retour[0] = true;
280  }
281 
282  @Override
283  public void onFailure(IMqttToken asyncActionToken, Throwable exception)
284  {
285  Log.w(TAG, "[onFailure()] topic = " + topicTTN);
286  retour[0] = false;
287  }
288  });
289  return retour[0];
290  }
291  catch (MqttException e)
292  {
293  Log.w(TAG, "Erreur topic = " + topicTTN);
294  e.printStackTrace();
295  return false;
296  }
297  }
298 
305  public boolean unsubscribe(String deviceID)
306  {
307  if(mqttAndroidClient == null && !mqttAndroidClient.isConnected())
308  {
309  return false;
310  }
311  final String topicTTN = clientId + "/devices/" + deviceID + "/up";
312  Log.w(TAG, "[unsouscrireTopic()] topic = " + topicTTN);
313  try
314  {
315  final boolean[] retour = {false};
316  mqttAndroidClient.unsubscribe(topicTTN, null, null);
317 
318  return retour[0];
319  }
320  catch (MqttException e)
321  {
322  Log.w(TAG, "Erreur topic = " + topicTTN);
323  e.printStackTrace();
324  return false;
325  }
326  }
327 
328 }
com.example.bee_honeyt.Communication.setPassword
void setPassword(String password)
Definition: Communication.java:99
com.example.bee_honeyt.Communication.TAG
static final String TAG
Definition: Communication.java:40
com.example.bee_honeyt.Communication.username
String username
nom d'utilisateur
Definition: Communication.java:57
com.example.bee_honeyt.Communication.Communication
Communication(Context context, final Handler handler)
Constructeur de la classe Communication.
Definition: Communication.java:67
com.example.bee_honeyt.Communication.TTN_CONNECTE
static final int TTN_CONNECTE
Definition: Communication.java:51
com.example.bee_honeyt.Communication.unsubscribe
boolean unsubscribe(String deviceID)
S'desabonne à un topic (deviceID = ESP32 d'une ruche dans TTN)
Definition: Communication.java:305
com.example.bee_honeyt.Communication.password
String password
mot de passe TTN
Definition: Communication.java:58
com.example.bee_honeyt.Communication.setUsername
void setUsername(String username)
Definition: Communication.java:90
com.example.bee_honeyt.Communication.handler
Handler handler
Definition: Communication.java:46
com.example.bee_honeyt.Communication.getUsername
String getUsername()
Definition: Communication.java:75
com.example.bee_honeyt.Communication.souscrireTopic
boolean souscrireTopic(String deviceID)
S'abonne à un topic (deviceID = ESP32 d'une ruche dans TTN)
Definition: Communication.java:260
com.example.bee_honeyt.Communication.setCallback
void setCallback(MqttCallbackExtended callback)
Installe les fonctions de rappel.
Definition: Communication.java:159
com.example.bee_honeyt.Communication.creerClientMQTTT
void creerClientMQTTT(Context context, Handler handler)
Definition: Communication.java:105
com.example.bee_honeyt.Communication.getPassword
String getPassword()
Definition: Communication.java:80
com.example.bee_honeyt.Communication.setClientId
void setClientId(String username)
Definition: Communication.java:85
com.example.bee_honeyt.Communication.deconnecter
void deconnecter()
Deconnexion du TTN.
Definition: Communication.java:212
com.example.bee_honeyt.Communication.TTN_MESSAGE
static final int TTN_MESSAGE
Definition: Communication.java:53
com.example.bee_honeyt.Communication.TTN_DECONNECTE
static final int TTN_DECONNECTE
Definition: Communication.java:52
com.example.bee_honeyt.Communication.mqttAndroidClient
MqttAndroidClient mqttAndroidClient
Definition: Communication.java:45
com.example.bee_honeyt.Communication.connecter
void connecter()
Connexion au TTN.
Definition: Communication.java:169
com.example.bee_honeyt.Communication.estConnecte
boolean estConnecte()
Retourne l'état de connexion au serveur TTN.
Definition: Communication.java:247
com.example.bee_honeyt.Communication
Déclaration de la classe Communication.
Definition: Communication.java:35